MDL-19793 Converted all print_footer() calls
[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
eccfc1ca 9require_once('../config.php');
4a173181 10require_once($CFG->dirroot .'/blog/lib.php');
4a173181 11
93caac3a 12$id = optional_param('id', 0, PARAM_INT);
93caac3a 13$start = optional_param('formstart', 0, PARAM_INT);
db32b054 14$userid = optional_param('userid', 0, PARAM_INT);
93caac3a 15$tag = optional_param('tag', '', PARAM_NOTAGS);
16$tagid = optional_param('tagid', 0, PARAM_INT);
db32b054 17$postid = optional_param('postid', 0, PARAM_INT);
856b6fe6 18$listing_type = optional_param('listing_type', '', PARAM_ALPHA);
19$listing_id = optional_param('listing_id', null, PARAM_INT);
93caac3a 20$edit = optional_param('edit', -1, PARAM_BOOL);
e96f2a77 21$courseid = optional_param('courseid', 0, PARAM_INT); // needed for user tabs and course tracking
22
856b6fe6 23//correct tagid if a text tag is provided as a param
24if (!empty($tag)) { //text tag parameter takes precedence
25 if ($tagrec = $DB->get_record_sql("SELECT * FROM {tag} WHERE name LIKE ?", array($tag))) {
26 $tagid = $tagrec->id;
27 } else {
28 unset($tagid);
29 }
30}
31
32//add courseid if modid or groupid is specified
33if (!empty($modid) and empty($courseid)) {
34 $courseid = $DB->get_field('course_modules', 'course', array('id'=>$modid));
35}
36
37if (!empty($groupid) and empty($courseid)) {
38 $courseid = $DB->get_field('groups', 'courseid', array('id'=>$groupid));
39}
4a173181 40
ab2f17b0 41if (empty($CFG->bloglevel)) {
8b141784 42 print_error('blogdisable', 'blog');
ab2f17b0 43}
bbbf2d40 44
d9d16e56 45$sitecontext = get_context_instance(CONTEXT_SYSTEM);
93caac3a 46
93caac3a 47// change block edit staus if not guest and logged in
48if (isloggedin() and !isguest() and $edit != -1) {
830dd6e9 49 $USER->editing = $edit;
93caac3a 50}
51
856b6fe6 52if (!$userid and has_capability('moodle/blog:view', $sitecontext) and $CFG->bloglevel > BLOG_USER_LEVEL) {
53 if ($postid) {
54 if (!$postobject = $DB->get_record('post', array('module'=>'blog', 'id'=>$postid))) {
55 print_error('nosuchentry', 'blog');
dde73b9e 56 }
856b6fe6 57 $userid = $postobject->userid;
4a173181 58 }
856b6fe6 59} else if (!$userid) {
60 // user might have capability to write blogs, but not read blogs at site level
61 // users might enter this url manually without parameters
62 $userid = $USER->id;
4a173181 63}
e96f2a77 64/// check access and prepare filters
4a173181 65
856b6fe6 66if (!empty($modid)) { //check mod access
67 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
68 print_error(get_string('nocourseblogs', 'blog'));
69 }
70 if (!$mod = $DB->get_record('course_modules', array('id' => $modid))) {
71 print_error(get_string('invalidmodid', 'blog'));
72 }
73 $courseid = $mod->course;
74}
e96f2a77 75
856b6fe6 76if ((empty($courseid) ? true : $courseid == SITEID) and empty($userid)) { //check site access
77 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
78 print_error('siteblogdisable', 'blog');
79 }
80 if ($CFG->bloglevel < BLOG_GLOBAL_LEVEL) {
81 require_login();
82 }
83 if (!has_capability('moodle/blog:view', $sitecontext)) {
84 print_error('cannotviewsiteblog', 'blog');
85 }
e96f2a77 86
856b6fe6 87 $COURSE = $DB->get_record('course', array('format'=>'site'));
88 $courseid = $COURSE->id;
89}
90
91if (!empty($courseid)) {
92 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
93 print_error('invalidcourseid');
94 }
95
96 $courseid = $course->id;
97 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
98
99 require_login($course);
100
101 if (!has_capability('moodle/blog:view', $coursecontext)) {
102 print_error('cannotviewcourseblog', 'blog');
103 }
104} else {
105 $coursecontext = get_context_instance(CONTEXT_COURSE, SITEID);
106}
107
108if (!empty($groupid)) {
109 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
110 print_error('groupblogdisable', 'blog');
111 }
e96f2a77 112
11993a60 113 // fix for MDL-9268
856b6fe6 114 if (! $group = groups_get_group($groupid)) { //TODO:check.
115 print_error(get_string('invalidgroupid', 'blog'));
116 }
e96f2a77 117
856b6fe6 118 if (!$course = $DB->get_record('course', array('id'=>$group->courseid))) {
119 print_error(get_string('invalidcourseid', 'blog'));
120 }
e96f2a77 121
856b6fe6 122 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
123 $courseid = $course->id;
124 require_login($course);
125
126 if (!has_capability('moodle/blog:view', $coursecontext)) {
127 print_error(get_string('cannotviewcourseorgroupblog', 'blog'));
128 }
f5fc83e8 129
856b6fe6 130 if (groups_get_course_groupmode($course) == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $coursecontext)) {
131 if (!groups_is_member($groupid)) {
132 print_error('notmemberofgroup');
e96f2a77 133 }
856b6fe6 134 }
135}
136
137if (!empty($user)) {
138 if ($CFG->bloglevel < BLOG_USER_LEVEL) {
139 print_error('blogdisable', 'blog');
140 }
141
142 if (!$user = $DB->get_record('user', array('id'=>$userid))) {
143 print_error('invaliduserid');
144 }
145
146 if ($user->deleted) {
147 print_header();
16ba76bd 148 echo $OUTPUT->heading(get_string('userdeleted'));
856b6fe6 149 print_footer();
150 die;
151 }
f4c85f46 152
856b6fe6 153 if ($USER->id == $userid) {
154 if (!has_capability('moodle/blog:create', $sitecontext)
155 and !has_capability('moodle/blog:view', $sitecontext)) {
156 print_error('donothaveblog', 'blog');
673bc55d 157 }
856b6fe6 158 } else {
159 $personalcontext = get_context_instance(CONTEXT_USER, $userid);
673bc55d 160
856b6fe6 161 if (!has_capability('moodle/blog:view', $sitecontext) and !has_capability('moodle/user:readuserblogs', $personalcontext)) {
162 print_error('cannotviewuserblog', 'blog');
163 }
e96f2a77 164
856b6fe6 165 if (!blog_user_can_view_user_post($userid)) {
166 print_error('cannotviewcourseblog', 'blog');
167 }
168 }
4a173181 169}
170
e96f2a77 171if (empty($courseid)) {
172 $courseid = SITEID;
4a173181 173}
f99fee6c 174
856b6fe6 175if(!empty($postid)) {
176 $filters['post'] = $postid;
177}
178
179if(!empty($courseid)) {
180 $filters['course'] = $courseid;
181}
182
183if(!empty($modid)) {
184 $filters['mod'] = $modid;
185}
186
187if(!empty($groupid)) {
188 $filters['group'] = $groupid;
189}
190
191if(!empty($userid)) {
192 $filters['user'] = $userid;
193}
194
195if(!empty($tagid)) {
196 $filters['tag'] = $tagid;
197}
198
199$PAGE->title = get_string('blog');
4a173181 200include($CFG->dirroot .'/blog/header.php');
201
e96f2a77 202blog_print_html_formatted_entries($postid, $filtertype, $filterselect, $tagid, $tag);
4a173181 203
06622f92 204add_to_log($courseid, 'blog', 'view', 'index.php?filtertype='.$filtertype.'&amp;filterselect='.$filterselect.'&amp;postid='.$postid.'&amp;tagid='.$tagid.'&amp;tag='.$tag, 'view blog entry');
92a019ac 205
4a173181 206include($CFG->dirroot .'/blog/footer.php');
207
856b6fe6 208print_footer();
d7bf6d17 209
4b10f08b 210?>