MDL-20700 coding style cleanup - cvs keywords removed, closign php tag removed, trail...
[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
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
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
1c7b8b93
NC
38if (!empty($tag)) {
39 $ILIKE = $DB->sql_ilike();
40 if ($tagrec = $DB->get_record_sql("SELECT * FROM {tag} WHERE name $ILIKE ?", array("%$tag%"))) {
856b6fe6 41 $tagid = $tagrec->id;
42 } else {
43 unset($tagid);
44 }
45}
46
cae83708 47// add courseid if modid or groupid is specified: This is used for navigation and title
48if (!empty($modid) && empty($courseid)) {
856b6fe6 49 $courseid = $DB->get_field('course_modules', 'course', array('id'=>$modid));
50}
51
cae83708 52if (!empty($groupid) && empty($courseid)) {
856b6fe6 53 $courseid = $DB->get_field('groups', 'courseid', array('id'=>$groupid));
54}
4a173181 55
ab2f17b0 56if (empty($CFG->bloglevel)) {
8b141784 57 print_error('blogdisable', 'blog');
ab2f17b0 58}
bbbf2d40 59
d9d16e56 60$sitecontext = get_context_instance(CONTEXT_SYSTEM);
cae83708 61$tabsfile = null;
93caac3a 62
cae83708 63if (!$userid && has_capability('moodle/blog:view', $sitecontext) && $CFG->bloglevel > BLOG_USER_LEVEL) {
64 if ($entryid) {
1c7b8b93 65 if (!$entryobject = $DB->get_record('post', array('id'=>$entryid))) {
856b6fe6 66 print_error('nosuchentry', 'blog');
dde73b9e 67 }
cae83708 68 $userid = $entryobject->userid;
4a173181 69 }
856b6fe6 70} else if (!$userid) {
856b6fe6 71 $userid = $USER->id;
4a173181 72}
4a173181 73
1c7b8b93 74if (!empty($modid)) {
856b6fe6 75 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
76 print_error(get_string('nocourseblogs', 'blog'));
77 }
78 if (!$mod = $DB->get_record('course_modules', array('id' => $modid))) {
79 print_error(get_string('invalidmodid', 'blog'));
80 }
81 $courseid = $mod->course;
82}
e96f2a77 83
1c7b8b93 84if ((empty($courseid) ? true : $courseid == SITEID) && empty($userid)) {
856b6fe6 85 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
86 print_error('siteblogdisable', 'blog');
87 }
88 if ($CFG->bloglevel < BLOG_GLOBAL_LEVEL) {
89 require_login();
90 }
91 if (!has_capability('moodle/blog:view', $sitecontext)) {
92 print_error('cannotviewsiteblog', 'blog');
93 }
e96f2a77 94
856b6fe6 95 $COURSE = $DB->get_record('course', array('format'=>'site'));
96 $courseid = $COURSE->id;
97}
98
99if (!empty($courseid)) {
100 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
101 print_error('invalidcourseid');
102 }
103
104 $courseid = $course->id;
105 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
106
107 require_login($course);
108
109 if (!has_capability('moodle/blog:view', $coursecontext)) {
110 print_error('cannotviewcourseblog', 'blog');
111 }
112} else {
113 $coursecontext = get_context_instance(CONTEXT_COURSE, SITEID);
114}
115
116if (!empty($groupid)) {
117 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
118 print_error('groupblogdisable', 'blog');
119 }
e96f2a77 120
1c7b8b93 121 if (! $group = groups_get_group($groupid)) {
856b6fe6 122 print_error(get_string('invalidgroupid', 'blog'));
123 }
e96f2a77 124
856b6fe6 125 if (!$course = $DB->get_record('course', array('id'=>$group->courseid))) {
126 print_error(get_string('invalidcourseid', 'blog'));
127 }
e96f2a77 128
856b6fe6 129 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
130 $courseid = $course->id;
131 require_login($course);
132
133 if (!has_capability('moodle/blog:view', $coursecontext)) {
134 print_error(get_string('cannotviewcourseorgroupblog', 'blog'));
135 }
f5fc83e8 136
cae83708 137 if (groups_get_course_groupmode($course) == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $coursecontext)) {
856b6fe6 138 if (!groups_is_member($groupid)) {
139 print_error('notmemberofgroup');
e96f2a77 140 }
856b6fe6 141 }
142}
143
144if (!empty($user)) {
145 if ($CFG->bloglevel < BLOG_USER_LEVEL) {
146 print_error('blogdisable', 'blog');
147 }
148
149 if (!$user = $DB->get_record('user', array('id'=>$userid))) {
150 print_error('invaliduserid');
151 }
152
153 if ($user->deleted) {
cae83708 154 echo $OUTPUT->header();
16ba76bd 155 echo $OUTPUT->heading(get_string('userdeleted'));
033e4aff 156 echo $OUTPUT->footer();
856b6fe6 157 die;
158 }
f4c85f46 159
856b6fe6 160 if ($USER->id == $userid) {
161 if (!has_capability('moodle/blog:create', $sitecontext)
cae83708 162 && !has_capability('moodle/blog:view', $sitecontext)) {
856b6fe6 163 print_error('donothaveblog', 'blog');
673bc55d 164 }
856b6fe6 165 } else {
166 $personalcontext = get_context_instance(CONTEXT_USER, $userid);
673bc55d 167
cae83708 168 if (!has_capability('moodle/blog:view', $sitecontext) && !has_capability('moodle/user:readuserblogs', $personalcontext)) {
856b6fe6 169 print_error('cannotviewuserblog', 'blog');
170 }
e96f2a77 171
cae83708 172 if (!blog_user_can_view_user_entry($userid)) {
856b6fe6 173 print_error('cannotviewcourseblog', 'blog');
174 }
175 }
cae83708 176 $tabsfile = $CFG->dirroot . '/user/tabs.php';
4a173181 177}
178
cae83708 179$courseid = (empty($courseid)) ? SITEID : $courseid;
f99fee6c 180
cae83708 181if (!empty($courseid)) {
cae83708 182 $PAGE->set_context(get_context_instance(CONTEXT_COURSE, $courseid));
856b6fe6 183}
184
cae83708 185if (!empty($modid)) {
cae83708 186 $PAGE->set_context(get_context_instance(CONTEXT_MODULE, $modid));
856b6fe6 187}
188
c5dc10ee 189$blogheaders = blog_get_headers();
23677261 190blog_extend_settings_navigation($PAGE->settingsnav);
cae83708 191
cae83708 192if (empty($entryid) && empty($modid) && empty($groupid)) {
1c7b8b93 193 $PAGE->set_context(get_context_instance(CONTEXT_USER, $USER->id));
cae83708 194}
195
c5dc10ee 196echo $OUTPUT->header();
cae83708 197
198if (!empty($tabsfile)) {
199 require_once($tabsfile);
200}
4a173181 201
c5dc10ee 202echo $OUTPUT->heading($blogheaders['heading'], 2);
92a019ac 203
1c7b8b93
NC
204$bloglisting = new blog_listing($blogheaders['filters']);
205$bloglisting->print_entries();
4a173181 206
033e4aff 207echo $OUTPUT->footer();
d7bf6d17 208
cae83708 209add_to_log($courseid, 'blog', 'view', 'index.php?entryid='.$entryid.'&amp;tagid='.@$tagid.'&amp;tag='.$tag, 'view blog entry');