weekly release 2.7.2+
[moodle.git] / blog / index.php
blob4bdfd35e3abf6366f4ee5865b830f80cb1ff82be
1 <?php
3 /**
4 * file index.php
5 * index page to view blogs. if no blog is specified then site wide entries are shown
6 * if a blog id is specified then the latest entries from that blog are shown
7 */
9 require_once(dirname(dirname(__FILE__)).'/config.php');
10 require_once($CFG->dirroot .'/blog/lib.php');
11 require_once($CFG->dirroot .'/blog/locallib.php');
12 require_once($CFG->dirroot .'/course/lib.php');
13 require_once($CFG->dirroot .'/tag/lib.php');
14 require_once($CFG->dirroot .'/comment/lib.php');
16 $id = optional_param('id', null, PARAM_INT);
17 $start = optional_param('formstart', 0, PARAM_INT);
18 $tag = optional_param('tag', '', PARAM_NOTAGS);
19 $userid = optional_param('userid', null, PARAM_INT);
20 $tagid = optional_param('tagid', null, PARAM_INT);
21 $modid = optional_param('modid', null, PARAM_INT);
22 $entryid = optional_param('entryid', null, PARAM_INT);
23 $groupid = optional_param('groupid', null, PARAM_INT);
24 $courseid = optional_param('courseid', null, PARAM_INT);
25 $search = optional_param('search', null, PARAM_RAW);
27 comment::init();
29 $url_params = compact('id', 'start', 'tag', 'userid', 'tagid', 'modid', 'entryid', 'groupid', 'courseid', 'search');
30 foreach ($url_params as $var => $val) {
31 if (empty($val)) {
32 unset($url_params[$var]);
35 $PAGE->set_url('/blog/index.php', $url_params);
37 //correct tagid if a text tag is provided as a param
38 if (!empty($tag)) {
39 if ($tagrec = $DB->get_record('tag', array('name' => $tag))) {
40 $tagid = $tagrec->id;
41 } else {
42 unset($tagid);
46 $sitecontext = context_system::instance();
47 // Blogs are always in system context.
48 $PAGE->set_context($sitecontext);
50 // check basic permissions
51 if ($CFG->bloglevel == BLOG_GLOBAL_LEVEL) {
52 // everybody can see anything - no login required unless site is locked down using forcelogin
53 if ($CFG->forcelogin) {
54 require_login();
57 } else if ($CFG->bloglevel == BLOG_SITE_LEVEL) {
58 // users must log in and can not be guests
59 require_login();
60 if (isguestuser()) {
61 // they must have entered the url manually...
62 print_error('blogdisable', 'blog');
65 } else if ($CFG->bloglevel == BLOG_USER_LEVEL) {
66 // users can see own blogs only! with the exception of ppl with special cap
67 require_login();
69 } else {
70 // weird!
71 print_error('blogdisable', 'blog');
74 if (empty($CFG->enableblogs)) {
75 print_error('blogdisable', 'blog');
78 // Add courseid if modid or groupid is specified: This is used for navigation and title.
79 if (!empty($modid) && empty($courseid)) {
80 $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid));
83 if (!empty($groupid) && empty($courseid)) {
84 $courseid = $DB->get_field('groups', 'courseid', array('id' => $groupid));
88 if (!$userid && has_capability('moodle/blog:view', $sitecontext) && $CFG->bloglevel > BLOG_USER_LEVEL) {
89 if ($entryid) {
90 if (!$entryobject = $DB->get_record('post', array('id'=>$entryid))) {
91 print_error('nosuchentry', 'blog');
93 $userid = $entryobject->userid;
95 } else if (!$userid) {
96 $userid = $USER->id;
99 if (!empty($modid)) {
100 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
101 print_error(get_string('nocourseblogs', 'blog'));
103 if (!$mod = $DB->get_record('course_modules', array('id' => $modid))) {
104 print_error(get_string('invalidmodid', 'blog'));
106 $courseid = $mod->course;
109 if ((empty($courseid) ? true : $courseid == SITEID) && empty($userid)) {
110 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
111 print_error('siteblogdisable', 'blog');
113 if (!has_capability('moodle/blog:view', $sitecontext)) {
114 print_error('cannotviewsiteblog', 'blog');
117 $COURSE = $DB->get_record('course', array('format'=>'site'));
118 $courseid = $COURSE->id;
121 if (!empty($courseid)) {
122 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
123 print_error('invalidcourseid');
126 $courseid = $course->id;
127 require_login($course);
129 if (!has_capability('moodle/blog:view', $sitecontext)) {
130 print_error('cannotviewcourseblog', 'blog');
132 } else {
133 $coursecontext = context_course::instance(SITEID);
136 if (!empty($groupid)) {
137 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
138 print_error('groupblogdisable', 'blog');
141 if (! $group = groups_get_group($groupid)) {
142 print_error(get_string('invalidgroupid', 'blog'));
145 if (!$course = $DB->get_record('course', array('id'=>$group->courseid))) {
146 print_error('invalidcourseid');
149 $coursecontext = context_course::instance($course->id);
150 $courseid = $course->id;
151 require_login($course);
153 if (!has_capability('moodle/blog:view', $sitecontext)) {
154 print_error(get_string('cannotviewcourseorgroupblog', 'blog'));
157 if (groups_get_course_groupmode($course) == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $coursecontext)) {
158 if (!groups_is_member($groupid)) {
159 print_error('notmemberofgroup');
164 if (!empty($userid)) {
165 if ($CFG->bloglevel < BLOG_USER_LEVEL) {
166 print_error('blogdisable', 'blog');
169 if (!$user = $DB->get_record('user', array('id'=>$userid))) {
170 print_error('invaliduserid');
173 if ($user->deleted) {
174 echo $OUTPUT->header();
175 echo $OUTPUT->heading(get_string('userdeleted'));
176 echo $OUTPUT->footer();
177 die;
180 if ($USER->id == $userid) {
181 if (!has_capability('moodle/blog:create', $sitecontext)
182 && !has_capability('moodle/blog:view', $sitecontext)) {
183 print_error('donothaveblog', 'blog');
185 } else {
186 if (!has_capability('moodle/blog:view', $sitecontext) || !blog_user_can_view_user_entry($userid)) {
187 print_error('cannotviewcourseblog', 'blog');
190 $PAGE->navigation->extend_for_user($user);
194 $courseid = (empty($courseid)) ? SITEID : $courseid;
197 $blogheaders = blog_get_headers();
199 if ($CFG->enablerssfeeds) {
200 $rsscontext = null;
201 $filtertype = null;
202 $thingid = null;
203 list($thingid, $rsscontext, $filtertype) = blog_rss_get_params($blogheaders['filters']);
204 if (empty($rsscontext)) {
205 $rsscontext = context_system::instance();
207 $rsstitle = $blogheaders['heading'];
209 //check we haven't started output by outputting an error message
210 if ($PAGE->state == moodle_page::STATE_BEFORE_HEADER) {
211 blog_rss_add_http_header($rsscontext, $rsstitle, $filtertype, $thingid, $tagid);
214 //this works but there isn't a great place to put the link
215 //blog_rss_print_link($rsscontext, $filtertype, $thingid, $tagid);
218 echo $OUTPUT->header();
220 echo $OUTPUT->heading($blogheaders['heading'], 2);
222 $bloglisting = new blog_listing($blogheaders['filters']);
223 $bloglisting->print_entries();
225 echo $OUTPUT->footer();
226 $eventparams = array(
227 'other' => array('entryid' => $entryid, 'tagid' => $tagid, 'userid' => $userid, 'modid' => $modid, 'groupid' => $groupid,
228 'search' => $search, 'fromstart' => $start)
230 if (!empty($userid)) {
231 $eventparams['relateduserid'] = $userid;
233 $eventparams['other']['courseid'] = ($courseid === SITEID) ? 0 : $courseid;
234 $event = \core\event\blog_entries_viewed::create($eventparams);
235 $event->trigger();