moodle/blog/rsslib.php

167 lines
5.3 KiB
PHP
Raw Normal View History

2006-03-17 07:38:08 +00:00
<?php
require_once($CFG->dirroot.'/lib/rsslib.php');
require_once($CFG->dirroot .'/blog/lib.php');
2006-03-17 07:38:08 +00:00
// This function returns the icon (from theme) with the link to rss/file.php
// needs some hacking to rss/file.php
2006-03-17 09:48:24 +00:00
function blog_rss_print_link($filtertype, $filterselect, $tagid=0, $tooltiptext='') {
2006-03-17 07:38:08 +00:00
global $CFG, $USER, $OUTPUT;
2006-03-17 07:38:08 +00:00
2006-03-17 09:48:24 +00:00
if (empty($USER->id)) {
$userid = 1;
} else {
$userid = $USER->id;
}
switch ($filtertype) {
case 'site':
$path = SITEID.'/'.$userid.'/blog/site/'.SITEID;
break;
case 'course':
$path = $filterselect.'/'.$userid.'/blog/course/'.$filterselect;
break;
case 'group':
2009-09-04 00:36:43 +00:00
$path = SITEID.'/'.$userid.'/blog/group/'.$filterselect;
2006-03-17 09:48:24 +00:00
break;
case 'user':
$path = SITEID.'/'.$userid.'/blog/user/'.$filterselect;
break;
}
if ($tagid) {
$path .= '/'.$tagid;
}
$path .= '/rss.xml';
$rsspix = $OUTPUT->old_icon_url('i/rss');
2006-03-17 07:38:08 +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
}
2006-03-17 09:48:24 +00:00
// Generate any blog RSS feed via one function (called by ../rss/file.php)
function blog_generate_rss_feed($type, $id, $tagid=0) {
2008-06-01 13:48:12 +00:00
global $CFG, $SITE, $DB;
if (empty($CFG->enablerssfeeds)) {
2006-09-18 13:24:45 +00:00
debugging('Sorry, RSS feeds are disabled on this site');
return '';
}
2006-03-17 09:48:24 +00:00
$filename = blog_rss_file_name($type, $id, $tagid);
if (file_exists($filename)) {
if (filemtime($filename) + 3600 > time()) {
return $filename; /// It's already done so we return cached version
}
}
2009-09-04 00:36:43 +00:00
/// Get all the entries from the database
2009-09-04 00:36:43 +00:00
$blogentries = blog_fetch_entries('', 20, '', $type, $id, $tagid);
2006-03-17 09:48:24 +00:00
/// Now generate an array of RSS items
2009-09-04 00:36:43 +00:00
if ($blogentries) {
$items = array();
2009-09-04 00:36:43 +00:00
foreach ($blogentries as $blog_entry) {
$item = NULL;
2009-09-04 00:36:43 +00:00
$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('blog_entries', $blog_entry->id)) ) {
if ($blogtags) {
$item->tags = $blogtags;
}
$item->tagscheme = $CFG->wwwroot . '/tag';
}
$items[] = $item;
}
$articles = rss_add_items($items); /// Change structure to XML
2006-03-17 09:48:24 +00:00
} else {
$articles = '';
2006-03-17 07:38:08 +00:00
}
/// Get header and footer information
2009-09-04 00:36:43 +00:00
switch ($type) {
case 'user':
2008-06-01 13:48:12 +00:00
$info = fullname($DB->get_record('user', array('id'=>$id), 'firstname,lastname'));
break;
case 'course':
2008-06-01 13:48:12 +00:00
$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;
2006-03-21 08:56:20 +00:00
default:
$info = '';
break;
}
if ($tagid) {
2008-06-01 13:48:12 +00:00
$info .= ': '.$DB->get_field('tags', 'text', array('id'=>$tagid));
2006-03-17 07:38:08 +00:00
}
2009-09-04 00:36:43 +00:00
$header = rss_standard_header(get_string($type.'blog','blog', $info),
$CFG->wwwroot.'/blog/index.php',
get_string('intro','blog'));
2009-09-04 00:36:43 +00:00
$footer = rss_standard_footer();
2006-03-17 07:38:08 +00:00
/// Save the XML contents to file.
2006-03-17 07:38:08 +00:00
$rssdata = $header.$articles.$footer;
2006-03-17 07:38:08 +00:00
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
}
}
function blog_rss_file_name($type, $id, $tagid=0) {
2006-03-17 07:38:08 +00:00
global $CFG;
if ($tagid) {
return "$CFG->dataroot/rss/blog/$type/$id/$tagid.xml";
} else {
return "$CFG->dataroot/rss/blog/$type/$id.xml";
2006-03-17 07:38:08 +00:00
}
}
2009-09-04 00:36:43 +00:00
//This function saves to file the rss feed specified in the parameters
function blog_rss_save_file($type, $id, $tagid=0, $contents='') {
2006-03-17 07:38:08 +00:00
global $CFG;
if (! $basedir = make_upload_directory("rss/blog/$type/$id")) {
return false;
2006-03-17 07:38:08 +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
}
return $status;
}
2006-03-17 07:38:08 +00:00
?>