2 // This file contains all the common stuff to be used in RSS System
4 //This function prints the icon (from theme) with the link to rss/file.php
5 function rss_print_link($courseid, $userid, $modulename, $id, $tooltiptext="") {
7 global $CFG, $THEME, $USER;
12 if ($CFG->slasharguments) {
13 $rsspath = "$CFG->wwwroot/rss/file.php/$courseid/$userid/$modulename/$id/rss.xml";
15 $rsspath = "$CFG->wwwroot/rss/file.php?file=/$courseid/$userid/$modulename/$id/rss.xml";
18 if (empty($pixpath)) {
19 if (empty($THEME->custompix)) {
20 $pixpath = "$CFG->wwwroot/pix";
22 $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix";
26 $rsspix = $pixpath."/i/rss.gif";
28 echo "<a href=\"".$rsspath."\"><img src=\"$rsspix\" title=\"$tooltiptext\"></a>";
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
35 function cron_rss_feeds () {
41 mtrace(" Generating rssfeeds...");
43 //Check for required functions...
44 if(!function_exists('utf8_encode')) {
45 mtrace(" ERROR: You need to add XML support to your PHP installation!");
49 if ($allmods = get_records("modules") ) {
50 foreach ($allmods as $mod) {
51 mtrace(' '.$mod->name.': ', '');
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)) {
60 mtrace('generating ', '');;
61 $status = $generaterssfeeds();
62 if (!empty($status)) {
68 mtrace("...SKIPPED (failed above)");
71 mtrace("...NOT SUPPORTED (function)");
74 mtrace("...NOT SUPPORTED (file)");
78 mtrace(" Ending rssfeeds...", '');
79 if (!empty($status)) {
88 //This function saves to file the rss feed specified in the parameters
89 function rss_save_file ($modname,$mod,$result) {
95 if (! $basedir = make_upload_directory ("rss/".$modname)) {
96 //Cannot be created, so error
101 $file = rss_file_name($modname, $mod);
102 $rss_file = fopen($file,"w");
104 $status = fwrite ($rss_file,$result);
114 function rss_file_name($modname, $mod) {
117 return "$CFG->dataroot/rss/$modname/$mod->id.xml";
120 //This function return all the common headers for every rss feed in the site
121 function rss_standard_header($title = NULL, $link = NULL, $description = NULL) {
123 global $CFG, $THEME, $USER;
125 static $pixpath = '';
130 if (!$site = get_site()) {
136 //Calculate title, link and description
138 $title = $site->fullname;
141 $link = $CFG->wwwroot;
143 if (empty($description)) {
144 $description = $site->summary;
148 $result .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
149 $result .= "<rss version=\"2.0\">\n";
152 $result .= rss_start_tag("channel",1,true);
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));
160 $result .= rss_full_tag("copyright",2,false,"© ".$today['year']." ".$site->fullname);
161 $result .= rss_full_tag("managingEditor",2,false,$USER->email);
162 $result .= rss_full_tag("webMaster",2,false,$USER->email);
165 //Calculate the origin
166 if (empty($pixpath)) {
167 if (empty($THEME->custompix)) {
168 $pixpath = "$CFG->wwwroot/pix";
170 $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix";
173 $rsspix = $pixpath."/i/rsssitelogo.gif";
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);
192 //This function returns the rss XML code for every item passed in the array
193 //item->title: The title of the item
194 //item->author: The author of the item. Optional !!
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
198 function rss_add_items($items) {
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));
210 //Include the author if exists
211 if (isset($item->author)) {
212 $item->description = get_string("byname","",$item->author)."<p>".$item->description;
214 $result .= rss_full_tag("description",3,false,$item->description);
215 $result .= rss_end_tag("item",2,true);
224 //This function return all the common footers for every rss feed in the site
225 function rss_standard_footer($title = NULL, $link = NULL, $description = NULL) {
233 $result .= rss_end_tag("channel",1,true);
234 ////Close the rss tag
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.
245 //Return the xml start tag
246 function rss_start_tag($tag,$level=0,$endline=false) {
252 return str_repeat(" ",$level*2)."<".$tag.">".$endchar;
255 //Return the xml end tag
256 function rss_end_tag($tag,$level=0,$endline=true) {
262 return str_repeat(" ",$level*2)."</".$tag.">".$endchar;
265 //Return the start tag, the contents and the end tag
266 function 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);
271 $co = preg_replace("/\r\n|\r/", "\n", utf8_encode(htmlspecialchars($content)));
273 $co = preg_replace("/\r\n|\r/", "\n", htmlspecialchars($content));
275 $et = rss_end_tag($tag,0,true);