index.php 6.37 KB
Newer Older
1
<?php
toyomoyo's avatar
toyomoyo committed
2
3
4
5
6
7
8

/**
 * file index.php
 * index page to view blogs. if no blog is specified then site wide entries are shown
 * if a blog id is specified then the latest entries from that blog are shown
 */

nicolasconnault's avatar
nicolasconnault committed
9
require_once(dirname(dirname(__FILE__)).'/config.php');
toyomoyo's avatar
toyomoyo committed
10
require_once($CFG->dirroot .'/blog/lib.php');
nicolasconnault's avatar
nicolasconnault committed
11
12
13
require_once($CFG->dirroot .'/blog/locallib.php');
require_once($CFG->dirroot .'/course/lib.php');
require_once($CFG->dirroot .'/tag/lib.php');
14
require_once($CFG->libdir .'/commentlib.php');
toyomoyo's avatar
toyomoyo committed
15

16
17
18
19
20
21
22
23
24
25
$id       = optional_param('id', null, PARAM_INT);
$start    = optional_param('formstart', 0, PARAM_INT);
$tag      = optional_param('tag', '', PARAM_NOTAGS);
$userid   = optional_param('userid', null, PARAM_INT);
$tagid    = optional_param('tagid', null, PARAM_INT);
$modid    = optional_param('modid', null, PARAM_INT);
$entryid  = optional_param('entryid', null, PARAM_INT);
$groupid  = optional_param('groupid', null, PARAM_INT);
$courseid = optional_param('courseid', null, PARAM_INT);
$search   = optional_param('search', null, PARAM_RAW);
nicolasconnault's avatar
nicolasconnault committed
26

27
28
comment::js();

nicolasconnault's avatar
nicolasconnault committed
29
30
31
32
33
34
35
$url_params = compact('id', 'start', 'tag', 'userid', 'tagid', 'modid', 'entryid', 'groupid', 'courseid', 'search');
foreach ($url_params as $var => $val) {
    if (empty($val)) {
        unset($url_params[$var]);
    }
}
$PAGE->set_url('blog/index.php', $url_params);
skodak's avatar
skodak committed
36

37
//correct tagid if a text tag is provided as a param
38
39
40
if (!empty($tag)) {
    $ILIKE = $DB->sql_ilike();
    if ($tagrec = $DB->get_record_sql("SELECT * FROM {tag} WHERE name $ILIKE ?", array("%$tag%"))) {
41
42
43
44
45
46
        $tagid = $tagrec->id;
    } else {
        unset($tagid);
    }
}

nicolasconnault's avatar
nicolasconnault committed
47
48
// add courseid if modid or groupid is specified: This is used for navigation and title
if (!empty($modid) && empty($courseid)) {
49
50
51
    $courseid = $DB->get_field('course_modules', 'course', array('id'=>$modid));
}

nicolasconnault's avatar
nicolasconnault committed
52
if (!empty($groupid) && empty($courseid)) {
53
54
    $courseid = $DB->get_field('groups', 'courseid', array('id'=>$groupid));
}
toyomoyo's avatar
toyomoyo committed
55

56
if (empty($CFG->bloglevel)) {
57
    print_error('blogdisable', 'blog');
58
}
59

60
$sitecontext = get_context_instance(CONTEXT_SYSTEM);
nicolasconnault's avatar
nicolasconnault committed
61
$tabsfile = null;
62

nicolasconnault's avatar
nicolasconnault committed
63
64
if (!$userid && has_capability('moodle/blog:view', $sitecontext) && $CFG->bloglevel > BLOG_USER_LEVEL) {
    if ($entryid) {
65
        if (!$entryobject = $DB->get_record('post', array('id'=>$entryid))) {
66
            print_error('nosuchentry', 'blog');
moodler's avatar
moodler committed
67
        }
nicolasconnault's avatar
nicolasconnault committed
68
        $userid = $entryobject->userid;
toyomoyo's avatar
toyomoyo committed
69
    }
70
71
} else if (!$userid) {
    $userid = $USER->id;
toyomoyo's avatar
toyomoyo committed
72
73
}

74
if (!empty($modid)) {
75
76
77
78
79
80
81
82
    if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
        print_error(get_string('nocourseblogs', 'blog'));
    }
    if (!$mod = $DB->get_record('course_modules', array('id' => $modid))) {
        print_error(get_string('invalidmodid', 'blog'));
    }
    $courseid = $mod->course;
}
skodak's avatar
skodak committed
83

84
if ((empty($courseid) ? true : $courseid == SITEID) && empty($userid)) {
85
86
87
88
89
90
91
92
93
    if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
        print_error('siteblogdisable', 'blog');
    }
    if ($CFG->bloglevel < BLOG_GLOBAL_LEVEL) {
        require_login();
    }
    if (!has_capability('moodle/blog:view', $sitecontext)) {
        print_error('cannotviewsiteblog', 'blog');
    }
skodak's avatar
skodak committed
94

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
    $COURSE = $DB->get_record('course', array('format'=>'site'));
    $courseid = $COURSE->id;
}

