weekly release 2.3dev
[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
8f6c1f34
PS
37if (empty($CFG->bloglevel)) {
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
8f6c1f34
PS
59$sitecontext = get_context_instance(CONTEXT_SYSTEM);
60
61// check basic permissions
62if ($CFG->bloglevel == BLOG_GLOBAL_LEVEL) {
63 // everybody can see anything - no login required unless site is locked down using forcelogin
64 if ($CFG->forcelogin) {
65 require_login();
66 }
67
68} else if ($CFG->bloglevel == BLOG_SITE_LEVEL) {
69 // users must log in and can not be guests
70 require_login();
71 if (isguestuser()) {
72 // they must have entered the url manually...
73 print_error('blogdisable', 'blog');
74 }
75
76} else if ($CFG->bloglevel == BLOG_USER_LEVEL) {
77 // users can see own blogs only! with the exception of ppl with special cap
78 require_login();
79
80} else {
81 // weird!
8b141784 82 print_error('blogdisable', 'blog');
ab2f17b0 83}
bbbf2d40 84
93caac3a 85
cae83708 86if (!$userid && has_capability('moodle/blog:view', $sitecontext) && $CFG->bloglevel > BLOG_USER_LEVEL) {
87 if ($entryid) {
1c7b8b93 88 if (!$entryobject = $DB->get_record('post', array('id'=>$entryid))) {
856b6fe6 89 print_error('nosuchentry', 'blog');
dde73b9e 90 }
cae83708 91 $userid = $entryobject->userid;
4a173181 92 }
856b6fe6 93} else if (!$userid) {
856b6fe6 94 $userid = $USER->id;
4a173181 95}
4a173181 96
1c7b8b93 97if (!empty($modid)) {
856b6fe6 98 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
99 print_error(get_string('nocourseblogs', 'blog'));
100 }
101 if (!$mod = $DB->get_record('course_modules', array('id' => $modid))) {
102 print_error(get_string('invalidmodid', 'blog'));
103 }
104 $courseid = $mod->course;
105}
e96f2a77 106
1c7b8b93 107if ((empty($courseid) ? true : $courseid == SITEID) && empty($userid)) {
856b6fe6 108 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
109 print_error('siteblogdisable', 'blog');
110 }
856b6fe6 111 if (!has_capability('moodle/blog:view', $sitecontext)) {
112 print_error('cannotviewsiteblog', 'blog');
113 }
e96f2a77 114
856b6fe6 115 $COURSE = $DB->get_record('course', array('format'=>'site'));
116 $courseid = $COURSE->id;
117}
118
119if (!empty($courseid)) {
120 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
121 print_error('invalidcourseid');
122 }
123
124 $courseid = $course->id;
125 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
126
127 require_login($course);
128
129 if (!has_capability('moodle/blog:view', $coursecontext)) {
130 print_error('cannotviewcourseblog', 'blog');
131 }
132} else {
133 $coursecontext = get_context_instance(CONTEXT_COURSE, SITEID);
134}
135
136if (!empty($groupid)) {
137 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
138 print_error('groupblogdisable', 'blog');
139 }
e96f2a77 140
1c7b8b93 141 if (! $group = groups_get_group($groupid)) {
856b6fe6 142 print_error(get_string('invalidgroupid', 'blog'));
143 }
e96f2a77 144
856b6fe6 145 if (!$course = $DB->get_record('course', array('id'=>$group->courseid))) {
146 print_error(get_string('invalidcourseid', 'blog'));
147 }
e96f2a77 148
856b6fe6 149 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
150 $courseid = $course->id;
151 require_login($course);
152
153 if (!has_capability('moodle/blog:view', $coursecontext)) {
154 print_error(get_string('cannotviewcourseorgroupblog', 'blog'));
155 }
f5fc83e8 156
cae83708 157 if (groups_get_course_groupmode($course) == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $coursecontext)) {
856b6fe6 158 if (!groups_is_member($groupid)) {
159 print_error('notmemberofgroup');
e96f2a77 160 }
856b6fe6 161 }
162}
163
b29ce44d 164if (!empty($userid)) {
856b6fe6 165 if ($CFG->bloglevel < BLOG_USER_LEVEL) {
166 print_error('blogdisable', 'blog');
167 }
168
169 if (!$user = $DB->get_record('user', array('id'=>$userid))) {
170 print_error('invaliduserid');
171 }
172
173 if ($user->deleted) {
cae83708 174 echo $OUTPUT->header();
16ba76bd 175 echo $OUTPUT->heading(get_string('userdeleted'));
033e4aff 176 echo $OUTPUT->footer();
856b6fe6 177 die;
178 }
f4c85f46 179
856b6fe6 180 if ($USER->id == $userid) {
181 if (!has_capability('moodle/blog:create', $sitecontext)
cae83708 182 && !has_capability('moodle/blog:view', $sitecontext)) {
856b6fe6 183 print_error('donothaveblog', 'blog');
673bc55d 184 }
856b6fe6 185 } else {
186 $personalcontext = get_context_instance(CONTEXT_USER, $userid);
673bc55d 187
cae83708 188 if (!has_capability('moodle/blog:view', $sitecontext) && !has_capability('moodle/user:readuserblogs', $personalcontext)) {
856b6fe6 189 print_error('cannotviewuserblog', 'blog');
190 }
e96f2a77 191
cae83708 192 if (!blog_user_can_view_user_entry($userid)) {
856b6fe6 193 print_error('cannotviewcourseblog', 'blog');
194 }
195 }
4a173181 196}
197
cae83708 198$courseid = (empty($courseid)) ? SITEID : $courseid;
f99fee6c 199
ae6313d4
JF
200if (empty($entryid) && empty($modid) && empty($groupid)) {
201 $PAGE->set_context(context_user::instance($USER->id));
202} else if (!empty($modid)) {
203 $PAGE->set_context(context_module::instance($modid));
204} else if (!empty($courseid)) {
205 $PAGE->set_context(context_course::instance($courseid));
206} else {
207 $PAGE->set_context(context_system::instance());
856b6fe6 208}
209
c5dc10ee 210$blogheaders = blog_get_headers();
cae83708 211
e858c368 212if ($CFG->enablerssfeeds) {
43c4f4d1
JF
213 $rsscontext = null;
214 $filtertype = null;
215 $thingid = null;
e858c368 216 list($thingid, $rsscontext, $filtertype) = blog_rss_get_params($blogheaders['filters']);
43c4f4d1
JF
217 if (empty($rsscontext)) {
218 $rsscontext = get_system_context();
219 }
e858c368
AD
220 $rsstitle = $blogheaders['heading'];
221
222 //check we haven't started output by outputting an error message
223 if ($PAGE->state == moodle_page::STATE_BEFORE_HEADER) {
224 blog_rss_add_http_header($rsscontext, $rsstitle, $filtertype, $thingid, $tagid);
225 }
226
227 //this works but there isn't a great place to put the link
228 //blog_rss_print_link($rsscontext, $filtertype, $thingid, $tagid);
229}
230
c5dc10ee 231echo $OUTPUT->header();
cae83708 232
c5dc10ee 233echo $OUTPUT->heading($blogheaders['heading'], 2);
92a019ac 234
1c7b8b93
NC
235$bloglisting = new blog_listing($blogheaders['filters']);
236$bloglisting->print_entries();
4a173181 237
033e4aff 238echo $OUTPUT->footer();
d7bf6d17 239
cae83708 240add_to_log($courseid, 'blog', 'view', 'index.php?entryid='.$entryid.'&amp;tagid='.@$tagid.'&amp;tag='.$tag, 'view blog entry');