Skip to content
This repository has been archived by the owner on Apr 8, 2022. It is now read-only.

Commit

Permalink
rss MDL-23391 refactored rss feeds to make them standard across compo…
Browse files Browse the repository at this point in the history
…nents
  • Loading branch information
Andrew Davis committed Jul 20, 2010
1 parent d333dc2 commit 274f984
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 55 deletions.
18 changes: 11 additions & 7 deletions blog/rsslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,20 @@ function blog_rss_print_link($filtertype, $filterselect, $tagid=0, $tooltiptext=


// Generate any blog RSS feed via one function (called by ../rss/file.php)
function blog_rss_get_feed($context, $cm, $instance, $args) {
function blog_rss_get_feed($context, $args) {
global $CFG, $SITE, $DB;

$type = $instance;
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;
}

$type = $args[3];
$id = (int) $args[4]; // could be groupid / courseid / userid depending on $instance

$tagid=0;
Expand All @@ -59,11 +68,6 @@ function blog_rss_get_feed($context, $cm, $instance, $args) {
$tagid = 0;
}

if (empty($CFG->enablerssfeeds)) {
debugging('Sorry, RSS feeds are disabled on this site');
return '';
}

$filename = blog_rss_file_name($type, $id, $tagid);

if (file_exists($filename)) {
Expand Down
19 changes: 9 additions & 10 deletions mod/data/rsslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

// This function is the main entry point to database module
// rss feeds generation.
function data_rss_get_feed($context, $cm, $instance, $args) {
function data_rss_get_feed($context, $args) {
global $CFG, $DB;

// Check CFG->data_enablerssfeeds.
Expand All @@ -12,18 +12,18 @@ function data_rss_get_feed($context, $cm, $instance, $args) {
return null;
}

//check capabilities
if (!has_capability('mod/data:managetemplates', $context)) {
if (!is_enrolled($context, null, 'mod/data:managetemplates')) {
return null;
}

$data = $DB->get_record('data', array('id' => $instance), '*', MUST_EXIST);
$dataid = $args[3];
$data = $DB->get_record('data', array('id' => $dataid), '*', MUST_EXIST);

if (!rss_enabled('data', $data, false, true)) {
return null;
}

$sql = data_rss_get_sql($data, $cm);
$sql = data_rss_get_sql($data);

//get the cache file info
$filename = rss_get_file_name($data, $sql);
Expand All @@ -35,7 +35,7 @@ function data_rss_get_feed($context, $cm, $instance, $args) {
$cachedfilelastmodified = filemtime($cachedfilepath);
}

if (data_rss_newstuff($data, $cm, $cachedfilelastmodified)) {
if (data_rss_newstuff($data, $cachedfilelastmodified)) {
require_once($CFG->dirroot . '/mod/data/lib.php');

// Get the first field in the list (a hack for now until we have a selector)
Expand Down Expand Up @@ -98,7 +98,7 @@ function data_rss_get_feed($context, $cm, $instance, $args) {
return $cachedfilepath;
}

function data_rss_get_sql($data, $cm, $time=0) {
function data_rss_get_sql($data, $time=0) {
//do we only want new posts?
if ($time) {
$time = " AND dr.timemodified > '$time'";
Expand All @@ -122,14 +122,13 @@ function data_rss_get_sql($data, $cm, $time=0) {
* Otherwise it returns false.
*
* @param object $data the data activity object
* @param object $cm
* @param int $time timestamp
* @return bool
*/
function data_rss_newstuff($data, $cm, $time) {
function data_rss_newstuff($data, $time) {
global $DB;

$sql = data_rss_get_sql($data, $cm, $time);
$sql = data_rss_get_sql($data, $time);

$recs = $DB->get_records_sql($sql, null, 0, 1);//limit of 1. If we get even 1 back we have new stuff
return ($recs && !empty($recs));
Expand Down
8 changes: 5 additions & 3 deletions mod/forum/rsslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* @param array $args the arguments received in the url
* @return string the full path to the cached RSS feed directory. Null if there is a problem.
*/
function forum_rss_get_feed($context, $cm, $forumid, $args) {
function forum_rss_get_feed($context, $args) {
global $CFG, $DB;

$status = true;
Expand All @@ -43,17 +43,19 @@ function forum_rss_get_feed($context, $cm, $forumid, $args) {
return null;
}

//check capabilities
if (!has_capability('mod/forum:viewdiscussion', $context)) {
if (!is_enrolled($context, null, 'mod/forum:viewdiscussion')) {
return null;
}

$forumid = $args[3];
$forum = $DB->get_record('forum', array('id' => $forumid), '*', MUST_EXIST);

if (!rss_enabled('forum', $forum)) {
return null;
}

$cm = get_coursemodule_from_instance('forum', $forumid, 0, false, MUST_EXIST);

//the sql that will retreive the data for the feed and be hashed to get the cache filename
$sql = forum_rss_get_sql($forum, $cm);

Expand Down
3 changes: 1 addition & 2 deletions mod/glossary/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2589,8 +2589,7 @@ function glossary_extend_settings_navigation(settings_navigation $settings, navi

$glossary = $DB->get_record('glossary', array("id" => $PAGE->cm->instance));

if (!empty($CFG->enablerssfeeds) && !empty($CFG->glossary_enablerssfeeds)
&& $glossary->rsstype && $glossary->rssarticles) {
if (!empty($CFG->enablerssfeeds) && !empty($CFG->glossary_enablerssfeeds) && $glossary->rsstype && $glossary->rssarticles) {
require_once("$CFG->libdir/rsslib.php");

$string = get_string('rsstype','forum');
Expand Down
19 changes: 11 additions & 8 deletions mod/glossary/rsslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

//This function is the main entry point to glossary
//rss feeds generation.
function glossary_rss_get_feed($context, $cm, $instance, $args) {
function glossary_rss_get_feed($context, $args) {
global $CFG, $DB;

if (empty($CFG->glossary_enablerssfeeds)) {
Expand All @@ -15,14 +15,18 @@ function glossary_rss_get_feed($context, $cm, $instance, $args) {

//check capabilities
//glossary module doesn't require any capabilities to view glossary entries (aside from being logged in)
if (!is_enrolled($context)) {
return null;
}

$glossary = $DB->get_record('glossary', array('id' => $instance), '*', MUST_EXIST);
$glossaryid = $args[3];
$glossary = $DB->get_record('glossary', array('id' => $glossaryid), '*', MUST_EXIST);

if (!rss_enabled('glossary', $glossary)) {
return null;
}

$sql = glossary_rss_get_sql($glossary, $cm);
$sql = glossary_rss_get_sql($glossary);

//get the cache file info
$filename = rss_get_file_name($glossary, $sql);
Expand All @@ -34,7 +38,7 @@ function glossary_rss_get_feed($context, $cm, $instance, $args) {
$cachedfilelastmodified = filemtime($cachedfilepath);
}

if (glossary_rss_newstuff($glossary, $cm, $cachedfilelastmodified)) {
if (glossary_rss_newstuff($glossary, $cachedfilelastmodified)) {
if (!$recs = $DB->get_records_sql($sql, array(), 0, $glossary->rssarticles)) {
return null;
}
Expand Down Expand Up @@ -90,7 +94,7 @@ function glossary_rss_get_feed($context, $cm, $instance, $args) {
return $cachedfilepath;
}

function glossary_rss_get_sql($glossary, $cm, $time=0) {
function glossary_rss_get_sql($glossary, $time=0) {
//do we only want new items?
if ($time) {
$time = "AND e.timecreated > $time";
Expand Down Expand Up @@ -138,14 +142,13 @@ function glossary_rss_get_sql($glossary, $cm, $time=0) {
* Otherwise it returns false.
*
* @param object $glossary the glossary activity object
* @param object $cm
* @param int $time timestamp
* @return bool
*/
function glossary_rss_newstuff($glossary, $cm, $time) {
function glossary_rss_newstuff($glossary, $time) {
global $DB;

$sql = glossary_rss_get_sql($glossary, $cm, $time);
$sql = glossary_rss_get_sql($glossary, $time);

$recs = $DB->get_records_sql($sql, null, 0, 1);//limit of 1. If we get even 1 back we have new stuff
return ($recs && !empty($recs));
Expand Down
28 changes: 3 additions & 25 deletions rss/file.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
$contextid = (int)$args[0];
$token = $args[1];
$componentname = clean_param($args[2], PARAM_FILE);
$instance = $args[3];
//$instance = $args[3];

$userid = rss_get_userid_from_token($token);
if (!$userid) {
Expand All @@ -70,36 +70,14 @@
//this will store the path to the cached rss feed contents
$pathname = null;

//check user's psuedo login created by session_set_user()
//NOTE the component providing the feed should do its own capability checks
try {
$cm = null;
if (!empty($plugin) && !empty($instance)) {
$cm = get_coursemodule_from_instance($plugin, $instance, 0, false, MUST_EXIST);
}

//Get course from context
//TODO: note that in the case of the hub rss feed, the feed is not related to a course context,
//it is more a "site" context. The Hub RSS bypass the following line using context id = 2
$coursecontext = get_course_context($context);

$course = null;
if ($coursecontext) {
$course = $DB->get_record('course', array('id' => $coursecontext->instanceid), '*', MUST_EXIST);
}

require_login($course, false, $cm, false, true);
} catch (Exception $e) {
rss_not_found();
}

if (file_exists($componentdir)) {
require_once("$componentdir/rsslib.php");
$functionname = $plugin.'_rss_get_feed';

if (function_exists($functionname)) {
//$pathname will be null if there was a problem or the user doesn't have the necessary capabilities
$pathname = $functionname($context, $cm, $instance, $args);
//NOTE the component providing the feed should do its own capability checks
$pathname = $functionname($context, $args);
}
}

Expand Down

0 comments on commit 274f984

Please sign in to comment.