if (!empty($courseid)) {
    if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
        print_error('invalidcourseid');
    }

    $courseid = $course->id;
    $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);

    require_login($course);

    if (!has_capability('moodle/blog:view', $coursecontext)) {
        print_error('cannotviewcourseblog', 'blog');
    }
} else {
    $coursecontext = get_context_instance(CONTEXT_COURSE, SITEID);
}

if (!empty($groupid)) {
    if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
        print_error('groupblogdisable', 'blog');
    }
skodak's avatar
skodak committed
120

121
    if (! $group = groups_get_group($groupid)) {
122
123
        print_error(get_string('invalidgroupid', 'blog'));
    }
skodak's avatar
skodak committed
124

125
126
127
    if (!$course = $DB->get_record('course', array('id'=>$group->courseid))) {
        print_error(get_string('invalidcourseid', 'blog'));
    }
skodak's avatar
skodak committed
128

129
130
131
132
133
134
135
    $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
    $courseid = $course->id;
    require_login($course);

    if (!has_capability('moodle/blog:view', $coursecontext)) {
        print_error(get_string('cannotviewcourseorgroupblog', 'blog'));
    }
136

nicolasconnault's avatar
nicolasconnault committed
137
    if (groups_get_course_groupmode($course) == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $coursecontext)) {
138
139
        if (!groups_is_member($groupid)) {
            print_error('notmemberofgroup');
skodak's avatar
skodak committed
140
        }
141
142
143
144
145
146
147
148
149
150
151
152
153
    }
}

if (!empty($user)) {
    if ($CFG->bloglevel < BLOG_USER_LEVEL) {
        print_error('blogdisable', 'blog');
    }

    if (!$user = $DB->get_record('user', array('id'=>$userid))) {
        print_error('invaliduserid');
    }

    if ($user->deleted) {
nicolasconnault's avatar
nicolasconnault committed
154
        echo $OUTPUT->header();
155
        echo $OUTPUT->heading(get_string('userdeleted'));
156
        echo $OUTPUT->footer();
157
158
        die;
    }
toyomoyo's avatar
toyomoyo committed
159

160
161
    if ($USER->id == $userid) {
        if (!has_capability('moodle/blog:create', $sitecontext)
nicolasconnault's avatar
nicolasconnault committed
162
          && !has_capability('moodle/blog:view', $sitecontext)) {
163
            print_error('donothaveblog', 'blog');
164
        }
165
166
    } else {
        $personalcontext = get_context_instance(CONTEXT_USER, $userid);
167

nicolasconnault's avatar
nicolasconnault committed
168
        if (!has_capability('moodle/blog:view', $sitecontext) && !has_capability('moodle/user:readuserblogs', $personalcontext)) {
169
170
            print_error('cannotviewuserblog', 'blog');
        }
skodak's avatar
skodak committed
171

nicolasconnault's avatar
nicolasconnault committed
172
        if (!blog_user_can_view_user_entry($userid)) {
173
174
175
            print_error('cannotviewcourseblog', 'blog');
        }
    }
nicolasconnault's avatar
nicolasconnault committed
176
    $tabsfile = $CFG->dirroot . '/user/tabs.php';
toyomoyo's avatar
toyomoyo committed
177
178
}

nicolasconnault's avatar
nicolasconnault committed
179
$courseid = (empty($courseid)) ? SITEID : $courseid;
180

nicolasconnault's avatar
nicolasconnault committed
181
182
if (!empty($courseid)) {
    $PAGE->set_context(get_context_instance(CONTEXT_COURSE, $courseid));
183
184
}

nicolasconnault's avatar
nicolasconnault committed
185
186
if (!empty($modid)) {
    $PAGE->set_context(get_context_instance(CONTEXT_MODULE, $modid));
187
188
}

189
$blogheaders = blog_get_headers();
190
blog_extend_settings_navigation($PAGE->settingsnav);
nicolasconnault's avatar
nicolasconnault committed
191
192

if (empty($entryid) && empty($modid) && empty($groupid)) {
193
    $PAGE->set_context(get_context_instance(CONTEXT_USER, $USER->id));
nicolasconnault's avatar
nicolasconnault committed
194
195
}

196
echo $OUTPUT->header();
nicolasconnault's avatar
nicolasconnault committed
197
198
199
200

if (!empty($tabsfile)) {
    require_once($tabsfile);
}
toyomoyo's avatar
toyomoyo committed
201

202
echo $OUTPUT->heading($blogheaders['heading'], 2);
toyomoyo's avatar
toyomoyo committed
203

204
205
$bloglisting = new blog_listing($blogheaders['filters']);
$bloglisting->print_entries();
toyomoyo's avatar
toyomoyo committed
206

207
echo $OUTPUT->footer();
vyshane's avatar
vyshane committed
208

nicolasconnault's avatar
nicolasconnault committed
209
add_to_log($courseid, 'blog', 'view', 'index.php?entryid='.$entryid.'&amp;tagid='.@$tagid.'&amp;tag='.$tag, 'view blog entry');