merged from MOODLE_14_STABLE;
[moodle.git] / rss / rsslib.php
CommitLineData
b2ddd9d7 1<?php // $Id$
8adcb49f 2 // This file contains all the common stuff to be used in RSS System
3
65f1df62 4//This function returns the icon (from theme) with the link to rss/file.php
5function rss_get_link($courseid, $userid, $modulename, $id, $tooltiptext="") {
8adcb49f 6
7 global $CFG, $THEME, $USER;
8
9 static $pixpath = '';
10 static $rsspath = '';
11
b2ddd9d7 12 //In site course, if no logged (userid), use admin->id. Bug 2048.
13 if ($courseid == SITEID and empty($userid)) {
14 $admin = get_admin();
15 $userid = $admin->id;
16 }
17
8adcb49f 18 if ($CFG->slasharguments) {
19 $rsspath = "$CFG->wwwroot/rss/file.php/$courseid/$userid/$modulename/$id/rss.xml";
20 } else {
21 $rsspath = "$CFG->wwwroot/rss/file.php?file=/$courseid/$userid/$modulename/$id/rss.xml";
22 }
23
24 if (empty($pixpath)) {
25 if (empty($THEME->custompix)) {
26 $pixpath = "$CFG->wwwroot/pix";
27 } else {
28 $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix";
29 }
30 }
31
32 $rsspix = $pixpath."/i/rss.gif";
33
839f2456 34 return "<a href=\"".$rsspath."\"><img src=\"$rsspix\" title=\"$tooltiptext\" alt=\"\" /></a>";
8adcb49f 35
36}
37
65f1df62 38//This function prints the icon (from theme) with the link to rss/file.php
39function rss_print_link($courseid, $userid, $modulename, $id, $tooltiptext="") {
40
41 echo rss_get_link($courseid, $userid, $modulename, $id, $tooltiptext);
42
43}
8adcb49f 44//This function iterates over each module in the server to see if
45//it supports generating rss feeds, searching for a MODULENAME_rss_feeds()
46//function and invoking it foreach activity as necessary
47function cron_rss_feeds () {
48
49 global $CFG;
50
51 $status = true;
52
b65a50e5 53 mtrace(" Generating rssfeeds...");
8adcb49f 54
2f29ee13 55 //Check for required functions...
56 if(!function_exists('utf8_encode')) {
b65a50e5 57 mtrace(" ERROR: You need to add XML support to your PHP installation!");
2f29ee13 58 return true;
59 }
60
8adcb49f 61 if ($allmods = get_records("modules") ) {
62 foreach ($allmods as $mod) {
b65a50e5 63 mtrace(' '.$mod->name.': ', '');
8adcb49f 64 $modname = $mod->name;
65 $modfile = "$CFG->dirroot/mod/$modname/rsslib.php";
66 //If file exists and we have selected to restore that type of module
67 if (file_exists($modfile)) {
68 include_once($modfile);
69 $generaterssfeeds = $modname.'_rss_feeds';
70 if (function_exists($generaterssfeeds)) {
71 if ($status) {
b65a50e5 72 mtrace('generating ', '');;
8adcb49f 73 $status = $generaterssfeeds();
74 if (!empty($status)) {
b65a50e5 75 mtrace("...OK");
8adcb49f 76 } else {
b65a50e5 77 mtrace("...FAILED");
8adcb49f 78 }
79 } else {
b65a50e5 80 mtrace("...SKIPPED (failed above)");
8adcb49f 81 }
82 } else {
b65a50e5 83 mtrace("...NOT SUPPORTED (function)");
8adcb49f 84 }
85 } else {
b65a50e5 86 mtrace("...NOT SUPPORTED (file)");
8adcb49f 87 }
88 }
89 }
b65a50e5 90 mtrace(" Ending rssfeeds...", '');
8adcb49f 91 if (!empty($status)) {
b65a50e5 92 mtrace("...OK");
8adcb49f 93 } else {
b65a50e5 94 mtrace("...FAILED");
8adcb49f 95 }
96
97 return $status;
98}
99
100//This function saves to file the rss feed specified in the parameters
101function rss_save_file ($modname,$mod,$result) {
102
103 global $CFG;
104
105 $status = true;
106
bc706aeb 107 if (! $basedir = make_upload_directory ("rss/".$modname)) {
108 //Cannot be created, so error
109 $status = false;
110 }
8adcb49f 111
112 if ($status) {
6069e206 113 $file = rss_file_name($modname, $mod);
8adcb49f 114 $rss_file = fopen($file,"w");
115 if ($rss_file) {
116 $status = fwrite ($rss_file,$result);
117 fclose($rss_file);
bc706aeb 118 } else {
119 $status = false;
8adcb49f 120 }
121 }
122 return $status;
123}
124
6069e206 125
126function rss_file_name($modname, $mod) {
127 global $CFG;
128
129 return "$CFG->dataroot/rss/$modname/$mod->id.xml";
130}
131
8adcb49f 132//This function return all the common headers for every rss feed in the site
133function rss_standard_header($title = NULL, $link = NULL, $description = NULL) {
134
1adf511c 135 global $CFG, $THEME, $USER;
8adcb49f 136
137 static $pixpath = '';
138
139 $status = true;
140 $result = "";
141
142 if (!$site = get_site()) {
143 $status = false;
144 }
145
146 if ($status) {
147
148 //Calculate title, link and description
149 if (empty($title)) {
150 $title = $site->fullname;
151 }
152 if (empty($link)) {
153 $link = $CFG->wwwroot;
154 }
155 if (empty($description)) {
156 $description = $site->summary;
157 }
158
159 //xml headers
160 $result .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
161 $result .= "<rss version=\"2.0\">\n";
162
163 //open the channel
164 $result .= rss_start_tag("channel",1,true);
165
166 //write channel info
167 $result .= rss_full_tag("title",2,false,$title);
168 $result .= rss_full_tag("link",2,false,$link);
169 $result .= rss_full_tag("description",2,false,$description);
170 $result .= rss_full_tag("language",2,false,substr($USER->lang,0,2));
171 $today = getdate();
172 $result .= rss_full_tag("copyright",2,false,"&copy; ".$today['year']." ".$site->fullname);
173 $result .= rss_full_tag("managingEditor",2,false,$USER->email);
174 $result .= rss_full_tag("webMaster",2,false,$USER->email);
175
176 //write image info
177 //Calculate the origin
178 if (empty($pixpath)) {
179 if (empty($THEME->custompix)) {
180 $pixpath = "$CFG->wwwroot/pix";
181 } else {
182 $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix";
183 }
184 }
185 $rsspix = $pixpath."/i/rsssitelogo.gif";
186
187 //write the info
188 $result .= rss_start_tag("image",2,true);
189 $result .= rss_full_tag("url",3,false,$rsspix);
190 $result .= rss_full_tag("title",3,false,"moodle");
191 $result .= rss_full_tag("link",3,false,$CFG->wwwroot);
192 $result .= rss_full_tag("width",3,false,"140");
193 $result .= rss_full_tag("height",3,false,"35");
194 $result .= rss_end_tag("image",2,true);
195 }
196
197 if (!$status) {
198 return false;
199 } else {
200 return $result;
201 }
202}
203
204//This function returns the rss XML code for every item passed in the array
205//item->title: The title of the item
ca87cbe1 206//item->author: The author of the item. Optional !!
8adcb49f 207//item->pubdate: The pubdate of the item
208//item->link: The link url of the item
209//item->description: The content of the item
210function rss_add_items($items) {
211
212 global $CFG;
213
214 $result = "";
215
216 if (!empty($items)) {
217 foreach ($items as $item) {
218 $result .= rss_start_tag("item",2,true);
219 $result .= rss_full_tag("title",3,false,$item->title);
220 $result .= rss_full_tag("link",3,false,$item->link);
221 $result .= rss_full_tag("pubDate",3,false,date("r",$item->pubdate));
ca87cbe1 222 //Include the author if exists
223 if (isset($item->author)) {
224 $item->description = get_string("byname","",$item->author)."<p>".$item->description;
225 }
8adcb49f 226 $result .= rss_full_tag("description",3,false,$item->description);
227 $result .= rss_end_tag("item",2,true);
228
229 }
230 } else {
231 $result = false;
232 }
233 return $result;
234}
235
236//This function return all the common footers for every rss feed in the site
237function rss_standard_footer($title = NULL, $link = NULL, $description = NULL) {
238
239 global $CFG, $USER;
240
241 $status = true;
242 $result = "";
243
244 //Close the chanel
245 $result .= rss_end_tag("channel",1,true);
246 ////Close the rss tag
247 $result .= "</rss>";
248
249 return $result;
250}
251
252// ===== This function are used to write XML tags =========
253// [stronk7]: They are similar to the glossary export and backup generation
254// but I've replicated them here because they have some minor
255// diferences. Someday all they should go to a common place.
256
257//Return the xml start tag
258function rss_start_tag($tag,$level=0,$endline=false) {
259 if ($endline) {
260 $endchar = "\n";
261 } else {
262 $endchar = "";
263 }
264 return str_repeat(" ",$level*2)."<".$tag.">".$endchar;
265}
266
267//Return the xml end tag
268function rss_end_tag($tag,$level=0,$endline=true) {
269 if ($endline) {
270 $endchar = "\n";
271 } else {
272 $endchar = "";
273 }
274 return str_repeat(" ",$level*2)."</".$tag.">".$endchar;
275}
276
277//Return the start tag, the contents and the end tag
278function rss_full_tag($tag,$level=0,$endline=true,$content,$to_utf=true) {
279 //Here we encode absolute links
280 $st = rss_start_tag($tag,$level,$endline);
281 $co="";
282 if ($to_utf) {
283 $co = preg_replace("/\r\n|\r/", "\n", utf8_encode(htmlspecialchars($content)));
284 } else {
285 $co = preg_replace("/\r\n|\r/", "\n", htmlspecialchars($content));
286 }
287 $et = rss_end_tag($tag,0,true);
288 return $st.$co.$et;
289}
290
291?>