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