Merge branch 'MDL-36741_master' of git://github.com/markn86/moodle
[moodle.git] / blog / rsslib.php
CommitLineData
516194d0 1<?php
2
13d1c9ed
JF
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Blog RSS Management
20 *
21 * @package core_blog
22 * @category rss
23 * @copyright 2010 Andrew Davis
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
1c7b8b93
NC
26require_once($CFG->dirroot.'/lib/rsslib.php');
27require_once($CFG->dirroot .'/blog/lib.php');
516194d0 28
13d1c9ed
JF
29/**
30 * Build the URL for the RSS feed
31 *
32 * @param int $contextid The context under which the URL should be created
33 * @param int $userid The id of the user requesting the RSS Feed
34 * @param string $filtertype The source of the RSS feed (site/course/group/user)
35 * @param int $filterselect The id of the item defined by $filtertype
36 * @param int $tagid The id of the row in the tag table that identifies the RSS Feed
37 * @return string
38 */
e858c368
AD
39function blog_rss_get_url($contextid, $userid, $filtertype, $filterselect=0, $tagid=0) {
40 $componentname = 'blog';
6619a7f4 41
e858c368 42 $additionalargs = null;
1c7b8b93
NC
43 switch ($filtertype) {
44 case 'site':
e858c368 45 $additionalargs = 'site/'.SITEID;
1c7b8b93
NC
46 break;
47 case 'course':
e858c368 48 $additionalargs = 'course/'.$filterselect;
1c7b8b93
NC
49 break;
50 case 'group':
e858c368 51 $additionalargs = 'group/'.$filterselect;
1c7b8b93
NC
52 break;
53 case 'user':
e858c368 54 $additionalargs = 'user/'.$filterselect;
1c7b8b93
NC
55 break;
56 }
6619a7f4 57
1c7b8b93 58 if ($tagid) {
e858c368
AD
59 $additionalargs .= '/'.$tagid;
60 }
61
62 return rss_get_url($contextid, $userid, $componentname, $additionalargs);
63}
64
13d1c9ed
JF
65/**
66 * Print the link for the RSS feed with the correct RSS icon (Theme based)
67 *
68 * @param stdClass $context The context under which the URL should be created
69 * @param string $filtertype The source of the RSS feed (site/course/group/user)
70 * @param int $filterselect The id of the item defined by $filtertype
71 * @param int $tagid The id of the row in the tag table that identifies the RSS Feed
72 * @param string $tooltiptext The tooltip to be displayed with the link
73 */
e858c368
AD
74function blog_rss_print_link($context, $filtertype, $filterselect=0, $tagid=0, $tooltiptext='') {
75 global $CFG, $USER, $OUTPUT;
76
77 if (!isloggedin()) {
78 $userid = $CFG->siteguest;
79 } else {
80 $userid = $USER->id;
1c7b8b93 81 }
6619a7f4 82
e858c368 83 $url = blog_rss_get_url($context->id, $userid, $filtertype, $filterselect, $tagid);
b5d0cafc 84 $rsspix = $OUTPUT->pix_url('i/rss');
e858c368
AD
85 print '<div class="mdl-right"><a href="'. $url .'"><img src="'. $rsspix .'" title="'. strip_tags($tooltiptext) .'" alt="'.get_string('rss').'" /></a></div>';
86}
516194d0 87
13d1c9ed
JF
88/**
89 * Build the URL for the RSS feed amd add it as a header
90 *
91 * @param stdClass $context The context under which the URL should be created
92 * @param string $title Name for the link to be added to the page header
93 * @param string $filtertype The source of the RSS feed (site/course/group/user)
94 * @param int $filterselect The id of the item defined by $filtertype
95 * @param int $tagid The id of the row in the tag table that identifies the RSS Feed
96 */
e858c368
AD
97function blog_rss_add_http_header($context, $title, $filtertype, $filterselect=0, $tagid=0) {
98 global $PAGE, $USER, $CFG;
516194d0 99
e858c368
AD
100 //$componentname = 'blog';
101 //rss_add_http_header($context, $componentname, $filterselect, $title);
71904f4d 102
e858c368
AD
103 if (!isloggedin()) {
104 $userid = $CFG->siteguest;
105 } else {
106 $userid = $USER->id;
107 }
108
109 $rsspath = blog_rss_get_url($context->id, $userid, $filtertype, $filterselect, $tagid);
110 $PAGE->add_alternate_version($title, $rsspath, 'application/rss+xml');
111}
112
113/**
114 * Utility function to extract parameters needed to generate RSS URLs from the blog filters
13d1c9ed
JF
115 *
116 * @param array $filters filters for the blog
117 * @return array array containing the id of the user/course/group, the relevant context and the filter type: site/user/course/group
e858c368
AD
118 */
119function blog_rss_get_params($filters) {
120 $thingid = $rsscontext = $filtertype = null;
121
41b38360 122 $sitecontext = context_system::instance();
fd7a6fe2 123
e858c368
AD
124 if (!$filters) {
125 $thingid = SITEID;
126 $rsscontext = $sitecontext;
127 $filtertype = 'site';
128 } else if (array_key_exists('course', $filters)) {
129 $thingid = $filters['course'];
130
41b38360 131 $coursecontext = context_course::instance($thingid);
e858c368
AD
132 $rsscontext = $coursecontext;
133
134 $filtertype = 'course';
135 } else if (array_key_exists('user', $filters)) {
136 $thingid = $filters['user'];
137
41b38360 138 $usercontext = context_user::instance($thingid);
e858c368
AD
139 $rsscontext = $usercontext;
140
141 $filtertype = 'user';
142 } else if (array_key_exists('group', $filters)) {
143 $thingid = $filters['group'];
144
145 $rsscontext = $sitecontext; //is this the context we should be using for group blogs?
146 $filtertype = 'group';
147 }
148
149 return array($thingid, $rsscontext, $filtertype);
1c7b8b93 150}
516194d0 151
13d1c9ed
JF
152/**
153 * Generate any blog RSS feed via one function
154 *
155 * @param stdClass $context The context of the blog for which the feed it being generated
156 * @param array $args An array of arguements needed to build the feed (contextid, token, componentname, type, id, tagid)
157 */
274f9840 158function blog_rss_get_feed($context, $args) {
1c7b8b93 159 global $CFG, $SITE, $DB;
ebb73f49 160
274f9840
AD
161 if (empty($CFG->enablerssfeeds)) {
162 debugging('Sorry, RSS feeds are disabled on this site');
163 return '';
164 }
165
21c3b51e
JF
166 if ($CFG->bloglevel == BLOG_SITE_LEVEL) {
167 if (isguestuser()) {
168 debugging(get_string('nopermissiontoshow','error'));
169 return '';
170 }
171 }
172
41b38360 173 $sitecontext = context_system::instance();
274f9840
AD
174 if (!has_capability('moodle/blog:view', $sitecontext)) {
175 return null;
176 }
690aa229 177
5ce534eb
AD
178 $type = clean_param($args[3], PARAM_ALPHA);
179 $id = clean_param($args[4], PARAM_INT); // could be groupid / courseid / userid depending on $type
690aa229
AD
180
181 $tagid=0;
182 if ($args[5] != 'rss.xml') {
5ce534eb 183 $tagid = clean_param($args[5], PARAM_INT);
690aa229
AD
184 } else {
185 $tagid = 0;
186 }
187
1c7b8b93 188 $filename = blog_rss_file_name($type, $id, $tagid);
6619a7f4 189
1c7b8b93
NC
190 if (file_exists($filename)) {
191 if (filemtime($filename) + 3600 > time()) {
e858c368 192 return $filename; // It's already done so we return cached version
6619a7f4 193 }
1c7b8b93 194 }
6619a7f4 195
451f1e38
AD
196 $courseid = $groupid = $userid = null;
197 switch ($type) {
198 case 'site':
199 //$siteid = $id;
200 break;
201 case 'course':
202 $courseid = $id;
203 break;
204 case 'group':
205 $groupid = $id;
206 break;
207 case 'user':
208 $userid = $id;
209 break;
210 }
211
e858c368 212 // Get all the entries from the database
e858c368 213 require_once($CFG->dirroot .'/blog/locallib.php');
451f1e38
AD
214 $blogheaders = blog_get_headers($courseid, $groupid, $userid, $tagid);
215
e858c368
AD
216 $bloglisting = new blog_listing($blogheaders['filters']);
217 $blogentries = $bloglisting->get_entries();
1c7b8b93 218
e858c368 219 // Now generate an array of RSS items
1c7b8b93
NC
220 if ($blogentries) {
221 $items = array();
222 foreach ($blogentries as $blog_entry) {
223 $item = NULL;
224 $item->author = fullname($DB->get_record('user', array('id'=>$blog_entry->userid))); // TODO: this is slow
225 $item->title = $blog_entry->subject;
226 $item->pubdate = $blog_entry->lastmodified;
227 $item->link = $CFG->wwwroot.'/blog/index.php?entryid='.$blog_entry->id;
28566555
DP
228 $summary = file_rewrite_pluginfile_urls($blog_entry->summary, 'pluginfile.php',
229 $sitecontext->id, 'blog', 'post', $blog_entry->id);
230 $item->description = format_text($summary, $blog_entry->format);
1c7b8b93
NC
231 if ( !empty($CFG->usetags) && ($blogtags = tag_get_tags_array('post', $blog_entry->id)) ) {
232 if ($blogtags) {
233 $item->tags = $blogtags;
08577617 234 }
1c7b8b93 235 $item->tagscheme = $CFG->wwwroot . '/tag';
ebb73f49 236 }
1c7b8b93 237 $items[] = $item;
516194d0 238 }
1c7b8b93
NC
239 $articles = rss_add_items($items); /// Change structure to XML
240 } else {
241 $articles = '';
242 }
516194d0 243
1c7b8b93 244/// Get header and footer information
dba2764d 245
1c7b8b93
NC
246 switch ($type) {
247 case 'user':
248 $info = fullname($DB->get_record('user', array('id'=>$id), 'firstname,lastname'));
249 break;
250 case 'course':
251 $info = $DB->get_field('course', 'fullname', array('id'=>$id));
41b38360 252 $info = format_string($info, true, array('context' => context_course::instance($id)));
1c7b8b93
NC
253 break;
254 case 'site':
41b38360 255 $info = format_string($SITE->fullname, true, array('context' => context_course::instance(SITEID)));
1c7b8b93
NC
256 break;
257 case 'group':
9250bf81 258 $group = groups_get_group($id);
1c7b8b93
NC
259 $info = $group->name; //TODO: $DB->get_field('groups', 'name', array('id'=>$id))
260 break;
261 default:
262 $info = '';
263 break;
264 }
516194d0 265
1c7b8b93
NC
266 if ($tagid) {
267 $info .= ': '.$DB->get_field('tags', 'text', array('id'=>$tagid));
268 }
cae83708 269
1c7b8b93
NC
270 $header = rss_standard_header(get_string($type.'blog','blog', $info),
271 $CFG->wwwroot.'/blog/index.php',
272 get_string('intro','blog'));
516194d0 273
1c7b8b93 274 $footer = rss_standard_footer();
516194d0 275
e858c368 276 // Save the XML contents to file.
1c7b8b93 277 $rssdata = $header.$articles.$footer;
1c7b8b93
NC
278 if (blog_rss_save_file($type,$id,$tagid,$rssdata)) {
279 return $filename;
280 } else {
281 return false; // Couldn't find it or make it
516194d0 282 }
1c7b8b93 283}
516194d0 284
13d1c9ed
JF
285/**
286 * Retrieve the location and file name of a cached RSS feed
287 *
288 * @param string $type The source of the RSS feed (site/course/group/user)
289 * @param int $id The id of the item defined by $type
290 * @param int $tagid The id of the row in the tag table that identifies the RSS Feed
291 * @return string
292 */
1c7b8b93
NC
293function blog_rss_file_name($type, $id, $tagid=0) {
294 global $CFG;
516194d0 295
1c7b8b93 296 if ($tagid) {
365bec4c 297 return "$CFG->cachedir/rss/blog/$type/$id/$tagid.xml";
1c7b8b93 298 } else {
365bec4c 299 return "$CFG->cachedir/rss/blog/$type/$id.xml";
516194d0 300 }
1c7b8b93 301}
cae83708 302
13d1c9ed
JF
303/**
304 * This function saves to file the rss feed specified in the parameters
305 *
306 * @param string $type The source of the RSS feed (site/course/group/user)
307 * @param int $id The id of the item defined by $type
308 * @param int $tagid The id of the row in the tag table that identifies the RSS Feed
309 * @param string $contents The contents of the RSS Feed file
310 * @return bool whether the save was successful or not
311 */
1c7b8b93
NC
312function blog_rss_save_file($type, $id, $tagid=0, $contents='') {
313 global $CFG;
71904f4d 314
e858c368 315 $status = true;
516194d0 316
e858c368 317 //blog creates some additional dirs within the rss cache so make sure they all exist
5a87c912
TL
318 make_cache_directory('rss/blog');
319 make_cache_directory('rss/blog/'.$type);
ebb73f49 320
71904f4d
PS
321 $filename = blog_rss_file_name($type, $id, $tagid);
322 $expandfilename = false; //we're supplying a full file path
323 $status = rss_save_file('blog', $filename, $contents, $expandfilename);
e858c368 324
1c7b8b93
NC
325 return $status;
326}
327