2006-03-17 07:38:08 +00:00
|
|
|
<?php
|
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
require_once($CFG->dirroot.'/lib/rsslib.php');
|
|
|
|
require_once($CFG->dirroot .'/blog/lib.php');
|
2006-03-17 07:38:08 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
// This function returns the icon (from theme) with the link to rss/file.php
|
|
|
|
// needs some hacking to rss/file.php
|
|
|
|
function blog_rss_print_link($filtertype, $filterselect, $tagid=0, $tooltiptext='') {
|
2006-03-17 07:38:08 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
global $CFG, $USER, $OUTPUT;
|
2006-03-17 07:38:08 +00:00
|
|
|
|
2010-03-31 07:41:31 +00:00
|
|
|
if (!isloggedin()) {
|
|
|
|
$userid = $CFG->siteguest;
|
2009-10-30 07:25:50 +00:00
|
|
|
} else {
|
|
|
|
$userid = $USER->id;
|
|
|
|
}
|
2006-03-17 09:48:24 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
switch ($filtertype) {
|
|
|
|
case 'site':
|
|
|
|
$path = SITEID.'/'.$userid.'/blog/site/'.SITEID;
|
|
|
|
break;
|
|
|
|
case 'course':
|
|
|
|
$path = $filterselect.'/'.$userid.'/blog/course/'.$filterselect;
|
|
|
|
break;
|
|
|
|
case 'group':
|
|
|
|
$path = SITEID.'/'.$userid.'/blog/group/'.$filterselect;
|
|
|
|
break;
|
|
|
|
case 'user':
|
|
|
|
$path = SITEID.'/'.$userid.'/blog/user/'.$filterselect;
|
|
|
|
break;
|
|
|
|
}
|
2006-03-17 09:48:24 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
if ($tagid) {
|
|
|
|
$path .= '/'.$tagid;
|
|
|
|
}
|
2006-03-17 09:48:24 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
$path .= '/rss.xml';
|
2009-12-16 21:50:45 +00:00
|
|
|
$rsspix = $OUTPUT->pix_url('i/rss');
|
2006-03-17 07:38:08 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
require_once($CFG->libdir.'/filelib.php');
|
|
|
|
$path = get_file_url($path, null, 'rssfile');
|
|
|
|
print '<div class="mdl-right"><a href="'. $path .'"><img src="'. $rsspix .'" title="'. strip_tags($tooltiptext) .'" alt="'.get_string('rss').'" /></a></div>';
|
2006-03-17 07:38:08 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
}
|
2006-03-17 07:38:08 +00:00
|
|
|
|
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
// Generate any blog RSS feed via one function (called by ../rss/file.php)
|
2010-07-20 07:49:34 +00:00
|
|
|
function blog_rss_get_feed($context, $args) {
|
2009-10-30 07:25:50 +00:00
|
|
|
global $CFG, $SITE, $DB;
|
2006-03-20 12:45:36 +00:00
|
|
|
|
2010-07-20 07:49:34 +00:00
|
|
|
if (empty($CFG->enablerssfeeds)) {
|
|
|
|
debugging('Sorry, RSS feeds are disabled on this site');
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$sitecontext = get_context_instance(CONTEXT_SYSTEM);
|
|
|
|
if (!has_capability('moodle/blog:view', $sitecontext)) {
|
|
|
|
return null;
|
|
|
|
}
|
2010-07-20 03:47:20 +00:00
|
|
|
|
2010-07-20 07:49:34 +00:00
|
|
|
$type = $args[3];
|
2010-07-20 03:47:20 +00:00
|
|
|
$id = (int) $args[4]; // could be groupid / courseid / userid depending on $instance
|
|
|
|
|
|
|
|
$tagid=0;
|
|
|
|
if ($args[5] != 'rss.xml') {
|
|
|
|
$tagid = (int) $args[5];
|
|
|
|
} else {
|
|
|
|
$tagid = 0;
|
|
|
|
}
|
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
$filename = blog_rss_file_name($type, $id, $tagid);
|
2006-03-17 09:48:24 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
if (file_exists($filename)) {
|
|
|
|
if (filemtime($filename) + 3600 > time()) {
|
|
|
|
return $filename; /// It's already done so we return cached version
|
2006-03-17 09:48:24 +00:00
|
|
|
}
|
2009-10-30 07:25:50 +00:00
|
|
|
}
|
2006-03-17 09:48:24 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
/// Get all the entries from the database
|
|
|
|
|
|
|
|
$blogentries = blog_fetch_entries('', 20, '', $type, $id, $tagid);
|
|
|
|
|
|
|
|
/// Now generate an array of RSS items
|
|
|
|
if ($blogentries) {
|
|
|
|
$items = array();
|
|
|
|
foreach ($blogentries as $blog_entry) {
|
|
|
|
$item = NULL;
|
|
|
|
$item->author = fullname($DB->get_record('user', array('id'=>$blog_entry->userid))); // TODO: this is slow
|
|
|
|
$item->title = $blog_entry->subject;
|
|
|
|
$item->pubdate = $blog_entry->lastmodified;
|
|
|
|
$item->link = $CFG->wwwroot.'/blog/index.php?entryid='.$blog_entry->id;
|
|
|
|
$item->description = format_text($blog_entry->summary, $blog_entry->format);
|
|
|
|
if ( !empty($CFG->usetags) && ($blogtags = tag_get_tags_array('post', $blog_entry->id)) ) {
|
|
|
|
if ($blogtags) {
|
|
|
|
$item->tags = $blogtags;
|
2009-05-22 15:16:58 +00:00
|
|
|
}
|
2009-10-30 07:25:50 +00:00
|
|
|
$item->tagscheme = $CFG->wwwroot . '/tag';
|
2006-03-20 12:45:36 +00:00
|
|
|
}
|
2009-10-30 07:25:50 +00:00
|
|
|
$items[] = $item;
|
2006-03-17 07:38:08 +00:00
|
|
|
}
|
2009-10-30 07:25:50 +00:00
|
|
|
$articles = rss_add_items($items); /// Change structure to XML
|
|
|
|
} else {
|
|
|
|
$articles = '';
|
|
|
|
}
|
2006-03-17 07:38:08 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
/// Get header and footer information
|
2006-03-21 08:56:20 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
switch ($type) {
|
|
|
|
case 'user':
|
|
|
|
$info = fullname($DB->get_record('user', array('id'=>$id), 'firstname,lastname'));
|
|
|
|
break;
|
|
|
|
case 'course':
|
|
|
|
$info = $DB->get_field('course', 'fullname', array('id'=>$id));
|
|
|
|
break;
|
|
|
|
case 'site':
|
|
|
|
$info = $SITE->fullname;
|
|
|
|
break;
|
|
|
|
case 'group':
|
|
|
|
$group = groups_get_group($id, false);
|
|
|
|
$info = $group->name; //TODO: $DB->get_field('groups', 'name', array('id'=>$id))
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$info = '';
|
|
|
|
break;
|
|
|
|
}
|
2006-03-17 07:38:08 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
if ($tagid) {
|
|
|
|
$info .= ': '.$DB->get_field('tags', 'text', array('id'=>$tagid));
|
|
|
|
}
|
2009-09-04 00:36:43 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
$header = rss_standard_header(get_string($type.'blog','blog', $info),
|
|
|
|
$CFG->wwwroot.'/blog/index.php',
|
|
|
|
get_string('intro','blog'));
|
2006-03-17 07:38:08 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
$footer = rss_standard_footer();
|
2006-03-17 07:38:08 +00:00
|
|
|
|
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
/// Save the XML contents to file.
|
2006-03-17 07:38:08 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
$rssdata = $header.$articles.$footer;
|
|
|
|
|
|
|
|
if (blog_rss_save_file($type,$id,$tagid,$rssdata)) {
|
|
|
|
return $filename;
|
|
|
|
} else {
|
|
|
|
return false; // Couldn't find it or make it
|
2006-03-17 07:38:08 +00:00
|
|
|
}
|
2009-10-30 07:25:50 +00:00
|
|
|
}
|
2006-03-17 07:38:08 +00:00
|
|
|
|
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
function blog_rss_file_name($type, $id, $tagid=0) {
|
|
|
|
global $CFG;
|
2006-03-17 07:38:08 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
if ($tagid) {
|
2010-07-19 06:46:23 +00:00
|
|
|
return "$CFG->dataroot/cache/rss/blog/$type/$id/$tagid.xml";
|
2009-10-30 07:25:50 +00:00
|
|
|
} else {
|
2010-07-19 06:46:23 +00:00
|
|
|
return "$CFG->dataroot/cache/rss/blog/$type/$id.xml";
|
2006-03-17 07:38:08 +00:00
|
|
|
}
|
2009-10-30 07:25:50 +00:00
|
|
|
}
|
2009-09-04 00:36:43 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
//This function saves to file the rss feed specified in the parameters
|
|
|
|
function blog_rss_save_file($type, $id, $tagid=0, $contents='') {
|
|
|
|
global $CFG;
|
2006-03-17 07:38:08 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
if (! $basedir = make_upload_directory("rss/blog/$type/$id")) {
|
|
|
|
return false;
|
|
|
|
}
|
2006-03-17 07:38:08 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
$file = blog_rss_file_name($type, $id, $tagid);
|
|
|
|
$rss_file = fopen($file, 'w');
|
|
|
|
if ($rss_file) {
|
|
|
|
$status = fwrite ($rss_file, $contents);
|
|
|
|
fclose($rss_file);
|
|
|
|
} else {
|
|
|
|
$status = false;
|
2006-03-17 07:38:08 +00:00
|
|
|
}
|
2006-03-20 12:45:36 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
|