3 require_once($CFG->dirroot.'/lib/rsslib.php');
4 require_once($CFG->dirroot .'/blog/lib.php');
6 // This function returns the icon (from theme) with the link to rss/file.php
7 // needs some hacking to rss/file.php
8 function blog_rss_print_link($filtertype, $filterselect, $tagid=0, $tooltiptext='') {
10 global $CFG, $USER, $OUTPUT;
13 $userid = $CFG->siteguest;
18 switch ($filtertype) {
20 $path = SITEID.'/'.$userid.'/blog/site/'.SITEID;
23 $path = $filterselect.'/'.$userid.'/blog/course/'.$filterselect;
26 $path = SITEID.'/'.$userid.'/blog/group/'.$filterselect;
29 $path = SITEID.'/'.$userid.'/blog/user/'.$filterselect;
38 $rsspix = $OUTPUT->pix_url('i/rss');
40 require_once($CFG->libdir.'/filelib.php');
41 $path = get_file_url($path, null, 'rssfile');
42 print '<div class="mdl-right"><a href="'. $path .'"><img src="'. $rsspix .'" title="'. strip_tags($tooltiptext) .'" alt="'.get_string('rss').'" /></a></div>';
47 // Generate any blog RSS feed via one function (called by ../rss/file.php)
48 function blog_rss_get_feed($context, $args) {
49 global $CFG, $SITE, $DB;
51 if (empty($CFG->enablerssfeeds)) {
52 debugging('Sorry, RSS feeds are disabled on this site');
56 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
57 if (!has_capability('moodle/blog:view', $sitecontext)) {
62 $id = (int) $args[4]; // could be groupid / courseid / userid depending on $instance
65 if ($args[5] != 'rss.xml') {
66 $tagid = (int) $args[5];
71 $filename = blog_rss_file_name($type, $id, $tagid);
73 if (file_exists($filename)) {
74 if (filemtime($filename) + 3600 > time()) {
75 return $filename; /// It's already done so we return cached version
79 /// Get all the entries from the database
81 $blogentries = blog_fetch_entries('', 20, '', $type, $id, $tagid);
83 /// Now generate an array of RSS items
86 foreach ($blogentries as $blog_entry) {
88 $item->author = fullname($DB->get_record('user', array('id'=>$blog_entry->userid))); // TODO: this is slow
89 $item->title = $blog_entry->subject;
90 $item->pubdate = $blog_entry->lastmodified;
91 $item->link = $CFG->wwwroot.'/blog/index.php?entryid='.$blog_entry->id;
92 $item->description = format_text($blog_entry->summary, $blog_entry->format);
93 if ( !empty($CFG->usetags) && ($blogtags = tag_get_tags_array('post', $blog_entry->id)) ) {
95 $item->tags = $blogtags;
97 $item->tagscheme = $CFG->wwwroot . '/tag';
101 $articles = rss_add_items($items); /// Change structure to XML
106 /// Get header and footer information
110 $info = fullname($DB->get_record('user', array('id'=>$id), 'firstname,lastname'));
113 $info = $DB->get_field('course', 'fullname', array('id'=>$id));
116 $info = $SITE->fullname;
119 $group = groups_get_group($id, false);
120 $info = $group->name; //TODO: $DB->get_field('groups', 'name', array('id'=>$id))
128 $info .= ': '.$DB->get_field('tags', 'text', array('id'=>$tagid));
131 $header = rss_standard_header(get_string($type.'blog','blog', $info),
132 $CFG->wwwroot.'/blog/index.php',
133 get_string('intro','blog'));
135 $footer = rss_standard_footer();
138 /// Save the XML contents to file.
140 $rssdata = $header.$articles.$footer;
142 if (blog_rss_save_file($type,$id,$tagid,$rssdata)) {
145 return false; // Couldn't find it or make it
150 function blog_rss_file_name($type, $id, $tagid=0) {
154 return "$CFG->dataroot/cache/rss/blog/$type/$id/$tagid.xml";
156 return "$CFG->dataroot/cache/rss/blog/$type/$id.xml";
160 //This function saves to file the rss feed specified in the parameters
161 function blog_rss_save_file($type, $id, $tagid=0, $contents='') {
164 if (! $basedir = make_upload_directory("rss/blog/$type/$id")) {
168 $file = blog_rss_file_name($type, $id, $tagid);
169 $rss_file = fopen($file, 'w');
171 $status = fwrite ($rss_file, $contents);