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