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