RSS for forums now checks the new posts against the file modification date,
[moodle.git] / mod / forum / rsslib.php
1 <?PHP  // $Id$
2     //This file adds support to rss feeds generation
4     //This function is the main entry point to forum
5     //rss feeds generation. Foreach site forum with rss enabled
6     //build one XML rss structure.
7     function forum_rss_feeds() {
9         global $CFG;
11         $status = true;
13         //Check CFG->enablerssfeeds
14         if (empty($CFG->enablerssfeeds)) {
15             //Some debug...
16             if ($CFG->debug > 7) {
17                 echo "DISABLED (admin variables)";
18             }
19         //Check CFG->forum_enablerssfeeds
20         } else if (empty($CFG->forum_enablerssfeeds)) {
21             //Some debug...
22             if ($CFG->debug > 7) {
23                 echo "DISABLED (module configuration)";
24             }
25         //It's working so we start...
26         } else {
27             //Iterate over all forums
28             if ($forums = get_records("forum")) {
29                 foreach ($forums as $forum) {
30                     if (!empty($forum->rsstype) && !empty($forum->rssarticles) && $status) {
32                         $filename = rss_file_name('forum', $forum);  // RSS file
34                         //First let's make sure there is work to do by checking existing files
35                         if (file_exists($filename)) {
36                             if ($lastmodified = filemtime($filename)) {
37                                 if (!forum_rss_newstuff($forum, $lastmodified)) {
38                                     continue;
39                                 }
40                             }
41                         }
43                         //Ignore hidden forums
44                         if (!instance_is_visible('forum',$forum)) {
45                             if (file_exists($filename)) {
46                                 @unlink($filename);
47                             }
48                             continue;
49                         }
51                         mtrace("Updating RSS feed for $forum->name, ID: $forum->id");
53                         //Some debug...
54                         if ($CFG->debug > 7) {
55                             echo "ID: $forum->id->";
56                         }
57                         //Get the XML contents
58                         $result = forum_rss_feed($forum);
59                         //Save the XML contents to file
60                         if (!empty($result)) {
61                             $status = rss_save_file("forum",$forum,$result);
62                         }
63                         //Some debug...
64                         if ($CFG->debug > 7) {
65                             if (empty($result)) {
66                                 echo "(empty) ";
67                             } else {
68                                 if (!empty($status)) {
69                                     echo "OK ";
70                                 } else {
71                                     echo "FAIL ";
72                                 }
73                             }
74                         }
75                     }
76                 }
77             }
78         }
79         return $status;
80     }
82     function forum_rss_newstuff($forum, $time) {
83     // If there is new stuff in the forum since $time then this returns
84     // true.  Otherwise it returns false.
85         if ($forum->rsstype == 1) {  
86             $items = forum_rss_feed_discussions($forum, $time);
87         } else {             
88             $items = forum_rss_feed_posts($forum, $time);
89         }
90         return (!empty($items));
91     }
93     //This function return the XML rss contents about the forum record passed as parameter
94     //It returns false if something is wrong
95     function forum_rss_feed($forum) {
97         global $CFG;
99         $status = true;
101         //Check CFG->enablerssfeeds
102         if (empty($CFG->enablerssfeeds)) {
103             //Some debug...
104             if ($CFG->debug > 7) {
105                 echo "DISABLED (admin variables)"; 
106             }           
107         //Check CFG->forum_enablerssfeeds
108         } else if (empty($CFG->forum_enablerssfeeds)) {
109             //Some debug... 
110             if ($CFG->debug > 7) {
111                 echo "DISABLED (module configuration)";
112             }           
113         //It's working so we start...
114         } else {
115             //Check the forum has rss activated
116             if (!empty($forum->rsstype) && !empty($forum->rssarticles)) {
117                 //Depending of the forum->rsstype, we are going to execute, different sqls
118                 if ($forum->rsstype == 1) {    //Discussion RSS
119                     $items = forum_rss_feed_discussions($forum);
120                 } else {                //Post RSS
121                     $items = forum_rss_feed_posts($forum);
122      
123                 }
124                 //Now, if items, we begin building the structure
125                 if (!empty($items)) {
126                     //First all rss feeds common headers
127                     $header = rss_standard_header($forum->name,
128                                                   $CFG->wwwroot."/mod/forum/view.php?f=".$forum->id,
129                                                   $forum->intro);
130                     //Now all the rss items
131                     if (!empty($header)) {
132                         $articles = rss_add_items($items);
133                     }
134                     //Now all rss feeds common footers
135                     if (!empty($header) && !empty($articles)) {
136                         $footer = rss_standard_footer();
137                     }
138                     //Now, if everything is ok, concatenate it
139                     if (!empty($header) && !empty($articles) && !empty($footer)) {
140                         $status = $header.$articles.$footer;
141                     } else {
142                         $status = false;
143                     } 
144                 } else {
145                     $status = false;
146                 }
147             }
148         }
149         return $status;
150     }
152     //This function returns "items" record array to be used to build the rss feed
153     //for a Type=discussions forum
154     function forum_rss_feed_discussions($forum, $newsince=0) {
156         global $CFG;
158         $items = array();
160         if ($newsince) {
161             $newsince = " AND p.created > '$newsince'";
162         } else {
163             $newsince = "";
164         }
166         if ($recs = get_records_sql ("SELECT d.id discussionid, 
167                                              d.name discussionname, 
168                                              u.id userid, 
169                                              u.firstname userfirstname,
170                                              u.lastname userlastname,
171                                              p.message postmessage,
172                                              p.created postcreated,
173                                              p.format postformat
174                                       FROM {$CFG->prefix}forum_discussions d,
175                                            {$CFG->prefix}forum_posts p,
176                                            {$CFG->prefix}user u
177                                       WHERE d.forum = '$forum->id' AND
178                                             p.discussion = d.id AND
179                                             p.parent = 0 AND
180                                             u.id = p.userid $newsince
181                                       ORDER BY p.created desc")) {
183             //Are we just looking for new ones?  If so, then return now.
184             if ($newsince) {
185                 return true;
186             }
188             //Iterate over each discussion to get forum->rssarticles records
189             $articlesleft = $forum->rssarticles;
190             $item = NULL;
191             $user = NULL;
192             foreach ($recs as $rec) {
193                 unset($item);
194                 unset($user);
195                 $item->title = $rec->discussionname;
196                 $user->firstname = $rec->userfirstname;
197                 $user->lastname = $rec->userlastname;
198                 $item->author = fullname($user);
199                 $item->pubdate = $rec->postcreated;
200                 $item->link = $CFG->wwwroot."/mod/forum/discuss.php?d=".$rec->discussionid;
201                 $item->description = format_text($rec->postmessage,$rec->postformat,NULL,$forum->course);
202                 $items[] = $item;
203                 $articlesleft--;
204                 if ($articlesleft < 1) {
205                     break;
206                 }
207             }
208         }
209         return $items;
210     }
211     
212     //This function returns "items" record array to be used to build the rss feed
213     //for a Type=posts forum
214     function forum_rss_feed_posts($forum, $newsince=0) {
216         global $CFG;
218         $items = array();
220         if ($newsince) {
221             $newsince = " AND p.created > '$newsince'";
222         } else {
223             $newsince = "";
224         }
226         if ($recs = get_records_sql ("SELECT p.id postid,
227                                              d.id discussionid,
228                                              u.id userid,
229                                              u.firstname userfirstname,
230                                              u.lastname userlastname,
231                                              p.subject postsubject,
232                                              p.message postmessage,
233                                              p.created postcreated,
234                                              p.format postformat
235                                       FROM {$CFG->prefix}forum_discussions d,
236                                            {$CFG->prefix}forum_posts p,
237                                            {$CFG->prefix}user u
238                                       WHERE d.forum = '$forum->id' AND
239                                             p.discussion = d.id AND
240                                             u.id = p.userid $newsince
241                                       ORDER BY p.created desc")) {
243             //Are we just looking for new ones?  If so, then return now.
244             if ($newsince) {
245                 return true;
246             }
248             //Iterate over each discussion to get forum->rssarticles records
249             $articlesleft = $forum->rssarticles;
250             $item = NULL;
251             $user = NULL;
252             foreach ($recs as $rec) {
253                 unset($item);
254                 unset($user);
255                 $item->title = $rec->postsubject;
256                 $user->firstname = $rec->userfirstname;
257                 $user->lastname = $rec->userlastname;
258                 $item->author = fullname($user);
259                 $item->pubdate = $rec->postcreated;
260                 $item->link = $CFG->wwwroot."/mod/forum/discuss.php?d=".$rec->discussionid."&parent=".$rec->postid;
261                 $item->description = format_text($rec->postmessage,$rec->postformat,NULL,$forum->course);
262                 $items[] = $item;
263                 $articlesleft--;
264                 if ($articlesleft < 1) {
265                     break;
266                 }
267             }
268         }
269         return $items;
270     }
271 ?>