Skip to content

Commit

Permalink
rss MDL-23473 made blog rss feeds work
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Davis committed Jul 28, 2010
1 parent 6ce2b0d commit e858c36
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 59 deletions.
15 changes: 15 additions & 0 deletions blog/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,21 @@
$PAGE->set_context(get_context_instance(CONTEXT_USER, $USER->id));
}

if ($CFG->enablerssfeeds) {
$rsscontext = $filtertype = $thingid = null;
list($thingid, $rsscontext, $filtertype) = blog_rss_get_params($blogheaders['filters']);

$rsstitle = $blogheaders['heading'];

//check we haven't started output by outputting an error message
if ($PAGE->state == moodle_page::STATE_BEFORE_HEADER) {
blog_rss_add_http_header($rsscontext, $rsstitle, $filtertype, $thingid, $tagid);
}

//this works but there isn't a great place to put the link
//blog_rss_print_link($rsscontext, $filtertype, $thingid, $tagid);
}

echo $OUTPUT->header();

echo $OUTPUT->heading($blogheaders['heading'], 2);
Expand Down
9 changes: 0 additions & 9 deletions blog/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -816,15 +816,6 @@ public function print_entries() {

echo $OUTPUT->render($pagingbar);

if ($CFG->enablerssfeeds) {
//todo reimplement blog rss feeds

//blog_rss_print_link($filtertype, $filterselect, $tag);

//require_once("$CFG->libdir/rsslib.php");
//rss_add_http_header($sitecontext, 'blog', $forum, $rsstitle);
}

if (has_capability('moodle/blog:create', $sitecontext)) {
//the user's blog is enabled and they are viewing their own blog
$userid = optional_param('userid', null, PARAM_INT);
Expand Down
135 changes: 97 additions & 38 deletions blog/rsslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,98 @@
require_once($CFG->dirroot.'/lib/rsslib.php');
require_once($CFG->dirroot .'/blog/lib.php');

// 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='') {

global $CFG, $USER, $OUTPUT;

if (!isloggedin()) {
$userid = $CFG->siteguest;
} else {
$userid = $USER->id;
}
function blog_rss_get_url($contextid, $userid, $filtertype, $filterselect=0, $tagid=0) {
$componentname = 'blog';

$additionalargs = null;
switch ($filtertype) {
case 'site':
$path = SITEID.'/'.$userid.'/blog/site/'.SITEID;
$additionalargs = 'site/'.SITEID;
break;
case 'course':
$path = $filterselect.'/'.$userid.'/blog/course/'.$filterselect;
$additionalargs = 'course/'.$filterselect;
break;
case 'group':
$path = SITEID.'/'.$userid.'/blog/group/'.$filterselect;
$additionalargs = 'group/'.$filterselect;
break;
case 'user':
$path = SITEID.'/'.$userid.'/blog/user/'.$filterselect;
$additionalargs = 'user/'.$filterselect;
break;
}

if ($tagid) {
$path .= '/'.$tagid;
$additionalargs .= '/'.$tagid;
}

return rss_get_url($contextid, $userid, $componentname, $additionalargs);
}

// 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($context, $filtertype, $filterselect=0, $tagid=0, $tooltiptext='') {
global $CFG, $USER, $OUTPUT;

if (!isloggedin()) {
$userid = $CFG->siteguest;
} else {
$userid = $USER->id;
}

$path .= '/rss.xml';
$url = blog_rss_get_url($context->id, $userid, $filtertype, $filterselect, $tagid);
$rsspix = $OUTPUT->pix_url('i/rss');
print '<div class="mdl-right"><a href="'. $url .'"><img src="'. $rsspix .'" title="'. strip_tags($tooltiptext) .'" alt="'.get_string('rss').'" /></a></div>';
}

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>';
function blog_rss_add_http_header($context, $title, $filtertype, $filterselect=0, $tagid=0) {
global $PAGE, $USER, $CFG;

//$componentname = 'blog';
//rss_add_http_header($context, $componentname, $filterselect, $title);

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
* @param <type> $filters
* @return array array containing the id of the user/course/group, the relevant context and the filter type (site/user/course/group)
*/
function blog_rss_get_params($filters) {
$thingid = $rsscontext = $filtertype = null;

if (!$filters) {
$thingid = SITEID;
$rsscontext = $sitecontext;
$filtertype = 'site';
} else if (array_key_exists('course', $filters)) {
$thingid = $filters['course'];

$coursecontext = get_context_instance(CONTEXT_COURSE, $thingid);
$rsscontext = $coursecontext;

$filtertype = 'course';
} else if (array_key_exists('user', $filters)) {
$thingid = $filters['user'];

$usercontext = get_context_instance(CONTEXT_USER, $thingid);
$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);
}


Expand Down Expand Up @@ -72,15 +126,18 @@ function blog_rss_get_feed($context, $args) {

if (file_exists($filename)) {
if (filemtime($filename) + 3600 > time()) {
return $filename; /// It's already done so we return cached version
return $filename; // It's already done so we return cached version
}
}

/// Get all the entries from the database

$blogentries = blog_fetch_entries('', 20, '', $type, $id, $tagid);
// Get all the entries from the database
//$blogentries = blog_fetch_entries('', 20, '', $type, $id, $tagid);
require_once($CFG->dirroot .'/blog/locallib.php');
$blogheaders = blog_get_headers();
$bloglisting = new blog_listing($blogheaders['filters']);
$blogentries = $bloglisting->get_entries();

/// Now generate an array of RSS items
// Now generate an array of RSS items
if ($blogentries) {
$items = array();
foreach ($blogentries as $blog_entry) {
Expand Down Expand Up @@ -134,11 +191,8 @@ function blog_rss_get_feed($context, $args) {

$footer = rss_standard_footer();


/// Save the XML contents to file.

// Save the XML contents to file.
$rssdata = $header.$articles.$footer;

if (blog_rss_save_file($type,$id,$tagid,$rssdata)) {
return $filename;
} else {
Expand All @@ -160,20 +214,25 @@ function blog_rss_file_name($type, $id, $tagid=0) {
//This function saves to file the rss feed specified in the parameters
function blog_rss_save_file($type, $id, $tagid=0, $contents='') {
global $CFG;

$status = true;

if (! $basedir = make_upload_directory("rss/blog/$type/$id")) {
return false;
//blog creates some additional dirs within the rss cache so make sure they all exist
if (! $basedir = make_upload_directory ('cache/rss/blog')) {
//Cannot be created, so error
$status = false;
}

$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 {
if (! $basedir = make_upload_directory ('cache/rss/blog/'.$type)) {
//Cannot be created, so error
$status = false;
}

if ($status) {
$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);
}

return $status;
}

28 changes: 20 additions & 8 deletions lib/rsslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@

function rss_add_http_header($context, $componentname, $componentinstance, $title) {
global $PAGE, $USER;
$rsspath = rss_get_url($context->id, $USER->id, $componentname, $componentinstance->id);

$componentid = null;
if (is_object($componentinstance)) {
$componentid = $componentinstance->id;
} else {
$componentid = $componentinstance;
}

$rsspath = rss_get_url($context->id, $USER->id, $componentname, $componentid);
$PAGE->add_alternate_version($title, $rsspath, 'application/rss+xml');
}

Expand Down Expand Up @@ -56,13 +64,13 @@ function rss_get_link($contextid, $userid, $componentname, $id, $tooltiptext='')
* @param int contextid the course id
* @param int userid the current user id
* @param string modulename the name of the current module. For example "forum"
* @param int id For modules, module instance id
* @param string $additionalargs For modules, module instance id
*/
function rss_get_url($contextid, $userid, $componentname, $id) {
function rss_get_url($contextid, $userid, $componentname, $additionalargs) {
global $CFG;
require_once($CFG->libdir.'/filelib.php');
$usertoken = rss_get_token($userid);
return get_file_url($contextid.'/'.$usertoken.'/'.$componentname.'/'.$id.'/rss.xml', null, 'rssfile');
return get_file_url($contextid.'/'.$usertoken.'/'.$componentname.'/'.$additionalargs.'/rss.xml', null, 'rssfile');
}

/**
Expand Down Expand Up @@ -130,9 +138,9 @@ function rss_enabled_for_mod($modname, $instance=null, $hasrsstype=true, $hasrss
* @global object
* @param string $componentname the module name ie forum. Used to create a cache directory.
* @param string $filename the name of the file to be created ie "1234"
* @param string $result the data to be written to the file
* @param string $contents the data to be written to the file
*/
function rss_save_file($componentname, $filename, $result) {
function rss_save_file($componentname, $filename, $contents, $expandfilename=true) {
global $CFG;

$status = true;
Expand All @@ -143,10 +151,14 @@ function rss_save_file($componentname, $filename, $result) {
}

if ($status) {
$fullfilename = rss_get_file_full_name($componentname, $filename);
$fullfilename = $filename;
if ($expandfilename) {
$fullfilename = rss_get_file_full_name($componentname, $filename);
}

$rss_file = fopen($fullfilename, "w");
if ($rss_file) {
$status = fwrite ($rss_file, $result);
$status = fwrite ($rss_file, $contents);
fclose($rss_file);
} else {
$status = false;
Expand Down
4 changes: 0 additions & 4 deletions message/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1029,10 +1029,6 @@ function message_history_link($userid1, $userid2, $returnstr=false, $keywords=''
$strmessagehistory = get_string('messagehistory', 'message');
}

//todo andrew user1 is generally the current user
//if (!$userid2) {
// $userid2 = $USER->id;
//}
if ($position) {
$position = "#$position";
}
Expand Down

0 comments on commit e858c36

Please sign in to comment.