enrol MDL-23384 fixed issue with cohort sync JavaScript not redirecting after success...
[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
690aa229
AD
14 //check capabilities
15 //glossary module doesn't require any capabilities to view glossary entries (aside from being logged in)
16
fcce139a 17 $glossary = $DB->get_record('glossary', array('id' => $instance), '*', MUST_EXIST);
2dda0bbb 18
fcce139a
AD
19 if (!rss_enabled('glossary', $glossary)) {
20 return null;
2dda0bbb 21 }
2dda0bbb 22
fcce139a 23 $sql = glossary_rss_get_sql($glossary, $cm);
2dda0bbb 24
fcce139a
AD
25 //get the cache file info
26 $filename = rss_get_file_name($glossary, $sql);
27 $cachedfilepath = rss_get_file_full_name('glossary', $filename);
ae8c3566 28
fcce139a
AD
29 //Is the cache out of date?
30 $cachedfilelastmodified = 0;
31 if (file_exists($cachedfilepath)) {
32 $cachedfilelastmodified = filemtime($cachedfilepath);
8f0cd6ef 33 }
44d6ae6d 34
fcce139a
AD
35 if (glossary_rss_newstuff($glossary, $cm, $cachedfilelastmodified)) {
36 if (!$recs = $DB->get_records_sql($sql, array(), 0, $glossary->rssarticles)) {
37 return null;
44d6ae6d 38 }
f8b92241 39
fcce139a
AD
40 $items = array();
41
f8b92241 42 $formatoptions = new object;
fcce139a 43 $formatoptions->trusttext = true;
f8b92241 44
2dda0bbb 45 foreach ($recs as $rec) {
ae8c3566 46 $item = new object();
fcce139a 47 $user = new object();
2dda0bbb 48 $item->title = $rec->entryconcept;
fcce139a
AD
49
50 if ($glossary->rsstype == 1) {//With author
51 $user->firstname = $rec->userfirstname;
52 $user->lastname = $rec->userlastname;
53
54 $item->author = fullname($user);
55 }
56
2dda0bbb 57 $item->pubdate = $rec->entrytimecreated;
8f0cd6ef 58 $item->link = $CFG->wwwroot."/mod/glossary/showentry.php?courseid=".$glossary->course."&eid=".$rec->entryid;
f8b92241 59 $item->description = format_text($rec->entrydefinition,$rec->entryformat,$formatoptions,$glossary->course);
2dda0bbb 60 $items[] = $item;
2dda0bbb 61 }
2dda0bbb 62
fcce139a
AD
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;
2dda0bbb 78
fcce139a
AD
79 //Save the XML contents to file.
80 $status = rss_save_file('glossary', $filename, $rss);
81 }
82 }
2dda0bbb 83
fcce139a
AD
84 return $cachedfilepath;
85 }
ae8c3566 86
fcce139a
AD
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";
44d6ae6d 91 } else {
fcce139a 92 $time = "";
44d6ae6d 93 }
94
fcce139a
AD
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 }
f8b92241 126
fcce139a
AD
127 return $sql;
128 }
f8b92241 129
fcce139a
AD
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;
141
142 $sql = glossary_rss_get_sql($glossary, $cm, $time);
143
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));
2dda0bbb 146 }
8f0cd6ef 147
1adbd2c3 148