Now glossaries can produce RSS feeds too !!
[moodle.git] / mod / glossary / rsslib.php
CommitLineData
2dda0bbb 1<?PHP // $Id$
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) {
31 //Some debug...
32 if ($CFG->debug > 7) {
33 echo "ID: $glossary->id->";
34 }
35 //Get the XML contents
36 $result = glossary_rss_feed($glossary);
37 //Save the XML contents to file
38 if (!empty($result)) {
39 $status = rss_save_file("glossary",$glossary,$result);
40 }
41 //Some debug...
42 if ($CFG->debug > 7) {
43 if (empty($result)) {
44 echo "(empty) ";
45 } else {
46 if (!empty($status)) {
47 echo "OK ";
48 } else {
49 echo "FAIL ";
50 }
51 }
52 }
53 }
54 }
55 }
56 }
57 return $status;
58 }
59
60 //This function return the XML rss contents about the glossary record passed as parameter
61 //It returns false if something is wrong
62 function glossary_rss_feed($glossary) {
63
64 global $CFG;
65
66 $status = true;
67
68 //Check CFG->enablerssfeeds
69 if (empty($CFG->enablerssfeeds)) {
70 //Some debug...
71 if ($CFG->debug > 7) {
72 echo "DISABLED (admin variables)";
73 }
74 //Check CFG->glossary_enablerssfeeds
75 } else if (empty($CFG->glossary_enablerssfeeds)) {
76 //Some debug...
77 if ($CFG->debug > 7) {
78 echo "DISABLED (module configuration)";
79 }
80 //It's working so we start...
81 } else {
82 //Check the glossary has rss activated
83 if (!empty($glossary->rsstype) && !empty($glossary->rssarticles)) {
84 //Depending of the glossary->rsstype, we are going to execute, different sqls
85 if ($glossary->rsstype == 1) { //With author RSS
86 $items = glossary_rss_feed_withauthor($glossary);
87 } else { //Without author RSS
88 $items = glossary_rss_feed_withoutauthor($glossary);
89
90 }
91 //Now, if items, we begin building the structure
92 if (!empty($items)) {
93 //First all rss feeds common headers
94 $header = rss_standard_header($glossary->name,
95 $CFG->wwwroot."/mod/glossary/view.php?f=".$glossary->id,
96 $glossary->intro);
97 //Now all the rss items
98 if (!empty($header)) {
99 $articles = rss_add_items($items);
100 }
101 //Now all rss feeds common footers
102 if (!empty($header) && !empty($articles)) {
103 $footer = rss_standard_footer();
104 }
105 //Now, if everything is ok, concatenate it
106 if (!empty($header) && !empty($articles) && !empty($footer)) {
107 $status = $header.$articles.$footer;
108 } else {
109 $status = false;
110 }
111 } else {
112 $status = false;
113 }
114 }
115 }
116 return $status;
117 }
118
119 //This function returns "items" record array to be used to build the rss feed
120 //for a Type=with author glossary
121 function glossary_rss_feed_withauthor($glossary) {
122
123 global $CFG;
124
125 $items = array();
126
127 if ($recs = get_records_sql ("SELECT e.id entryid,
128 e.concept entryconcept,
129 e.definition entrydefinition,
130 e.format entryformat,
131 e.timecreated entrytimecreated,
132 u.id userid,
133 u.firstname userfirstname,
134 u.lastname userlastname
135 FROM {$CFG->prefix}glossary_entries e,
136 {$CFG->prefix}user u
137 WHERE e.glossaryid = '$glossary->id' AND
138 u.id = e.userid
139 ORDER BY e.timecreated desc")) {
140 //Iterate over each entry to get glossary->rssarticles records
141 $articlesleft = $glossary->rssarticles;
142 $item = NULL;
143 $user = NULL;
144 foreach ($recs as $rec) {
145 unset($item);
146 unset($user);
147 $item->title = $rec->entryconcept;
148 $user->firstname = $rec->userfirstname;
149 $user->lastname = $rec->userlastname;
150 $item->author = fullname($user);
151 $item->pubdate = $rec->entrytimecreated;
152 $item->link = $CFG->wwwroot."/mod/glossary/showentry.php?courseid=".$glossary->course."&eid=".$rec->entryid;
153 $item->description = format_text($rec->entrydefinition,$rec->entryformat,NULL,$glossary->course);
154 $items[] = $item;
155 $articlesleft--;
156 if ($articlesleft < 1) {
157 break;
158 }
159 }
160 }
161 return $items;
162 }
163
164 //This function returns "items" record array to be used to build the rss feed
165 //for a Type=without author glossary
166 function glossary_rss_feed_withoutauthor($glossary) {
167
168 global $CFG;
169
170 $items = array();
171
172 if ($recs = get_records_sql ("SELECT e.id entryid,
173 e.concept entryconcept,
174 e.definition entrydefinition,
175 e.format entryformat,
176 e.timecreated entrytimecreated,
177 u.id userid,
178 u.firstname userfirstname,
179 u.lastname userlastname
180 FROM {$CFG->prefix}glossary_entries e,
181 {$CFG->prefix}user u
182 WHERE e.glossaryid = '$glossary->id' AND
183 u.id = e.userid
184 ORDER BY e.timecreated desc")) {
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;
197 $item->link = $CFG->wwwroot."/mod/glossary/showentry.php?courseid=".$glossary->course."&eid=".$rec->entryid;
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?>