Merge branch 'MDL-63349-Master' of github.com:tuanngocnguyen/moodle
[moodle.git] / blog / index.php
CommitLineData
dfede59d 1<?php
2b6e53e8
AD
2// This file is part of Moodle - http://moodle.org/
3//
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.
8//
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.
13//
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/>.
4a173181 16
17/**
18 * file index.php
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
21 */
22
1fcf0ca8 23require_once(__DIR__ . '/../config.php');
4a173181 24require_once($CFG->dirroot .'/blog/lib.php');
cae83708 25require_once($CFG->dirroot .'/blog/locallib.php');
26require_once($CFG->dirroot .'/course/lib.php');
36051c9e 27require_once($CFG->dirroot .'/comment/lib.php');
4a173181 28
1c7b8b93
NC
29$id = optional_param('id', null, PARAM_INT);
30$start = optional_param('formstart', 0, PARAM_INT);
31$tag = optional_param('tag', '', PARAM_NOTAGS);
32$userid = optional_param('userid', null, PARAM_INT);
33$tagid = optional_param('tagid', null, PARAM_INT);
34$modid = optional_param('modid', null, PARAM_INT);
35$entryid = optional_param('entryid', null, PARAM_INT);
36$groupid = optional_param('groupid', null, PARAM_INT);
37$courseid = optional_param('courseid', null, PARAM_INT);
38$search = optional_param('search', null, PARAM_RAW);
cae83708 39
36051c9e 40comment::init();
b73d1ca4 41
0e32a565
AD
42$urlparams = compact('id', 'start', 'tag', 'userid', 'tagid', 'modid', 'entryid', 'groupid', 'courseid', 'search');
43foreach ($urlparams as $var => $val) {
cae83708 44 if (empty($val)) {
0e32a565 45 unset($urlparams[$var]);
cae83708 46 }
47}
0e32a565 48$PAGE->set_url('/blog/index.php', $urlparams);
e96f2a77 49
2b6e53e8 50// Correct tagid if a text tag is provided as a param.
1c7b8b93 51if (!empty($tag)) {
1af9063e 52 if ($tagrec = $DB->get_record('tag', array('name' => $tag))) {
856b6fe6 53 $tagid = $tagrec->id;
54 } else {
55 unset($tagid);
56 }
57}
58
45367bdf
AG
59// Set the userid to the entry author if we have the entry ID.
60if ($entryid and !isset($userid)) {
61 $entry = new blog_entry($entryid);
62 $userid = $entry->userid;
63}
64
cfd1bcfa 65if (isset($userid) && empty($courseid) && empty($modid)) {
880c5073 66 $context = context_user::instance($userid);
afb7ec92 67} else if (!empty($courseid) && $courseid != SITEID) {
c1f97c77 68 $context = context_course::instance($courseid);
880c5073
AG
69} else {
70 $context = context_system::instance();
71}
72$PAGE->set_context($context);
73
f495187d
AG
74if (isset($userid) && $USER->id == $userid) {
75 $blognode = $PAGE->navigation->find('siteblog', null);
0c713162
AG
76 if ($blognode) {
77 $blognode->make_inactive();
78 }
f495187d
AG
79}
80
2b6e53e8 81// Check basic permissions.
8f6c1f34 82if ($CFG->bloglevel == BLOG_GLOBAL_LEVEL) {
2b6e53e8 83 // Everybody can see anything - no login required unless site is locked down using forcelogin.
8f6c1f34
PS
84 if ($CFG->forcelogin) {
85 require_login();
86 }
87
88} else if ($CFG->bloglevel == BLOG_SITE_LEVEL) {
2b6e53e8 89 // Users must log in and can not be guests.
8f6c1f34
PS
90 require_login();
91 if (isguestuser()) {
2b6e53e8 92 // They must have entered the url manually.
67a1f639 93 print_error('noguest');
8f6c1f34
PS
94 }
95
96} else if ($CFG->bloglevel == BLOG_USER_LEVEL) {
2b6e53e8 97 // Users can see own blogs only! with the exception of people with special cap.
8f6c1f34
PS
98 require_login();
99
100} else {
2b6e53e8 101 // Weird!
8b141784 102 print_error('blogdisable', 'blog');
ab2f17b0 103}
bbbf2d40 104
e9fb99b1
AD
105if (empty($CFG->enableblogs)) {
106 print_error('blogdisable', 'blog');
107}
108
9de44021 109list($courseid, $userid) = blog_validate_access($courseid, $modid, $groupid, $entryid, $userid);
856b6fe6 110
9de44021 111$courseid = (empty($courseid)) ? SITEID : $courseid;
e96f2a77 112
9de44021
JL
113if ($courseid != SITEID) {
114 $course = get_course($courseid);
856b6fe6 115 require_login($course);
856b6fe6 116}
117
b29ce44d 118if (!empty($userid)) {
9de44021
JL
119 $user = core_user::get_user($userid);
120 $PAGE->navigation->extend_for_user($user);
4a173181 121}
122
c5dc10ee 123$blogheaders = blog_get_headers();
cae83708 124
5ae35b9d
DW
125$rsscontext = null;
126$filtertype = null;
127$thingid = null;
128$rsstitle = '';
e858c368 129if ($CFG->enablerssfeeds) {
e858c368 130 list($thingid, $rsscontext, $filtertype) = blog_rss_get_params($blogheaders['filters']);
43c4f4d1 131 if (empty($rsscontext)) {
f42d2a22 132 $rsscontext = context_system::instance();
43c4f4d1 133 }
e858c368
AD
134 $rsstitle = $blogheaders['heading'];
135
2b6e53e8 136 // Check we haven't started output by outputting an error message.
e858c368
AD
137 if ($PAGE->state == moodle_page::STATE_BEFORE_HEADER) {
138 blog_rss_add_http_header($rsscontext, $rsstitle, $filtertype, $thingid, $tagid);
139 }
e858c368 140}
c1f97c77
DC
141
142$usernode = $PAGE->navigation->find('user'.$userid, null);
143if ($usernode && $courseid != SITEID) {
144 $courseblogsnode = $PAGE->navigation->find('courseblogs', null);
145 if ($courseblogsnode) {
146 $courseblogsnode->remove();
147 }
148 $blogurl = new moodle_url($PAGE->url);
149 $blognode = $usernode->add(get_string('blogscourse', 'blog'), $blogurl);
150 $blognode->make_active();
151}
152
153if ($courseid != SITEID) {
154 $PAGE->set_heading($course->fullname);
155 echo $OUTPUT->header();
156 if (!empty($user)) {
157 $headerinfo = array('heading' => fullname($user), 'user' => $user);
158 echo $OUTPUT->context_header($headerinfo, 2);
159 }
160} else if (isset($userid)) {
880c5073 161 $PAGE->set_heading(fullname($user));
c1f97c77
DC
162 echo $OUTPUT->header();
163} else if ($courseid == SITEID) {
164 echo $OUTPUT->header();
880c5073 165}
e858c368 166
c5dc10ee 167echo $OUTPUT->heading($blogheaders['heading'], 2);
92a019ac 168
1c7b8b93
NC
169$bloglisting = new blog_listing($blogheaders['filters']);
170$bloglisting->print_entries();
4a173181 171
5ae35b9d
DW
172if ($CFG->enablerssfeeds) {
173 blog_rss_print_link($rsscontext, $filtertype, $thingid, $tagid, get_string('rssfeed', 'blog'));
174}
175
033e4aff 176echo $OUTPUT->footer();
6b364115
AA
177$eventparams = array(
178 'other' => array('entryid' => $entryid, 'tagid' => $tagid, 'userid' => $userid, 'modid' => $modid, 'groupid' => $groupid,
179 'search' => $search, 'fromstart' => $start)
180);
181if (!empty($userid)) {
182 $eventparams['relateduserid'] = $userid;
183}
184$eventparams['other']['courseid'] = ($courseid === SITEID) ? 0 : $courseid;
185$event = \core\event\blog_entries_viewed::create($eventparams);
186$event->trigger();