MDL-27814 blogs: Deprecate associate blog caps
[moodle.git] / blog / index.php
CommitLineData
dfede59d 1<?php
4a173181 2
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 */
8
cae83708 9require_once(dirname(dirname(__FILE__)).'/config.php');
4a173181 10require_once($CFG->dirroot .'/blog/lib.php');
cae83708 11require_once($CFG->dirroot .'/blog/locallib.php');
12require_once($CFG->dirroot .'/course/lib.php');
13require_once($CFG->dirroot .'/tag/lib.php');
36051c9e 14require_once($CFG->dirroot .'/comment/lib.php');
4a173181 15
1c7b8b93
NC
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);
cae83708 26
36051c9e 27comment::init();
b73d1ca4 28
cae83708 29$url_params = compact('id', 'start', 'tag', 'userid', 'tagid', 'modid', 'entryid', 'groupid', 'courseid', 'search');
30foreach ($url_params as $var => $val) {
31 if (empty($val)) {
32 unset($url_params[$var]);
33 }
34}
a6855934 35$PAGE->set_url('/blog/index.php', $url_params);
e96f2a77 36
850d2db8 37if (empty($CFG->enableblogs)) {
8f6c1f34
PS
38 print_error('blogdisable', 'blog');
39}
40
856b6fe6 41//correct tagid if a text tag is provided as a param
1c7b8b93 42if (!empty($tag)) {
b6859e0e 43 if ($tagrec = $DB->get_record_sql("SELECT * FROM {tag} WHERE ". $DB->sql_like('name', '?', false), array("%$tag%"))) {
856b6fe6 44 $tagid = $tagrec->id;
45 } else {
46 unset($tagid);
47 }
48}
49
cae83708 50// add courseid if modid or groupid is specified: This is used for navigation and title
51if (!empty($modid) && empty($courseid)) {
856b6fe6 52 $courseid = $DB->get_field('course_modules', 'course', array('id'=>$modid));
53}
54
cae83708 55if (!empty($groupid) && empty($courseid)) {
856b6fe6 56 $courseid = $DB->get_field('groups', 'courseid', array('id'=>$groupid));
57}
4a173181 58
41b38360 59$sitecontext = context_system::instance();
68fc1cc2
AA
60// Blogs are always in system context.
61$PAGE->set_context($sitecontext);
8f6c1f34
PS
62
63// check basic permissions
64if ($CFG->bloglevel == BLOG_GLOBAL_LEVEL) {
65 // everybody can see anything - no login required unless site is locked down using forcelogin
66 if ($CFG->forcelogin) {
67 require_login();
68 }
69
70} else if ($CFG->bloglevel == BLOG_SITE_LEVEL) {
71 // users must log in and can not be guests
72 require_login();
73 if (isguestuser()) {
74 // they must have entered the url manually...
75 print_error('blogdisable', 'blog');
76 }
77
78} else if ($CFG->bloglevel == BLOG_USER_LEVEL) {
79 // users can see own blogs only! with the exception of ppl with special cap
80 require_login();
81
82} else {
83 // weird!
8b141784 84 print_error('blogdisable', 'blog');
ab2f17b0 85}
bbbf2d40 86
93caac3a 87
cae83708 88if (!$userid && has_capability('moodle/blog:view', $sitecontext) && $CFG->bloglevel > BLOG_USER_LEVEL) {
89 if ($entryid) {
1c7b8b93 90 if (!$entryobject = $DB->get_record('post', array('id'=>$entryid))) {
856b6fe6 91 print_error('nosuchentry', 'blog');
dde73b9e 92 }
cae83708 93 $userid = $entryobject->userid;
4a173181 94 }
856b6fe6 95} else if (!$userid) {
856b6fe6 96 $userid = $USER->id;
4a173181 97}
4a173181 98
1c7b8b93 99if (!empty($modid)) {
856b6fe6 100 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
101 print_error(get_string('nocourseblogs', 'blog'));
102 }
103 if (!$mod = $DB->get_record('course_modules', array('id' => $modid))) {
104 print_error(get_string('invalidmodid', 'blog'));
105 }
106 $courseid = $mod->course;
107}
e96f2a77 108
1c7b8b93 109if ((empty($courseid) ? true : $courseid == SITEID) && empty($userid)) {
856b6fe6 110 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
111 print_error('siteblogdisable', 'blog');
112 }
856b6fe6 113 if (!has_capability('moodle/blog:view', $sitecontext)) {
114 print_error('cannotviewsiteblog', 'blog');
115 }
e96f2a77 116
856b6fe6 117 $COURSE = $DB->get_record('course', array('format'=>'site'));
118 $courseid = $COURSE->id;
119}
120
121if (!empty($courseid)) {
122 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
123 print_error('invalidcourseid');
124 }
125
126 $courseid = $course->id;
41b38360 127 $coursecontext = context_course::instance($course->id);
856b6fe6 128
129 require_login($course);
130
131 if (!has_capability('moodle/blog:view', $coursecontext)) {
132 print_error('cannotviewcourseblog', 'blog');
133 }
134} else {
41b38360 135 $coursecontext = context_course::instance(SITEID);
856b6fe6 136}
137
138if (!empty($groupid)) {
139 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
140 print_error('groupblogdisable', 'blog');
141 }
e96f2a77 142
1c7b8b93 143 if (! $group = groups_get_group($groupid)) {
856b6fe6 144 print_error(get_string('invalidgroupid', 'blog'));
145 }
e96f2a77 146
856b6fe6 147 if (!$course = $DB->get_record('course', array('id'=>$group->courseid))) {
19a86468 148 print_error('invalidcourseid');
856b6fe6 149 }
e96f2a77 150
41b38360 151 $coursecontext = context_course::instance($course->id);
856b6fe6 152 $courseid = $course->id;
153 require_login($course);
154
155 if (!has_capability('moodle/blog:view', $coursecontext)) {
156 print_error(get_string('cannotviewcourseorgroupblog', 'blog'));
157 }
f5fc83e8 158
cae83708 159 if (groups_get_course_groupmode($course) == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $coursecontext)) {
856b6fe6 160 if (!groups_is_member($groupid)) {
161 print_error('notmemberofgroup');
e96f2a77 162 }
856b6fe6 163 }
164}
165
b29ce44d 166if (!empty($userid)) {
856b6fe6 167 if ($CFG->bloglevel < BLOG_USER_LEVEL) {
168 print_error('blogdisable', 'blog');
169 }
170
171 if (!$user = $DB->get_record('user', array('id'=>$userid))) {
172 print_error('invaliduserid');
173 }
174
175 if ($user->deleted) {
cae83708 176 echo $OUTPUT->header();
16ba76bd 177 echo $OUTPUT->heading(get_string('userdeleted'));
033e4aff 178 echo $OUTPUT->footer();
856b6fe6 179 die;
180 }
f4c85f46 181
856b6fe6 182 if ($USER->id == $userid) {
183 if (!has_capability('moodle/blog:create', $sitecontext)
cae83708 184 && !has_capability('moodle/blog:view', $sitecontext)) {
856b6fe6 185 print_error('donothaveblog', 'blog');
673bc55d 186 }
856b6fe6 187 } else {
41b38360 188 $personalcontext = context_user::instance($userid);
673bc55d 189
cae83708 190 if (!has_capability('moodle/blog:view', $sitecontext) && !has_capability('moodle/user:readuserblogs', $personalcontext)) {
856b6fe6 191 print_error('cannotviewuserblog', 'blog');
192 }
e96f2a77 193
cae83708 194 if (!blog_user_can_view_user_entry($userid)) {
856b6fe6 195 print_error('cannotviewcourseblog', 'blog');
196 }
c53a6444
AD
197
198 $PAGE->navigation->extend_for_user($user);
856b6fe6 199 }
4a173181 200}
201
cae83708 202$courseid = (empty($courseid)) ? SITEID : $courseid;
f99fee6c 203
856b6fe6 204
c5dc10ee 205$blogheaders = blog_get_headers();
cae83708 206
e858c368 207if ($CFG->enablerssfeeds) {
43c4f4d1
JF
208 $rsscontext = null;
209 $filtertype = null;
210 $thingid = null;
e858c368 211 list($thingid, $rsscontext, $filtertype) = blog_rss_get_params($blogheaders['filters']);
43c4f4d1
JF
212 if (empty($rsscontext)) {
213 $rsscontext = get_system_context();
214 }
e858c368
AD
215 $rsstitle = $blogheaders['heading'];
216
217 //check we haven't started output by outputting an error message
218 if ($PAGE->state == moodle_page::STATE_BEFORE_HEADER) {
219 blog_rss_add_http_header($rsscontext, $rsstitle, $filtertype, $thingid, $tagid);
220 }
221
222 //this works but there isn't a great place to put the link
223 //blog_rss_print_link($rsscontext, $filtertype, $thingid, $tagid);
224}
225
c5dc10ee 226echo $OUTPUT->header();
cae83708 227
c5dc10ee 228echo $OUTPUT->heading($blogheaders['heading'], 2);
92a019ac 229
1c7b8b93
NC
230$bloglisting = new blog_listing($blogheaders['filters']);
231$bloglisting->print_entries();
4a173181 232
033e4aff 233echo $OUTPUT->footer();
d7bf6d17 234
cae83708 235add_to_log($courseid, 'blog', 'view', 'index.php?entryid='.$entryid.'&amp;tagid='.@$tagid.'&amp;tag='.$tag, 'view blog entry');