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