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