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