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