fixed file component regression
[moodle.git] / blog / rsslib.php
CommitLineData
516194d0 1<?php
2
1c7b8b93
NC
3require_once($CFG->dirroot.'/lib/rsslib.php');
4require_once($CFG->dirroot .'/blog/lib.php');
516194d0 5
1c7b8b93
NC
6// This function returns the icon (from theme) with the link to rss/file.php
7// needs some hacking to rss/file.php
8function blog_rss_print_link($filtertype, $filterselect, $tagid=0, $tooltiptext='') {
516194d0 9
1c7b8b93 10 global $CFG, $USER, $OUTPUT;
516194d0 11
4f0c2d00
PS
12 if (!isloggedin()) {
13 $userid = $CFG->siteguest;
1c7b8b93
NC
14 } else {
15 $userid = $USER->id;
16 }
6619a7f4 17
1c7b8b93
NC
18 switch ($filtertype) {
19 case 'site':
20 $path = SITEID.'/'.$userid.'/blog/site/'.SITEID;
21 break;
22 case 'course':
23 $path = $filterselect.'/'.$userid.'/blog/course/'.$filterselect;
24 break;
25 case 'group':
26 $path = SITEID.'/'.$userid.'/blog/group/'.$filterselect;
27 break;
28 case 'user':
29 $path = SITEID.'/'.$userid.'/blog/user/'.$filterselect;
30 break;
31 }
6619a7f4 32
1c7b8b93
NC
33 if ($tagid) {
34 $path .= '/'.$tagid;
35 }
6619a7f4 36
1c7b8b93 37 $path .= '/rss.xml';
b5d0cafc 38 $rsspix = $OUTPUT->pix_url('i/rss');
516194d0 39
1c7b8b93
NC
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>';
516194d0 43
1c7b8b93 44}
516194d0 45
516194d0 46
1c7b8b93 47// Generate any blog RSS feed via one function (called by ../rss/file.php)
690aa229 48function blog_rss_get_feed($context, $cm, $instance, $args) {
1c7b8b93 49 global $CFG, $SITE, $DB;
ebb73f49 50
690aa229
AD
51 $type = $instance;
52
53 $id = (int) $args[4]; // could be groupid / courseid / userid depending on $instance
54
55 $tagid=0;
56 if ($args[5] != 'rss.xml') {
57 $tagid = (int) $args[5];
58 } else {
59 $tagid = 0;
60 }
61
1c7b8b93
NC
62 if (empty($CFG->enablerssfeeds)) {
63 debugging('Sorry, RSS feeds are disabled on this site');
64 return '';
65 }
6619a7f4 66
1c7b8b93 67 $filename = blog_rss_file_name($type, $id, $tagid);
6619a7f4 68
1c7b8b93
NC
69 if (file_exists($filename)) {
70 if (filemtime($filename) + 3600 > time()) {
71 return $filename; /// It's already done so we return cached version
6619a7f4 72 }
1c7b8b93 73 }
6619a7f4 74
1c7b8b93
NC
75/// Get all the entries from the database
76
77 $blogentries = blog_fetch_entries('', 20, '', $type, $id, $tagid);
78
79/// Now generate an array of RSS items
80 if ($blogentries) {
81 $items = array();
82 foreach ($blogentries as $blog_entry) {
83 $item = NULL;
84 $item->author = fullname($DB->get_record('user', array('id'=>$blog_entry->userid))); // TODO: this is slow
85 $item->title = $blog_entry->subject;
86 $item->pubdate = $blog_entry->lastmodified;
87 $item->link = $CFG->wwwroot.'/blog/index.php?entryid='.$blog_entry->id;
88 $item->description = format_text($blog_entry->summary, $blog_entry->format);
89 if ( !empty($CFG->usetags) && ($blogtags = tag_get_tags_array('post', $blog_entry->id)) ) {
90 if ($blogtags) {
91 $item->tags = $blogtags;
08577617 92 }
1c7b8b93 93 $item->tagscheme = $CFG->wwwroot . '/tag';
ebb73f49 94 }
1c7b8b93 95 $items[] = $item;
516194d0 96 }
1c7b8b93
NC
97 $articles = rss_add_items($items); /// Change structure to XML
98 } else {
99 $articles = '';
100 }
516194d0 101
1c7b8b93 102/// Get header and footer information
dba2764d 103
1c7b8b93
NC
104 switch ($type) {
105 case 'user':
106 $info = fullname($DB->get_record('user', array('id'=>$id), 'firstname,lastname'));
107 break;
108 case 'course':
109 $info = $DB->get_field('course', 'fullname', array('id'=>$id));
110 break;
111 case 'site':
112 $info = $SITE->fullname;
113 break;
114 case 'group':
115 $group = groups_get_group($id, false);
116 $info = $group->name; //TODO: $DB->get_field('groups', 'name', array('id'=>$id))
117 break;
118 default:
119 $info = '';
120 break;
121 }
516194d0 122
1c7b8b93
NC
123 if ($tagid) {
124 $info .= ': '.$DB->get_field('tags', 'text', array('id'=>$tagid));
125 }
cae83708 126
1c7b8b93
NC
127 $header = rss_standard_header(get_string($type.'blog','blog', $info),
128 $CFG->wwwroot.'/blog/index.php',
129 get_string('intro','blog'));
516194d0 130
1c7b8b93 131 $footer = rss_standard_footer();
516194d0 132
133
1c7b8b93 134/// Save the XML contents to file.
516194d0 135
1c7b8b93
NC
136 $rssdata = $header.$articles.$footer;
137
138 if (blog_rss_save_file($type,$id,$tagid,$rssdata)) {
139 return $filename;
140 } else {
141 return false; // Couldn't find it or make it
516194d0 142 }
1c7b8b93 143}
516194d0 144
516194d0 145
1c7b8b93
NC
146function blog_rss_file_name($type, $id, $tagid=0) {
147 global $CFG;
516194d0 148
1c7b8b93 149 if ($tagid) {
d5f25d95 150 return "$CFG->dataroot/cache/rss/blog/$type/$id/$tagid.xml";
1c7b8b93 151 } else {
d5f25d95 152 return "$CFG->dataroot/cache/rss/blog/$type/$id.xml";
516194d0 153 }
1c7b8b93 154}
cae83708 155
1c7b8b93
NC
156//This function saves to file the rss feed specified in the parameters
157function blog_rss_save_file($type, $id, $tagid=0, $contents='') {
158 global $CFG;
516194d0 159
1c7b8b93
NC
160 if (! $basedir = make_upload_directory("rss/blog/$type/$id")) {
161 return false;
162 }
516194d0 163
1c7b8b93
NC
164 $file = blog_rss_file_name($type, $id, $tagid);
165 $rss_file = fopen($file, 'w');
166 if ($rss_file) {
167 $status = fwrite ($rss_file, $contents);
168 fclose($rss_file);
169 } else {
170 $status = false;
516194d0 171 }
ebb73f49 172
1c7b8b93
NC
173 return $status;
174}
175