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