fixed undefined sitecontext
[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
856b6fe6 37//correct tagid if a text tag is provided as a param
1c7b8b93 38if (!empty($tag)) {
b6859e0e 39 if ($tagrec = $DB->get_record_sql("SELECT * FROM {tag} WHERE ". $DB->sql_like('name', '?', false), array("%$tag%"))) {
856b6fe6 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);
93caac3a 60
cae83708 61if (!$userid && has_capability('moodle/blog:view', $sitecontext) && $CFG->bloglevel > BLOG_USER_LEVEL) {
62 if ($entryid) {
1c7b8b93 63 if (!$entryobject = $DB->get_record('post', array('id'=>$entryid))) {
856b6fe6 64 print_error('nosuchentry', 'blog');
dde73b9e 65 }
cae83708 66 $userid = $entryobject->userid;
4a173181 67 }
856b6fe6 68} else if (!$userid) {
856b6fe6 69 $userid = $USER->id;
4a173181 70}
4a173181 71
1c7b8b93 72if (!empty($modid)) {
856b6fe6 73 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
74 print_error(get_string('nocourseblogs', 'blog'));
75 }
76 if (!$mod = $DB->get_record('course_modules', array('id' => $modid))) {
77 print_error(get_string('invalidmodid', 'blog'));
78 }
79 $courseid = $mod->course;
80}
e96f2a77 81
1c7b8b93 82if ((empty($courseid) ? true : $courseid == SITEID) && empty($userid)) {
856b6fe6 83 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
84 print_error('siteblogdisable', 'blog');
85 }
86 if ($CFG->bloglevel < BLOG_GLOBAL_LEVEL) {
87 require_login();
88 }
89 if (!has_capability('moodle/blog:view', $sitecontext)) {
90 print_error('cannotviewsiteblog', 'blog');
91 }
e96f2a77 92
856b6fe6 93 $COURSE = $DB->get_record('course', array('format'=>'site'));
94 $courseid = $COURSE->id;
95}
96
97if (!empty($courseid)) {
98 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
99 print_error('invalidcourseid');
100 }
101
102 $courseid = $course->id;
103 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
104
105 require_login($course);
106
107 if (!has_capability('moodle/blog:view', $coursecontext)) {
108 print_error('cannotviewcourseblog', 'blog');
109 }
110} else {
111 $coursecontext = get_context_instance(CONTEXT_COURSE, SITEID);
112}
113
114if (!empty($groupid)) {
115 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
116 print_error('groupblogdisable', 'blog');
117 }
e96f2a77 118
1c7b8b93 119 if (! $group = groups_get_group($groupid)) {
856b6fe6 120 print_error(get_string('invalidgroupid', 'blog'));
121 }
e96f2a77 122
856b6fe6 123 if (!$course = $DB->get_record('course', array('id'=>$group->courseid))) {
124 print_error(get_string('invalidcourseid', 'blog'));
125 }
e96f2a77 126
856b6fe6 127 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
128 $courseid = $course->id;
129 require_login($course);
130
131 if (!has_capability('moodle/blog:view', $coursecontext)) {
132 print_error(get_string('cannotviewcourseorgroupblog', 'blog'));
133 }
f5fc83e8 134
cae83708 135 if (groups_get_course_groupmode($course) == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $coursecontext)) {
856b6fe6 136 if (!groups_is_member($groupid)) {
137 print_error('notmemberofgroup');
e96f2a77 138 }
856b6fe6 139 }
140}
141
142if (!empty($user)) {
143 if ($CFG->bloglevel < BLOG_USER_LEVEL) {
144 print_error('blogdisable', 'blog');
145 }
146
147 if (!$user = $DB->get_record('user', array('id'=>$userid))) {
148 print_error('invaliduserid');
149 }
150
151 if ($user->deleted) {
cae83708 152 echo $OUTPUT->header();
16ba76bd 153 echo $OUTPUT->heading(get_string('userdeleted'));
033e4aff 154 echo $OUTPUT->footer();
856b6fe6 155 die;
156 }
f4c85f46 157
856b6fe6 158 if ($USER->id == $userid) {
159 if (!has_capability('moodle/blog:create', $sitecontext)
cae83708 160 && !has_capability('moodle/blog:view', $sitecontext)) {
856b6fe6 161 print_error('donothaveblog', 'blog');
673bc55d 162 }
856b6fe6 163 } else {
164 $personalcontext = get_context_instance(CONTEXT_USER, $userid);
673bc55d 165
cae83708 166 if (!has_capability('moodle/blog:view', $sitecontext) && !has_capability('moodle/user:readuserblogs', $personalcontext)) {
856b6fe6 167 print_error('cannotviewuserblog', 'blog');
168 }
e96f2a77 169
cae83708 170 if (!blog_user_can_view_user_entry($userid)) {
856b6fe6 171 print_error('cannotviewcourseblog', 'blog');
172 }
173 }
4a173181 174}
175
cae83708 176$courseid = (empty($courseid)) ? SITEID : $courseid;
f99fee6c 177
cae83708 178if (!empty($courseid)) {
cae83708 179 $PAGE->set_context(get_context_instance(CONTEXT_COURSE, $courseid));
856b6fe6 180}
181
cae83708 182if (!empty($modid)) {
cae83708 183 $PAGE->set_context(get_context_instance(CONTEXT_MODULE, $modid));
856b6fe6 184}
185
c5dc10ee 186$blogheaders = blog_get_headers();
cae83708 187
cae83708 188if (empty($entryid) && empty($modid) && empty($groupid)) {
1c7b8b93 189 $PAGE->set_context(get_context_instance(CONTEXT_USER, $USER->id));
cae83708 190}
191
e858c368
AD
192if ($CFG->enablerssfeeds) {
193 $rsscontext = $filtertype = $thingid = null;
194 list($thingid, $rsscontext, $filtertype) = blog_rss_get_params($blogheaders['filters']);
195
196 $rsstitle = $blogheaders['heading'];
197
198 //check we haven't started output by outputting an error message
199 if ($PAGE->state == moodle_page::STATE_BEFORE_HEADER) {
200 blog_rss_add_http_header($rsscontext, $rsstitle, $filtertype, $thingid, $tagid);
201 }
202
203 //this works but there isn't a great place to put the link
204 //blog_rss_print_link($rsscontext, $filtertype, $thingid, $tagid);
205}
206
c5dc10ee 207echo $OUTPUT->header();
cae83708 208
c5dc10ee 209echo $OUTPUT->heading($blogheaders['heading'], 2);
92a019ac 210
1c7b8b93
NC
211$bloglisting = new blog_listing($blogheaders['filters']);
212$bloglisting->print_entries();
4a173181 213
033e4aff 214echo $OUTPUT->footer();
d7bf6d17 215
cae83708 216add_to_log($courseid, 'blog', 'view', 'index.php?entryid='.$entryid.'&amp;tagid='.@$tagid.'&amp;tag='.$tag, 'view blog entry');