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