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