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