weekly on-sync release 3.0dev
[moodle.git] / blog / index.php
CommitLineData
dfede59d 1<?php
2b6e53e8
AD
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
4a173181 16
17/**
18 * file index.php
19 * index page to view blogs. if no blog is specified then site wide entries are shown
20 * if a blog id is specified then the latest entries from that blog are shown
21 */
22
cae83708 23require_once(dirname(dirname(__FILE__)).'/config.php');
4a173181 24require_once($CFG->dirroot .'/blog/lib.php');
cae83708 25require_once($CFG->dirroot .'/blog/locallib.php');
26require_once($CFG->dirroot .'/course/lib.php');
27require_once($CFG->dirroot .'/tag/lib.php');
36051c9e 28require_once($CFG->dirroot .'/comment/lib.php');
4a173181 29
1c7b8b93
NC
30$id = optional_param('id', null, PARAM_INT);
31$start = optional_param('formstart', 0, PARAM_INT);
32$tag = optional_param('tag', '', PARAM_NOTAGS);
33$userid = optional_param('userid', null, PARAM_INT);
34$tagid = optional_param('tagid', null, PARAM_INT);
35$modid = optional_param('modid', null, PARAM_INT);
36$entryid = optional_param('entryid', null, PARAM_INT);
37$groupid = optional_param('groupid', null, PARAM_INT);
38$courseid = optional_param('courseid', null, PARAM_INT);
39$search = optional_param('search', null, PARAM_RAW);
cae83708 40
36051c9e 41comment::init();
b73d1ca4 42
cae83708 43$url_params = compact('id', 'start', 'tag', 'userid', 'tagid', 'modid', 'entryid', 'groupid', 'courseid', 'search');
44foreach ($url_params as $var => $val) {
45 if (empty($val)) {
46 unset($url_params[$var]);
47 }
48}
a6855934 49$PAGE->set_url('/blog/index.php', $url_params);
e96f2a77 50
2b6e53e8 51// Correct tagid if a text tag is provided as a param.
1c7b8b93 52if (!empty($tag)) {
1af9063e 53 if ($tagrec = $DB->get_record('tag', array('name' => $tag))) {
856b6fe6 54 $tagid = $tagrec->id;
55 } else {
56 unset($tagid);
57 }
58}
59
45367bdf
AG
60// Set the userid to the entry author if we have the entry ID.
61if ($entryid and !isset($userid)) {
62 $entry = new blog_entry($entryid);
63 $userid = $entry->userid;
64}
65
c1f97c77 66if (isset($userid) && !isset($courseid)) {
880c5073 67 $context = context_user::instance($userid);
c1f97c77
DC
68} else if (isset($courseid) && $courseid != SITEID) {
69 $context = context_course::instance($courseid);
880c5073
AG
70} else {
71 $context = context_system::instance();
72}
73$PAGE->set_context($context);
74
41b38360 75$sitecontext = context_system::instance();
8f6c1f34 76
f495187d
AG
77if (isset($userid) && $USER->id == $userid) {
78 $blognode = $PAGE->navigation->find('siteblog', null);
79 $blognode->make_inactive();
80}
81
2b6e53e8 82// Check basic permissions.
8f6c1f34 83if ($CFG->bloglevel == BLOG_GLOBAL_LEVEL) {
2b6e53e8 84 // Everybody can see anything - no login required unless site is locked down using forcelogin.
8f6c1f34
PS
85 if ($CFG->forcelogin) {
86 require_login();
87 }
88
89} else if ($CFG->bloglevel == BLOG_SITE_LEVEL) {
2b6e53e8 90 // Users must log in and can not be guests.
8f6c1f34
PS
91 require_login();
92 if (isguestuser()) {
2b6e53e8 93 // They must have entered the url manually.
8f6c1f34
PS
94 print_error('blogdisable', 'blog');
95 }
96
97} else if ($CFG->bloglevel == BLOG_USER_LEVEL) {
2b6e53e8 98 // Users can see own blogs only! with the exception of people with special cap.
8f6c1f34
PS
99 require_login();
100
101} else {
2b6e53e8 102 // Weird!
8b141784 103 print_error('blogdisable', 'blog');
ab2f17b0 104}
bbbf2d40 105
e9fb99b1
AD
106if (empty($CFG->enableblogs)) {
107 print_error('blogdisable', 'blog');
108}
109
110// Add courseid if modid or groupid is specified: This is used for navigation and title.
111if (!empty($modid) && empty($courseid)) {
112 $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid));
113}
114
115if (!empty($groupid) && empty($courseid)) {
116 $courseid = $DB->get_field('groups', 'courseid', array('id' => $groupid));
117}
118
93caac3a 119
cae83708 120if (!$userid && has_capability('moodle/blog:view', $sitecontext) && $CFG->bloglevel > BLOG_USER_LEVEL) {
121 if ($entryid) {
1c7b8b93 122 if (!$entryobject = $DB->get_record('post', array('id'=>$entryid))) {
856b6fe6 123 print_error('nosuchentry', 'blog');
dde73b9e 124 }
cae83708 125 $userid = $entryobject->userid;
4a173181 126 }
856b6fe6 127} else if (!$userid) {
856b6fe6 128 $userid = $USER->id;
4a173181 129}
4a173181 130
1c7b8b93 131if (!empty($modid)) {
856b6fe6 132 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
133 print_error(get_string('nocourseblogs', 'blog'));
134 }
135 if (!$mod = $DB->get_record('course_modules', array('id' => $modid))) {
136 print_error(get_string('invalidmodid', 'blog'));
137 }
138 $courseid = $mod->course;
139}
e96f2a77 140
1c7b8b93 141if ((empty($courseid) ? true : $courseid == SITEID) && empty($userid)) {
856b6fe6 142 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
143 print_error('siteblogdisable', 'blog');
144 }
856b6fe6 145 if (!has_capability('moodle/blog:view', $sitecontext)) {
146 print_error('cannotviewsiteblog', 'blog');
147 }
e96f2a77 148
856b6fe6 149 $COURSE = $DB->get_record('course', array('format'=>'site'));
150 $courseid = $COURSE->id;
151}
152
153if (!empty($courseid)) {
154 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
155 print_error('invalidcourseid');
156 }
157
158 $courseid = $course->id;
856b6fe6 159 require_login($course);
160
f42d2a22 161 if (!has_capability('moodle/blog:view', $sitecontext)) {
856b6fe6 162 print_error('cannotviewcourseblog', 'blog');
163 }
164} else {
41b38360 165 $coursecontext = context_course::instance(SITEID);
856b6fe6 166}
167
168if (!empty($groupid)) {
169 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
170 print_error('groupblogdisable', 'blog');
171 }
e96f2a77 172
1c7b8b93 173 if (! $group = groups_get_group($groupid)) {
856b6fe6 174 print_error(get_string('invalidgroupid', 'blog'));
175 }
e96f2a77 176
856b6fe6 177 if (!$course = $DB->get_record('course', array('id'=>$group->courseid))) {
19a86468 178 print_error('invalidcourseid');
856b6fe6 179 }
e96f2a77 180
41b38360 181 $coursecontext = context_course::instance($course->id);
856b6fe6 182 $courseid = $course->id;
183 require_login($course);
184
f42d2a22 185 if (!has_capability('moodle/blog:view', $sitecontext)) {
856b6fe6 186 print_error(get_string('cannotviewcourseorgroupblog', 'blog'));
187 }
f5fc83e8 188
cae83708 189 if (groups_get_course_groupmode($course) == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $coursecontext)) {
856b6fe6 190 if (!groups_is_member($groupid)) {
191 print_error('notmemberofgroup');
e96f2a77 192 }
856b6fe6 193 }
194}
195
b29ce44d 196if (!empty($userid)) {
856b6fe6 197 if ($CFG->bloglevel < BLOG_USER_LEVEL) {
198 print_error('blogdisable', 'blog');
199 }
200
201 if (!$user = $DB->get_record('user', array('id'=>$userid))) {
202 print_error('invaliduserid');
203 }
204
205 if ($user->deleted) {
cae83708 206 echo $OUTPUT->header();
16ba76bd 207 echo $OUTPUT->heading(get_string('userdeleted'));
033e4aff 208 echo $OUTPUT->footer();
856b6fe6 209 die;
210 }
f4c85f46 211
856b6fe6 212 if ($USER->id == $userid) {
213 if (!has_capability('moodle/blog:create', $sitecontext)
cae83708 214 && !has_capability('moodle/blog:view', $sitecontext)) {
856b6fe6 215 print_error('donothaveblog', 'blog');
673bc55d 216 }
856b6fe6 217 } else {
9a909b1a 218 if (!has_capability('moodle/blog:view', $sitecontext) || !blog_user_can_view_user_entry($userid)) {
856b6fe6 219 print_error('cannotviewcourseblog', 'blog');
220 }
c53a6444
AD
221
222 $PAGE->navigation->extend_for_user($user);
856b6fe6 223 }
4a173181 224}
225
cae83708 226$courseid = (empty($courseid)) ? SITEID : $courseid;
f99fee6c 227
856b6fe6 228
c5dc10ee 229$blogheaders = blog_get_headers();
cae83708 230
e858c368 231if ($CFG->enablerssfeeds) {
43c4f4d1
JF
232 $rsscontext = null;
233 $filtertype = null;
234 $thingid = null;
e858c368 235 list($thingid, $rsscontext, $filtertype) = blog_rss_get_params($blogheaders['filters']);
43c4f4d1 236 if (empty($rsscontext)) {
f42d2a22 237 $rsscontext = context_system::instance();
43c4f4d1 238 }
e858c368
AD
239 $rsstitle = $blogheaders['heading'];
240
2b6e53e8 241 // Check we haven't started output by outputting an error message.
e858c368
AD
242 if ($PAGE->state == moodle_page::STATE_BEFORE_HEADER) {
243 blog_rss_add_http_header($rsscontext, $rsstitle, $filtertype, $thingid, $tagid);
244 }
e858c368 245}
c1f97c77
DC
246
247$usernode = $PAGE->navigation->find('user'.$userid, null);
248if ($usernode && $courseid != SITEID) {
249 $courseblogsnode = $PAGE->navigation->find('courseblogs', null);
250 if ($courseblogsnode) {
251 $courseblogsnode->remove();
252 }
253 $blogurl = new moodle_url($PAGE->url);
254 $blognode = $usernode->add(get_string('blogscourse', 'blog'), $blogurl);
255 $blognode->make_active();
256}
257
258if ($courseid != SITEID) {
259 $PAGE->set_heading($course->fullname);
260 echo $OUTPUT->header();
261 if (!empty($user)) {
262 $headerinfo = array('heading' => fullname($user), 'user' => $user);
263 echo $OUTPUT->context_header($headerinfo, 2);
264 }
265} else if (isset($userid)) {
880c5073 266 $PAGE->set_heading(fullname($user));
c1f97c77
DC
267 echo $OUTPUT->header();
268} else if ($courseid == SITEID) {
269 echo $OUTPUT->header();
880c5073 270}
e858c368 271
c5dc10ee 272echo $OUTPUT->heading($blogheaders['heading'], 2);
92a019ac 273
1c7b8b93
NC
274$bloglisting = new blog_listing($blogheaders['filters']);
275$bloglisting->print_entries();
4a173181 276
033e4aff 277echo $OUTPUT->footer();
6b364115
AA
278$eventparams = array(
279 'other' => array('entryid' => $entryid, 'tagid' => $tagid, 'userid' => $userid, 'modid' => $modid, 'groupid' => $groupid,
280 'search' => $search, 'fromstart' => $start)
281);
282if (!empty($userid)) {
283 $eventparams['relateduserid'] = $userid;
284}
285$eventparams['other']['courseid'] = ($courseid === SITEID) ? 0 : $courseid;
286$event = \core\event\blog_entries_viewed::create($eventparams);
287$event->trigger();