MDL-30376 - Glossary - Fixing ability to see the RSS feed for a glossary
[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 5 //rss feeds generation.
274f9840 6 function glossary_rss_get_feed($context, $args) {
1a9528e0 7 global $CFG, $DB, $COURSE, $USER;
2dda0bbb 8
3ad3f248 9 $status = true;
10
fcce139a 11 if (empty($CFG->glossary_enablerssfeeds)) {
fdb1cf2c 12 debugging("DISABLED (module configuration)");
fcce139a 13 return null;
2dda0bbb 14 }
2dda0bbb 15
3ad3f248 16 $glossaryid = clean_param($args[3], PARAM_INT);
17 $cm = get_coursemodule_from_instance('glossary', $glossaryid, 0, false, MUST_EXIST);
18 if ($cm) {
19 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
7138d3b3 20
3ad3f248 21 //context id from db should match the submitted one
22 //no specific capability required to view glossary entries so just check user is enrolled
1a9528e0 23 if ($context->id != $modcontext->id || !can_access_course($COURSE, $USER)) {
3ad3f248 24 return null;
25 }
274f9840 26 }
690aa229 27
274f9840 28 $glossary = $DB->get_record('glossary', array('id' => $glossaryid), '*', MUST_EXIST);
43b92251 29 if (!rss_enabled_for_mod('glossary', $glossary)) {
fcce139a 30 return null;
2dda0bbb 31 }
2dda0bbb 32
274f9840 33 $sql = glossary_rss_get_sql($glossary);
2dda0bbb 34
fcce139a
AD
35 //get the cache file info
36 $filename = rss_get_file_name($glossary, $sql);
43b92251 37 $cachedfilepath = rss_get_file_full_name('mod_glossary', $filename);
ae8c3566 38
fcce139a
AD
39 //Is the cache out of date?
40 $cachedfilelastmodified = 0;
41 if (file_exists($cachedfilepath)) {
42 $cachedfilelastmodified = filemtime($cachedfilepath);
8f0cd6ef 43 }
c02f338e
AD
44 //if the cache is more than 60 seconds old and there's new stuff
45 $dontrecheckcutoff = time()-60;
46 if ( $dontrecheckcutoff > $cachedfilelastmodified && glossary_rss_newstuff($glossary, $cachedfilelastmodified)) {
fcce139a
AD
47 if (!$recs = $DB->get_records_sql($sql, array(), 0, $glossary->rssarticles)) {
48 return null;
44d6ae6d 49 }
f8b92241 50
fcce139a 51 $items = array();
39790bd8
PS
52
53 $formatoptions = new stdClass();
fcce139a 54 $formatoptions->trusttext = true;
f8b92241 55
2dda0bbb 56 foreach ($recs as $rec) {
39790bd8
PS
57 $item = new stdClass();
58 $user = new stdClass();
2dda0bbb 59 $item->title = $rec->entryconcept;
fcce139a
AD
60
61 if ($glossary->rsstype == 1) {//With author
62 $user->firstname = $rec->userfirstname;
63 $user->lastname = $rec->userlastname;
39790bd8 64
fcce139a
AD
65 $item->author = fullname($user);
66 }
67
2dda0bbb 68 $item->pubdate = $rec->entrytimecreated;
8f0cd6ef 69 $item->link = $CFG->wwwroot."/mod/glossary/showentry.php?courseid=".$glossary->course."&eid=".$rec->entryid;
f8b92241 70 $item->description = format_text($rec->entrydefinition,$rec->entryformat,$formatoptions,$glossary->course);
2dda0bbb 71 $items[] = $item;
2dda0bbb 72 }
2dda0bbb 73
fcce139a
AD
74 //First all rss feeds common headers
75 $header = rss_standard_header(format_string($glossary->name,true),
76 $CFG->wwwroot."/mod/glossary/view.php?g=".$glossary->id,
77 format_string($glossary->intro,true));
78 //Now all the rss items
79 if (!empty($header)) {
80 $articles = rss_add_items($items);
81 }
82 //Now all rss feeds common footers
83 if (!empty($header) && !empty($articles)) {
84 $footer = rss_standard_footer();
85 }
86 //Now, if everything is ok, concatenate it
87 if (!empty($header) && !empty($articles) && !empty($footer)) {
88 $rss = $header.$articles.$footer;
2dda0bbb 89
fcce139a 90 //Save the XML contents to file.
43b92251 91 $status = rss_save_file('mod_glossary', $filename, $rss);
fcce139a
AD
92 }
93 }
2dda0bbb 94
7138d3b3
AD
95 if (!$status) {
96 $cachedfilepath = null;
97 }
98
fcce139a
AD
99 return $cachedfilepath;
100 }
ae8c3566 101
274f9840 102 function glossary_rss_get_sql($glossary, $time=0) {
fcce139a
AD
103 //do we only want new items?
104 if ($time) {
105 $time = "AND e.timecreated > $time";
44d6ae6d 106 } else {
fcce139a 107 $time = "";
44d6ae6d 108 }
109
fcce139a
AD
110 if ($glossary->rsstype == 1) {//With author
111 $sql = "SELECT e.id AS entryid,
112 e.concept AS entryconcept,
113 e.definition AS entrydefinition,
114 e.definitionformat AS entryformat,
115 e.definitiontrust AS entrytrust,
116 e.timecreated AS entrytimecreated,
117 u.id AS userid,
118 u.firstname AS userfirstname,
119 u.lastname AS userlastname
120 FROM {glossary_entries} e,
121 {user} u
122 WHERE e.glossaryid = {$glossary->id} AND
123 u.id = e.userid AND
124 e.approved = 1 $time
125 ORDER BY e.timecreated desc";
126 } else {//Without author
127 $sql = "SELECT e.id AS entryid,
128 e.concept AS entryconcept,
129 e.definition AS entrydefinition,
130 e.definitionformat AS entryformat,
131 e.definitiontrust AS entrytrust,
132 e.timecreated AS entrytimecreated,
133 u.id AS userid
134 FROM {glossary_entries} e,
135 {user} u
136 WHERE e.glossaryid = {$glossary->id} AND
137 u.id = e.userid AND
138 e.approved = 1 $time
139 ORDER BY e.timecreated desc";
140 }
f8b92241 141
fcce139a
AD
142 return $sql;
143 }
f8b92241 144
fcce139a
AD
145 /**
146 * If there is new stuff in since $time this returns true
147 * Otherwise it returns false.
148 *
149 * @param object $glossary the glossary activity object
fcce139a
AD
150 * @param int $time timestamp
151 * @return bool
152 */
274f9840 153 function glossary_rss_newstuff($glossary, $time) {
fcce139a
AD
154 global $DB;
155
274f9840 156 $sql = glossary_rss_get_sql($glossary, $time);
fcce139a
AD
157
158 $recs = $DB->get_records_sql($sql, null, 0, 1);//limit of 1. If we get even 1 back we have new stuff
159 return ($recs && !empty($recs));
2dda0bbb 160 }
8f0cd6ef 161
1adbd2c3 162