moodle/blog/rsslib.php

167 lines
5.1 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;
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':
$path = SITEID.'/'.$userid.'/blog/group/'.$filterselect;
break;
case 'user':
$path = SITEID.'/'.$userid.'/blog/user/'.$filterselect;
break;
}
if ($tagid) {
$path .= '/'.$tagid;
}
$path .= '/rss.xml';
2006-03-17 07:38:08 +00:00
$rsspix = $CFG->pixpath .'/i/rss.gif';
if ($CFG->slasharguments) {
2006-03-17 09:48:24 +00:00
$path = $CFG->wwwroot.'/rss/file.php/'.$path;
2006-03-17 07:38:08 +00:00
} else {
2006-03-17 09:48:24 +00:00
$path = $CFG->wwwroot.'/rss/file.php?file='.$path;
2006-03-17 07:38:08 +00:00
}
2006-03-17 09:48:24 +00:00
print '<div align="right"><a href="'. $path .'"><img src="'. $rsspix .'" title="'. strip_tags($tooltiptext) .'" alt="" /></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) {
global $CFG, $SITE;
if (empty($CFG->enablerssfeeds)) {
if ($CFG->debug > 7) {
notify('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
}
}
/// Get all the posts from the database
//$blogFilter =& new BlogFilter(0, 0, 20, 0, $type, $id, $tagid);
$blogposts = fetch_entries(0, 0, 20, 0, $type, $id, $tagid); //$blogFilter->fetch_entries();
2006-03-17 09:48:24 +00:00
/// Now generate an array of RSS items
if ($blogposts) {
$items = array();
foreach ($blogposts as $blogpost) {
$item = NULL;
$item->author = fullname(get_record('user','id',$blogpost->userid));
$item->title = $blogpost->subject;
$item->pubdate = $blogpost->lastmodified;
$item->link = $CFG->wwwroot.'/blog/index.php?postid='.$blogpost->id;
$item->description = format_text($blogpost->summary, $blogpost->format);
$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
2006-03-17 07:38:08 +00:00
switch ($type) {
case 'user':
$info = fullname(get_record('user', 'id', $id, '','','','','firstname,lastname'));
break;
case 'course':
$info = get_field('course', 'fullname', 'id', $id);
break;
case 'site':
$info = $SITE->fullname;
break;
case 'group':
$info = get_field('groups', 'name', 'id', $id);
break;
2006-03-21 08:56:20 +00:00
default:
$info = '';
break;
}
if ($tagid) {
$info .= ': '.get_field('tags', 'text', 'id', $tagid);
2006-03-17 07:38:08 +00:00
}
$header = rss_standard_header(get_string($type.'blog','blog', $info),
$CFG->wwwroot.'/blog/index.php',
get_string('intro','blog'));
$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
}
}
//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
?>