"CHAT MDL-23374, removed require_capability"
[moodle.git] / blog / rsslib.php
CommitLineData
516194d0 1<?php
2
1c7b8b93
NC
3require_once($CFG->dirroot.'/lib/rsslib.php');
4require_once($CFG->dirroot .'/blog/lib.php');
516194d0 5
e858c368
AD
6function blog_rss_get_url($contextid, $userid, $filtertype, $filterselect=0, $tagid=0) {
7 $componentname = 'blog';
6619a7f4 8
e858c368 9 $additionalargs = null;
1c7b8b93
NC
10 switch ($filtertype) {
11 case 'site':
e858c368 12 $additionalargs = 'site/'.SITEID;
1c7b8b93
NC
13 break;
14 case 'course':
e858c368 15 $additionalargs = 'course/'.$filterselect;
1c7b8b93
NC
16 break;
17 case 'group':
e858c368 18 $additionalargs = 'group/'.$filterselect;
1c7b8b93
NC
19 break;
20 case 'user':
e858c368 21 $additionalargs = 'user/'.$filterselect;
1c7b8b93
NC
22 break;
23 }
6619a7f4 24
1c7b8b93 25 if ($tagid) {
e858c368
AD
26 $additionalargs .= '/'.$tagid;
27 }
28
29 return rss_get_url($contextid, $userid, $componentname, $additionalargs);
30}
31
32// This function returns the icon (from theme) with the link to rss/file.php
33// needs some hacking to rss/file.php
34function blog_rss_print_link($context, $filtertype, $filterselect=0, $tagid=0, $tooltiptext='') {
35 global $CFG, $USER, $OUTPUT;
36
37 if (!isloggedin()) {
38 $userid = $CFG->siteguest;
39 } else {
40 $userid = $USER->id;
1c7b8b93 41 }
6619a7f4 42
e858c368 43 $url = blog_rss_get_url($context->id, $userid, $filtertype, $filterselect, $tagid);
b5d0cafc 44 $rsspix = $OUTPUT->pix_url('i/rss');
e858c368
AD
45 print '<div class="mdl-right"><a href="'. $url .'"><img src="'. $rsspix .'" title="'. strip_tags($tooltiptext) .'" alt="'.get_string('rss').'" /></a></div>';
46}
516194d0 47
e858c368
AD
48function blog_rss_add_http_header($context, $title, $filtertype, $filterselect=0, $tagid=0) {
49 global $PAGE, $USER, $CFG;
516194d0 50
e858c368
AD
51 //$componentname = 'blog';
52 //rss_add_http_header($context, $componentname, $filterselect, $title);
53
54 if (!isloggedin()) {
55 $userid = $CFG->siteguest;
56 } else {
57 $userid = $USER->id;
58 }
59
60 $rsspath = blog_rss_get_url($context->id, $userid, $filtertype, $filterselect, $tagid);
61 $PAGE->add_alternate_version($title, $rsspath, 'application/rss+xml');
62}
63
64/**
65 * Utility function to extract parameters needed to generate RSS URLs from the blog filters
66 * @param <type> $filters
67 * @return array array containing the id of the user/course/group, the relevant context and the filter type (site/user/course/group)
68 */
69function blog_rss_get_params($filters) {
70 $thingid = $rsscontext = $filtertype = null;
71
72 if (!$filters) {
73 $thingid = SITEID;
74 $rsscontext = $sitecontext;
75 $filtertype = 'site';
76 } else if (array_key_exists('course', $filters)) {
77 $thingid = $filters['course'];
78
79 $coursecontext = get_context_instance(CONTEXT_COURSE, $thingid);
80 $rsscontext = $coursecontext;
81
82 $filtertype = 'course';
83 } else if (array_key_exists('user', $filters)) {
84 $thingid = $filters['user'];
85
86 $usercontext = get_context_instance(CONTEXT_USER, $thingid);
87 $rsscontext = $usercontext;
88
89 $filtertype = 'user';
90 } else if (array_key_exists('group', $filters)) {
91 $thingid = $filters['group'];
92
93 $rsscontext = $sitecontext; //is this the context we should be using for group blogs?
94 $filtertype = 'group';
95 }
96
97 return array($thingid, $rsscontext, $filtertype);
1c7b8b93 98}
516194d0 99
516194d0 100
1c7b8b93 101// Generate any blog RSS feed via one function (called by ../rss/file.php)
274f9840 102function blog_rss_get_feed($context, $args) {
1c7b8b93 103 global $CFG, $SITE, $DB;
ebb73f49 104
274f9840
AD
105 if (empty($CFG->enablerssfeeds)) {
106 debugging('Sorry, RSS feeds are disabled on this site');
107 return '';
108 }
109
110 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
111 if (!has_capability('moodle/blog:view', $sitecontext)) {
112 return null;
113 }
690aa229 114
274f9840 115 $type = $args[3];
690aa229
AD
116 $id = (int) $args[4]; // could be groupid / courseid / userid depending on $instance
117
118 $tagid=0;
119 if ($args[5] != 'rss.xml') {
120 $tagid = (int) $args[5];
121 } else {
122 $tagid = 0;
123 }
124
1c7b8b93 125 $filename = blog_rss_file_name($type, $id, $tagid);
6619a7f4 126
1c7b8b93
NC
127 if (file_exists($filename)) {
128 if (filemtime($filename) + 3600 > time()) {
e858c368 129 return $filename; // It's already done so we return cached version
6619a7f4 130 }
1c7b8b93 131 }
6619a7f4 132
e858c368
AD
133 // Get all the entries from the database
134 //$blogentries = blog_fetch_entries('', 20, '', $type, $id, $tagid);
135 require_once($CFG->dirroot .'/blog/locallib.php');
136 $blogheaders = blog_get_headers();
137 $bloglisting = new blog_listing($blogheaders['filters']);
138 $blogentries = $bloglisting->get_entries();
1c7b8b93 139
e858c368 140 // Now generate an array of RSS items
1c7b8b93
NC
141 if ($blogentries) {
142 $items = array();
143 foreach ($blogentries as $blog_entry) {
144 $item = NULL;
145 $item->author = fullname($DB->get_record('user', array('id'=>$blog_entry->userid))); // TODO: this is slow
146 $item->title = $blog_entry->subject;
147 $item->pubdate = $blog_entry->lastmodified;
148 $item->link = $CFG->wwwroot.'/blog/index.php?entryid='.$blog_entry->id;
149 $item->description = format_text($blog_entry->summary, $blog_entry->format);
150 if ( !empty($CFG->usetags) && ($blogtags = tag_get_tags_array('post', $blog_entry->id)) ) {
151 if ($blogtags) {
152 $item->tags = $blogtags;
08577617 153 }
1c7b8b93 154 $item->tagscheme = $CFG->wwwroot . '/tag';
ebb73f49 155 }
1c7b8b93 156 $items[] = $item;
516194d0 157 }
1c7b8b93
NC
158 $articles = rss_add_items($items); /// Change structure to XML
159 } else {
160 $articles = '';
161 }
516194d0 162
1c7b8b93 163/// Get header and footer information
dba2764d 164
1c7b8b93
NC
165 switch ($type) {
166 case 'user':
167 $info = fullname($DB->get_record('user', array('id'=>$id), 'firstname,lastname'));
168 break;
169 case 'course':
170 $info = $DB->get_field('course', 'fullname', array('id'=>$id));
171 break;
172 case 'site':
173 $info = $SITE->fullname;
174 break;
175 case 'group':
176 $group = groups_get_group($id, false);
177 $info = $group->name; //TODO: $DB->get_field('groups', 'name', array('id'=>$id))
178 break;
179 default:
180 $info = '';
181 break;
182 }
516194d0 183
1c7b8b93
NC
184 if ($tagid) {
185 $info .= ': '.$DB->get_field('tags', 'text', array('id'=>$tagid));
186 }
cae83708 187
1c7b8b93
NC
188 $header = rss_standard_header(get_string($type.'blog','blog', $info),
189 $CFG->wwwroot.'/blog/index.php',
190 get_string('intro','blog'));
516194d0 191
1c7b8b93 192 $footer = rss_standard_footer();
516194d0 193
e858c368 194 // Save the XML contents to file.
1c7b8b93 195 $rssdata = $header.$articles.$footer;
1c7b8b93
NC
196 if (blog_rss_save_file($type,$id,$tagid,$rssdata)) {
197 return $filename;
198 } else {
199 return false; // Couldn't find it or make it
516194d0 200 }
1c7b8b93 201}
516194d0 202
516194d0 203
1c7b8b93
NC
204function blog_rss_file_name($type, $id, $tagid=0) {
205 global $CFG;
516194d0 206
1c7b8b93 207 if ($tagid) {
d5f25d95 208 return "$CFG->dataroot/cache/rss/blog/$type/$id/$tagid.xml";
1c7b8b93 209 } else {
d5f25d95 210 return "$CFG->dataroot/cache/rss/blog/$type/$id.xml";
516194d0 211 }
1c7b8b93 212}
cae83708 213
1c7b8b93
NC
214//This function saves to file the rss feed specified in the parameters
215function blog_rss_save_file($type, $id, $tagid=0, $contents='') {
216 global $CFG;
e858c368
AD
217
218 $status = true;
516194d0 219
e858c368
AD
220 //blog creates some additional dirs within the rss cache so make sure they all exist
221 if (! $basedir = make_upload_directory ('cache/rss/blog')) {
222 //Cannot be created, so error
223 $status = false;
1c7b8b93 224 }
e858c368
AD
225 if (! $basedir = make_upload_directory ('cache/rss/blog/'.$type)) {
226 //Cannot be created, so error
1c7b8b93 227 $status = false;
516194d0 228 }
ebb73f49 229
e858c368
AD
230 if ($status) {
231 $filename = blog_rss_file_name($type, $id, $tagid);
232 $expandfilename = false; //we're supplying a full file path
233 $status = rss_save_file('blog', $filename, $contents, $expandfilename);
234 }
235
1c7b8b93
NC
236 return $status;
237}
238