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