MDL-48931 navigation: Update to context header renderer.
[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
880c5073
AG
60if (isset($userid)) {
61 $context = context_user::instance($userid);
62} else {
63 $context = context_system::instance();
64}
65$PAGE->set_context($context);
66
41b38360 67$sitecontext = context_system::instance();
8f6c1f34 68
2b6e53e8 69// Check basic permissions.
8f6c1f34 70if ($CFG->bloglevel == BLOG_GLOBAL_LEVEL) {
2b6e53e8 71 // Everybody can see anything - no login required unless site is locked down using forcelogin.
8f6c1f34
PS
72 if ($CFG->forcelogin) {
73 require_login();
74 }
75
76} else if ($CFG->bloglevel == BLOG_SITE_LEVEL) {
2b6e53e8 77 // Users must log in and can not be guests.
8f6c1f34
PS
78 require_login();
79 if (isguestuser()) {
2b6e53e8 80 // They must have entered the url manually.
8f6c1f34
PS
81 print_error('blogdisable', 'blog');
82 }
83
84} else if ($CFG->bloglevel == BLOG_USER_LEVEL) {
2b6e53e8 85 // Users can see own blogs only! with the exception of people with special cap.
8f6c1f34
PS
86 require_login();
87
88} else {
2b6e53e8 89 // Weird!
8b141784 90 print_error('blogdisable', 'blog');
ab2f17b0 91}
bbbf2d40 92
e9fb99b1
AD
93if (empty($CFG->enableblogs)) {
94 print_error('blogdisable', 'blog');
95}
96
97// Add courseid if modid or groupid is specified: This is used for navigation and title.
98if (!empty($modid) && empty($courseid)) {
99 $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid));
100}
101
102if (!empty($groupid) && empty($courseid)) {
103 $courseid = $DB->get_field('groups', 'courseid', array('id' => $groupid));
104}
105
93caac3a 106
cae83708 107if (!$userid && has_capability('moodle/blog:view', $sitecontext) && $CFG->bloglevel > BLOG_USER_LEVEL) {
108 if ($entryid) {
1c7b8b93 109 if (!$entryobject = $DB->get_record('post', array('id'=>$entryid))) {
856b6fe6 110 print_error('nosuchentry', 'blog');
dde73b9e 111 }
cae83708 112 $userid = $entryobject->userid;
4a173181 113 }
856b6fe6 114} else if (!$userid) {
856b6fe6 115 $userid = $USER->id;
4a173181 116}
4a173181 117
1c7b8b93 118if (!empty($modid)) {
856b6fe6 119 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
120 print_error(get_string('nocourseblogs', 'blog'));
121 }
122 if (!$mod = $DB->get_record('course_modules', array('id' => $modid))) {
123 print_error(get_string('invalidmodid', 'blog'));
124 }
125 $courseid = $mod->course;
126}
e96f2a77 127
1c7b8b93 128if ((empty($courseid) ? true : $courseid == SITEID) && empty($userid)) {
856b6fe6 129 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
130 print_error('siteblogdisable', 'blog');
131 }
856b6fe6 132 if (!has_capability('moodle/blog:view', $sitecontext)) {
133 print_error('cannotviewsiteblog', 'blog');
134 }
e96f2a77 135
856b6fe6 136 $COURSE = $DB->get_record('course', array('format'=>'site'));
137 $courseid = $COURSE->id;
138}
139
140if (!empty($courseid)) {
141 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
142 print_error('invalidcourseid');
143 }
144
145 $courseid = $course->id;
856b6fe6 146 require_login($course);
147
f42d2a22 148 if (!has_capability('moodle/blog:view', $sitecontext)) {
856b6fe6 149 print_error('cannotviewcourseblog', 'blog');
150 }
151} else {
41b38360 152 $coursecontext = context_course::instance(SITEID);
856b6fe6 153}
154
155if (!empty($groupid)) {
156 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
157 print_error('groupblogdisable', 'blog');
158 }
e96f2a77 159
1c7b8b93 160 if (! $group = groups_get_group($groupid)) {
856b6fe6 161 print_error(get_string('invalidgroupid', 'blog'));
162 }
e96f2a77 163
856b6fe6 164 if (!$course = $DB->get_record('course', array('id'=>$group->courseid))) {
19a86468 165 print_error('invalidcourseid');
856b6fe6 166 }
e96f2a77 167
41b38360 168 $coursecontext = context_course::instance($course->id);
856b6fe6 169 $courseid = $course->id;
170 require_login($course);
171
f42d2a22 172 if (!has_capability('moodle/blog:view', $sitecontext)) {
856b6fe6 173 print_error(get_string('cannotviewcourseorgroupblog', 'blog'));
174 }
f5fc83e8 175
cae83708 176 if (groups_get_course_groupmode($course) == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $coursecontext)) {
856b6fe6 177 if (!groups_is_member($groupid)) {
178 print_error('notmemberofgroup');
e96f2a77 179 }
856b6fe6 180 }
181}
182
b29ce44d 183if (!empty($userid)) {
856b6fe6 184 if ($CFG->bloglevel < BLOG_USER_LEVEL) {
185 print_error('blogdisable', 'blog');
186 }
187
188 if (!$user = $DB->get_record('user', array('id'=>$userid))) {
189 print_error('invaliduserid');
190 }
191
192 if ($user->deleted) {
cae83708 193 echo $OUTPUT->header();
16ba76bd 194 echo $OUTPUT->heading(get_string('userdeleted'));
033e4aff 195 echo $OUTPUT->footer();
856b6fe6 196 die;
197 }
f4c85f46 198
856b6fe6 199 if ($USER->id == $userid) {
200 if (!has_capability('moodle/blog:create', $sitecontext)
cae83708 201 && !has_capability('moodle/blog:view', $sitecontext)) {
856b6fe6 202 print_error('donothaveblog', 'blog');
673bc55d 203 }
856b6fe6 204 } else {
9a909b1a 205 if (!has_capability('moodle/blog:view', $sitecontext) || !blog_user_can_view_user_entry($userid)) {
856b6fe6 206 print_error('cannotviewcourseblog', 'blog');
207 }
c53a6444
AD
208
209 $PAGE->navigation->extend_for_user($user);
856b6fe6 210 }
4a173181 211}
212
cae83708 213$courseid = (empty($courseid)) ? SITEID : $courseid;
f99fee6c 214
856b6fe6 215
c5dc10ee 216$blogheaders = blog_get_headers();
cae83708 217
e858c368 218if ($CFG->enablerssfeeds) {
43c4f4d1
JF
219 $rsscontext = null;
220 $filtertype = null;
221 $thingid = null;
e858c368 222 list($thingid, $rsscontext, $filtertype) = blog_rss_get_params($blogheaders['filters']);
43c4f4d1 223 if (empty($rsscontext)) {
f42d2a22 224 $rsscontext = context_system::instance();
43c4f4d1 225 }
e858c368
AD
226 $rsstitle = $blogheaders['heading'];
227
2b6e53e8 228 // Check we haven't started output by outputting an error message.
e858c368
AD
229 if ($PAGE->state == moodle_page::STATE_BEFORE_HEADER) {
230 blog_rss_add_http_header($rsscontext, $rsstitle, $filtertype, $thingid, $tagid);
231 }
e858c368 232}
880c5073
AG
233if (isset($userid)) {
234 $PAGE->set_heading(fullname($user));
235}
e858c368 236
c5dc10ee 237echo $OUTPUT->header();
c5dc10ee 238echo $OUTPUT->heading($blogheaders['heading'], 2);
92a019ac 239
1c7b8b93
NC
240$bloglisting = new blog_listing($blogheaders['filters']);
241$bloglisting->print_entries();
4a173181 242
033e4aff 243echo $OUTPUT->footer();
6b364115
AA
244$eventparams = array(
245 'other' => array('entryid' => $entryid, 'tagid' => $tagid, 'userid' => $userid, 'modid' => $modid, 'groupid' => $groupid,
246 'search' => $search, 'fromstart' => $start)
247);
248if (!empty($userid)) {
249 $eventparams['relateduserid'] = $userid;
250}
251$eventparams['other']['courseid'] = ($courseid === SITEID) ? 0 : $courseid;
252$event = \core\event\blog_entries_viewed::create($eventparams);
253$event->trigger();