navigation MDL-20216 Refactored add arguments
[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');
4a173181 14
cae83708 15$id = optional_param('id', null, PARAM_INT);
93caac3a 16$start = optional_param('formstart', 0, PARAM_INT);
93caac3a 17$tag = optional_param('tag', '', PARAM_NOTAGS);
cae83708 18$userid = optional_param('userid', null, PARAM_INT);
19$tagid = optional_param('tagid', null, PARAM_INT);
20$modid = optional_param('modid', null, PARAM_INT);
21$entryid = optional_param('entryid', null, PARAM_INT);
22$groupid = optional_param('groupid', null, PARAM_INT);
23$courseid = optional_param('courseid', null, PARAM_INT); // needed for user tabs and course tracking
24$search = optional_param('search', null, PARAM_RAW);
25
26$url_params = compact('id', 'start', 'tag', 'userid', 'tagid', 'modid', 'entryid', 'groupid', 'courseid', 'search');
27foreach ($url_params as $var => $val) {
28 if (empty($val)) {
29 unset($url_params[$var]);
30 }
31}
32$PAGE->set_url('blog/index.php', $url_params);
e96f2a77 33
856b6fe6 34//correct tagid if a text tag is provided as a param
35if (!empty($tag)) { //text tag parameter takes precedence
36 if ($tagrec = $DB->get_record_sql("SELECT * FROM {tag} WHERE name LIKE ?", array($tag))) {
37 $tagid = $tagrec->id;
38 } else {
39 unset($tagid);
40 }
41}
42
cae83708 43// add courseid if modid or groupid is specified: This is used for navigation and title
44if (!empty($modid) && empty($courseid)) {
856b6fe6 45 $courseid = $DB->get_field('course_modules', 'course', array('id'=>$modid));
46}
47
cae83708 48if (!empty($groupid) && empty($courseid)) {
856b6fe6 49 $courseid = $DB->get_field('groups', 'courseid', array('id'=>$groupid));
50}
4a173181 51
ab2f17b0 52if (empty($CFG->bloglevel)) {
8b141784 53 print_error('blogdisable', 'blog');
ab2f17b0 54}
bbbf2d40 55
d9d16e56 56$sitecontext = get_context_instance(CONTEXT_SYSTEM);
cae83708 57$tabsfile = null;
93caac3a 58
cae83708 59if (!$userid && has_capability('moodle/blog:view', $sitecontext) && $CFG->bloglevel > BLOG_USER_LEVEL) {
60 if ($entryid) {
61 if (!$entryobject = $DB->get_record('post', array('module'=>'blog', 'id'=>$entryid))) {
856b6fe6 62 print_error('nosuchentry', 'blog');
dde73b9e 63 }
cae83708 64 $userid = $entryobject->userid;
4a173181 65 }
856b6fe6 66} else if (!$userid) {
67 // user might have capability to write blogs, but not read blogs at site level
68 // users might enter this url manually without parameters
69 $userid = $USER->id;
4a173181 70}
e96f2a77 71/// check access and prepare filters
4a173181 72
856b6fe6 73if (!empty($modid)) { //check mod access
74 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
75 print_error(get_string('nocourseblogs', 'blog'));
76 }
77 if (!$mod = $DB->get_record('course_modules', array('id' => $modid))) {
78 print_error(get_string('invalidmodid', 'blog'));
79 }
80 $courseid = $mod->course;
81}
e96f2a77 82
cae83708 83if ((empty($courseid) ? true : $courseid == SITEID) && empty($userid)) { //check site access
856b6fe6 84 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
85 print_error('siteblogdisable', 'blog');
86 }
87 if ($CFG->bloglevel < BLOG_GLOBAL_LEVEL) {
88 require_login();
89 }
90 if (!has_capability('moodle/blog:view', $sitecontext)) {
91 print_error('cannotviewsiteblog', 'blog');
92 }
e96f2a77 93
856b6fe6 94 $COURSE = $DB->get_record('course', array('format'=>'site'));
95 $courseid = $COURSE->id;
96}
97
98if (!empty($courseid)) {
99 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
100 print_error('invalidcourseid');
101 }
102
103 $courseid = $course->id;
104 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
105
106 require_login($course);
107
108 if (!has_capability('moodle/blog:view', $coursecontext)) {
109 print_error('cannotviewcourseblog', 'blog');
110 }
111} else {
112 $coursecontext = get_context_instance(CONTEXT_COURSE, SITEID);
113}
114
115if (!empty($groupid)) {
116 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
117 print_error('groupblogdisable', 'blog');
118 }
e96f2a77 119
11993a60 120 // fix for MDL-9268
856b6fe6 121 if (! $group = groups_get_group($groupid)) { //TODO:check.
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)) {
856b6fe6 182 $filters['course'] = $courseid;
cae83708 183 $PAGE->set_context(get_context_instance(CONTEXT_COURSE, $courseid));
856b6fe6 184}
185
cae83708 186if (!empty($modid)) {
187 $filters['module'] = $modid;
188 $PAGE->set_context(get_context_instance(CONTEXT_MODULE, $modid));
856b6fe6 189}
190
cae83708 191if (!empty($groupid)) {
856b6fe6 192 $filters['group'] = $groupid;
193}
194
cae83708 195if (!empty($userid)) {
856b6fe6 196 $filters['user'] = $userid;
197}
198
cae83708 199if (!empty($tagid)) {
856b6fe6 200 $filters['tag'] = $tagid;
201}
202
cae83708 203if (!empty($search)) {
204 $filters['search'] = $search;
205}
4a173181 206
cae83708 207if (!empty($entryid)) {
208 $filters['entry'] = $entryid;
209}
210$blog_headers = blog_get_headers();
211
212$navigation = build_navigation($blog_headers['navlinks'], $blog_headers['cm']);
213
214// prints the tabs
215$showroles = !empty($userid);
216$currenttab = 'blogs';
217
218$user = $USER;
219$userid = $USER->id;
220
221// Unless this is a user's blog listing, the context is system
222if (empty($entryid) && empty($modid) && empty($groupid)) {
223 $PAGE->set_context(get_context_instance(CONTEXT_USER, $userid));
224}
225
226$PAGE->set_title($blog_headers['title']);
227$PAGE->set_heading($blog_headers['title']);
228
229echo $OUTPUT->header($navigation);
230
231if (!empty($tabsfile)) {
232 require_once($tabsfile);
233}
4a173181 234
cae83708 235echo $OUTPUT->heading($blog_headers['heading'], 2);
92a019ac 236
cae83708 237$blog_listing = new blog_listing($filters);
238$blog_listing->print_entries();
4a173181 239
033e4aff 240echo $OUTPUT->footer();
d7bf6d17 241
cae83708 242add_to_log($courseid, 'blog', 'view', 'index.php?entryid='.$entryid.'&amp;tagid='.@$tagid.'&amp;tag='.$tag, 'view blog entry');