MDL-22112, fixed xmldb install.xml
[moodle.git] / mod / glossary / rsslib.php
CommitLineData
1adbd2c3 1<?php
2dda0bbb 2 //This file adds support to rss feeds generation
3
4 //This function is the main entry point to glossary
5 //rss feeds generation. Foreach site glossary with rss enabled
6 //build one XML rss structure.
7 function glossary_rss_feeds() {
ae8c3566 8 global $CFG, $DB;
2dda0bbb 9
10 $status = true;
11
12 //Check CFG->enablerssfeeds
13 if (empty($CFG->enablerssfeeds)) {
fdb1cf2c 14 debugging("DISABLED (admin variables)");
2dda0bbb 15 //Check CFG->glossary_enablerssfeeds
16 } else if (empty($CFG->glossary_enablerssfeeds)) {
fdb1cf2c 17 debugging("DISABLED (module configuration)");
2dda0bbb 18 //It's working so we start...
19 } else {
20 //Iterate over all glossaries
ae8c3566 21 if ($glossaries = $DB->get_records("glossary")) {
2dda0bbb 22 foreach ($glossaries as $glossary) {
23 if (!empty($glossary->rsstype) && !empty($glossary->rssarticles) && $status) {
44d6ae6d 24
25 $filename = rss_file_name('glossary', $glossary); // RSS file
26
27 //First let's make sure there is work to do by checking existing files
28 if (file_exists($filename)) {
29 if ($lastmodified = filemtime($filename)) {
30 if (!glossary_rss_newstuff($glossary, $lastmodified)) {
31 continue;
32 }
33 }
34 }
35
36 //Ignore hidden forums
37 if (!instance_is_visible('glossary',$glossary)) {
38 if (file_exists($filename)) {
39 @unlink($filename);
40 }
41 continue;
42 }
43
5bd9aa93 44 mtrace("Updating RSS feed for ".format_string($glossary->name,true).", ID: $glossary->id");
44d6ae6d 45
2dda0bbb 46 //Get the XML contents
47 $result = glossary_rss_feed($glossary);
48 //Save the XML contents to file
49 if (!empty($result)) {
50 $status = rss_save_file("glossary",$glossary,$result);
51 }
52 //Some debug...
fdb1cf2c 53 if (debugging()) {
2dda0bbb 54 if (empty($result)) {
fdb1cf2c 55 echo "ID: $glossary->id-> (empty) ";
2dda0bbb 56 } else {
57 if (!empty($status)) {
fdb1cf2c 58 echo "ID: $glossary->id-> OK ";
2dda0bbb 59 } else {
fdb1cf2c 60 echo "ID: $glossary->id-> FAIL ";
2dda0bbb 61 }
62 }
63 }
64 }
65 }
66 }
67 }
68 return $status;
69 }
70
44d6ae6d 71 function glossary_rss_newstuff($glossary, $time) {
72 // If there is new stuff in the glossary since $time then this returns
73 // true. Otherwise it returns false.
74 if ($glossary->rsstype == 1) {
75 $items = glossary_rss_feed_withauthor($glossary, $time);
76 } else {
77 $items = glossary_rss_feed_withoutauthor($glossary, $time);
78 }
79 return (!empty($items));
80 }
81
2dda0bbb 82 //This function return the XML rss contents about the glossary record passed as parameter
83 //It returns false if something is wrong
84 function glossary_rss_feed($glossary) {
ae8c3566 85 global $CFG, $DB;
2dda0bbb 86
87 $status = true;
88
89 //Check CFG->enablerssfeeds
90 if (empty($CFG->enablerssfeeds)) {
fdb1cf2c 91 debugging("DISABLED (admin variables)");
2dda0bbb 92 //Check CFG->glossary_enablerssfeeds
93 } else if (empty($CFG->glossary_enablerssfeeds)) {
fdb1cf2c 94 debugging("DISABLED (module configuration)");
2dda0bbb 95 //It's working so we start...
96 } else {
97 //Check the glossary has rss activated
98 if (!empty($glossary->rsstype) && !empty($glossary->rssarticles)) {
99 //Depending of the glossary->rsstype, we are going to execute, different sqls
100 if ($glossary->rsstype == 1) { //With author RSS
101 $items = glossary_rss_feed_withauthor($glossary);
102 } else { //Without author RSS
103 $items = glossary_rss_feed_withoutauthor($glossary);
8f0cd6ef 104
2dda0bbb 105 }
106 //Now, if items, we begin building the structure
107 if (!empty($items)) {
108 //First all rss feeds common headers
5bd9aa93 109 $header = rss_standard_header(format_string($glossary->name,true),
11cc84ee 110 $CFG->wwwroot."/mod/glossary/view.php?g=".$glossary->id,
4ba2221a 111 format_string($glossary->intro,true)); //TODO: fix format
2dda0bbb 112 //Now all the rss items
113 if (!empty($header)) {
114 $articles = rss_add_items($items);
115 }
116 //Now all rss feeds common footers
117 if (!empty($header) && !empty($articles)) {
118 $footer = rss_standard_footer();
119 }
120 //Now, if everything is ok, concatenate it
121 if (!empty($header) && !empty($articles) && !empty($footer)) {
122 $status = $header.$articles.$footer;
123 } else {
124 $status = false;
8f0cd6ef 125 }
2dda0bbb 126 } else {
127 $status = false;
128 }
129 }
130 }
131 return $status;
132 }
133
134 //This function returns "items" record array to be used to build the rss feed
135 //for a Type=with author glossary
44d6ae6d 136 function glossary_rss_feed_withauthor($glossary, $newsince=0) {
ae8c3566 137 global $CFG, $DB;
2dda0bbb 138
139 $items = array();
140
cbc2b5df 141 $params = array('gid'=>$glossary->id, 'newsince'=>$newsince);
ae8c3566 142
44d6ae6d 143 if ($newsince) {
ae8c3566 144 $newsince = "AND e.timecreated > :newsince";
8f0cd6ef 145 } else {
44d6ae6d 146 $newsince = "";
8f0cd6ef 147 }
44d6ae6d 148
cbc2b5df 149 if ($recs = $DB->get_records_sql ("SELECT e.id AS entryid,
150 e.concept AS entryconcept,
151 e.definition AS entrydefinition,
152 e.definitionformat AS entryformat,
153 e.definitiontrust AS entrytrust,
154 e.timecreated AS entrytimecreated,
155 u.id AS userid,
ae8c3566 156 u.firstname AS userfirstname,
157 u.lastname AS userlastname
158 FROM {glossary_entries} e,
159 {user} u
160 WHERE e.glossaryid = :gid AND
161 u.id = e.userid AND
162 e.approved = 1 $newsince
163 ORDER BY e.timecreated desc", $params)) {
44d6ae6d 164
165 //Are we just looking for new ones? If so, then return now.
166 if ($newsince) {
167 return true;
168 }
2dda0bbb 169 //Iterate over each entry to get glossary->rssarticles records
170 $articlesleft = $glossary->rssarticles;
f8b92241 171
172 $formatoptions = new object;
cbc2b5df 173 $formatoptions->trusted = $comment->entrytrust;
f8b92241 174
2dda0bbb 175 foreach ($recs as $rec) {
ae8c3566 176 $item = new object();
177 $user = new user();
2dda0bbb 178 $item->title = $rec->entryconcept;
179 $user->firstname = $rec->userfirstname;
180 $user->lastname = $rec->userlastname;
181 $item->author = fullname($user);
182 $item->pubdate = $rec->entrytimecreated;
8f0cd6ef 183 $item->link = $CFG->wwwroot."/mod/glossary/showentry.php?courseid=".$glossary->course."&eid=".$rec->entryid;
f8b92241 184 $item->description = format_text($rec->entrydefinition,$rec->entryformat,$formatoptions,$glossary->course);
2dda0bbb 185 $items[] = $item;
186 $articlesleft--;
187 if ($articlesleft < 1) {
188 break;
189 }
190 }
191 }
192 return $items;
193 }
194
195 //This function returns "items" record array to be used to build the rss feed
196 //for a Type=without author glossary
44d6ae6d 197 function glossary_rss_feed_withoutauthor($glossary, $newsince=0) {
ae8c3566 198 global $CFG, $DB;
2dda0bbb 199
200 $items = array();
201
cbc2b5df 202 $params = array('gid'=>$glossary->id, 'newsince'=>$newsince);
ae8c3566 203
44d6ae6d 204 if ($newsince) {
ae8c3566 205 $newsince = "AND e.timecreated > :newsince";
44d6ae6d 206 } else {
207 $newsince = "";
208 }
209
ae8c3566 210 if ($recs = $DB->get_records_sql ("SELECT e.id AS entryid,
211 e.concept AS entryconcept,
212 e.definition AS entrydefinition,
cbc2b5df 213 e.definitionformat AS entryformat,
214 e.definitiontrust AS entrytrust,
ae8c3566 215 e.timecreated AS entrytimecreated,
216 u.id AS userid,
217 u.firstname AS userfirstname,
218 u.lastname AS userlastname
219 FROM {glossary_entries} e,
220 {user} u
221 WHERE e.glossaryid = :gid AND
222 u.id = e.userid AND
223 e.approved = 1 $newsince
224 ORDER BY e.timecreated desc", $params)) {
44d6ae6d 225
226 //Are we just looking for new ones? If so, then return now.
227 if ($newsince) {
228 return true;
229 }
230
2dda0bbb 231 //Iterate over each entry to get glossary->rssarticles records
232 $articlesleft = $glossary->rssarticles;
f8b92241 233
234 $formatoptions = new object;
cbc2b5df 235 $formatoptions->trusted = $comment->entrytrust;
f8b92241 236
2dda0bbb 237 foreach ($recs as $rec) {
ae8c3566 238 $item = new object();
239 $user = new object();
2dda0bbb 240 $item->title = $rec->entryconcept;
241 $user->firstname = $rec->userfirstname;
242 $user->lastname = $rec->userlastname;
243 //$item->author = fullname($user);
244 $item->pubdate = $rec->entrytimecreated;
8f0cd6ef 245 $item->link = $CFG->wwwroot."/mod/glossary/showentry.php?courseid=".$glossary->course."&eid=".$rec->entryid;
f8b92241 246 $item->description = format_text($rec->entrydefinition,$rec->entryformat,$formatoptions,$glossary->course);
2dda0bbb 247 $items[] = $item;
248 $articlesleft--;
249 if ($articlesleft < 1) {
250 break;
251 }
252 }
253 }
254 return $items;
255 }
8f0cd6ef 256
1adbd2c3 257