MDL-51239 core_blog: coding style fixes
[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 */
0e32a565 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 */
0e32a565 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 */
0e32a565 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);
220 if ( !empty($CFG->usetags) && ($blogtags = tag_get_tags_array('post', $blogentry->id)) ) {
1c7b8b93
NC
221 if ($blogtags) {
222 $item->tags = $blogtags;
08577617 223 }
1c7b8b93 224 $item->tagscheme = $CFG->wwwroot . '/tag';
ebb73f49 225 }
1c7b8b93 226 $items[] = $item;
516194d0 227 }
2b6e53e8 228 $articles = rss_add_items($items); // Change structure to XML.
1c7b8b93
NC
229 } else {
230 $articles = '';
231 }
516194d0 232
2b6e53e8 233 // Get header and footer information.
dba2764d 234
1c7b8b93
NC
235 switch ($type) {
236 case 'user':
0e32a565 237 $info = fullname($DB->get_record('user', array('id' => $id), 'firstname,lastname'));
1c7b8b93
NC
238 break;
239 case 'course':
0e32a565 240 $info = $DB->get_field('course', 'fullname', array('id' => $id));
41b38360 241 $info = format_string($info, true, array('context' => context_course::instance($id)));
1c7b8b93
NC
242 break;
243 case 'site':
41b38360 244 $info = format_string($SITE->fullname, true, array('context' => context_course::instance(SITEID)));
1c7b8b93
NC
245 break;
246 case 'group':
9250bf81 247 $group = groups_get_group($id);
0e32a565 248 $info = $group->name; // TODO: $DB->get_field('groups', 'name', array('id' => $id)).
1c7b8b93
NC
249 break;
250 default:
251 $info = '';
252 break;
253 }
516194d0 254
1c7b8b93 255 if ($tagid) {
0e32a565 256 $info .= ': '.$DB->get_field('tags', 'text', array('id' => $tagid));
1c7b8b93 257 }
cae83708 258
2b6e53e8 259 $header = rss_standard_header(get_string($type.'blog', 'blog', $info),
1c7b8b93 260 $CFG->wwwroot.'/blog/index.php',
2b6e53e8 261 get_string('intro', 'blog'));
516194d0 262
1c7b8b93 263 $footer = rss_standard_footer();
516194d0 264
e858c368 265 // Save the XML contents to file.
1c7b8b93 266 $rssdata = $header.$articles.$footer;
2b6e53e8 267 if (blog_rss_save_file($type, $id, $tagid, $rssdata)) {
1c7b8b93
NC
268 return $filename;
269 } else {
2b6e53e8 270 return false; // Couldn't find it or make it.
516194d0 271 }
1c7b8b93 272}
516194d0 273
13d1c9ed
JF
274/**
275 * Retrieve the location and file name of a cached RSS feed
276 *
277 * @param string $type The source of the RSS feed (site/course/group/user)
278 * @param int $id The id of the item defined by $type
279 * @param int $tagid The id of the row in the tag table that identifies the RSS Feed
280 * @return string
281 */
0e32a565 282function blog_rss_file_name($type, $id, $tagid =0) {
1c7b8b93 283 global $CFG;
516194d0 284
1c7b8b93 285 if ($tagid) {
365bec4c 286 return "$CFG->cachedir/rss/blog/$type/$id/$tagid.xml";
1c7b8b93 287 } else {
365bec4c 288 return "$CFG->cachedir/rss/blog/$type/$id.xml";
516194d0 289 }
1c7b8b93 290}
cae83708 291
13d1c9ed
JF
292/**
293 * This function saves to file the rss feed specified in the parameters
294 *
295 * @param string $type The source of the RSS feed (site/course/group/user)
296 * @param int $id The id of the item defined by $type
297 * @param int $tagid The id of the row in the tag table that identifies the RSS Feed
298 * @param string $contents The contents of the RSS Feed file
299 * @return bool whether the save was successful or not
300 */
0e32a565 301function blog_rss_save_file($type, $id, $tagid =0, $contents='') {
1c7b8b93 302 global $CFG;
71904f4d 303
e858c368 304 $status = true;
516194d0 305
2b6e53e8 306 // Blog creates some additional dirs within the rss cache so make sure they all exist.
5a87c912
TL
307 make_cache_directory('rss/blog');
308 make_cache_directory('rss/blog/'.$type);
ebb73f49 309
71904f4d 310 $filename = blog_rss_file_name($type, $id, $tagid);
2b6e53e8 311 $expandfilename = false; // We are supplying a full file path.
71904f4d 312 $status = rss_save_file('blog', $filename, $contents, $expandfilename);
e858c368 313
1c7b8b93
NC
314 return $status;
315}
316
54eb02a4
AD
317/**
318 * Delete the supplied user's cached blog post RSS feed.
319 * Only user blogs are available by RSS.
320 * This doesn't call rss_delete_file() as blog RSS caching uses it's own file structure.
321 *
322 * @param int $userid
323 */
324function blog_rss_delete_file($userid) {
325 $filename = blog_rss_file_name('user', $userid);
326 if (file_exists($filename)) {
327 unlink($filename);
328 }
329}
330