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