iImproved RSS
[moodle.git] / blog / rsslib.php
CommitLineData
516194d0 1<?php
2
3 require_once($CFG->dirroot.'/lib/rsslib.php');
4
5
6 // This function returns the icon (from theme) with the link to rss/file.php
7 // needs some hacking to rss/file.php
6619a7f4 8 function blog_rss_print_link($filtertype, $filterselect, $tagid=0, $tooltiptext='') {
516194d0 9
10 global $CFG, $USER;
11
6619a7f4 12 if (empty($USER->id)) {
13 $userid = 1;
14 } else {
15 $userid = $USER->id;
16 }
17
18 switch ($filtertype) {
19 case 'site':
20 $path = SITEID.'/'.$userid.'/blog/site/'.SITEID;
21 break;
22 case 'course':
23 $path = $filterselect.'/'.$userid.'/blog/course/'.$filterselect;
24 break;
25 case 'group':
26 $path = SITEID.'/'.$userid.'/blog/group/'.$filterselect;
27 break;
28 case 'user':
29 $path = SITEID.'/'.$userid.'/blog/user/'.$filterselect;
30 break;
31 }
32
33 if ($tagid) {
34 $path .= '/'.$tagid;
35 }
36
37 $path .= '/rss.xml';
516194d0 38 $rsspix = $CFG->pixpath .'/i/rss.gif';
39
40 if ($CFG->slasharguments) {
6619a7f4 41 $path = $CFG->wwwroot.'/rss/file.php/'.$path;
516194d0 42 } else {
6619a7f4 43 $path = $CFG->wwwroot.'/rss/file.php?file='.$path;
516194d0 44 }
6619a7f4 45 print '<div align="right"><a href="'. $path .'"><img src="'. $rsspix .'" title="'. strip_tags($tooltiptext) .'" alt="" /></a></div>';
516194d0 46
47 }
48
516194d0 49
6619a7f4 50 // Generate any blog RSS feed via one function (called by ../rss/file.php)
51 function blog_generate_rss_feed($type, $id, $tagid=0) {
52
53 $filename = blog_rss_file_name($type, $id, $tagid);
54
55 if (file_exists($filename)) {
56 if (filemtime($filename) + 3600 > time()) {
57 return $filename; /// It's already done so we return cached version
58 }
59 }
60
61 // Proceed to generate it
a848c48c 62
a848c48c 63 switch ($type) {
64 case 'site':
6619a7f4 65 if (blog_site_feeds($tagid)) {
66 return $filename;
67 }
a848c48c 68 break;
69 case 'course':
6619a7f4 70 if ( blog_course_feed($id,$tagid)) {
71 return $filename;
72 }
a848c48c 73 break;
74 case 'group':
6619a7f4 75 if ( blog_group_feed($id,$tagid)) {
76 return $filename;
77 }
a848c48c 78 break;
79 case 'user':
6619a7f4 80 if ( blog_user_feed($id,$tagid)) {
81 return $filename;
82 }
a848c48c 83 break;
84 }
85
6619a7f4 86 return false; // Couldn't find it or make it
a848c48c 87 }
88
6619a7f4 89
516194d0 90 /* Rss files for blogs
91 * 4 different ways to store feeds.
6619a7f4 92 * site - $CFG->dataroot/rss/blog/site/SITEID.xml
93 * course - $CFG->dataroot/rss/blog/course/courseid.xml
94 * group - $CFG->dataroot/rss/blog/group/groupid.xml
95 * user - $CFG->dataroot/rss/blog/user/userid.xml
516194d0 96 */
6619a7f4 97 function blog_rss_file_name($type, $id, $tagid=0) {
516194d0 98 global $CFG;
6619a7f4 99
100 if ($tagid) {
101 return "$CFG->dataroot/rss/blog/$type/$id/$tagid.xml";
102 } else {
103 return "$CFG->dataroot/rss/blog/$type/$id.xml";
104 }
516194d0 105 }
106
107 //This function saves to file the rss feed specified in the parameters
108 function blog_rss_save_file($type, $id, $result) {
109 global $CFG;
110
111 $status = true;
112
113 if (! $basedir = make_upload_directory ('rss/blogs/'. $type.'/'.$id)) {
114 //Cannot be created, so error
115 $status = false;
116 }
117
118 if ($status) {
6619a7f4 119 $file = blog_rss_file_name($type, $id, $tagid);
516194d0 120 $rss_file = fopen($file, "w");
121 if ($rss_file) {
122 $status = fwrite ($rss_file, $result);
123 fclose($rss_file);
124 } else {
125 $status = false;
126 }
127 }
128 return $status;
129 }
130
131
132 // Only 1 view, site level feeds
6619a7f4 133 function blog_site_feeds($tagid=0) {
516194d0 134
135 global $CFG;
136 $status = true;
137
138 //////$CFG->debug = true;
139
140 // Check CFG->enablerssfeeds.
141 if (empty($CFG->enablerssfeeds)) {
142 //Some debug...
143 if ($CFG->debug > 7) {
144 echo "DISABLED (admin variables)";
145 }
146 }
147
148 // It's working so we start...
149 else {
150 // Iterate over all data.
6619a7f4 151 $filename = blog_rss_file_name('site', SITEID, $tagid); // RSS file
516194d0 152 // Get the most recent 20 posts
153 $sql = 'SELECT p.* FROM '.$CFG->prefix.'post p,
154 '.$CFG->prefix.'user u
155 WHERE p.userid = u.id
156 AND (p.publishstate = \'site\' OR p.publishstate = \'public\')
157 AND u.deleted = 0 ORDER BY lastmodified DESC LIMIT 0,20';
158
159 $blogposts = get_records_sql($sql);
160
161 // Now all the rss items.
162 $items = array();
163
164 foreach ($blogposts as $blogpost) {
165 $item = null;
166 $temp = array();
167 array_push($temp, $blogpost);
168
169 $user = get_record('user','id',$blogpost->userid);
170 $item->author = fullname($user);
171 $item->title = $blogpost->subject;
172 $item->pubdate = $blogpost->lastmodified;
173 $item->link = $CFG->wwwroot.'/blog/index.php?postid='.$blogpost->id;
174 $item->description = format_text($blogpost->summary, $blogpost->format);
175 array_push($items, $item);
176 }
177
178 // First all rss feeds common headers.
179 $header = rss_standard_header(format_string('siteblog',true),
180 $CFG->wwwroot.'/blog/index.php',
181 format_string('intro',true));
182
183 if (!empty($header)) {
184 $articles = rss_add_items($items);
185 }
186
187 // Now all rss feeds common footers.
188 if (!empty($header) && !empty($articles)) {
189 $footer = rss_standard_footer();
190 }
191 // Now, if everything is ok, concatenate it.
192 if (!empty($header) && !empty($articles) && !empty($footer)) {
193 $rss = $header.$articles.$footer;
194
195 //Save the XML contents to file.
196 $status = blog_rss_save_file('site', SITEID, $rss);
197 }
198 else {
199 $status = false;
200 }
201 }
202 return $status;
203 }
204
205
206 /// Generate the feeds for all courses
207 function blog_course_feeds() {
208
209 $courses = get_records('course');
210 foreach ($courses as $course) {
211 if ($course->id != SITEID) {
212 blog_course_feed($course);
213 }
214 }
215 }
216
217 // takes in course object from db
6619a7f4 218 function blog_course_feed($course, $tagid=0) {
516194d0 219
220 global $CFG;
221 $status = true;
222
223 ////$CFG->debug = true;
224
225 // Check CFG->enablerssfeeds.
226 if (empty($CFG->enablerssfeeds)) {
227 //Some debug...
228 if ($CFG->debug > 7) {
229 echo "DISABLED (admin variables)";
230 }
231 }
232
233 // It's working so we start...
234 else {
235 // Iterate over all data.
6619a7f4 236 $filename = blog_rss_file_name('course', $course->id, $tagid); // RSS file
516194d0 237 // Get the most recent 20 posts
238
239 $sql = '(SELECT p.* FROM '.$CFG->prefix.'post p, '
240 .$CFG->prefix.'user_students u
241 WHERE p.userid = u.userid
242 AND u.course = '.$course->id.'
243 AND (p.publishstate = \'site\' OR p.publishstate = \'public\'))
244
245 UNION
246
247 (SELECT p.* FROM '.$CFG->prefix.'post p, '
248 .$CFG->prefix.'user_teachers u
249 WHERE p.userid = u.userid
250 AND u.course = '.$course->id.'
251 AND (p.publishstate = \'site\' OR p.publishstate = \'public\')) ORDER BY lastmodified DESC LIMIT 0,20';
252
253 $blogposts = get_records_sql($sql);
254
255 // Now all the rss items.
256 $items = array();
257
258 foreach ($blogposts as $blogpost) {
259 $item = null;
260 $temp = array();
261 array_push($temp, $blogpost);
262
263 $user = get_record('user','id',$blogpost->userid);
264 $item->author = fullname($user);
265 $item->title = $blogpost->subject;
266 $item->pubdate = $blogpost->lastmodified;
267 $item->link = $CFG->wwwroot.'/blog/index.php?postid='.$blogpost->id;
268 $item->description = format_text($blogpost->summary, $blogpost->format);
269 array_push($items, $item);
270 }
271
272 // First all rss feeds common headers.
273 $header = rss_standard_header(format_string('courseblog',true),
274 $CFG->wwwroot.'/blog/index.php',
275 format_string('intro',true));
276
277 if (!empty($header)) {
278 $articles = rss_add_items($items);
279 }
280
281 // Now all rss feeds common footers.
282 if (!empty($header) && !empty($articles)) {
283 $footer = rss_standard_footer();
284 }
285 // Now, if everything is ok, concatenate it.
286 if (!empty($header) && !empty($articles) && !empty($footer)) {
287 $rss = $header.$articles.$footer;
288
289 //Save the XML contents to file.
290 $status = blog_rss_save_file('course',$course->id, $rss);
291 }
292 else {
293 $status = false;
294 }
295 }
296 return $status;
297 }
298
299
300 function blog_group_feeds() {
301
302 $groups = get_records('groups');
303 foreach ($groups as $group) {
304 blog_group_feed($group);
305 }
306 }
307
308 // takes in course object from db
6619a7f4 309 function blog_group_feed($group, $tagid=0) {
516194d0 310
311 global $CFG;
312 $status = true;
313
314 //$CFG->debug = true;
315
316 // Check CFG->enablerssfeeds.
317 if (empty($CFG->enablerssfeeds)) {
318 //Some debug...
319 if ($CFG->debug > 7) {
320 echo "DISABLED (admin variables)";
321 }
322 }
323
324 // It's working so we start...
325 else {
326 // Iterate over all data.
6619a7f4 327 $filename = blog_rss_file_name('group', $group->id, $tagid); // RSS file
516194d0 328 // Get the most recent 20 posts
329
330 $sql= 'SELECT p.* FROM '.$CFG->prefix.'post p, '
331 .$CFG->prefix.'groups_members m
332 WHERE p.userid = m.userid
333 AND m.groupid = '.$group->id.'
334 AND (p.publishstate = \'site\' OR p.publishstate = \'public\') ORDER BY lastmodified DESC LIMIT 0,20';
335
336
337
338 // Now all the rss items.
339 $items = array();
340 if ($blogposts = get_records_sql($sql)) {
341 foreach ($blogposts as $blogpost) {
342 $item = null;
343 $temp = array();
344 array_push($temp, $blogpost);
345
346 $user = get_record('user','id',$blogpost->userid);
347 $item->author = fullname($user);
348 $item->title = $blogpost->subject;
349 $item->pubdate = $blogpost->lastmodified;
350 $item->link = $CFG->wwwroot.'/blog/index.php?postid='.$blogpost->id;
351 $item->description = format_text($blogpost->summary, $blogpost->format);
352 array_push($items, $item);
353 }
354 }
355
356 // First all rss feeds common headers.
357 $header = rss_standard_header(format_string('groupblog',true),
358 $CFG->wwwroot.'/blog/index.php',
359 format_string('intro',true));
360
361 if (!empty($header)) {
362 $articles = rss_add_items($items);
363 }
364
365 // Now all rss feeds common footers.
366 if (!empty($header) && !empty($articles)) {
367 $footer = rss_standard_footer();
368 }
369 // Now, if everything is ok, concatenate it.
370 if (!empty($header) && !empty($articles) && !empty($footer)) {
371 $rss = $header.$articles.$footer;
372
373 //Save the XML contents to file.
374 $status = blog_rss_save_file('group',$group->id, $rss);
375 }
376 else {
377 $status = false;
378 }
379 }
380 return $status;
381 }
382
383
384 function blog_user_feeds() {
385
386 $users = get_records('user');
387 foreach ($users as $user) {
388 blog_user_feed($user);
389 }
390 }
391
392 // takes in course object from db
6619a7f4 393 function blog_user_feed($user, $tagid=0) {
516194d0 394
395 global $CFG;
396 $status = true;
397
398 ////$CFG->debug = true;
399
400 // Check CFG->enablerssfeeds.
401 if (empty($CFG->enablerssfeeds)) {
402 //Some debug...
403 if ($CFG->debug > 7) {
404 echo "DISABLED (admin variables)";
405 }
406 }
407
408 // It's working so we start...
409 else {
410 // Iterate over all data.
6619a7f4 411 $filename = blog_rss_file_name('user', $user->id, $tagid); // RSS file
516194d0 412 // Get the most recent 20 posts
413
414 $sql = 'SELECT p.* FROM '.$CFG->prefix.'post p, '
415 .$CFG->prefix.'user u
416 WHERE p.userid = u.id
417 AND u.id = '.$user->id.'
418 AND (p.publishstate = \'site\' OR p.publishstate = \'public\') ORDER BY lastmodified DESC LIMIT 0,20';
419
420
421
422 // Now all the rss items.
423 $items = array();
424 if ($blogposts = get_records_sql($sql)) {
425 foreach ($blogposts as $blogpost) {
426 $item = null;
427 $temp = array();
428 array_push($temp, $blogpost);
429
430 $user = get_record('user','id',$blogpost->userid);
431 $item->author = fullname($user);
432 $item->title = $blogpost->subject;
433 $item->pubdate = $blogpost->lastmodified;
434 $item->link = $CFG->wwwroot.'/blog/index.php?postid='.$blogpost->id;
435 $item->description = format_text($blogpost->summary, $blogpost->format);
436 array_push($items, $item);
437 }
438 }
439 // First all rss feeds common headers.
440 $header = rss_standard_header(format_string('userblog',true),
441 $CFG->wwwroot.'/blog/index.php',
442 format_string('intro',true));
443
444 if (!empty($header)) {
445 $articles = rss_add_items($items);
446 }
447
448 // Now all rss feeds common footers.
449 if (!empty($header) && !empty($articles)) {
450 $footer = rss_standard_footer();
451 }
452 // Now, if everything is ok, concatenate it.
453 if (!empty($header) && !empty($articles) && !empty($footer)) {
454 $rss = $header.$articles.$footer;
455
456 //Save the XML contents to file.
457 $status = blog_rss_save_file('user',$user->id, $rss);
458 }
459 else {
460 $status = false;
461 }
462 }
463 return $status;
464 }
465?>