MDL-40266 improve emulate_bound_params() for mysqli
[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
fd7a6fe2
PS
122 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
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
131 $coursecontext = get_context_instance(CONTEXT_COURSE, $thingid);
132 $rsscontext = $coursecontext;
133
134 $filtertype = 'course';
135 } else if (array_key_exists('user', $filters)) {
136 $thingid = $filters['user'];
137
138 $usercontext = get_context_instance(CONTEXT_USER, $thingid);
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
40440a58
DM
161 if (empty($CFG->bloglevel)) {
162 debugging('Blogging disabled on this site, RSS feeds are not available');
163 return null;
164 }
165
274f9840
AD
166 if (empty($CFG->enablerssfeeds)) {
167 debugging('Sorry, RSS feeds are disabled on this site');
168 return '';
169 }
170
a201a0ff
JF
171 if ($CFG->bloglevel == BLOG_SITE_LEVEL) {
172 if (isguestuser()) {
173 debugging(get_string('nopermissiontoshow','error'));
174 return '';
175 }
176 }
177
274f9840
AD
178 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
179 if (!has_capability('moodle/blog:view', $sitecontext)) {
180 return null;
181 }
690aa229 182
5ce534eb
AD
183 $type = clean_param($args[3], PARAM_ALPHA);
184 $id = clean_param($args[4], PARAM_INT); // could be groupid / courseid / userid depending on $type
690aa229
AD
185
186 $tagid=0;
187 if ($args[5] != 'rss.xml') {
5ce534eb 188 $tagid = clean_param($args[5], PARAM_INT);
690aa229
AD
189 } else {
190 $tagid = 0;
191 }
192
1c7b8b93 193 $filename = blog_rss_file_name($type, $id, $tagid);
6619a7f4 194
1c7b8b93
NC
195 if (file_exists($filename)) {
196 if (filemtime($filename) + 3600 > time()) {
e858c368 197 return $filename; // It's already done so we return cached version
6619a7f4 198 }
1c7b8b93 199 }
6619a7f4 200
451f1e38
AD
201 $courseid = $groupid = $userid = null;
202 switch ($type) {
203 case 'site':
204 //$siteid = $id;
205 break;
206 case 'course':
207 $courseid = $id;
208 break;
209 case 'group':
210 $groupid = $id;
211 break;
212 case 'user':
213 $userid = $id;
214 break;
215 }
216
e858c368 217 // Get all the entries from the database
e858c368 218 require_once($CFG->dirroot .'/blog/locallib.php');
451f1e38
AD
219 $blogheaders = blog_get_headers($courseid, $groupid, $userid, $tagid);
220
e858c368
AD
221 $bloglisting = new blog_listing($blogheaders['filters']);
222 $blogentries = $bloglisting->get_entries();
1c7b8b93 223
e858c368 224 // Now generate an array of RSS items
1c7b8b93
NC
225 if ($blogentries) {
226 $items = array();
227 foreach ($blogentries as $blog_entry) {
228 $item = NULL;
229 $item->author = fullname($DB->get_record('user', array('id'=>$blog_entry->userid))); // TODO: this is slow
230 $item->title = $blog_entry->subject;
231 $item->pubdate = $blog_entry->lastmodified;
232 $item->link = $CFG->wwwroot.'/blog/index.php?entryid='.$blog_entry->id;
28566555
DP
233 $summary = file_rewrite_pluginfile_urls($blog_entry->summary, 'pluginfile.php',
234 $sitecontext->id, 'blog', 'post', $blog_entry->id);
235 $item->description = format_text($summary, $blog_entry->format);
1c7b8b93
NC
236 if ( !empty($CFG->usetags) && ($blogtags = tag_get_tags_array('post', $blog_entry->id)) ) {
237 if ($blogtags) {
238 $item->tags = $blogtags;
08577617 239 }
1c7b8b93 240 $item->tagscheme = $CFG->wwwroot . '/tag';
ebb73f49 241 }
1c7b8b93 242 $items[] = $item;
516194d0 243 }
1c7b8b93
NC
244 $articles = rss_add_items($items); /// Change structure to XML
245 } else {
246 $articles = '';
247 }
516194d0 248
1c7b8b93 249/// Get header and footer information
dba2764d 250
1c7b8b93
NC
251 switch ($type) {
252 case 'user':
253 $info = fullname($DB->get_record('user', array('id'=>$id), 'firstname,lastname'));
254 break;
255 case 'course':
256 $info = $DB->get_field('course', 'fullname', array('id'=>$id));
91d284c1 257 $info = format_string($info, true, array('context' => get_context_instance(CONTEXT_COURSE, $id)));
1c7b8b93
NC
258 break;
259 case 'site':
91d284c1 260 $info = format_string($SITE->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID)));
1c7b8b93
NC
261 break;
262 case 'group':
9250bf81 263 $group = groups_get_group($id);
1c7b8b93
NC
264 $info = $group->name; //TODO: $DB->get_field('groups', 'name', array('id'=>$id))
265 break;
266 default:
267 $info = '';
268 break;
269 }
516194d0 270
1c7b8b93
NC
271 if ($tagid) {
272 $info .= ': '.$DB->get_field('tags', 'text', array('id'=>$tagid));
273 }
cae83708 274
1c7b8b93
NC
275 $header = rss_standard_header(get_string($type.'blog','blog', $info),
276 $CFG->wwwroot.'/blog/index.php',
277 get_string('intro','blog'));
516194d0 278
1c7b8b93 279 $footer = rss_standard_footer();
516194d0 280
e858c368 281 // Save the XML contents to file.
1c7b8b93 282 $rssdata = $header.$articles.$footer;
1c7b8b93
NC
283 if (blog_rss_save_file($type,$id,$tagid,$rssdata)) {
284 return $filename;
285 } else {
286 return false; // Couldn't find it or make it
516194d0 287 }
1c7b8b93 288}
516194d0 289
13d1c9ed
JF
290/**
291 * Retrieve the location and file name of a cached RSS feed
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 * @return string
297 */
1c7b8b93
NC
298function blog_rss_file_name($type, $id, $tagid=0) {
299 global $CFG;
516194d0 300
1c7b8b93 301 if ($tagid) {
365bec4c 302 return "$CFG->cachedir/rss/blog/$type/$id/$tagid.xml";
1c7b8b93 303 } else {
365bec4c 304 return "$CFG->cachedir/rss/blog/$type/$id.xml";
516194d0 305 }
1c7b8b93 306}
cae83708 307
13d1c9ed
JF
308/**
309 * This function saves to file the rss feed specified in the parameters
310 *
311 * @param string $type The source of the RSS feed (site/course/group/user)
312 * @param int $id The id of the item defined by $type
313 * @param int $tagid The id of the row in the tag table that identifies the RSS Feed
314 * @param string $contents The contents of the RSS Feed file
315 * @return bool whether the save was successful or not
316 */
1c7b8b93
NC
317function blog_rss_save_file($type, $id, $tagid=0, $contents='') {
318 global $CFG;
71904f4d 319
e858c368 320 $status = true;
516194d0 321
e858c368 322 //blog creates some additional dirs within the rss cache so make sure they all exist
5a87c912
TL
323 make_cache_directory('rss/blog');
324 make_cache_directory('rss/blog/'.$type);
ebb73f49 325
71904f4d
PS
326 $filename = blog_rss_file_name($type, $id, $tagid);
327 $expandfilename = false; //we're supplying a full file path
328 $status = rss_save_file('blog', $filename, $contents, $expandfilename);
e858c368 329
1c7b8b93
NC
330 return $status;
331}
332