MDL-28701 Change uses of make_upload_directory(temp) to make_temp_directory().
[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);
71904f4d 53
e858c368
AD
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
fd7a6fe2
PS
72 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
73
e858c368
AD
74 if (!$filters) {
75 $thingid = SITEID;
76 $rsscontext = $sitecontext;
77 $filtertype = 'site';
78 } else if (array_key_exists('course', $filters)) {
79 $thingid = $filters['course'];
80
81 $coursecontext = get_context_instance(CONTEXT_COURSE, $thingid);
82 $rsscontext = $coursecontext;
83
84 $filtertype = 'course';
85 } else if (array_key_exists('user', $filters)) {
86 $thingid = $filters['user'];
87
88 $usercontext = get_context_instance(CONTEXT_USER, $thingid);
89 $rsscontext = $usercontext;
90
91 $filtertype = 'user';
92 } else if (array_key_exists('group', $filters)) {
93 $thingid = $filters['group'];
94
95 $rsscontext = $sitecontext; //is this the context we should be using for group blogs?
96 $filtertype = 'group';
97 }
98
99 return array($thingid, $rsscontext, $filtertype);
1c7b8b93 100}
516194d0 101
516194d0 102
1c7b8b93 103// Generate any blog RSS feed via one function (called by ../rss/file.php)
274f9840 104function blog_rss_get_feed($context, $args) {
1c7b8b93 105 global $CFG, $SITE, $DB;
ebb73f49 106
274f9840
AD
107 if (empty($CFG->enablerssfeeds)) {
108 debugging('Sorry, RSS feeds are disabled on this site');
109 return '';
110 }
111
112 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
113 if (!has_capability('moodle/blog:view', $sitecontext)) {
114 return null;
115 }
690aa229 116
5ce534eb
AD
117 $type = clean_param($args[3], PARAM_ALPHA);
118 $id = clean_param($args[4], PARAM_INT); // could be groupid / courseid / userid depending on $type
690aa229
AD
119
120 $tagid=0;
121 if ($args[5] != 'rss.xml') {
5ce534eb 122 $tagid = clean_param($args[5], PARAM_INT);
690aa229
AD
123 } else {
124 $tagid = 0;
125 }
126
1c7b8b93 127 $filename = blog_rss_file_name($type, $id, $tagid);
6619a7f4 128
1c7b8b93
NC
129 if (file_exists($filename)) {
130 if (filemtime($filename) + 3600 > time()) {
e858c368 131 return $filename; // It's already done so we return cached version
6619a7f4 132 }
1c7b8b93 133 }
6619a7f4 134
451f1e38
AD
135 $courseid = $groupid = $userid = null;
136 switch ($type) {
137 case 'site':
138 //$siteid = $id;
139 break;
140 case 'course':
141 $courseid = $id;
142 break;
143 case 'group':
144 $groupid = $id;
145 break;
146 case 'user':
147 $userid = $id;
148 break;
149 }
150
e858c368 151 // Get all the entries from the database
e858c368 152 require_once($CFG->dirroot .'/blog/locallib.php');
451f1e38
AD
153 $blogheaders = blog_get_headers($courseid, $groupid, $userid, $tagid);
154
e858c368
AD
155 $bloglisting = new blog_listing($blogheaders['filters']);
156 $blogentries = $bloglisting->get_entries();
1c7b8b93 157
e858c368 158 // Now generate an array of RSS items
1c7b8b93
NC
159 if ($blogentries) {
160 $items = array();
161 foreach ($blogentries as $blog_entry) {
162 $item = NULL;
163 $item->author = fullname($DB->get_record('user', array('id'=>$blog_entry->userid))); // TODO: this is slow
164 $item->title = $blog_entry->subject;
165 $item->pubdate = $blog_entry->lastmodified;
166 $item->link = $CFG->wwwroot.'/blog/index.php?entryid='.$blog_entry->id;
167 $item->description = format_text($blog_entry->summary, $blog_entry->format);
168 if ( !empty($CFG->usetags) && ($blogtags = tag_get_tags_array('post', $blog_entry->id)) ) {
169 if ($blogtags) {
170 $item->tags = $blogtags;
08577617 171 }
1c7b8b93 172 $item->tagscheme = $CFG->wwwroot . '/tag';
ebb73f49 173 }
1c7b8b93 174 $items[] = $item;
516194d0 175 }
1c7b8b93
NC
176 $articles = rss_add_items($items); /// Change structure to XML
177 } else {
178 $articles = '';
179 }
516194d0 180
1c7b8b93 181/// Get header and footer information
dba2764d 182
1c7b8b93
NC
183 switch ($type) {
184 case 'user':
185 $info = fullname($DB->get_record('user', array('id'=>$id), 'firstname,lastname'));
186 break;
187 case 'course':
188 $info = $DB->get_field('course', 'fullname', array('id'=>$id));
189 break;
190 case 'site':
191 $info = $SITE->fullname;
192 break;
193 case 'group':
9250bf81 194 $group = groups_get_group($id);
1c7b8b93
NC
195 $info = $group->name; //TODO: $DB->get_field('groups', 'name', array('id'=>$id))
196 break;
197 default:
198 $info = '';
199 break;
200 }
516194d0 201
1c7b8b93
NC
202 if ($tagid) {
203 $info .= ': '.$DB->get_field('tags', 'text', array('id'=>$tagid));
204 }
cae83708 205
1c7b8b93
NC
206 $header = rss_standard_header(get_string($type.'blog','blog', $info),
207 $CFG->wwwroot.'/blog/index.php',
208 get_string('intro','blog'));
516194d0 209
1c7b8b93 210 $footer = rss_standard_footer();
516194d0 211
e858c368 212 // Save the XML contents to file.
1c7b8b93 213 $rssdata = $header.$articles.$footer;
1c7b8b93
NC
214 if (blog_rss_save_file($type,$id,$tagid,$rssdata)) {
215 return $filename;
216 } else {
217 return false; // Couldn't find it or make it
516194d0 218 }
1c7b8b93 219}
516194d0 220
516194d0 221
1c7b8b93
NC
222function blog_rss_file_name($type, $id, $tagid=0) {
223 global $CFG;
516194d0 224
1c7b8b93 225 if ($tagid) {
365bec4c 226 return "$CFG->cachedir/rss/blog/$type/$id/$tagid.xml";
1c7b8b93 227 } else {
365bec4c 228 return "$CFG->cachedir/rss/blog/$type/$id.xml";
516194d0 229 }
1c7b8b93 230}
cae83708 231
1c7b8b93
NC
232//This function saves to file the rss feed specified in the parameters
233function blog_rss_save_file($type, $id, $tagid=0, $contents='') {
234 global $CFG;
71904f4d 235
e858c368 236 $status = true;
516194d0 237
e858c368 238 //blog creates some additional dirs within the rss cache so make sure they all exist
71904f4d
PS
239 make_upload_directory('cache/rss/blog');
240 make_upload_directory('cache/rss/blog/'.$type);
ebb73f49 241
71904f4d
PS
242 $filename = blog_rss_file_name($type, $id, $tagid);
243 $expandfilename = false; //we're supplying a full file path
244 $status = rss_save_file('blog', $filename, $contents, $expandfilename);
e858c368 245
1c7b8b93
NC
246 return $status;
247}
248