webservice MDL-23536 fix missing test when no missing capability
[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)
274f9840 48function blog_rss_get_feed($context, $args) {
1c7b8b93 49 global $CFG, $SITE, $DB;
ebb73f49 50
274f9840
AD
51 if (empty($CFG->enablerssfeeds)) {
52 debugging('Sorry, RSS feeds are disabled on this site');
53 return '';
54 }
55
56 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
57 if (!has_capability('moodle/blog:view', $sitecontext)) {
58 return null;
59 }
690aa229 60
274f9840 61 $type = $args[3];
690aa229
AD
62 $id = (int) $args[4]; // could be groupid / courseid / userid depending on $instance
63
64 $tagid=0;
65 if ($args[5] != 'rss.xml') {
66 $tagid = (int) $args[5];
67 } else {
68 $tagid = 0;
69 }
70
1c7b8b93 71 $filename = blog_rss_file_name($type, $id, $tagid);
6619a7f4 72
1c7b8b93
NC
73 if (file_exists($filename)) {
74 if (filemtime($filename) + 3600 > time()) {
75 return $filename; /// It's already done so we return cached version
6619a7f4 76 }
1c7b8b93 77 }
6619a7f4 78
1c7b8b93
NC
79/// Get all the entries from the database
80
81 $blogentries = blog_fetch_entries('', 20, '', $type, $id, $tagid);
82
83/// Now generate an array of RSS items
84 if ($blogentries) {
85 $items = array();
86 foreach ($blogentries as $blog_entry) {
87 $item = NULL;
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)) ) {
94 if ($blogtags) {
95 $item->tags = $blogtags;
08577617 96 }
1c7b8b93 97 $item->tagscheme = $CFG->wwwroot . '/tag';
ebb73f49 98 }
1c7b8b93 99 $items[] = $item;
516194d0 100 }
1c7b8b93
NC
101 $articles = rss_add_items($items); /// Change structure to XML
102 } else {
103 $articles = '';
104 }
516194d0 105
1c7b8b93 106/// Get header and footer information
dba2764d 107
1c7b8b93
NC
108 switch ($type) {
109 case 'user':
110 $info = fullname($DB->get_record('user', array('id'=>$id), 'firstname,lastname'));
111 break;
112 case 'course':
113 $info = $DB->get_field('course', 'fullname', array('id'=>$id));
114 break;
115 case 'site':
116 $info = $SITE->fullname;
117 break;
118 case 'group':
119 $group = groups_get_group($id, false);
120 $info = $group->name; //TODO: $DB->get_field('groups', 'name', array('id'=>$id))
121 break;
122 default:
123 $info = '';
124 break;
125 }
516194d0 126
1c7b8b93
NC
127 if ($tagid) {
128 $info .= ': '.$DB->get_field('tags', 'text', array('id'=>$tagid));
129 }
cae83708 130
1c7b8b93
NC
131 $header = rss_standard_header(get_string($type.'blog','blog', $info),
132 $CFG->wwwroot.'/blog/index.php',
133 get_string('intro','blog'));
516194d0 134
1c7b8b93 135 $footer = rss_standard_footer();
516194d0 136
137
1c7b8b93 138/// Save the XML contents to file.
516194d0 139
1c7b8b93
NC
140 $rssdata = $header.$articles.$footer;
141
142 if (blog_rss_save_file($type,$id,$tagid,$rssdata)) {
143 return $filename;
144 } else {
145 return false; // Couldn't find it or make it
516194d0 146 }
1c7b8b93 147}
516194d0 148
516194d0 149
1c7b8b93
NC
150function blog_rss_file_name($type, $id, $tagid=0) {
151 global $CFG;
516194d0 152
1c7b8b93 153 if ($tagid) {
d5f25d95 154 return "$CFG->dataroot/cache/rss/blog/$type/$id/$tagid.xml";
1c7b8b93 155 } else {
d5f25d95 156 return "$CFG->dataroot/cache/rss/blog/$type/$id.xml";
516194d0 157 }
1c7b8b93 158}
cae83708 159
1c7b8b93
NC
160//This function saves to file the rss feed specified in the parameters
161function blog_rss_save_file($type, $id, $tagid=0, $contents='') {
162 global $CFG;
516194d0 163
1c7b8b93
NC
164 if (! $basedir = make_upload_directory("rss/blog/$type/$id")) {
165 return false;
166 }
516194d0 167
1c7b8b93
NC
168 $file = blog_rss_file_name($type, $id, $tagid);
169 $rss_file = fopen($file, 'w');
170 if ($rss_file) {
171 $status = fwrite ($rss_file, $contents);
172 fclose($rss_file);
173 } else {
174 $status = false;
516194d0 175 }
ebb73f49 176
1c7b8b93
NC
177 return $status;
178}
179