Assignment MDL-22843
[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
NC
47// Generate any blog RSS feed via one function (called by ../rss/file.php)
48function blog_generate_rss_feed($type, $id, $tagid=0) {
49 global $CFG, $SITE, $DB;
ebb73f49 50
1c7b8b93
NC
51 if (empty($CFG->enablerssfeeds)) {
52 debugging('Sorry, RSS feeds are disabled on this site');
53 return '';
54 }
6619a7f4 55
1c7b8b93 56 $filename = blog_rss_file_name($type, $id, $tagid);
6619a7f4 57
1c7b8b93
NC
58 if (file_exists($filename)) {
59 if (filemtime($filename) + 3600 > time()) {
60 return $filename; /// It's already done so we return cached version
6619a7f4 61 }
1c7b8b93 62 }
6619a7f4 63
1c7b8b93
NC
64/// Get all the entries from the database
65
66 $blogentries = blog_fetch_entries('', 20, '', $type, $id, $tagid);
67
68/// Now generate an array of RSS items
69 if ($blogentries) {
70 $items = array();
71 foreach ($blogentries as $blog_entry) {
72 $item = NULL;
73 $item->author = fullname($DB->get_record('user', array('id'=>$blog_entry->userid))); // TODO: this is slow
74 $item->title = $blog_entry->subject;
75 $item->pubdate = $blog_entry->lastmodified;
76 $item->link = $CFG->wwwroot.'/blog/index.php?entryid='.$blog_entry->id;
77 $item->description = format_text($blog_entry->summary, $blog_entry->format);
78 if ( !empty($CFG->usetags) && ($blogtags = tag_get_tags_array('post', $blog_entry->id)) ) {
79 if ($blogtags) {
80 $item->tags = $blogtags;
08577617 81 }
1c7b8b93 82 $item->tagscheme = $CFG->wwwroot . '/tag';
ebb73f49 83 }
1c7b8b93 84 $items[] = $item;
516194d0 85 }
1c7b8b93
NC
86 $articles = rss_add_items($items); /// Change structure to XML
87 } else {
88 $articles = '';
89 }
516194d0 90
1c7b8b93 91/// Get header and footer information
dba2764d 92
1c7b8b93
NC
93 switch ($type) {
94 case 'user':
95 $info = fullname($DB->get_record('user', array('id'=>$id), 'firstname,lastname'));
96 break;
97 case 'course':
98 $info = $DB->get_field('course', 'fullname', array('id'=>$id));
99 break;
100 case 'site':
101 $info = $SITE->fullname;
102 break;
103 case 'group':
104 $group = groups_get_group($id, false);
105 $info = $group->name; //TODO: $DB->get_field('groups', 'name', array('id'=>$id))
106 break;
107 default:
108 $info = '';
109 break;
110 }
516194d0 111
1c7b8b93
NC
112 if ($tagid) {
113 $info .= ': '.$DB->get_field('tags', 'text', array('id'=>$tagid));
114 }
cae83708 115
1c7b8b93
NC
116 $header = rss_standard_header(get_string($type.'blog','blog', $info),
117 $CFG->wwwroot.'/blog/index.php',
118 get_string('intro','blog'));
516194d0 119
1c7b8b93 120 $footer = rss_standard_footer();
516194d0 121
122
1c7b8b93 123/// Save the XML contents to file.
516194d0 124
1c7b8b93
NC
125 $rssdata = $header.$articles.$footer;
126
127 if (blog_rss_save_file($type,$id,$tagid,$rssdata)) {
128 return $filename;
129 } else {
130 return false; // Couldn't find it or make it
516194d0 131 }
1c7b8b93 132}
516194d0 133
516194d0 134
1c7b8b93
NC
135function blog_rss_file_name($type, $id, $tagid=0) {
136 global $CFG;
516194d0 137
1c7b8b93
NC
138 if ($tagid) {
139 return "$CFG->dataroot/rss/blog/$type/$id/$tagid.xml";
140 } else {
141 return "$CFG->dataroot/rss/blog/$type/$id.xml";
516194d0 142 }
1c7b8b93 143}
cae83708 144
1c7b8b93
NC
145//This function saves to file the rss feed specified in the parameters
146function blog_rss_save_file($type, $id, $tagid=0, $contents='') {
147 global $CFG;
516194d0 148
1c7b8b93
NC
149 if (! $basedir = make_upload_directory("rss/blog/$type/$id")) {
150 return false;
151 }
516194d0 152
1c7b8b93
NC
153 $file = blog_rss_file_name($type, $id, $tagid);
154 $rss_file = fopen($file, 'w');
155 if ($rss_file) {
156 $status = fwrite ($rss_file, $contents);
157 fclose($rss_file);
158 } else {
159 $status = false;
516194d0 160 }
ebb73f49 161
1c7b8b93
NC
162 return $status;
163}
164