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