2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * index page to view blogs. if no blog is specified then site wide entries are shown
20 * if a blog id is specified then the latest entries from that blog are shown
23 require_once(dirname(dirname(__FILE__
)).'/config.php');
24 require_once($CFG->dirroot
.'/blog/lib.php');
25 require_once($CFG->dirroot
.'/blog/locallib.php');
26 require_once($CFG->dirroot
.'/course/lib.php');
27 require_once($CFG->dirroot
.'/tag/lib.php');
28 require_once($CFG->dirroot
.'/comment/lib.php');
30 $id = optional_param('id', null, PARAM_INT
);
31 $start = optional_param('formstart', 0, PARAM_INT
);
32 $tag = optional_param('tag', '', PARAM_NOTAGS
);
33 $userid = optional_param('userid', null, PARAM_INT
);
34 $tagid = optional_param('tagid', null, PARAM_INT
);
35 $modid = optional_param('modid', null, PARAM_INT
);
36 $entryid = optional_param('entryid', null, PARAM_INT
);
37 $groupid = optional_param('groupid', null, PARAM_INT
);
38 $courseid = optional_param('courseid', null, PARAM_INT
);
39 $search = optional_param('search', null, PARAM_RAW
);
43 $url_params = compact('id', 'start', 'tag', 'userid', 'tagid', 'modid', 'entryid', 'groupid', 'courseid', 'search');
44 foreach ($url_params as $var => $val) {
46 unset($url_params[$var]);
49 $PAGE->set_url('/blog/index.php', $url_params);
51 // Correct tagid if a text tag is provided as a param.
53 if ($tagrec = $DB->get_record('tag', array('name' => $tag))) {
60 $sitecontext = context_system
::instance();
61 // Blogs are always in system context.
62 $PAGE->set_context($sitecontext);
64 // Check basic permissions.
65 if ($CFG->bloglevel
== BLOG_GLOBAL_LEVEL
) {
66 // Everybody can see anything - no login required unless site is locked down using forcelogin.
67 if ($CFG->forcelogin
) {
71 } else if ($CFG->bloglevel
== BLOG_SITE_LEVEL
) {
72 // Users must log in and can not be guests.
75 // They must have entered the url manually.
76 print_error('blogdisable', 'blog');
79 } else if ($CFG->bloglevel
== BLOG_USER_LEVEL
) {
80 // Users can see own blogs only! with the exception of people with special cap.
85 print_error('blogdisable', 'blog');
88 if (empty($CFG->enableblogs
)) {
89 print_error('blogdisable', 'blog');
92 // Add courseid if modid or groupid is specified: This is used for navigation and title.
93 if (!empty($modid) && empty($courseid)) {
94 $courseid = $DB->get_field('course_modules', 'course', array('id' => $modid));
97 if (!empty($groupid) && empty($courseid)) {
98 $courseid = $DB->get_field('groups', 'courseid', array('id' => $groupid));
102 if (!$userid && has_capability('moodle/blog:view', $sitecontext) && $CFG->bloglevel
> BLOG_USER_LEVEL
) {
104 if (!$entryobject = $DB->get_record('post', array('id'=>$entryid))) {
105 print_error('nosuchentry', 'blog');
107 $userid = $entryobject->userid
;
109 } else if (!$userid) {
113 if (!empty($modid)) {
114 if ($CFG->bloglevel
< BLOG_SITE_LEVEL
) {
115 print_error(get_string('nocourseblogs', 'blog'));
117 if (!$mod = $DB->get_record('course_modules', array('id' => $modid))) {
118 print_error(get_string('invalidmodid', 'blog'));
120 $courseid = $mod->course
;
123 if ((empty($courseid) ?
true : $courseid == SITEID
) && empty($userid)) {
124 if ($CFG->bloglevel
< BLOG_SITE_LEVEL
) {
125 print_error('siteblogdisable', 'blog');
127 if (!has_capability('moodle/blog:view', $sitecontext)) {
128 print_error('cannotviewsiteblog', 'blog');
131 $COURSE = $DB->get_record('course', array('format'=>'site'));
132 $courseid = $COURSE->id
;
135 if (!empty($courseid)) {
136 if (!$course = $DB->get_record('course', array('id'=>$courseid))) {
137 print_error('invalidcourseid');
140 $courseid = $course->id
;
141 require_login($course);
143 if (!has_capability('moodle/blog:view', $sitecontext)) {
144 print_error('cannotviewcourseblog', 'blog');
147 $coursecontext = context_course
::instance(SITEID
);
150 if (!empty($groupid)) {
151 if ($CFG->bloglevel
< BLOG_SITE_LEVEL
) {
152 print_error('groupblogdisable', 'blog');
155 if (! $group = groups_get_group($groupid)) {
156 print_error(get_string('invalidgroupid', 'blog'));
159 if (!$course = $DB->get_record('course', array('id'=>$group->courseid
))) {
160 print_error('invalidcourseid');
163 $coursecontext = context_course
::instance($course->id
);
164 $courseid = $course->id
;
165 require_login($course);
167 if (!has_capability('moodle/blog:view', $sitecontext)) {
168 print_error(get_string('cannotviewcourseorgroupblog', 'blog'));
171 if (groups_get_course_groupmode($course) == SEPARATEGROUPS
&& !has_capability('moodle/site:accessallgroups', $coursecontext)) {
172 if (!groups_is_member($groupid)) {
173 print_error('notmemberofgroup');
178 if (!empty($userid)) {
179 if ($CFG->bloglevel
< BLOG_USER_LEVEL
) {
180 print_error('blogdisable', 'blog');
183 if (!$user = $DB->get_record('user', array('id'=>$userid))) {
184 print_error('invaliduserid');
187 if ($user->deleted
) {
188 echo $OUTPUT->header();
189 echo $OUTPUT->heading(get_string('userdeleted'));
190 echo $OUTPUT->footer();
194 if ($USER->id
== $userid) {
195 if (!has_capability('moodle/blog:create', $sitecontext)
196 && !has_capability('moodle/blog:view', $sitecontext)) {
197 print_error('donothaveblog', 'blog');
200 if (!has_capability('moodle/blog:view', $sitecontext) ||
!blog_user_can_view_user_entry($userid)) {
201 print_error('cannotviewcourseblog', 'blog');
204 $PAGE->navigation
->extend_for_user($user);
208 $courseid = (empty($courseid)) ? SITEID
: $courseid;
211 $blogheaders = blog_get_headers();
213 if ($CFG->enablerssfeeds
) {
217 list($thingid, $rsscontext, $filtertype) = blog_rss_get_params($blogheaders['filters']);
218 if (empty($rsscontext)) {
219 $rsscontext = context_system
::instance();
221 $rsstitle = $blogheaders['heading'];
223 // Check we haven't started output by outputting an error message.
224 if ($PAGE->state
== moodle_page
::STATE_BEFORE_HEADER
) {
225 blog_rss_add_http_header($rsscontext, $rsstitle, $filtertype, $thingid, $tagid);
229 echo $OUTPUT->header();
231 echo $OUTPUT->heading($blogheaders['heading'], 2);
233 $bloglisting = new blog_listing($blogheaders['filters']);
234 $bloglisting->print_entries();
236 echo $OUTPUT->footer();
237 $eventparams = array(
238 'other' => array('entryid' => $entryid, 'tagid' => $tagid, 'userid' => $userid, 'modid' => $modid, 'groupid' => $groupid,
239 'search' => $search, 'fromstart' => $start)
241 if (!empty($userid)) {
242 $eventparams['relateduserid'] = $userid;
244 $eventparams['other']['courseid'] = ($courseid === SITEID
) ?
0 : $courseid;
245 $event = \core\event\blog_entries_viewed
::create($eventparams);