glossary MDL-23391 glossary rss feed generation tweak
[moodle.git] / mod / glossary / rsslib.php
CommitLineData
1adbd2c3 1<?php
2dda0bbb 2 //This file adds support to rss feeds generation
3
4 //This function is the main entry point to glossary
fcce139a
AD
5 //rss feeds generation.
6 function glossary_rss_get_feed($context, $cm, $instance, $args) {
ae8c3566 7 global $CFG, $DB;
2dda0bbb 8
fcce139a 9 if (empty($CFG->glossary_enablerssfeeds)) {
fdb1cf2c 10 debugging("DISABLED (module configuration)");
fcce139a 11 return null;
2dda0bbb 12 }
2dda0bbb 13
7138d3b3
AD
14 $status = true;
15
690aa229
AD
16 //check capabilities
17 //glossary module doesn't require any capabilities to view glossary entries (aside from being logged in)
18
fcce139a 19 $glossary = $DB->get_record('glossary', array('id' => $instance), '*', MUST_EXIST);
2dda0bbb 20
fcce139a
AD
21 if (!rss_enabled('glossary', $glossary)) {
22 return null;
2dda0bbb 23 }
2dda0bbb 24
fcce139a 25 $sql = glossary_rss_get_sql($glossary, $cm);
2dda0bbb 26
fcce139a
AD
27 //get the cache file info
28 $filename = rss_get_file_name($glossary, $sql);
29 $cachedfilepath = rss_get_file_full_name('glossary', $filename);
ae8c3566 30
fcce139a
AD
31 //Is the cache out of date?
32 $cachedfilelastmodified = 0;
33 if (file_exists($cachedfilepath)) {
34 $cachedfilelastmodified = filemtime($cachedfilepath);
8f0cd6ef 35 }
44d6ae6d 36
fcce139a
AD
37 if (glossary_rss_newstuff($glossary, $cm, $cachedfilelastmodified)) {
38 if (!$recs = $DB->get_records_sql($sql, array(), 0, $glossary->rssarticles)) {
39 return null;
44d6ae6d 40 }
f8b92241 41
fcce139a
AD
42 $items = array();
43
f8b92241 44 $formatoptions = new object;
fcce139a 45 $formatoptions->trusttext = true;
f8b92241 46
2dda0bbb 47 foreach ($recs as $rec) {
ae8c3566 48 $item = new object();
fcce139a 49 $user = new object();
2dda0bbb 50 $item->title = $rec->entryconcept;
fcce139a
AD
51
52 if ($glossary->rsstype == 1) {//With author
53 $user->firstname = $rec->userfirstname;
54 $user->lastname = $rec->userlastname;
55
56 $item->author = fullname($user);
57 }
58
2dda0bbb 59 $item->pubdate = $rec->entrytimecreated;
8f0cd6ef 60 $item->link = $CFG->wwwroot."/mod/glossary/showentry.php?courseid=".$glossary->course."&eid=".$rec->entryid;
f8b92241 61 $item->description = format_text($rec->entrydefinition,$rec->entryformat,$formatoptions,$glossary->course);
2dda0bbb 62 $items[] = $item;
2dda0bbb 63 }
2dda0bbb 64
fcce139a
AD
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;
2dda0bbb 80
fcce139a
AD
81 //Save the XML contents to file.
82 $status = rss_save_file('glossary', $filename, $rss);
83 }
84 }
2dda0bbb 85
7138d3b3
AD
86 if (!$status) {
87 $cachedfilepath = null;
88 }
89
fcce139a
AD
90 return $cachedfilepath;
91 }
ae8c3566 92
fcce139a
AD
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";
44d6ae6d 97 } else {
fcce139a 98 $time = "";
44d6ae6d 99 }
100
fcce139a
AD
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 }
f8b92241 132
fcce139a
AD
133 return $sql;
134 }
f8b92241 135
fcce139a
AD
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;
147
148 $sql = glossary_rss_get_sql($glossary, $cm, $time);
149
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));
2dda0bbb 152 }
8f0cd6ef 153
1adbd2c3 154