522b227a6f338841b95cfc2e2237d8602a3e6bda
[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, $cm, $instance, $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)
19         $glossary = $DB->get_record('glossary', array('id' => $instance), '*', MUST_EXIST);
21         if (!rss_enabled('glossary', $glossary)) {
22             return null;
23         }
25         $sql = glossary_rss_get_sql($glossary, $cm);
27         //get the cache file info
28         $filename = rss_get_file_name($glossary, $sql);
29         $cachedfilepath = rss_get_file_full_name('glossary', $filename);
31         //Is the cache out of date?
32         $cachedfilelastmodified = 0;
33         if (file_exists($cachedfilepath)) {
34             $cachedfilelastmodified = filemtime($cachedfilepath);
35         }
37         if (glossary_rss_newstuff($glossary, $cm, $cachedfilelastmodified)) {
38             if (!$recs = $DB->get_records_sql($sql, array(), 0, $glossary->rssarticles)) {
39                 return null;
40             }
42             $items = array();
43             
44             $formatoptions = new object;
45             $formatoptions->trusttext = true;
47             foreach ($recs as $rec) {
48                 $item = new object();
49                 $user = new object();
50                 $item->title = $rec->entryconcept;
52                 if ($glossary->rsstype == 1) {//With author
53                     $user->firstname = $rec->userfirstname;
54                     $user->lastname = $rec->userlastname;
55                     
56                     $item->author = fullname($user);
57                 }
59                 $item->pubdate = $rec->entrytimecreated;
60                 $item->link = $CFG->wwwroot."/mod/glossary/showentry.php?courseid=".$glossary->course."&eid=".$rec->entryid;
61                 $item->description = format_text($rec->entrydefinition,$rec->entryformat,$formatoptions,$glossary->course);
62                 $items[] = $item;
63             }
65             //First all rss feeds common headers
66             $header = rss_standard_header(format_string($glossary->name,true),
67                                           $CFG->wwwroot."/mod/glossary/view.php?g=".$glossary->id,
68                                           format_string($glossary->intro,true));
69             //Now all the rss items
70             if (!empty($header)) {
71                 $articles = rss_add_items($items);
72             }
73             //Now all rss feeds common footers
74             if (!empty($header) && !empty($articles)) {
75                 $footer = rss_standard_footer();
76             }
77             //Now, if everything is ok, concatenate it
78             if (!empty($header) && !empty($articles) && !empty($footer)) {
79                 $rss = $header.$articles.$footer;
81                 //Save the XML contents to file.
82                 $status = rss_save_file('glossary', $filename, $rss);
83             }
84         }
86         if (!$status) {
87             $cachedfilepath = null;
88         }
90         return $cachedfilepath;
91     }
93     function glossary_rss_get_sql($glossary, $cm, $time=0) {
94         //do we only want new items?
95         if ($time) {
96             $time = "AND e.timecreated > $time";
97         } else {
98             $time = "";
99         }
101         if ($glossary->rsstype == 1) {//With author
102             $sql = "SELECT e.id AS entryid,
103                       e.concept AS entryconcept,
104                       e.definition AS entrydefinition,
105                       e.definitionformat AS entryformat,
106                       e.definitiontrust AS entrytrust,
107                       e.timecreated AS entrytimecreated,
108                       u.id AS userid,
109                       u.firstname AS userfirstname,
110                       u.lastname AS userlastname
111                  FROM {glossary_entries} e,
112                       {user} u
113                 WHERE e.glossaryid = {$glossary->id} AND
114                       u.id = e.userid AND
115                       e.approved = 1 $time
116              ORDER BY e.timecreated desc";
117         } else {//Without author
118             $sql = "SELECT e.id AS entryid,
119                       e.concept AS entryconcept,
120                       e.definition AS entrydefinition,
121                       e.definitionformat AS entryformat,
122                       e.definitiontrust AS entrytrust,
123                       e.timecreated AS entrytimecreated,
124                       u.id AS userid
125                  FROM {glossary_entries} e,
126                       {user} u
127                 WHERE e.glossaryid = {$glossary->id} AND
128                       u.id = e.userid AND
129                       e.approved = 1 $time
130              ORDER BY e.timecreated desc";
131         }
133         return $sql;
134     }
136     /**
137      * If there is new stuff in since $time this returns true
138      * Otherwise it returns false.
139      *
140      * @param object $glossary the glossary activity object
141      * @param object $cm
142      * @param int $time timestamp
143      * @return bool
144      */
145     function glossary_rss_newstuff($glossary, $cm, $time) {
146         global $DB;
148         $sql = glossary_rss_get_sql($glossary, $cm, $time);
150         $recs = $DB->get_records_sql($sql, null, 0, 1);//limit of 1. If we get even 1 back we have new stuff
151         return ($recs && !empty($recs));
152     }