2006-03-17 07:38:08 +00:00
|
|
|
<?php
|
|
|
|
|
2012-01-11 14:02:53 +08:00
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Blog RSS Management
|
|
|
|
*
|
|
|
|
* @package core_blog
|
|
|
|
* @category rss
|
|
|
|
* @copyright 2010 Andrew Davis
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
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
|
|
|
|
2012-01-11 14:02:53 +08:00
|
|
|
/**
|
|
|
|
* Build the URL for the RSS feed
|
|
|
|
*
|
|
|
|
* @param int $contextid The context under which the URL should be created
|
|
|
|
* @param int $userid The id of the user requesting the RSS Feed
|
|
|
|
* @param string $filtertype The source of the RSS feed (site/course/group/user)
|
|
|
|
* @param int $filterselect The id of the item defined by $filtertype
|
|
|
|
* @param int $tagid The id of the row in the tag table that identifies the RSS Feed
|
|
|
|
* @return string
|
|
|
|
*/
|
2010-07-28 02:32:11 +00:00
|
|
|
function blog_rss_get_url($contextid, $userid, $filtertype, $filterselect=0, $tagid=0) {
|
|
|
|
$componentname = 'blog';
|
2006-03-17 09:48:24 +00:00
|
|
|
|
2010-07-28 02:32:11 +00:00
|
|
|
$additionalargs = null;
|
2009-10-30 07:25:50 +00:00
|
|
|
switch ($filtertype) {
|
|
|
|
case 'site':
|
2010-07-28 02:32:11 +00:00
|
|
|
$additionalargs = 'site/'.SITEID;
|
2009-10-30 07:25:50 +00:00
|
|
|
break;
|
|
|
|
case 'course':
|
2010-07-28 02:32:11 +00:00
|
|
|
$additionalargs = 'course/'.$filterselect;
|
2009-10-30 07:25:50 +00:00
|
|
|
break;
|
|
|
|
case 'group':
|
2010-07-28 02:32:11 +00:00
|
|
|
$additionalargs = 'group/'.$filterselect;
|
2009-10-30 07:25:50 +00:00
|
|
|
break;
|
|
|
|
case 'user':
|
2010-07-28 02:32:11 +00:00
|
|
|
$additionalargs = 'user/'.$filterselect;
|
2009-10-30 07:25:50 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-03-17 09:48:24 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
if ($tagid) {
|
2010-07-28 02:32:11 +00:00
|
|
|
$additionalargs .= '/'.$tagid;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rss_get_url($contextid, $userid, $componentname, $additionalargs);
|
|
|
|
}
|
|
|
|
|
2012-01-11 14:02:53 +08:00
|
|
|
/**
|
|
|
|
* Print the link for the RSS feed with the correct RSS icon (Theme based)
|
|
|
|
*
|
|
|
|
* @param stdClass $context The context under which the URL should be created
|
|
|
|
* @param string $filtertype The source of the RSS feed (site/course/group/user)
|
|
|
|
* @param int $filterselect The id of the item defined by $filtertype
|
|
|
|
* @param int $tagid The id of the row in the tag table that identifies the RSS Feed
|
|
|
|
* @param string $tooltiptext The tooltip to be displayed with the link
|
|
|
|
*/
|
2010-07-28 02:32:11 +00:00
|
|
|
function blog_rss_print_link($context, $filtertype, $filterselect=0, $tagid=0, $tooltiptext='') {
|
|
|
|
global $CFG, $USER, $OUTPUT;
|
|
|
|
|
|
|
|
if (!isloggedin()) {
|
|
|
|
$userid = $CFG->siteguest;
|
|
|
|
} else {
|
|
|
|
$userid = $USER->id;
|
2009-10-30 07:25:50 +00:00
|
|
|
}
|
2006-03-17 09:48:24 +00:00
|
|
|
|
2010-07-28 02:32:11 +00:00
|
|
|
$url = blog_rss_get_url($context->id, $userid, $filtertype, $filterselect, $tagid);
|
2009-12-16 21:50:45 +00:00
|
|
|
$rsspix = $OUTPUT->pix_url('i/rss');
|
2010-07-28 02:32:11 +00:00
|
|
|
print '<div class="mdl-right"><a href="'. $url .'"><img src="'. $rsspix .'" title="'. strip_tags($tooltiptext) .'" alt="'.get_string('rss').'" /></a></div>';
|
|
|
|
}
|
2006-03-17 07:38:08 +00:00
|
|
|
|
2012-01-11 14:02:53 +08:00
|
|
|
/**
|
|
|
|
* Build the URL for the RSS feed amd add it as a header
|
|
|
|
*
|
|
|
|
* @param stdClass $context The context under which the URL should be created
|
|
|
|
* @param string $title Name for the link to be added to the page header
|
|
|
|
* @param string $filtertype The source of the RSS feed (site/course/group/user)
|
|
|
|
* @param int $filterselect The id of the item defined by $filtertype
|
|
|
|
* @param int $tagid The id of the row in the tag table that identifies the RSS Feed
|
|
|
|
*/
|
2010-07-28 02:32:11 +00:00
|
|
|
function blog_rss_add_http_header($context, $title, $filtertype, $filterselect=0, $tagid=0) {
|
|
|
|
global $PAGE, $USER, $CFG;
|
2006-03-17 07:38:08 +00:00
|
|
|
|
2010-07-28 02:32:11 +00:00
|
|
|
//$componentname = 'blog';
|
|
|
|
//rss_add_http_header($context, $componentname, $filterselect, $title);
|
2010-08-29 14:59:14 +00:00
|
|
|
|
2010-07-28 02:32:11 +00:00
|
|
|
if (!isloggedin()) {
|
|
|
|
$userid = $CFG->siteguest;
|
|
|
|
} else {
|
|
|
|
$userid = $USER->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$rsspath = blog_rss_get_url($context->id, $userid, $filtertype, $filterselect, $tagid);
|
|
|
|
$PAGE->add_alternate_version($title, $rsspath, 'application/rss+xml');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility function to extract parameters needed to generate RSS URLs from the blog filters
|
2012-01-11 14:02:53 +08:00
|
|
|
*
|
|
|
|
* @param array $filters filters for the blog
|
|
|
|
* @return array array containing the id of the user/course/group, the relevant context and the filter type: site/user/course/group
|
2010-07-28 02:32:11 +00:00
|
|
|
*/
|
|
|
|
function blog_rss_get_params($filters) {
|
|
|
|
$thingid = $rsscontext = $filtertype = null;
|
|
|
|
|
2012-07-24 09:52:41 +08:00
|
|
|
$sitecontext = context_system::instance();
|
2010-09-17 11:24:35 +00:00
|
|
|
|
2010-07-28 02:32:11 +00:00
|
|
|
if (!$filters) {
|
|
|
|
$thingid = SITEID;
|
|
|
|
$rsscontext = $sitecontext;
|
|
|
|
$filtertype = 'site';
|
|
|
|
} else if (array_key_exists('course', $filters)) {
|
|
|
|
$thingid = $filters['course'];
|
|
|
|
|
2012-07-24 09:52:41 +08:00
|
|
|
$coursecontext = context_course::instance($thingid);
|
2010-07-28 02:32:11 +00:00
|
|
|
$rsscontext = $coursecontext;
|
|
|
|
|
|
|
|
$filtertype = 'course';
|
|
|
|
} else if (array_key_exists('user', $filters)) {
|
|
|
|
$thingid = $filters['user'];
|
|
|
|
|
2012-07-24 09:52:41 +08:00
|
|
|
$usercontext = context_user::instance($thingid);
|
2010-07-28 02:32:11 +00:00
|
|
|
$rsscontext = $usercontext;
|
|
|
|
|
|
|
|
$filtertype = 'user';
|
|
|
|
} else if (array_key_exists('group', $filters)) {
|
|
|
|
$thingid = $filters['group'];
|
|
|
|
|
|
|
|
$rsscontext = $sitecontext; //is this the context we should be using for group blogs?
|
|
|
|
$filtertype = 'group';
|
|
|
|
}
|
|
|
|
|
|
|
|
return array($thingid, $rsscontext, $filtertype);
|
2009-10-30 07:25:50 +00:00
|
|
|
}
|
2006-03-17 07:38:08 +00:00
|
|
|
|
2012-01-11 14:02:53 +08:00
|
|
|
/**
|
|
|
|
* Generate any blog RSS feed via one function
|
|
|
|
*
|
|
|
|
* @param stdClass $context The context of the blog for which the feed it being generated
|
|
|
|
* @param array $args An array of arguements needed to build the feed (contextid, token, componentname, type, id, tagid)
|
|
|
|
*/
|
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 '';
|
|
|
|
}
|
|
|
|
|
2012-11-22 15:44:27 +08:00
|
|
|
if ($CFG->bloglevel == BLOG_SITE_LEVEL) {
|
|
|
|
if (isguestuser()) {
|
|
|
|
debugging(get_string('nopermissiontoshow','error'));
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-24 09:52:41 +08:00
|
|
|
$sitecontext = context_system::instance();
|
2010-07-20 07:49:34 +00:00
|
|
|
if (!has_capability('moodle/blog:view', $sitecontext)) {
|
|
|
|
return null;
|
|
|
|
}
|
2010-07-20 03:47:20 +00:00
|
|
|
|
2010-10-04 08:12:28 +00:00
|
|
|
$type = clean_param($args[3], PARAM_ALPHA);
|
|
|
|
$id = clean_param($args[4], PARAM_INT); // could be groupid / courseid / userid depending on $type
|
2010-07-20 03:47:20 +00:00
|
|
|
|
|
|
|
$tagid=0;
|
|
|
|
if ($args[5] != 'rss.xml') {
|
2010-10-04 08:12:28 +00:00
|
|
|
$tagid = clean_param($args[5], PARAM_INT);
|
2010-07-20 03:47:20 +00:00
|
|
|
} 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()) {
|
2010-07-28 02:32:11 +00:00
|
|
|
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
|
|
|
|
2010-07-30 03:16:14 +00:00
|
|
|
$courseid = $groupid = $userid = null;
|
|
|
|
switch ($type) {
|
|
|
|
case 'site':
|
|
|
|
//$siteid = $id;
|
|
|
|
break;
|
|
|
|
case 'course':
|
|
|
|
$courseid = $id;
|
|
|
|
break;
|
|
|
|
case 'group':
|
|
|
|
$groupid = $id;
|
|
|
|
break;
|
|
|
|
case 'user':
|
|
|
|
$userid = $id;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-07-28 02:32:11 +00:00
|
|
|
// Get all the entries from the database
|
|
|
|
require_once($CFG->dirroot .'/blog/locallib.php');
|
2010-07-30 03:16:14 +00:00
|
|
|
$blogheaders = blog_get_headers($courseid, $groupid, $userid, $tagid);
|
|
|
|
|
2010-07-28 02:32:11 +00:00
|
|
|
$bloglisting = new blog_listing($blogheaders['filters']);
|
|
|
|
$blogentries = $bloglisting->get_entries();
|
2009-10-30 07:25:50 +00:00
|
|
|
|
2010-07-28 02:32:11 +00:00
|
|
|
// Now generate an array of RSS items
|
2009-10-30 07:25:50 +00:00
|
|
|
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;
|
2012-03-30 20:03:10 +08:00
|
|
|
$summary = file_rewrite_pluginfile_urls($blog_entry->summary, 'pluginfile.php',
|
|
|
|
$sitecontext->id, 'blog', 'post', $blog_entry->id);
|
|
|
|
$item->description = format_text($summary, $blog_entry->format);
|
2009-10-30 07:25:50 +00:00
|
|
|
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));
|
2012-07-24 09:52:41 +08:00
|
|
|
$info = format_string($info, true, array('context' => context_course::instance($id)));
|
2009-10-30 07:25:50 +00:00
|
|
|
break;
|
|
|
|
case 'site':
|
2012-07-24 09:52:41 +08:00
|
|
|
$info = format_string($SITE->fullname, true, array('context' => context_course::instance(SITEID)));
|
2009-10-30 07:25:50 +00:00
|
|
|
break;
|
|
|
|
case 'group':
|
2011-02-06 18:15:36 +01:00
|
|
|
$group = groups_get_group($id);
|
2009-10-30 07:25:50 +00:00
|
|
|
$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
|
|
|
|
2010-07-28 02:32:11 +00:00
|
|
|
// Save the XML contents to file.
|
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
|
|
|
|
2012-01-11 14:02:53 +08:00
|
|
|
/**
|
|
|
|
* Retrieve the location and file name of a cached RSS feed
|
|
|
|
*
|
|
|
|
* @param string $type The source of the RSS feed (site/course/group/user)
|
|
|
|
* @param int $id The id of the item defined by $type
|
|
|
|
* @param int $tagid The id of the row in the tag table that identifies the RSS Feed
|
|
|
|
* @return string
|
|
|
|
*/
|
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) {
|
2011-08-11 03:25:38 +09:30
|
|
|
return "$CFG->cachedir/rss/blog/$type/$id/$tagid.xml";
|
2009-10-30 07:25:50 +00:00
|
|
|
} else {
|
2011-08-11 03:25:38 +09:30
|
|
|
return "$CFG->cachedir/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
|
|
|
|
2012-01-11 14:02:53 +08:00
|
|
|
/**
|
|
|
|
* This function saves to file the rss feed specified in the parameters
|
|
|
|
*
|
|
|
|
* @param string $type The source of the RSS feed (site/course/group/user)
|
|
|
|
* @param int $id The id of the item defined by $type
|
|
|
|
* @param int $tagid The id of the row in the tag table that identifies the RSS Feed
|
|
|
|
* @param string $contents The contents of the RSS Feed file
|
|
|
|
* @return bool whether the save was successful or not
|
|
|
|
*/
|
2009-10-30 07:25:50 +00:00
|
|
|
function blog_rss_save_file($type, $id, $tagid=0, $contents='') {
|
|
|
|
global $CFG;
|
2010-08-29 14:59:14 +00:00
|
|
|
|
2010-07-28 02:32:11 +00:00
|
|
|
$status = true;
|
2006-03-17 07:38:08 +00:00
|
|
|
|
2010-07-28 02:32:11 +00:00
|
|
|
//blog creates some additional dirs within the rss cache so make sure they all exist
|
2011-08-23 12:06:50 +09:30
|
|
|
make_cache_directory('rss/blog');
|
|
|
|
make_cache_directory('rss/blog/'.$type);
|
2006-03-20 12:45:36 +00:00
|
|
|
|
2010-08-29 14:59:14 +00:00
|
|
|
$filename = blog_rss_file_name($type, $id, $tagid);
|
|
|
|
$expandfilename = false; //we're supplying a full file path
|
|
|
|
$status = rss_save_file('blog', $filename, $contents, $expandfilename);
|
2010-07-28 02:32:11 +00:00
|
|
|
|
2009-10-30 07:25:50 +00:00
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
|