MDL-22112, fixed xmldb install.xml
[moodle.git] / mod / forum / rsslib.php
CommitLineData
1adbd2c3 1<?php
8adcb49f 2
8f685009
SH
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * This file adds support to rss feeds generation
20 *
21 * @package mod-forum
22 * @copyright 2001 Eloy Lafuente (stronk7) http://contiento.com
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26 /**
27 * This function is the main entry point to forum rss feeds generation.
28 * Foreach site forum with rss enabled build one XML rss structure.
29 */
8adcb49f 30 function forum_rss_feeds() {
31
4e445355 32 global $CFG, $DB;
8adcb49f 33
34 $status = true;
35
36 //Check CFG->enablerssfeeds
37 if (empty($CFG->enablerssfeeds)) {
fdb1cf2c 38 debugging('DISABLED (admin variables)');
8adcb49f 39 //Check CFG->forum_enablerssfeeds
40 } else if (empty($CFG->forum_enablerssfeeds)) {
fdb1cf2c 41 debugging('DISABLED (module configuration)');
8adcb49f 42 //It's working so we start...
43 } else {
44 //Iterate over all forums
4e445355 45 if ($forums = $DB->get_records("forum")) {
8adcb49f 46 foreach ($forums as $forum) {
47 if (!empty($forum->rsstype) && !empty($forum->rssarticles) && $status) {
6069e206 48
49 $filename = rss_file_name('forum', $forum); // RSS file
50
51 //First let's make sure there is work to do by checking existing files
52 if (file_exists($filename)) {
53 if ($lastmodified = filemtime($filename)) {
54 if (!forum_rss_newstuff($forum, $lastmodified)) {
55 continue;
56 }
57 }
58 }
59
60 //Ignore hidden forums
61 if (!instance_is_visible('forum',$forum)) {
62 if (file_exists($filename)) {
63 @unlink($filename);
64 }
65 continue;
66 }
67
3849dae8 68 mtrace("Updating RSS feed for ".format_string($forum->name,true).", ID: $forum->id");
6069e206 69
8adcb49f 70 //Get the XML contents
71 $result = forum_rss_feed($forum);
72 //Save the XML contents to file
73 if (!empty($result)) {
74 $status = rss_save_file("forum",$forum,$result);
75 }
fdb1cf2c 76 if (debugging()) {
8adcb49f 77 if (empty($result)) {
fdb1cf2c 78 echo "ID: $forum->id-> (empty) ";
8adcb49f 79 } else {
80 if (!empty($status)) {
fdb1cf2c 81 echo "ID: $forum->id-> OK ";
8adcb49f 82 } else {
fdb1cf2c 83 echo "ID: $forum->id-> FAIL ";
8adcb49f 84 }
85 }
86 }
87 }
88 }
89 }
90 }
91 return $status;
92 }
93
83da3d28 94
8f685009
SH
95 /**
96 * Given a forum object, deletes the RSS file
97 *
98 * @param 0object $forum
99 * @return bool
100 */
83da3d28 101 function forum_rss_delete_file($forum) {
102 global $CFG;
103 $rssfile = rss_file_name('forum', $forum);
104 if (file_exists($rssfile)) {
105 return unlink($rssfile);
106 } else {
107 return true;
108 }
109 }
110
111
8f685009
SH
112 /**
113 * If there is new stuff in the forum since $time then this returns
114 * true. Otherwise it returns false.
115 *
116 * @param object $forum
117 * @param int $time
118 * @return bool
119 */
6069e206 120 function forum_rss_newstuff($forum, $time) {
8f0cd6ef 121 if ($forum->rsstype == 1) {
6069e206 122 $items = forum_rss_feed_discussions($forum, $time);
8f0cd6ef 123 } else {
6069e206 124 $items = forum_rss_feed_posts($forum, $time);
125 }
126 return (!empty($items));
127 }
128
8f685009
SH
129 /**
130 * This function return the XML rss contents about the forum record passed as parameter
131 * It returns false if something is wrong
132 *
133 * @param object $forum
134 * @param bool
135 */
8adcb49f 136 function forum_rss_feed($forum) {
137
138 global $CFG;
139
140 $status = true;
141
142 //Check CFG->enablerssfeeds
143 if (empty($CFG->enablerssfeeds)) {
fdb1cf2c 144 debugging("DISABLED (admin variables)");
8adcb49f 145 //Check CFG->forum_enablerssfeeds
146 } else if (empty($CFG->forum_enablerssfeeds)) {
fdb1cf2c 147 debugging("DISABLED (module configuration)");
8adcb49f 148 //It's working so we start...
149 } else {
150 //Check the forum has rss activated
151 if (!empty($forum->rsstype) && !empty($forum->rssarticles)) {
152 //Depending of the forum->rsstype, we are going to execute, different sqls
153 if ($forum->rsstype == 1) { //Discussion RSS
154 $items = forum_rss_feed_discussions($forum);
155 } else { //Post RSS
156 $items = forum_rss_feed_posts($forum);
8f0cd6ef 157
8adcb49f 158 }
159 //Now, if items, we begin building the structure
160 if (!empty($items)) {
161 //First all rss feeds common headers
fbec0acb 162 $header = rss_standard_header(strip_tags(format_string($forum->name,true)),
8adcb49f 163 $CFG->wwwroot."/mod/forum/view.php?f=".$forum->id,
4ba2221a 164 format_string($forum->intro,true)); // TODO: fix format
8adcb49f 165 //Now all the rss items
166 if (!empty($header)) {
167 $articles = rss_add_items($items);
168 }
169 //Now all rss feeds common footers
170 if (!empty($header) && !empty($articles)) {
171 $footer = rss_standard_footer();
172 }
173 //Now, if everything is ok, concatenate it
174 if (!empty($header) && !empty($articles) && !empty($footer)) {
175 $status = $header.$articles.$footer;
176 } else {
177 $status = false;
8f0cd6ef 178 }
8adcb49f 179 } else {
180 $status = false;
181 }
182 }
183 }
184 return $status;
185 }
186
8f685009
SH
187 /**
188 * This function returns "items" record array to be used to build the rss feed
189 * for a Type=discussions forum
190 *
191 * @param object $forum
192 * @param int $newsince
193 */
6069e206 194 function forum_rss_feed_discussions($forum, $newsince=0) {
8adcb49f 195
4e445355 196 global $CFG, $DB;
8adcb49f 197
198 $items = array();
199
6069e206 200 if ($newsince) {
bbbf2d40 201 $newsince = " AND p.modified > '$newsince'";
6069e206 202 } else {
203 $newsince = "";
204 }
205
4e445355 206 if ($recs = $DB->get_records_sql ("SELECT d.id AS discussionid,
207 d.name AS discussionname,
208 u.id AS userid,
7a2c8b00 209 u.firstname AS userfirstname,
210 u.lastname AS userlastname,
211 p.message AS postmessage,
212 p.created AS postcreated,
e2d7687f 213 p.messageformat AS postformat,
214 p.messagetrust AS posttrust
4e445355 215 FROM {forum_discussions} d,
216 {forum_posts} p,
217 {user} u
8adcb49f 218 WHERE d.forum = '$forum->id' AND
219 p.discussion = d.id AND
220 p.parent = 0 AND
6069e206 221 u.id = p.userid $newsince
4e445355 222 ORDER BY p.created desc",
223 array($forum->id, $newsince), 0, $forum->rssarticles)) {
6069e206 224
8adcb49f 225 $item = NULL;
226 $user = NULL;
ec41cb3c 227
228 $formatoptions = new object;
e2d7687f 229 $formatoptions->trusted = $rec->posttrust;
ec41cb3c 230
8adcb49f 231 foreach ($recs as $rec) {
232 unset($item);
233 unset($user);
72da35d5 234 $item->title = format_string($rec->discussionname);
8adcb49f 235 $user->firstname = $rec->userfirstname;
236 $user->lastname = $rec->userlastname;
237 $item->author = fullname($user);
238 $item->pubdate = $rec->postcreated;
239 $item->link = $CFG->wwwroot."/mod/forum/discuss.php?d=".$rec->discussionid;
ec41cb3c 240 $item->description = format_text($rec->postmessage,$rec->postformat,$formatoptions,$forum->course);
8adcb49f 241 $items[] = $item;
8adcb49f 242 }
243 }
244 return $items;
245 }
8f0cd6ef 246
8f685009
SH
247 /**
248 * This function returns "items" record array to be used to build the rss feed
249 * for a Type=posts forum
2f67a9b3 250 *
8f685009
SH
251 * @param object $forum
252 * @param int $newsince
253 */
6069e206 254 function forum_rss_feed_posts($forum, $newsince=0) {
8adcb49f 255
4e445355 256 global $CFG, $DB;
8adcb49f 257
258 $items = array();
259
6069e206 260 if ($newsince) {
bbbf2d40 261 $newsince = " AND p.modified > '$newsince'";
6069e206 262 } else {
263 $newsince = "";
264 }
265
4e445355 266 if ($recs = $DB->get_records_sql ("SELECT p.id AS postid,
7a2c8b00 267 d.id AS discussionid,
bcf306f6 268 d.name AS discussionname,
7a2c8b00 269 u.id AS userid,
270 u.firstname AS userfirstname,
271 u.lastname AS userlastname,
272 p.subject AS postsubject,
273 p.message AS postmessage,
274 p.created AS postcreated,
e2d7687f 275 p.messageformat AS postformat,
276 p.messagetrust AS posttrust
4e445355 277 FROM {forum_discussions} d,
278 {forum_posts} p,
279 {user} u
280 WHERE d.forum = ? AND
8adcb49f 281 p.discussion = d.id AND
17e5633c
PS
282 u.id = p.userid $newsince
283 ORDER BY p.created desc", array($forum->id), 0, $forum->rssarticles)) {
6069e206 284
8adcb49f 285 $item = NULL;
286 $user = NULL;
318f2100 287
288 $formatoptions = new object;
e2d7687f 289 $formatoptions->trusted = $rec->posttrust;
318f2100 290
8adcb49f 291 foreach ($recs as $rec) {
292 unset($item);
293 unset($user);
bcf306f6 294 $item->category = $rec->discussionname;
ca87cbe1 295 $item->title = $rec->postsubject;
8adcb49f 296 $user->firstname = $rec->userfirstname;
297 $user->lastname = $rec->userlastname;
298 $item->author = fullname($user);
299 $item->pubdate = $rec->postcreated;
8f0cd6ef 300 $item->link = $CFG->wwwroot."/mod/forum/discuss.php?d=".$rec->discussionid."&parent=".$rec->postid;
318f2100 301 $item->description = format_text($rec->postmessage,$rec->postformat,$formatoptions,$forum->course);
410a24c0 302
303
318f2100 304 $post_file_area_name = str_replace('//', '/', "$forum->course/$CFG->moddata/forum/$forum->id/$rec->postid");
410a24c0 305 $post_files = get_directory_list("$CFG->dataroot/$post_file_area_name");
4e445355 306
307 if (!empty($post_files)) {
410a24c0 308 $item->attachments = array();
0faf6791 309 //TODO: rewrite attachment handling
410a24c0 310 }
311
8adcb49f 312 $items[] = $item;
8adcb49f 313 }
314 }
315 return $items;
8f685009 316 }