MDL-23984 improvements of check_dir_exists() - replacing by make_upload_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);
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];
451f1e38 116 $id = (int) $args[4]; // could be groupid / courseid / userid depending on $type
690aa229
AD
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
451f1e38
AD
133 $courseid = $groupid = $userid = null;
134 switch ($type) {
135 case 'site':
136 //$siteid = $id;
137 break;
138 case 'course':
139 $courseid = $id;
140 break;
141 case 'group':
142 $groupid = $id;
143 break;
144 case 'user':
145 $userid = $id;
146 break;
147 }
148
e858c368 149 // Get all the entries from the database
e858c368 150 require_once($CFG->dirroot .'/blog/locallib.php');
451f1e38
AD
151 $blogheaders = blog_get_headers($courseid, $groupid, $userid, $tagid);
152
e858c368
AD
153 $bloglisting = new blog_listing($blogheaders['filters']);
154 $blogentries = $bloglisting->get_entries();
1c7b8b93 155
e858c368 156 // Now generate an array of RSS items
1c7b8b93
NC
157 if ($blogentries) {
158 $items = array();
159 foreach ($blogentries as $blog_entry) {
160 $item = NULL;
161 $item->author = fullname($DB->get_record('user', array('id'=>$blog_entry->userid))); // TODO: this is slow
162 $item->title = $blog_entry->subject;
163 $item->pubdate = $blog_entry->lastmodified;
164 $item->link = $CFG->wwwroot.'/blog/index.php?entryid='.$blog_entry->id;
165 $item->description = format_text($blog_entry->summary, $blog_entry->format);
166 if ( !empty($CFG->usetags) && ($blogtags = tag_get_tags_array('post', $blog_entry->id)) ) {
167 if ($blogtags) {
168 $item->tags = $blogtags;
08577617 169 }
1c7b8b93 170 $item->tagscheme = $CFG->wwwroot . '/tag';
ebb73f49 171 }
1c7b8b93 172 $items[] = $item;
516194d0 173 }
1c7b8b93
NC
174 $articles = rss_add_items($items); /// Change structure to XML
175 } else {
176 $articles = '';
177 }
516194d0 178
1c7b8b93 179/// Get header and footer information
dba2764d 180
1c7b8b93
NC
181 switch ($type) {
182 case 'user':
183 $info = fullname($DB->get_record('user', array('id'=>$id), 'firstname,lastname'));
184 break;
185 case 'course':
186 $info = $DB->get_field('course', 'fullname', array('id'=>$id));
187 break;
188 case 'site':
189 $info = $SITE->fullname;
190 break;
191 case 'group':
192 $group = groups_get_group($id, false);
193 $info = $group->name; //TODO: $DB->get_field('groups', 'name', array('id'=>$id))
194 break;
195 default:
196 $info = '';
197 break;
198 }
516194d0 199
1c7b8b93
NC
200 if ($tagid) {
201 $info .= ': '.$DB->get_field('tags', 'text', array('id'=>$tagid));
202 }
cae83708 203
1c7b8b93
NC
204 $header = rss_standard_header(get_string($type.'blog','blog', $info),
205 $CFG->wwwroot.'/blog/index.php',
206 get_string('intro','blog'));
516194d0 207
1c7b8b93 208 $footer = rss_standard_footer();
516194d0 209
e858c368 210 // Save the XML contents to file.
1c7b8b93 211 $rssdata = $header.$articles.$footer;
1c7b8b93
NC
212 if (blog_rss_save_file($type,$id,$tagid,$rssdata)) {
213 return $filename;
214 } else {
215 return false; // Couldn't find it or make it
516194d0 216 }
1c7b8b93 217}
516194d0 218
516194d0 219
1c7b8b93
NC
220function blog_rss_file_name($type, $id, $tagid=0) {
221 global $CFG;
516194d0 222
1c7b8b93 223 if ($tagid) {
d5f25d95 224 return "$CFG->dataroot/cache/rss/blog/$type/$id/$tagid.xml";
1c7b8b93 225 } else {
d5f25d95 226 return "$CFG->dataroot/cache/rss/blog/$type/$id.xml";
516194d0 227 }
1c7b8b93 228}
cae83708 229
1c7b8b93
NC
230//This function saves to file the rss feed specified in the parameters
231function blog_rss_save_file($type, $id, $tagid=0, $contents='') {
232 global $CFG;
e858c368
AD
233
234 $status = true;
516194d0 235
e858c368
AD
236 //blog creates some additional dirs within the rss cache so make sure they all exist
237 if (! $basedir = make_upload_directory ('cache/rss/blog')) {
238 //Cannot be created, so error
239 $status = false;
1c7b8b93 240 }
e858c368
AD
241 if (! $basedir = make_upload_directory ('cache/rss/blog/'.$type)) {
242 //Cannot be created, so error
1c7b8b93 243 $status = false;
516194d0 244 }
ebb73f49 245
e858c368
AD
246 if ($status) {
247 $filename = blog_rss_file_name($type, $id, $tagid);
248 $expandfilename = false; //we're supplying a full file path
249 $status = rss_save_file('blog', $filename, $contents, $expandfilename);
250 }
251
1c7b8b93
NC
252 return $status;
253}
254