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