fd4bc4b5a61ab6c7dbf4a641e0e81066bb20495c
[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) {
31                         //Some debug...
32                         if ($CFG->debug > 7) {
33                             echo "ID: $forum->id->";
34                         }
35                         //Get the XML contents
36                         $result = forum_rss_feed($forum);
37                         //Save the XML contents to file
38                         if (!empty($result)) {
39                             $status = rss_save_file("forum",$forum,$result);
40                         }
41                         //Some debug...
42                         if ($CFG->debug > 7) {
43                             if (empty($result)) {
44                                 echo "(empty) ";
45                             } else {
46                                 if (!empty($status)) {
47                                     echo "OK ";
48                                 } else {
49                                     echo "FAIL ";
50                                 }
51                             }
52                         }
53                     }
54                 }
55             }
56         }
57         return $status;
58     }
60     //This function return the XML rss contents about the forum record passed as parameter
61     //It returns false if something is wrong
62     function forum_rss_feed($forum) {
64         global $CFG;
66         $status = true;
68         //Check CFG->enablerssfeeds
69         if (empty($CFG->enablerssfeeds)) {
70             //Some debug...
71             if ($CFG->debug > 7) {
72                 echo "DISABLED (admin variables)"; 
73             }           
74         //Check CFG->forum_enablerssfeeds
75         } else if (empty($CFG->forum_enablerssfeeds)) {
76             //Some debug... 
77             if ($CFG->debug > 7) {
78                 echo "DISABLED (module configuration)";
79             }           
80         //It's working so we start...
81         } else {
82             //Check the forum has rss activated
83             if (!empty($forum->rsstype) && !empty($forum->rssarticles)) {
84                 //Depending of the forum->rsstype, we are going to execute, different sqls
85                 if ($forum->rsstype == 1) {    //Discussion RSS
86                     $items = forum_rss_feed_discussions($forum);
87                 } else {                //Post RSS
88                     $items = forum_rss_feed_posts($forum);
89      
90                 }
91                 //Now, if items, we begin building the structure
92                 if (!empty($items)) {
93                     //First all rss feeds common headers
94                     $header = rss_standard_header($forum->name,
95                                                   $CFG->wwwroot."/mod/forum/view.php?f=".$forum->id,
96                                                   $forum->intro);
97                     //Now all the rss items
98                     if (!empty($header)) {
99                         $articles = rss_add_items($items);
100                     }
101                     //Now all rss feeds common footers
102                     if (!empty($header) && !empty($articles)) {
103                         $footer = rss_standard_footer();
104                     }
105                     //Now, if everything is ok, concatenate it
106                     if (!empty($header) && !empty($articles) && !empty($footer)) {
107                         $status = $header.$articles.$footer;
108                     } else {
109                         $status = false;
110                     } 
111                 } else {
112                     $status = false;
113                 }
114             }
115         }
116         return $status;
117     }
119     //This function returns "items" record array to be used to build the rss feed
120     //for a Type=discussions forum
121     function forum_rss_feed_discussions($forum) {
123         global $CFG;
125         $items = array();
127         if ($recs = get_records_sql ("SELECT d.id discussionid, 
128                                              d.name discussionname, 
129                                              u.id userid, 
130                                              u.firstname userfirstname,
131                                              u.lastname userlastname,
132                                              p.message postmessage,
133                                              p.created postcreated,
134                                              p.format postformat
135                                       FROM {$CFG->prefix}forum_discussions d,
136                                            {$CFG->prefix}forum_posts p,
137                                            {$CFG->prefix}user u
138                                       WHERE d.forum = '$forum->id' AND
139                                             p.discussion = d.id AND
140                                             p.parent = 0 AND
141                                             u.id = p.userid
142                                       ORDER BY p.created desc")) {
143             //Iterate over each discussion to get forum->rssarticles records
144             $articlesleft = $forum->rssarticles;
145             $item = NULL;
146             $user = NULL;
147             foreach ($recs as $rec) {
148                 unset($item);
149                 unset($user);
150                 $item->title = $rec->discussionname;
151                 $user->firstname = $rec->userfirstname;
152                 $user->lastname = $rec->userlastname;
153                 $item->author = fullname($user);
154                 $item->pubdate = $rec->postcreated;
155                 $item->link = $CFG->wwwroot."/mod/forum/discuss.php?d=".$rec->discussionid;
156                 $item->description = format_text($rec->postmessage,$rec->postformat,NULL,$forum->course);
157                 $items[] = $item;
158                 $articlesleft--;
159                 if ($articlesleft < 1) {
160                     break;
161                 }
162             }
163         }
164         return $items;
165     }
166     
167     //This function returns "items" record array to be used to build the rss feed
168     //for a Type=posts forum
169     function forum_rss_feed_posts($forum) {
171         global $CFG;
173         $items = array();
175         if ($recs = get_records_sql ("SELECT p.id postid,
176                                              d.id discussionid,
177                                              u.id userid,
178                                              u.firstname userfirstname,
179                                              u.lastname userlastname,
180                                              p.subject postsubject,
181                                              p.message postmessage,
182                                              p.created postcreated,
183                                              p.format postformat
184                                       FROM {$CFG->prefix}forum_discussions d,
185                                            {$CFG->prefix}forum_posts p,
186                                            {$CFG->prefix}user u
187                                       WHERE d.forum = '$forum->id' AND
188                                             p.discussion = d.id AND
189                                             u.id = p.userid
190                                       ORDER BY p.created desc")) {
191             //Iterate over each discussion to get forum->rssarticles records
192             $articlesleft = $forum->rssarticles;
193             $item = NULL;
194             $user = NULL;
195             foreach ($recs as $rec) {
196                 unset($item);
197                 unset($user);
198                 $item->title = $rec->postsubject;
199                 $user->firstname = $rec->userfirstname;
200                 $user->lastname = $rec->userlastname;
201                 $item->author = fullname($user);
202                 $item->pubdate = $rec->postcreated;
203                 $item->link = $CFG->wwwroot."/mod/forum/discuss.php?d=".$rec->discussionid."&parent=".$rec->postid;
204                 $item->description = format_text($rec->postmessage,$rec->postformat,NULL,$forum->course);
205                 $items[] = $item;
206                 $articlesleft--;
207                 if ($articlesleft < 1) {
208                     break;
209                 }
210             }
211         }
212         return $items;
213     }
214 ?>