Merge branch 'MDL-31122-m21' of git://github.com/sammarshallou/moodle into MOODLE_21_...
[moodle.git] / blog / index.php
blobe1c0c22d4d5d1becab0c4582a6f81290a3660b75
1 <?php
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 */
9 require_once(dirname(dirname(__FILE__)).'/config.php');
10 require_once($CFG->dirroot .'/blog/lib.php');
11 require_once($CFG->dirroot .'/blog/locallib.php');
12 require_once($CFG->dirroot .'/course/lib.php');
13 require_once($CFG->dirroot .'/tag/lib.php');
14 require_once($CFG->dirroot .'/comment/lib.php');
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);
27 comment::init();
29 $url_params = compact('id', 'start', 'tag', 'userid', 'tagid', 'modid', 'entryid', 'groupid', 'courseid', 'search');
30 foreach ($url_params as $var => $val) {
31 if (empty($val)) {
32 unset($url_params[$var]);
35 $PAGE->set_url('/blog/index.php', $url_params);
37 if (empty($CFG->bloglevel)) {
38 print_error('blogdisable', 'blog');
41 //correct tagid if a text tag is provided as a param
42 if (!empty($tag)) {
43 if ($tagrec = $DB->get_record_sql("SELECT * FROM {tag} WHERE ". $DB->sql_like('name', '?', false), array("%$tag%"))) {
44 $tagid = $tagrec->id;
45 } else {
46 unset($tagid);
50 // add courseid if modid or groupid is specified: This is used for navigation and title
51 if (!empty($modid) && empty($courseid)) {
52 $courseid = $DB->get_field('course_modules', 'course', array('id'=>$modid));
55 if (!empty($groupid) && empty($courseid)) {
56 $courseid = $DB->get_field('groups', 'courseid', array('id'=>$groupid));
59 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
61 // check basic permissions
62 if ($CFG->bloglevel == BLOG_GLOBAL_LEVEL) {
63 // everybody can see anything - no login required unless site is locked down using forcelogin
64 if ($CFG->forcelogin) {
65 require_login();
68 } else if ($CFG->bloglevel == BLOG_SITE_LEVEL) {
69 // users must log in and can not be guests
70 require_login();
71 if (isguestuser()) {
72 // they must have entered the url manually...
73 print_error('blogdisable', 'blog');
76 } else if ($CFG->bloglevel == BLOG_USER_LEVEL) {
77 // users can see own blogs only! with the exception of ppl with special cap
78 require_login();
80 } else {
81 // weird!
82 print_error('blogdisable', 'blog');
86 if (!$userid && has_capability('moodle/blog:view', $sitecontext) && $CFG->bloglevel > BLOG_USER_LEVEL) {
87 if ($entryid) {
88 if (!$entryobject = $DB->get_record('post', array('id'=>$entryid))) {
89 print_error('nosuchentry', 'blog');
91 $userid = $entryobject->userid;
93 } else if (!$userid) {
94 $userid = $USER->id;
97 if (!empty($modid)) {
98 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
99 print_error(get_string('nocourseblogs', 'blog'));
101 if (!$mod = $DB->get_record('course_modules', array('id' => $modid))) {
102 print_error(get_string('invalidmodid', 'blog'));
104 $courseid = $mod->course;
107 if ((empty($courseid) ? true : $courseid == SITEID) && empty($userid)) {
108 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
109 print_error('siteblogdisable', 'blog');
111 if (!has_capability('moodle/blog:view', $sitecontext)) {
112 print_error('cannotviewsiteblog', 'blog');
115 $COURSE = $DB->get_record('course', array('format'=>'site'));
116 $courseid = $COURSE->id;
119 if (!empty($courseid)) {
120 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
121 print_error('invalidcourseid');
124 $courseid = $course->id;
125 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
127 require_login($course);
129 if (!has_capability('moodle/blog:view', $coursecontext)) {
130 print_error('cannotviewcourseblog', 'blog');
132 } else {
133 $coursecontext = get_context_instance(CONTEXT_COURSE, SITEID);
136 if (!empty($groupid)) {
137 if ($CFG->bloglevel < BLOG_SITE_LEVEL) {
138 print_error('groupblogdisable', 'blog');
141 if (! $group = groups_get_group($groupid)) {
142 print_error(get_string('invalidgroupid', 'blog'));
145 if (!$course = $DB->get_record('course', array('id'=>$group->courseid))) {
146 print_error(get_string('invalidcourseid', 'blog'));
149 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
150 $courseid = $course->id;
151 require_login($course);
153 if (!has_capability('moodle/blog:view', $coursecontext)) {
154 print_error(get_string('cannotviewcourseorgroupblog', 'blog'));
157 if (groups_get_course_groupmode($course) == SEPARATEGROUPS && !has_capability('moodle/site:accessallgroups', $coursecontext)) {
158 if (!groups_is_member($groupid)) {
159 print_error('notmemberofgroup');
164 if (!empty($userid)) {
165 if ($CFG->bloglevel < BLOG_USER_LEVEL) {
166 print_error('blogdisable', 'blog');
169 if (!$user = $DB->get_record('user', array('id'=>$userid))) {
170 print_error('invaliduserid');
173 if ($user->deleted) {
174 echo $OUTPUT->header();
175 echo $OUTPUT->heading(get_string('userdeleted'));
176 echo $OUTPUT->footer();
177 die;
180 if ($USER->id == $userid) {
181 if (!has_capability('moodle/blog:create', $sitecontext)
182 && !has_capability('moodle/blog:view', $sitecontext)) {
183 print_error('donothaveblog', 'blog');
185 } else {
186 $personalcontext = get_context_instance(CONTEXT_USER, $userid);
188 if (!has_capability('moodle/blog:view', $sitecontext) && !has_capability('moodle/user:readuserblogs', $personalcontext)) {
189 print_error('cannotviewuserblog', 'blog');
192 if (!blog_user_can_view_user_entry($userid)) {
193 print_error('cannotviewcourseblog', 'blog');
198 $courseid = (empty($courseid)) ? SITEID : $courseid;
200 if (!empty($courseid)) {
201 $PAGE->set_context(get_context_instance(CONTEXT_COURSE, $courseid));
204 if (!empty($modid)) {
205 $PAGE->set_context(get_context_instance(CONTEXT_MODULE, $modid));
208 $blogheaders = blog_get_headers();
210 if (empty($entryid) && empty($modid) && empty($groupid)) {
211 $PAGE->set_context(get_context_instance(CONTEXT_USER, $USER->id));
214 if ($CFG->enablerssfeeds) {
215 $rsscontext = null;
216 $filtertype = null;
217 $thingid = null;
218 list($thingid, $rsscontext, $filtertype) = blog_rss_get_params($blogheaders['filters']);
219 if (empty($rsscontext)) {
220 $rsscontext = get_system_context();
222 $rsstitle = $blogheaders['heading'];
224 //check we haven't started output by outputting an error message
225 if ($PAGE->state == moodle_page::STATE_BEFORE_HEADER) {
226 blog_rss_add_http_header($rsscontext, $rsstitle, $filtertype, $thingid, $tagid);
229 //this works but there isn't a great place to put the link
230 //blog_rss_print_link($rsscontext, $filtertype, $thingid, $tagid);
233 echo $OUTPUT->header();
235 echo $OUTPUT->heading($blogheaders['heading'], 2);
237 $bloglisting = new blog_listing($blogheaders['filters']);
238 $bloglisting->print_entries();
240 echo $OUTPUT->footer();
242 add_to_log($courseid, 'blog', 'view', 'index.php?entryid='.$entryid.'&amp;tagid='.@$tagid.'&amp;tag='.$tag, 'view blog entry');