Skip to content

Commit

Permalink
MDL-57638 block_rss_client: warn users about failed RSS feeds
Browse files Browse the repository at this point in the history
  • Loading branch information
davosmith committed Jan 23, 2017
1 parent 83e5234 commit cd698c8
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 4 deletions.
15 changes: 15 additions & 0 deletions blocks/rss_client/block_rss_client.php
Expand Up @@ -33,6 +33,9 @@ class block_rss_client extends block_base {
/** The maximum time in seconds that cron will wait between attempts to retry failing RSS feeds. */
const CLIENT_MAX_SKIPTIME = 43200; // 60 * 60 * 12 seconds.

/** @var bool track whether any of the output feeds have recorded failures */
private $hasfailedfeeds = false;

function init() {
$this->title = get_string('pluginname', 'block_rss_client');
}
Expand All @@ -59,6 +62,7 @@ function specialization() {
* @return block_rss_client\output\footer|null The renderable footer or null if none should be displayed.
*/
protected function get_footer($feedrecords) {
global $PAGE;
$footer = null;

if ($this->config->block_rss_client_show_channel_link) {
Expand All @@ -74,6 +78,16 @@ protected function get_footer($feedrecords) {
}
}

if ($this->hasfailedfeeds) {
if (has_any_capability(['block/rss_client:manageownfeeds', 'block/rss_client:manageanyfeeds'], $this->context)) {
if ($footer === null) {
$footer = new block_rss_client\output\footer();
}
$manageurl = new moodle_url('/blocks/rss_client/managefeeds.php', ['courseid' => $PAGE->course->id]);
$footer->set_failed($manageurl);
}
}

return $footer;
}

Expand Down Expand Up @@ -175,6 +189,7 @@ public function get_feed($feedrecord, $maxentries, $showtitle) {

if ($feedrecord->skipuntil) {
// Last attempt to gather this feed via cron failed - do not try to fetch it now.
$this->hasfailedfeeds = true;
return null;
}

Expand Down
27 changes: 24 additions & 3 deletions blocks/rss_client/classes/output/footer.php
Expand Up @@ -40,16 +40,23 @@ class footer implements \renderable, \templatable {
/**
* The link provided in the RSS channel
*
* @var \moodle_url
* @var \moodle_url|null
*/
protected $channelurl;

/**
* Link to manage feeds, only provided if a feed has failed.
*
* @var \moodle_url|null
*/
protected $manageurl = null;

/**
* Constructor
*
* @param \moodle_url $channelurl The link provided in the RSS channel
* @param \moodle_url $channelurl (optional) The link provided in the RSS channel
*/
public function __construct(\moodle_url $channelurl) {
public function __construct($channelurl = null) {
$this->channelurl = $channelurl;
}

Expand All @@ -65,6 +72,16 @@ public function set_channelurl(\moodle_url $channelurl) {
return $this;
}

/**
* Record the fact that there is at least one failed feed (and the URL for viewing
* these failed feeds).
*
* @param \moodle_url $manageurl the URL to link to for more information
*/
public function set_failed(\moodle_url $manageurl) {
$this->manageurl = $manageurl;
}

/**
* Get the channel url
*
Expand All @@ -84,6 +101,10 @@ public function get_channelurl() {
public function export_for_template(\renderer_base $output) {
$data = new \stdClass();
$data->channellink = clean_param($this->channelurl, PARAM_URL);
if ($this->manageurl) {
$data->hasfailedfeeds = true;
$data->manageurl = clean_param($this->manageurl, PARAM_URL);
}

return $data;
}
Expand Down
2 changes: 2 additions & 0 deletions blocks/rss_client/lang/en/block_rss_client.php
Expand Up @@ -44,6 +44,8 @@
$string['editrssblock'] = 'Edit RSS headline block';
$string['enableautodiscovery'] = 'Enable auto-discovery of feeds?';
$string['enableautodiscovery_help'] = 'If enabled, feeds on web pages are found automatically. For example, if http://docs.moodle.org is entered, then http://docs.moodle.org/en/index.php?title=Special:RecentChanges&feed=rss would be found.';
$string['failedfeed'] = 'Feed failed to download - will retry after {$a}';
$string['failedfeeds'] = 'One or more RSS feeds have failed';
$string['feed'] = 'Feed';
$string['feedadded'] = 'News feed added';
$string['feeddeleted'] = 'News feed deleted';
Expand Down
7 changes: 7 additions & 0 deletions blocks/rss_client/managefeeds.php
Expand Up @@ -114,6 +114,13 @@
$feedinfo = '<div class="title">' . $viewlink . '</div>' .
'<div class="url">' . html_writer::link($feed->url, $feed->url) .'</div>' .
'<div class="description">' . $feed->description . '</div>';
if ($feed->skipuntil) {
$skipuntil = userdate($feed->skipuntil, get_string('strftimedatetime', 'langconfig'));
$skipmsg = get_string('failedfeed', 'block_rss_client', $skipuntil);
$notification = new \core\output\notification($skipmsg, 'error');
$notification->set_show_closebutton(false);
$feedinfo .= $OUTPUT->render($notification);
}

$editurl = new moodle_url('/blocks/rss_client/editfeed.php?rssid=' . $feed->id . $extraparams);
$editaction = $OUTPUT->action_icon($editurl, new pix_icon('t/edit', get_string('edit')));
Expand Down
8 changes: 7 additions & 1 deletion blocks/rss_client/templates/footer.mustache
Expand Up @@ -33,4 +33,10 @@
"channellink": "https://www.example.com/feeds/rss"
}
}}
<a href="{{{channellink}}}">{{#str}} clientchannellink, block_rss_client {{/str}}</a>
{{#channellink}}
<a href="{{{channellink}}}">{{#str}} clientchannellink, block_rss_client {{/str}}</a>
{{#hasfailedfeeds}}<br>{{/hasfailedfeeds}}
{{/channellink}}
{{#hasfailedfeeds}}
<a href="{{{manageurl}}}">{{#str}} failedfeeds, block_rss_client {{/str}}</a>
{{/hasfailedfeeds}}

0 comments on commit cd698c8

Please sign in to comment.