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