1b64b42fe5686dddff6d2fcf5397132cdac3d607
[moodle.git] / blog / index.php
1 <?php
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/>.
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  */
23 require_once(dirname(dirname(__FILE__)).'/config.php');
24 require_once($CFG->dirroot .'/blog/lib.php');
25 require_once($CFG->dirroot .'/blog/locallib.php');
26 require_once($CFG->dirroot .'/course/lib.php');
27 require_once($CFG->dirroot .'/comment/lib.php');
29 $id       = optional_param('id', null, PARAM_INT);
30 $start    = optional_param('formstart', 0, PARAM_INT);
31 $tag      = optional_param('tag', '', PARAM_NOTAGS);
32 $userid   = optional_param('userid', null, PARAM_INT);
33 $tagid    = optional_param('tagid', null, PARAM_INT);
34 $modid    = optional_param('modid', null, PARAM_INT);
35 $entryid  = optional_param('entryid', null, PARAM_INT);
36 $groupid  = optional_param('groupid', null, PARAM_INT);
37 $courseid = optional_param('courseid', null, PARAM_INT);
38 $search   = optional_param('search', null, PARAM_RAW);
40 comment::init();
42 $urlparams = compact('id', 'start', 'tag', 'userid', 'tagid', 'modid', 'entryid', 'groupid', 'courseid', 'search');
43 foreach ($urlparams as $var => $val) {
44     if (empty($val)) {
45         unset($urlparams[$var]);
46     }
47 }
48 $PAGE->set_url('/blog/index.php', $urlparams);
50 // Correct tagid if a text tag is provided as a param.
51 if (!empty($tag)) {
52     if ($tagrec = $DB->get_record('tag', array('name' => $tag))) {
53         $tagid = $tagrec->id;
54     } else {
55         unset($tagid);
56     }
57 }
59 // Set the userid to the entry author if we have the entry ID.
60 if ($entryid and !isset($userid)) {
61     $entry = new blog_entry($entryid);
62     $userid = $entry->userid;
63 }
65 if (isset($userid) && empty($courseid)) {
66     $context = context_user::instance($userid);
67 } else if (!empty($courseid) && $courseid != SITEID) {
68     $context = context_course::instance($courseid);
69 } else {
70     $context = context_system::instance();
71 }
72 $PAGE->set_context($context);
74 $sitecontext = context_system::instance();
76 if (isset($userid) && $USER->id == $userid) {
77     $blognode = $PAGE->navigation->find('siteblog', null);
78     if ($blognode) {
79         $blognode->make_inactive();
80     }
81 }
83 // Check basic permissions.
84 if ($CFG->bloglevel == BLOG_GLOBAL_LEVEL) {
85     // Everybody can see anything - no login required unless site is locked down using forcelogin.
86     if ($CFG->forcelogin) {
87         require_login();
88     }
90 } else if ($CFG->bloglevel == BLOG_SITE_LEVEL) {
91     // Users must log in and can not be guests.
92     require_login();
93     if (isguestuser()) {
94         // They must have entered the url manually.
95         print_error('blogdisable', 'blog');
96     }
98 } else if ($CFG->bloglevel == BLOG_USER_LEVEL) {
99     // Users can see own blogs only! with the exception of people with special cap.
100     require_login();
102 } else {
103     // Weird!
104     print_error('blogdisable', 'blog');
107 if (empty($CFG->enableblogs)) {
108     print_error('blogdisable', 'blog');
111 // Add courseid if modid or groupid is specified: This is used for navigation and title.
112 if (!empty($modid) && empty($courseid)) {
113     $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid));
116 if (!empty($groupid) && empty($courseid)) {
117     $courseid = $DB->get_field('groups', 'courseid', array('id' => $groupid));
121 if (!$userid && has_capability('moodle/blog:view', $sitecontext) && $CFG->bloglevel > BLOG_USER_LEVEL) {
122     if ($entryid) {
123         if (!$entryobject = $DB->get_record('post', array('id' => $entryid))) {
124             print_error('nosuchentry', 'blog');
125         }
126         $userid = $entryobject->userid;
127     }
128 } else if (!$userid) {
129     $userid = $USER->id;
132 if (!empty($modid)) {
133     if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
134         print_error(get_string('nocourseblogs', 'blog'));
135     }
136     if (!$mod = $DB->get_record('course_modules', array('id' => $modid))) {
137         print_error(get_string('invalidmodid', 'blog'));
138     }
139     $courseid = $mod->course;
142 if ((empty($courseid) ? true : $courseid == SITEID) && empty($userid)) {
143     if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
144         print_error('siteblogdisable', 'blog');
145     }
146     if (!has_capability('moodle/blog:view', $sitecontext)) {
147         print_error('cannotviewsiteblog', 'blog');
148     }
150     $COURSE = $DB->get_record('course', array('format' => 'site'));
151     $courseid = $COURSE->id;
154 if (!empty($courseid)) {
155     if (!$course = $DB->get_record('course', array('id' => $courseid))) {
156         print_error('invalidcourseid');
157     }
159     $courseid = $course->id;
160     require_login($course);
162     if (!has_capability('moodle/blog:view', $sitecontext)) {
163         print_error('cannotviewcourseblog', 'blog');
164     }
165 } else {
166     $coursecontext = context_course::instance(SITEID);
169 if (!empty($groupid)) {
170     if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
171         print_error('groupblogdisable', 'blog');
172     }
174     if (! $group = groups_get_group($groupid)) {
175         print_error(get_string('invalidgroupid', 'blog'));
176     }
178     if (!$course = $DB->get_record('course', array('id' => $group->courseid))) {
179         print_error('invalidcourseid');
180     }
182     $coursecontext = context_course::instance($course->id);
183     $courseid = $course->id;
184     require_login($course);
186     if (!has_capability('moodle/blog:view', $sitecontext)) {
187         print_error(get_string('cannotviewcourseorgroupblog', 'blog'));
188     }
190     if (groups_get_course_groupmode($course) == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $coursecontext)) {
191         if (!groups_is_member($groupid)) {
192             print_error('notmemberofgroup');
193         }
194     }
197 if (!empty($userid)) {
198     if ($CFG->bloglevel < BLOG_USER_LEVEL) {
199         print_error('blogdisable', 'blog');
200     }
202     if (!$user = $DB->get_record('user', array('id' => $userid))) {
203         print_error('invaliduserid');
204     }
206     if ($user->deleted) {
207         echo $OUTPUT->header();
208         echo $OUTPUT->heading(get_string('userdeleted'));
209         echo $OUTPUT->footer();
210         die;
211     }
213     if ($USER->id == $userid) {
214         if (!has_capability('moodle/blog:create', $sitecontext)
215           && !has_capability('moodle/blog:view', $sitecontext)) {
216             print_error('donothaveblog', 'blog');
217         }
218     } else {
219         if (!has_capability('moodle/blog:view', $sitecontext) || !blog_user_can_view_user_entry($userid)) {
220             print_error('cannotviewcourseblog', 'blog');
221         }
223         $PAGE->navigation->extend_for_user($user);
224     }
227 $courseid = (empty($courseid)) ? SITEID : $courseid;
230 $blogheaders = blog_get_headers();
232 if ($CFG->enablerssfeeds) {
233     $rsscontext = null;
234     $filtertype = null;
235     $thingid = null;
236     list($thingid, $rsscontext, $filtertype) = blog_rss_get_params($blogheaders['filters']);
237     if (empty($rsscontext)) {
238         $rsscontext = context_system::instance();
239     }
240     $rsstitle = $blogheaders['heading'];
242     // Check we haven't started output by outputting an error message.
243     if ($PAGE->state == moodle_page::STATE_BEFORE_HEADER) {
244         blog_rss_add_http_header($rsscontext, $rsstitle, $filtertype, $thingid, $tagid);
245     }
248 $usernode = $PAGE->navigation->find('user'.$userid, null);
249 if ($usernode && $courseid != SITEID) {
250     $courseblogsnode = $PAGE->navigation->find('courseblogs', null);
251     if ($courseblogsnode) {
252         $courseblogsnode->remove();
253     }
254     $blogurl = new moodle_url($PAGE->url);
255     $blognode = $usernode->add(get_string('blogscourse', 'blog'), $blogurl);
256     $blognode->make_active();
259 if ($courseid != SITEID) {
260     $PAGE->set_heading($course->fullname);
261     echo $OUTPUT->header();
262     if (!empty($user)) {
263         $headerinfo = array('heading' => fullname($user), 'user' => $user);
264         echo $OUTPUT->context_header($headerinfo, 2);
265     }
266 } else if (isset($userid)) {
267     $PAGE->set_heading(fullname($user));
268     echo $OUTPUT->header();
269 } else if ($courseid == SITEID) {
270     echo $OUTPUT->header();
273 echo $OUTPUT->heading($blogheaders['heading'], 2);
275 $bloglisting = new blog_listing($blogheaders['filters']);
276 $bloglisting->print_entries();
278 echo $OUTPUT->footer();
279 $eventparams = array(
280     'other' => array('entryid' => $entryid, 'tagid' => $tagid, 'userid' => $userid, 'modid' => $modid, 'groupid' => $groupid,
281                      'search' => $search, 'fromstart' => $start)
282 );
283 if (!empty($userid)) {
284     $eventparams['relateduserid'] = $userid;
286 $eventparams['other']['courseid'] = ($courseid === SITEID) ? 0 : $courseid;
287 $event = \core\event\blog_entries_viewed::create($eventparams);
288 $event->trigger();