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