MDL-20523 Script for upgrading sites that use the old bloglevel settings.
[moodle.git] / blog / index.php
CommitLineData
4a173181 1<?php // $Id$
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');
b73d1ca4 14require_once($CFG->libdir .'/commentlib.php');
4a173181 15
cae83708 16$id = optional_param('id', null, PARAM_INT);
93caac3a 17$start = optional_param('formstart', 0, PARAM_INT);
93caac3a 18$tag = optional_param('tag', '', PARAM_NOTAGS);
cae83708 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); // needed for user tabs and course tracking
25$search = optional_param('search', null, PARAM_RAW);
26
b73d1ca4 27comment::js();
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}
35$PAGE->set_url('blog/index.php', $url_params);
e96f2a77 36
856b6fe6 37//correct tagid if a text tag is provided as a param
38if (!empty($tag)) { //text tag parameter takes precedence
39 if ($tagrec = $DB->get_record_sql("SELECT * FROM {tag} WHERE name LIKE ?", array($tag))) {
40 $tagid = $tagrec->id;
41 } else {
42 unset($tagid);
43 }
44}
45
cae83708 46// add courseid if modid or groupid is specified: This is used for navigation and title
47if (!empty($modid) && empty($courseid)) {
856b6fe6 48 $courseid = $DB->get_field('course_modules', 'course', array('id'=>$modid));
49}
50
cae83708 51if (!empty($groupid) && empty($courseid)) {
856b6fe6 52 $courseid = $DB->get_field('groups', 'courseid', array('id'=>$groupid));
53}
4a173181 54
ab2f17b0 55if (empty($CFG->bloglevel)) {
8b141784 56 print_error('blogdisable', 'blog');
ab2f17b0 57}
bbbf2d40 58
d9d16e56 59$sitecontext = get_context_instance(CONTEXT_SYSTEM);
cae83708 60$tabsfile = null;
93caac3a 61
cae83708 62if (!$userid && has_capability('moodle/blog:view', $sitecontext) && $CFG->bloglevel > BLOG_USER_LEVEL) {
63 if ($entryid) {
947eaf57 64 if (!$entryobject = $DB->get_record('blog_entries', array('id'=>$entryid))) {
856b6fe6 65 print_error('nosuchentry', 'blog');
dde73b9e 66 }
cae83708 67 $userid = $entryobject->userid;
4a173181 68 }
856b6fe6 69} else if (!$userid) {
70 // user might have capability to write blogs, but not read blogs at site level
71 // users might enter this url manually without parameters
72 $userid = $USER->id;
4a173181 73}
e96f2a77 74/// check access and prepare filters
4a173181 75
856b6fe6 76if (!empty($modid)) { //check mod access
77 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
78 print_error(get_string('nocourseblogs', 'blog'));
79 }
80 if (!$mod = $DB->get_record('course_modules', array('id' => $modid))) {
81 print_error(get_string('invalidmodid', 'blog'));
82 }
83 $courseid = $mod->course;
84}
e96f2a77 85
cae83708 86if ((empty($courseid) ? true : $courseid == SITEID) && empty($userid)) { //check site access
856b6fe6 87 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
88 print_error('siteblogdisable', 'blog');
89 }
90 if ($CFG->bloglevel < BLOG_GLOBAL_LEVEL) {
91 require_login();
92 }
93 if (!has_capability('moodle/blog:view', $sitecontext)) {
94 print_error('cannotviewsiteblog', 'blog');
95 }
e96f2a77 96
856b6fe6 97 $COURSE = $DB->get_record('course', array('format'=>'site'));
98 $courseid = $COURSE->id;
99}
100
101if (!empty($courseid)) {
102 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
103 print_error('invalidcourseid');
104 }
105
106 $courseid = $course->id;
107 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
108
109 require_login($course);
110
111 if (!has_capability('moodle/blog:view', $coursecontext)) {
112 print_error('cannotviewcourseblog', 'blog');
113 }
114} else {
115 $coursecontext = get_context_instance(CONTEXT_COURSE, SITEID);
116}
117
118if (!empty($groupid)) {
119 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
120 print_error('groupblogdisable', 'blog');
121 }
e96f2a77 122
11993a60 123 // fix for MDL-9268
856b6fe6 124 if (! $group = groups_get_group($groupid)) { //TODO:check.
125 print_error(get_string('invalidgroupid', 'blog'));
126 }
e96f2a77 127
856b6fe6 128 if (!$course = $DB->get_record('course', array('id'=>$group->courseid))) {
129 print_error(get_string('invalidcourseid', 'blog'));
130 }
e96f2a77 131
856b6fe6 132 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
133 $courseid = $course->id;
134 require_login($course);
135
136 if (!has_capability('moodle/blog:view', $coursecontext)) {
137 print_error(get_string('cannotviewcourseorgroupblog', 'blog'));
138 }
f5fc83e8 139
cae83708 140 if (groups_get_course_groupmode($course) == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $coursecontext)) {
856b6fe6 141 if (!groups_is_member($groupid)) {
142 print_error('notmemberofgroup');
e96f2a77 143 }
856b6fe6 144 }
145}
146
147if (!empty($user)) {
148 if ($CFG->bloglevel < BLOG_USER_LEVEL) {
149 print_error('blogdisable', 'blog');
150 }
151
152 if (!$user = $DB->get_record('user', array('id'=>$userid))) {
153 print_error('invaliduserid');
154 }
155
156 if ($user->deleted) {
cae83708 157 echo $OUTPUT->header();
16ba76bd 158 echo $OUTPUT->heading(get_string('userdeleted'));
033e4aff 159 echo $OUTPUT->footer();
856b6fe6 160 die;
161 }
f4c85f46 162
856b6fe6 163 if ($USER->id == $userid) {
164 if (!has_capability('moodle/blog:create', $sitecontext)
cae83708 165 && !has_capability('moodle/blog:view', $sitecontext)) {
856b6fe6 166 print_error('donothaveblog', 'blog');
673bc55d 167 }
856b6fe6 168 } else {
169 $personalcontext = get_context_instance(CONTEXT_USER, $userid);
673bc55d 170
cae83708 171 if (!has_capability('moodle/blog:view', $sitecontext) && !has_capability('moodle/user:readuserblogs', $personalcontext)) {
856b6fe6 172 print_error('cannotviewuserblog', 'blog');
173 }
e96f2a77 174
cae83708 175 if (!blog_user_can_view_user_entry($userid)) {
856b6fe6 176 print_error('cannotviewcourseblog', 'blog');
177 }
178 }
cae83708 179 $tabsfile = $CFG->dirroot . '/user/tabs.php';
4a173181 180}
181
cae83708 182$courseid = (empty($courseid)) ? SITEID : $courseid;
f99fee6c 183
cae83708 184if (!empty($courseid)) {
856b6fe6 185 $filters['course'] = $courseid;
cae83708 186 $PAGE->set_context(get_context_instance(CONTEXT_COURSE, $courseid));
856b6fe6 187}
188
cae83708 189if (!empty($modid)) {
190 $filters['module'] = $modid;
191 $PAGE->set_context(get_context_instance(CONTEXT_MODULE, $modid));
856b6fe6 192}
193
cae83708 194if (!empty($groupid)) {
856b6fe6 195 $filters['group'] = $groupid;
196}
197
cae83708 198if (!empty($userid)) {
856b6fe6 199 $filters['user'] = $userid;
200}
201
cae83708 202if (!empty($tagid)) {
856b6fe6 203 $filters['tag'] = $tagid;
204}
205
cae83708 206if (!empty($search)) {
207 $filters['search'] = $search;
208}
4a173181 209
cae83708 210if (!empty($entryid)) {
211 $filters['entry'] = $entryid;
212}
c5dc10ee 213$blogheaders = blog_get_headers();
23677261 214blog_extend_settings_navigation($PAGE->settingsnav);
cae83708 215
216// prints the tabs
217$showroles = !empty($userid);
218$currenttab = 'blogs';
219
220$user = $USER;
221$userid = $USER->id;
222
223// Unless this is a user's blog listing, the context is system
224if (empty($entryid) && empty($modid) && empty($groupid)) {
225 $PAGE->set_context(get_context_instance(CONTEXT_USER, $userid));
226}
227
c5dc10ee 228echo $OUTPUT->header();
cae83708 229
230if (!empty($tabsfile)) {
231 require_once($tabsfile);
232}
4a173181 233
c5dc10ee 234echo $OUTPUT->heading($blogheaders['heading'], 2);
92a019ac 235
cae83708 236$blog_listing = new blog_listing($filters);
237$blog_listing->print_entries();
4a173181 238
033e4aff 239echo $OUTPUT->footer();
d7bf6d17 240
cae83708 241add_to_log($courseid, 'blog', 'view', 'index.php?entryid='.$entryid.'&amp;tagid='.@$tagid.'&amp;tag='.$tag, 'view blog entry');