New strings added
[moodle.git] / mod / forum / rsslib.php
CommitLineData
41905731 1<?php // $Id$
8adcb49f 2 //This file adds support to rss feeds generation
3
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() {
8
9 global $CFG;
10
11 $status = true;
12
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) {
6069e206 31
32 $filename = rss_file_name('forum', $forum); // RSS file
33
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 }
42
43 //Ignore hidden forums
44 if (!instance_is_visible('forum',$forum)) {
45 if (file_exists($filename)) {
46 @unlink($filename);
47 }
48 continue;
49 }
50
51 mtrace("Updating RSS feed for $forum->name, ID: $forum->id");
52
8adcb49f 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 }
81
6069e206 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.
8f0cd6ef 85 if ($forum->rsstype == 1) {
6069e206 86 $items = forum_rss_feed_discussions($forum, $time);
8f0cd6ef 87 } else {
6069e206 88 $items = forum_rss_feed_posts($forum, $time);
89 }
90 return (!empty($items));
91 }
92
8adcb49f 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) {
96
97 global $CFG;
98
99 $status = true;
100
101 //Check CFG->enablerssfeeds
102 if (empty($CFG->enablerssfeeds)) {
103 //Some debug...
104 if ($CFG->debug > 7) {
8f0cd6ef 105 echo "DISABLED (admin variables)";
106 }
8adcb49f 107 //Check CFG->forum_enablerssfeeds
108 } else if (empty($CFG->forum_enablerssfeeds)) {
8f0cd6ef 109 //Some debug...
8adcb49f 110 if ($CFG->debug > 7) {
111 echo "DISABLED (module configuration)";
8f0cd6ef 112 }
8adcb49f 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);
8f0cd6ef 122
8adcb49f 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;
8f0cd6ef 143 }
8adcb49f 144 } else {
145 $status = false;
146 }
147 }
148 }
149 return $status;
150 }
151
152 //This function returns "items" record array to be used to build the rss feed
153 //for a Type=discussions forum
6069e206 154 function forum_rss_feed_discussions($forum, $newsince=0) {
8adcb49f 155
156 global $CFG;
157
158 $items = array();
159
6069e206 160 if ($newsince) {
161 $newsince = " AND p.created > '$newsince'";
162 } else {
163 $newsince = "";
164 }
165
8f0cd6ef 166 if ($recs = get_records_sql ("SELECT d.id discussionid,
167 d.name discussionname,
168 u.id userid,
8adcb49f 169 u.firstname userfirstname,
170 u.lastname userlastname,
171 p.message postmessage,
ca87cbe1 172 p.created postcreated,
173 p.format postformat
8adcb49f 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
6069e206 180 u.id = p.userid $newsince
8adcb49f 181 ORDER BY p.created desc")) {
6069e206 182
183 //Are we just looking for new ones? If so, then return now.
184 if ($newsince) {
185 return true;
186 }
187
8adcb49f 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;
ca87cbe1 201 $item->description = format_text($rec->postmessage,$rec->postformat,NULL,$forum->course);
8adcb49f 202 $items[] = $item;
203 $articlesleft--;
204 if ($articlesleft < 1) {
205 break;
206 }
207 }
208 }
209 return $items;
210 }
8f0cd6ef 211
8adcb49f 212 //This function returns "items" record array to be used to build the rss feed
213 //for a Type=posts forum
6069e206 214 function forum_rss_feed_posts($forum, $newsince=0) {
8adcb49f 215
216 global $CFG;
217
218 $items = array();
219
6069e206 220 if ($newsince) {
221 $newsince = " AND p.created > '$newsince'";
222 } else {
223 $newsince = "";
224 }
225
8adcb49f 226 if ($recs = get_records_sql ("SELECT p.id postid,
227 d.id discussionid,
8adcb49f 228 u.id userid,
229 u.firstname userfirstname,
230 u.lastname userlastname,
ca87cbe1 231 p.subject postsubject,
8adcb49f 232 p.message postmessage,
ca87cbe1 233 p.created postcreated,
234 p.format postformat
8adcb49f 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
6069e206 240 u.id = p.userid $newsince
8adcb49f 241 ORDER BY p.created desc")) {
6069e206 242
243 //Are we just looking for new ones? If so, then return now.
244 if ($newsince) {
245 return true;
246 }
247
8adcb49f 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);
ca87cbe1 255 $item->title = $rec->postsubject;
8adcb49f 256 $user->firstname = $rec->userfirstname;
257 $user->lastname = $rec->userlastname;
258 $item->author = fullname($user);
259 $item->pubdate = $rec->postcreated;
8f0cd6ef 260 $item->link = $CFG->wwwroot."/mod/forum/discuss.php?d=".$rec->discussionid."&parent=".$rec->postid;
ca87cbe1 261 $item->description = format_text($rec->postmessage,$rec->postformat,NULL,$forum->course);
8adcb49f 262 $items[] = $item;
263 $articlesleft--;
264 if ($articlesleft < 1) {
265 break;
266 }
267 }
268 }
269 return $items;
270 }
271?>