MDL-19677 Upgrading sites that use $CFG->bloglevel == BLOG_GROUP_LEVEL or BLOG_COURSE...
[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);
93caac3a 18$filtertype = optional_param('filtertype', '', PARAM_ALPHA);
9154b440 19$filterselect = optional_param('filterselect', 0, PARAM_INT);
e96f2a77 20
93caac3a 21$edit = optional_param('edit', -1, PARAM_BOOL);
e96f2a77 22$courseid = optional_param('courseid', 0, PARAM_INT); // needed for user tabs and course tracking
23
4a173181 24
ab2f17b0 25if (empty($CFG->bloglevel)) {
8b141784 26 print_error('blogdisable', 'blog');
ab2f17b0 27}
bbbf2d40 28
d9d16e56 29$sitecontext = get_context_instance(CONTEXT_SYSTEM);
93caac3a 30
93caac3a 31// change block edit staus if not guest and logged in
32if (isloggedin() and !isguest() and $edit != -1) {
830dd6e9 33 $USER->editing = $edit;
93caac3a 34}
35
e96f2a77 36if (empty($filtertype)) {
37 if ($userid) { // default to user if specified
38 $filtertype = 'user';
39 $filterselect = $userid;
40 } else if (has_capability('moodle/blog:view', $sitecontext) and $CFG->bloglevel > BLOG_USER_LEVEL) {
dde73b9e 41 if ($postid) {
42 $filtertype = 'user';
45df7de3 43 if (!$postobject = $DB->get_record('post', array('module'=>'blog', 'id'=>$postid))) {
9afff849 44 print_error('nosuchentry', 'blog');
dde73b9e 45 }
46 $filterselect = $postobject->userid;
47 } else {
48 $filtertype = 'site';
49 $filterselect = '';
50 }
e96f2a77 51 } else {
52 // user might have capability to write blogs, but not read blogs at site level
53 // users might enter this url manually without parameters
54 $filtertype = 'user';
55 $filterselect = $USER->id;
4a173181 56 }
4a173181 57}
e96f2a77 58/// check access and prepare filters
4a173181 59
60switch ($filtertype) {
e96f2a77 61
4a173181 62 case 'site':
d7bf6d17 63 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
8b141784 64 print_error('siteblogdisable', 'blog');
e96f2a77 65 }
66 if ($CFG->bloglevel < BLOG_GLOBAL_LEVEL) {
4a173181 67 require_login();
68 }
e96f2a77 69 if (!has_capability('moodle/blog:view', $sitecontext)) {
8b141784 70 print_error('cannotviewsiteblog', 'blog');
e96f2a77 71 }
4a173181 72 break;
e96f2a77 73
4a173181 74 case 'course':
d7bf6d17 75 if ($CFG->bloglevel < BLOG_COURSE_LEVEL) {
8b141784 76 print_error('courseblogdisable', 'blog');
4a173181 77 }
45df7de3 78 if (!$course = $DB->get_record('course', array('id'=>$filterselect))) {
8b141784 79 print_error('invalidcourseid');
e96f2a77 80 }
81 $courseid = $course->id;
82 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
673bc55d 83 require_login($course);
11b03793 84 if (!has_capability('moodle/blog:view', $coursecontext)) {
8b141784 85 print_error('cannotviewcourseblog', 'blog');
e96f2a77 86 }
4a173181 87 break;
e96f2a77 88
4a173181 89 case 'group':
d7bf6d17 90 if ($CFG->bloglevel < BLOG_GROUP_LEVEL) {
8b141784 91 print_error('groupblogdisable', 'blog');
e96f2a77 92 }
11993a60 93
94 // fix for MDL-9268
95 if (! $group = groups_get_group($filterselect)) { //TODO:check.
8b141784 96 print_error('invalidgroupid');
e96f2a77 97 }
45df7de3 98 if (!$course = $DB->get_record('course', array('id'=>$group->courseid))) {
8b141784 99 print_error('invalidcourseid');
4a173181 100 }
e96f2a77 101 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
102 $courseid = $course->id;
673bc55d 103 require_login($course);
e96f2a77 104 if (!has_capability('moodle/blog:view', $coursecontext)) {
8b141784 105 print_error('cannotviewcourseorgroupblog', 'blog');
e96f2a77 106 }
ffc536af 107 if (groups_get_course_groupmode($course) == SEPARATEGROUPS
e96f2a77 108 and !has_capability('moodle/site:accessallgroups', $coursecontext)) {
2c386f82 109 if (!groups_is_member($filterselect)) {
8b141784 110 print_error('notmemberofgroup');
4a173181 111 }
112 }
e96f2a77 113
4a173181 114 break;
e96f2a77 115
4a173181 116 case 'user':
d7bf6d17 117 if ($CFG->bloglevel < BLOG_USER_LEVEL) {
8b141784 118 print_error('blogdisable', 'blog');
4a173181 119 }
45df7de3 120 if (!$user = $DB->get_record('user', array('id'=>$filterselect))) {
8b141784 121 print_error('invaliduserid');
f4c85f46 122 }
f5fc83e8 123 if ($user->deleted) {
124 print_header();
125 print_heading(get_string('userdeleted'));
126 print_footer();
127 die;
128 }
129
e96f2a77 130 if ($USER->id == $filterselect) {
131 if (!has_capability('moodle/blog:create', $sitecontext)
132 and !has_capability('moodle/blog:view', $sitecontext)) {
8b141784 133 print_error('donothaveblog', 'blog');
e96f2a77 134 }
135 } else {
136 $personalcontext = get_context_instance(CONTEXT_USER, $filterselect);
137 if (!has_capability('moodle/blog:view', $sitecontext)
138 and !has_capability('moodle/user:readuserblogs', $personalcontext)) {
8b141784 139 print_error('cannotviewuserblog', 'blog');
e96f2a77 140 }
141 if (!blog_user_can_view_user_post($filterselect)) {
8b141784 142 print_error('cannotviewcourseblog', 'blog');
e96f2a77 143 }
144 }
145 $userid = $filterselect;
f4c85f46 146
673bc55d 147 if (!empty($courseid)) {
148 require_login($courseid);
149 }
150
4a173181 151 break;
e96f2a77 152
4a173181 153 default:
8b141784 154 print_error('incorrectblogfilter', 'blog');
4a173181 155 break;
156}
157
e96f2a77 158if (empty($courseid)) {
159 $courseid = SITEID;
4a173181 160}
f99fee6c 161
4a173181 162include($CFG->dirroot .'/blog/header.php');
163
e96f2a77 164blog_print_html_formatted_entries($postid, $filtertype, $filterselect, $tagid, $tag);
4a173181 165
06622f92 166add_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 167
4a173181 168include($CFG->dirroot .'/blog/footer.php');
169
d7bf6d17 170
4b10f08b 171?>