rss MDL-23391 refactored rss feeds to make them standard across components
[moodle.git] / mod / glossary / rsslib.php
1 <?php
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) {
7         global $CFG, $DB;
9         if (empty($CFG->glossary_enablerssfeeds)) {
10             debugging("DISABLED (module configuration)");
11             return null;
12         }
14         $status = true;
16         //check capabilities
17         //glossary module doesn't require any capabilities to view glossary entries (aside from being logged in)
18         if (!is_enrolled($context)) {
19             return null;
20         }
22         $glossaryid = $args[3];
23         $glossary = $DB->get_record('glossary', array('id' => $glossaryid), '*', MUST_EXIST);
25         if (!rss_enabled('glossary', $glossary)) {
26             return null;
27         }
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);
39         }
41         if (glossary_rss_newstuff($glossary, $cachedfilelastmodified)) {
42             if (!$recs = $DB->get_records_sql($sql, array(), 0, $glossary->rssarticles)) {
43                 return null;
44             }
46             $items = array();
47             
48             $formatoptions = new object;
49             $formatoptions->trusttext = true;
51             foreach ($recs as $rec) {
52                 $item = new object();
53                 $user = new object();
54                 $item->title = $rec->entryconcept;
56                 if ($glossary->rsstype == 1) {//With author
57                     $user->firstname = $rec->userfirstname;
58                     $user->lastname = $rec->userlastname;
59                     
60                     $item->author = fullname($user);
61                 }
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);
66                 $items[] = $item;
67             }
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);
76             }
77             //Now all rss feeds common footers
78             if (!empty($header) && !empty($articles)) {
79                 $footer = rss_standard_footer();
80             }
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);
87             }
88         }
90         if (!$status) {
91             $cachedfilepath = null;
92         }
94         return $cachedfilepath;
95     }
97     function glossary_rss_get_sql($glossary, $time=0) {
98         //do we only want new items?
99         if ($time) {
100             $time = "AND e.timecreated > $time";
101         } else {
102             $time = "";
103         }
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,
112                       u.id AS userid,
113                       u.firstname AS userfirstname,
114                       u.lastname AS userlastname
115                  FROM {glossary_entries} e,
116                       {user} u
117                 WHERE e.glossaryid = {$glossary->id} AND
118                       u.id = e.userid AND
119                       e.approved = 1 $time
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,
128                       u.id AS userid
129                  FROM {glossary_entries} e,
130                       {user} u
131                 WHERE e.glossaryid = {$glossary->id} AND
132                       u.id = e.userid AND
133                       e.approved = 1 $time
134              ORDER BY e.timecreated desc";
135         }
137         return $sql;
138     }
140     /**
141      * If there is new stuff in since $time this returns true
142      * Otherwise it returns false.
143      *
144      * @param object $glossary the glossary activity object
145      * @param int $time timestamp
146      * @return bool
147      */
148     function glossary_rss_newstuff($glossary, $time) {
149         global $DB;
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));
155     }