2 //This file adds support to rss feeds generation
4 //This function is the main entry point to glossary
5 //rss feeds generation.
6 function glossary_rss_get_feed($context, $args) {
9 if (empty($CFG->glossary_enablerssfeeds)) {
10 debugging("DISABLED (module configuration)");
17 //glossary module doesn't require any capabilities to view glossary entries (aside from being logged in)
18 if (!is_enrolled($context)) {
22 $glossaryid = $args[3];
23 $glossary = $DB->get_record('glossary', array('id' => $glossaryid), '*', MUST_EXIST);
25 if (!rss_enabled('glossary', $glossary)) {
29 $sql = glossary_rss_get_sql($glossary);
31 //get the cache file info
32 $filename = rss_get_file_name($glossary, $sql);
33 $cachedfilepath = rss_get_file_full_name('glossary', $filename);
35 //Is the cache out of date?
36 $cachedfilelastmodified = 0;
37 if (file_exists($cachedfilepath)) {
38 $cachedfilelastmodified = filemtime($cachedfilepath);
41 if (glossary_rss_newstuff($glossary, $cachedfilelastmodified)) {
42 if (!$recs = $DB->get_records_sql($sql, array(), 0, $glossary->rssarticles)) {
48 $formatoptions = new object;
49 $formatoptions->trusttext = true;
51 foreach ($recs as $rec) {
54 $item->title = $rec->entryconcept;
56 if ($glossary->rsstype == 1) {//With author
57 $user->firstname = $rec->userfirstname;
58 $user->lastname = $rec->userlastname;
60 $item->author = fullname($user);
63 $item->pubdate = $rec->entrytimecreated;
64 $item->link = $CFG->wwwroot."/mod/glossary/showentry.php?courseid=".$glossary->course."&eid=".$rec->entryid;
65 $item->description = format_text($rec->entrydefinition,$rec->entryformat,$formatoptions,$glossary->course);
69 //First all rss feeds common headers
70 $header = rss_standard_header(format_string($glossary->name,true),
71 $CFG->wwwroot."/mod/glossary/view.php?g=".$glossary->id,
72 format_string($glossary->intro,true));
73 //Now all the rss items
74 if (!empty($header)) {
75 $articles = rss_add_items($items);
77 //Now all rss feeds common footers
78 if (!empty($header) && !empty($articles)) {
79 $footer = rss_standard_footer();
81 //Now, if everything is ok, concatenate it
82 if (!empty($header) && !empty($articles) && !empty($footer)) {
83 $rss = $header.$articles.$footer;
85 //Save the XML contents to file.
86 $status = rss_save_file('glossary', $filename, $rss);
91 $cachedfilepath = null;
94 return $cachedfilepath;
97 function glossary_rss_get_sql($glossary, $time=0) {
98 //do we only want new items?
100 $time = "AND e.timecreated > $time";
105 if ($glossary->rsstype == 1) {//With author
106 $sql = "SELECT e.id AS entryid,
107 e.concept AS entryconcept,
108 e.definition AS entrydefinition,
109 e.definitionformat AS entryformat,
110 e.definitiontrust AS entrytrust,
111 e.timecreated AS entrytimecreated,
113 u.firstname AS userfirstname,
114 u.lastname AS userlastname
115 FROM {glossary_entries} e,
117 WHERE e.glossaryid = {$glossary->id} AND
120 ORDER BY e.timecreated desc";
121 } else {//Without author
122 $sql = "SELECT e.id AS entryid,
123 e.concept AS entryconcept,
124 e.definition AS entrydefinition,
125 e.definitionformat AS entryformat,
126 e.definitiontrust AS entrytrust,
127 e.timecreated AS entrytimecreated,
129 FROM {glossary_entries} e,
131 WHERE e.glossaryid = {$glossary->id} AND
134 ORDER BY e.timecreated desc";
141 * If there is new stuff in since $time this returns true
142 * Otherwise it returns false.
144 * @param object $glossary the glossary activity object
145 * @param int $time timestamp
148 function glossary_rss_newstuff($glossary, $time) {
151 $sql = glossary_rss_get_sql($glossary, $time);
153 $recs = $DB->get_records_sql($sql, null, 0, 1);//limit of 1. If we get even 1 back we have new stuff
154 return ($recs && !empty($recs));