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 = $basedir .= "/".$mod->id.".xml";
102 $rss_file = fopen($file,"w");
104 $status = fwrite ($rss_file,$result);
113 //This function return all the common headers for every rss feed in the site
114 function rss_standard_header($title = NULL, $link = NULL, $description = NULL) {
116 global $CFG, $THEME, $USER;
118 static $pixpath = '';
123 if (!$site = get_site()) {
129 //Calculate title, link and description
131 $title = $site->fullname;
134 $link = $CFG->wwwroot;
136 if (empty($description)) {
137 $description = $site->summary;
141 $result .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
142 $result .= "<rss version=\"2.0\">\n";
145 $result .= rss_start_tag("channel",1,true);
148 $result .= rss_full_tag("title",2,false,$title);
149 $result .= rss_full_tag("link",2,false,$link);
150 $result .= rss_full_tag("description",2,false,$description);
151 $result .= rss_full_tag("language",2,false,substr($USER->lang,0,2));
153 $result .= rss_full_tag("copyright",2,false,"© ".$today['year']." ".$site->fullname);
154 $result .= rss_full_tag("managingEditor",2,false,$USER->email);
155 $result .= rss_full_tag("webMaster",2,false,$USER->email);
158 //Calculate the origin
159 if (empty($pixpath)) {
160 if (empty($THEME->custompix)) {
161 $pixpath = "$CFG->wwwroot/pix";
163 $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix";
166 $rsspix = $pixpath."/i/rsssitelogo.gif";
169 $result .= rss_start_tag("image",2,true);
170 $result .= rss_full_tag("url",3,false,$rsspix);
171 $result .= rss_full_tag("title",3,false,"moodle");
172 $result .= rss_full_tag("link",3,false,$CFG->wwwroot);
173 $result .= rss_full_tag("width",3,false,"140");
174 $result .= rss_full_tag("height",3,false,"35");
175 $result .= rss_end_tag("image",2,true);
185 //This function returns the rss XML code for every item passed in the array
186 //item->title: The title of the item
187 //item->author: The author of the item. Optional !!
188 //item->pubdate: The pubdate of the item
189 //item->link: The link url of the item
190 //item->description: The content of the item
191 function rss_add_items($items) {
197 if (!empty($items)) {
198 foreach ($items as $item) {
199 $result .= rss_start_tag("item",2,true);
200 $result .= rss_full_tag("title",3,false,$item->title);
201 $result .= rss_full_tag("link",3,false,$item->link);
202 $result .= rss_full_tag("pubDate",3,false,date("r",$item->pubdate));
203 //Include the author if exists
204 if (isset($item->author)) {
205 $item->description = get_string("byname","",$item->author)."<p>".$item->description;
207 $result .= rss_full_tag("description",3,false,$item->description);
208 $result .= rss_end_tag("item",2,true);
217 //This function return all the common footers for every rss feed in the site
218 function rss_standard_footer($title = NULL, $link = NULL, $description = NULL) {
226 $result .= rss_end_tag("channel",1,true);
227 ////Close the rss tag
233 // ===== This function are used to write XML tags =========
234 // [stronk7]: They are similar to the glossary export and backup generation
235 // but I've replicated them here because they have some minor
236 // diferences. Someday all they should go to a common place.
238 //Return the xml start tag
239 function rss_start_tag($tag,$level=0,$endline=false) {
245 return str_repeat(" ",$level*2)."<".$tag.">".$endchar;
248 //Return the xml end tag
249 function rss_end_tag($tag,$level=0,$endline=true) {
255 return str_repeat(" ",$level*2)."</".$tag.">".$endchar;
258 //Return the start tag, the contents and the end tag
259 function rss_full_tag($tag,$level=0,$endline=true,$content,$to_utf=true) {
260 //Here we encode absolute links
261 $st = rss_start_tag($tag,$level,$endline);
264 $co = preg_replace("/\r\n|\r/", "\n", utf8_encode(htmlspecialchars($content)));
266 $co = preg_replace("/\r\n|\r/", "\n", htmlspecialchars($content));
268 $et = rss_end_tag($tag,0,true);