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 echo " Generating rssfeeds...\n";
43 if ($allmods = get_records("modules") ) {
44 foreach ($allmods as $mod) {
45 echo ' '.$mod->name.': ';
46 $modname = $mod->name;
47 $modfile = "$CFG->dirroot/mod/$modname/rsslib.php";
48 //If file exists and we have selected to restore that type of module
49 if (file_exists($modfile)) {
50 include_once($modfile);
51 $generaterssfeeds = $modname.'_rss_feeds';
52 if (function_exists($generaterssfeeds)) {
55 $status = $generaterssfeeds();
56 if (!empty($status)) {
62 echo "...SKIPPED (failed above)\n";
65 echo "...NOT SUPPORTED (function)\n";
68 echo "...NOT SUPPORTED (file)\n";
72 echo " Ending rssfeeds...";
73 if (!empty($status)) {
82 //This function saves to file the rss feed specified in the parameters
83 function rss_save_file ($modname,$mod,$result) {
89 $basedir = $CFG->dataroot."/rss";
91 //Check dataroot/rss exists
92 $status = check_dir_exists($basedir,true);
94 $basedir .= "/".$modname;
96 //Check dataroot/rrs/$modname exists
97 $status = check_dir_exists($basedir,true);
100 $file = $basedir .= "/".$mod->id.".xml";
101 $rss_file = fopen($file,"w");
103 $status = fwrite ($rss_file,$result);
110 //This function return all the common headers for every rss feed in the site
111 function rss_standard_header($title = NULL, $link = NULL, $description = NULL) {
113 global $CFG, $THEME, $USER;
115 static $pixpath = '';
120 if (!$site = get_site()) {
126 //Calculate title, link and description
128 $title = $site->fullname;
131 $link = $CFG->wwwroot;
133 if (empty($description)) {
134 $description = $site->summary;
138 $result .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
139 $result .= "<rss version=\"2.0\">\n";
142 $result .= rss_start_tag("channel",1,true);
145 $result .= rss_full_tag("title",2,false,$title);
146 $result .= rss_full_tag("link",2,false,$link);
147 $result .= rss_full_tag("description",2,false,$description);
148 $result .= rss_full_tag("language",2,false,substr($USER->lang,0,2));
150 $result .= rss_full_tag("copyright",2,false,"© ".$today['year']." ".$site->fullname);
151 $result .= rss_full_tag("managingEditor",2,false,$USER->email);
152 $result .= rss_full_tag("webMaster",2,false,$USER->email);
155 //Calculate the origin
156 if (empty($pixpath)) {
157 if (empty($THEME->custompix)) {
158 $pixpath = "$CFG->wwwroot/pix";
160 $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix";
163 $rsspix = $pixpath."/i/rsssitelogo.gif";
166 $result .= rss_start_tag("image",2,true);
167 $result .= rss_full_tag("url",3,false,$rsspix);
168 $result .= rss_full_tag("title",3,false,"moodle");
169 $result .= rss_full_tag("link",3,false,$CFG->wwwroot);
170 $result .= rss_full_tag("width",3,false,"140");
171 $result .= rss_full_tag("height",3,false,"35");
172 $result .= rss_end_tag("image",2,true);
182 //This function returns the rss XML code for every item passed in the array
183 //item->title: The title of the item
184 //item->author: The author of the item
185 //item->pubdate: The pubdate of the item
186 //item->link: The link url of the item
187 //item->description: The content of the item
188 function rss_add_items($items) {
194 if (!empty($items)) {
195 foreach ($items as $item) {
196 $result .= rss_start_tag("item",2,true);
197 $result .= rss_full_tag("title",3,false,$item->title);
198 $result .= rss_full_tag("link",3,false,$item->link);
199 $result .= rss_full_tag("pubDate",3,false,date("r",$item->pubdate));
200 $result .= rss_full_tag("description",3,false,$item->description);
201 $result .= rss_end_tag("item",2,true);
210 //This function return all the common footers for every rss feed in the site
211 function rss_standard_footer($title = NULL, $link = NULL, $description = NULL) {
219 $result .= rss_end_tag("channel",1,true);
220 ////Close the rss tag
226 // ===== This function are used to write XML tags =========
227 // [stronk7]: They are similar to the glossary export and backup generation
228 // but I've replicated them here because they have some minor
229 // diferences. Someday all they should go to a common place.
231 //Return the xml start tag
232 function rss_start_tag($tag,$level=0,$endline=false) {
238 return str_repeat(" ",$level*2)."<".$tag.">".$endchar;
241 //Return the xml end tag
242 function rss_end_tag($tag,$level=0,$endline=true) {
248 return str_repeat(" ",$level*2)."</".$tag.">".$endchar;
251 //Return the start tag, the contents and the end tag
252 function rss_full_tag($tag,$level=0,$endline=true,$content,$to_utf=true) {
253 //Here we encode absolute links
254 $st = rss_start_tag($tag,$level,$endline);
257 $co = preg_replace("/\r\n|\r/", "\n", utf8_encode(htmlspecialchars($content)));
259 $co = preg_replace("/\r\n|\r/", "\n", htmlspecialchars($content));
261 $et = rss_end_tag($tag,0,true);