Now scheduled bakup checks for required XML functions to work.
[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
41 echo " Generating rssfeeds...\n";
42
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)) {
53 if ($status) {
54 echo 'generating ';
55 $status = $generaterssfeeds();
56 if (!empty($status)) {
57 echo "...OK\n";
58 } else {
59 echo "...FAILED\n";
60 }
61 } else {
62 echo "...SKIPPED (failed above)\n";
63 }
64 } else {
65 echo "...NOT SUPPORTED (function)\n";
66 }
67 } else {
68 echo "...NOT SUPPORTED (file)\n";
69 }
70 }
71 }
72 echo " Ending rssfeeds...";
73 if (!empty($status)) {
74 echo "...OK\n";
75 } else {
76 echo "...FAILED\n";
77 }
78
79 return $status;
80}
81
82//This function saves to file the rss feed specified in the parameters
83function rss_save_file ($modname,$mod,$result) {
84
85 global $CFG;
86
87 $status = true;
88
89 $basedir = $CFG->dataroot."/rss";
90
91 //Check dataroot/rss exists
92 $status = check_dir_exists($basedir,true);
93
94 $basedir .= "/".$modname;
95
96 //Check dataroot/rrs/$modname exists
97 $status = check_dir_exists($basedir,true);
98
99 if ($status) {
100 $file = $basedir .= "/".$mod->id.".xml";
101 $rss_file = fopen($file,"w");
102 if ($rss_file) {
103 $status = fwrite ($rss_file,$result);
104 fclose($rss_file);
105 }
106 }
107 return $status;
108}
109
110//This function return all the common headers for every rss feed in the site
111function rss_standard_header($title = NULL, $link = NULL, $description = NULL) {
112
1adf511c 113 global $CFG, $THEME, $USER;
8adcb49f 114
115 static $pixpath = '';
116
117 $status = true;
118 $result = "";
119
120 if (!$site = get_site()) {
121 $status = false;
122 }
123
124 if ($status) {
125
126 //Calculate title, link and description
127 if (empty($title)) {
128 $title = $site->fullname;
129 }
130 if (empty($link)) {
131 $link = $CFG->wwwroot;
132 }
133 if (empty($description)) {
134 $description = $site->summary;
135 }
136
137 //xml headers
138 $result .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
139 $result .= "<rss version=\"2.0\">\n";
140
141 //open the channel
142 $result .= rss_start_tag("channel",1,true);
143
144 //write channel info
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));
149 $today = getdate();
150 $result .= rss_full_tag("copyright",2,false,"&copy; ".$today['year']." ".$site->fullname);
151 $result .= rss_full_tag("managingEditor",2,false,$USER->email);
152 $result .= rss_full_tag("webMaster",2,false,$USER->email);
153
154 //write image info
155 //Calculate the origin
156 if (empty($pixpath)) {
157 if (empty($THEME->custompix)) {
158 $pixpath = "$CFG->wwwroot/pix";
159 } else {
160 $pixpath = "$CFG->wwwroot/theme/$CFG->theme/pix";
161 }
162 }
163 $rsspix = $pixpath."/i/rsssitelogo.gif";
164
165 //write the info
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);
173 }
174
175 if (!$status) {
176 return false;
177 } else {
178 return $result;
179 }
180}
181
182//This function returns the rss XML code for every item passed in the array
183//item->title: The title of the item
ca87cbe1 184//item->author: The author of the item. Optional !!
8adcb49f 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
188function rss_add_items($items) {
189
190 global $CFG;
191
192 $result = "";
193
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));
ca87cbe1 200 //Include the author if exists
201 if (isset($item->author)) {
202 $item->description = get_string("byname","",$item->author)."<p>".$item->description;
203 }
8adcb49f 204 $result .= rss_full_tag("description",3,false,$item->description);
205 $result .= rss_end_tag("item",2,true);
206
207 }
208 } else {
209 $result = false;
210 }
211 return $result;
212}
213
214//This function return all the common footers for every rss feed in the site
215function rss_standard_footer($title = NULL, $link = NULL, $description = NULL) {
216
217 global $CFG, $USER;
218
219 $status = true;
220 $result = "";
221
222 //Close the chanel
223 $result .= rss_end_tag("channel",1,true);
224 ////Close the rss tag
225 $result .= "</rss>";
226
227 return $result;
228}
229
230// ===== This function are used to write XML tags =========
231// [stronk7]: They are similar to the glossary export and backup generation
232// but I've replicated them here because they have some minor
233// diferences. Someday all they should go to a common place.
234
235//Return the xml start tag
236function rss_start_tag($tag,$level=0,$endline=false) {
237 if ($endline) {
238 $endchar = "\n";
239 } else {
240 $endchar = "";
241 }
242 return str_repeat(" ",$level*2)."<".$tag.">".$endchar;
243}
244
245//Return the xml end tag
246function rss_end_tag($tag,$level=0,$endline=true) {
247 if ($endline) {
248 $endchar = "\n";
249 } else {
250 $endchar = "";
251 }
252 return str_repeat(" ",$level*2)."</".$tag.">".$endchar;
253}
254
255//Return the start tag, the contents and the end tag
256function rss_full_tag($tag,$level=0,$endline=true,$content,$to_utf=true) {
257 //Here we encode absolute links
258 $st = rss_start_tag($tag,$level,$endline);
259 $co="";
260 if ($to_utf) {
261 $co = preg_replace("/\r\n|\r/", "\n", utf8_encode(htmlspecialchars($content)));
262 } else {
263 $co = preg_replace("/\r\n|\r/", "\n", htmlspecialchars($content));
264 }
265 $et = rss_end_tag($tag,0,true);
266 return $st.$co.$et;
267}
268
269?>