Skip to content

Commit

Permalink
Updated for better reporting when loading from remote url fails. Now …
Browse files Browse the repository at this point in the history
…displays error when adding URL when it cannot load even when ->debug is off. Added new feature - select multiple feeds to display in each block instance.
  • Loading branch information
dhawes committed Dec 30, 2004
1 parent 10f2f07 commit 92ce1eb
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 66 deletions.
76 changes: 50 additions & 26 deletions blocks/rss_client/block_rss_client.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ function init() {
}

function specialization() {
// After the block has been loaded we customize the block's title display
if (!empty($this->config) && !empty($this->config->title)) {
// There is a customized block title, display it
$this->title = $this->config->title;
} else {
// No customized block title, use localized remote news feed string
$this->title = get_string('block_rss_remote_news_feed', 'block_rss_client');
}
}

Expand All @@ -22,7 +27,7 @@ function get_content() {
}

$this->content = new stdClass;
$this->content->footer = '';
$this->content->footer = '';

if (empty($this->instance) || empty($CFG->blog_version)) {
// Either we're being asked for content without
Expand All @@ -43,7 +48,13 @@ function get_content() {

if (!empty($this->config)) {
if (!empty($this->config->rssid)) {
$rssid = intval($this->config->rssid);
if (blog_array_count($this->config->rssid)) {
// rssid is an array of rssids
$rssidarray = $this->config->rssid;
} else {
// rssid is a single rssid
$rssidarray = array($this->config->rssid);
}
}
if (!empty($this->config->display_description)) {
$display_description = intval($this->config->display_description);
Expand All @@ -70,10 +81,39 @@ function get_content() {
}
}

// Daryl Hawes note: if count of rssidarray is greater than 1
// we should possibly display a drop down menu of selected feed titles
// so user can select a single feed to view (similar to RSSFeed)
foreach ($rssidarray as $rssid) {
$output .= $this->get_rss_by_id($rssid, $display_description, $shownumentries);
}

$this->content->text = $output;
return $this->content;
}

function instance_allow_multiple() {
return true;
}

function has_config() {
return true;
}

function instance_allow_config() {
return true;
}

function get_rss_by_id($rssid, $display_description, $shownumentries) {
$returnstring = '';
$rss_record = get_record('block_rss_client', 'id', $rssid);
if (isset($rss_record) && isset($rss_record->id)) {
$rss = rss_get_feed($rss_record->id, $rss_record->url, $rss_record->type);
// print_object($rss); //debug
if (empty($rss)) {
// There was a failure in loading the rss feed
return;
}

if ($shownumentries > 0 && $shownumentries < count($rss->items) ) {
$count_to = $shownumentries;
Expand All @@ -84,49 +124,33 @@ function get_content() {
for ($y = 0; $y < $count_to; $y++) {
if ($rss->items[$y]['title'] == '') {
// $rss->items[$y]['description'] = blog_unhtmlentities($rss->items[$y]['description']);
//can define an additional instance/admin config item for this (20) - max_description_length
//Daryl Hawes note: might want to define an additional instance/admin config item for this (20) - max_description_length
$rss->items[$y]['title'] = substr(strip_tags($rss->items[$y]['description']), 0, 20) . '...';
}

if ($rss->items[$y]['link'] == '') {
$rss->items[$y]['link'] = $rss->items[$y]['guid'];
}

$output .= '<a href="'. $rss->items[$y]['link'] .'" target=_new>'. $rss->items[$y]['title'] . '</a><br />' ."\n";
$returnstring .= '<a href="'. $rss->items[$y]['link'] .'" target=_new>'. $rss->items[$y]['title'] . '</a><br />' ."\n";

if ($display_description){
$output .= $rss->items[$y]['description'] . '<br />' ."\n";
if ($display_description && !empty($rss->items[$y]['description'])){
$returnstring .= $rss->items[$y]['description'] . '<br />' ."\n";
}
}

$output .= '<br />';
// print_object($rss); //debug
$feedtitle = get_string('block_rss_remote_news_feed', 'block_rss_client');

// print_object($rss); //debug
if ( isset($rss->channel['link']) && isset($rss->channel['title']) ) {
$feedtitle = '<a href="'. $rss->channel['link'] .'">'. $rss->channel['title'] .'</a>';
}
}

//can we reset the title here?
if (isset($feedtitle) && $feedtitle != '') {
if (isset($feedtitle) && $feedtitle != '' && $feedtitle != '<a href="'. $rss->channel['link'] .'"></a>') {
$this->title = $feedtitle;
}

$this->content->text = $output;
return $this->content;
}

function instance_allow_multiple() {
return true;
}

function has_config() {
return true;
$returnstring .= '<br />';
return $returnstring;
}

function instance_allow_config() {
return true;
}
}
?>
4 changes: 4 additions & 0 deletions blocks/rss_client/block_rss_client_action.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@
}

$rss = rss_get_feed($rssid, $url, $rsstype);

if (empty($rss)) {
print 'There was an error loading this rss feed. You may want to verify the url you have specified before using it.';
}

$dataobject->id = $rssid;
if (!empty($rss->channel['description'])) {
Expand Down
5 changes: 3 additions & 2 deletions blocks/rss_client/config_global.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
} ?>" />
</td>
<td>
<?php print_string('block_rss_num_entries', 'block_rss_client') ?>
<?php print_string('block_rss_client_num_entries', 'block_rss_client') ?>
</td>
</tr>
<tr valign="top">
Expand Down Expand Up @@ -39,6 +39,7 @@
<input type="submit" value="<?php print_string('savechanges') ?>"></td>
</tr>
<tr>
<td align=center><a href=" <?php echo $CFG->wwwroot; ?>/blocks/rss_client/block_rss_client_action.php?courseid=<?php echo $courseid; ?>"><?php print_string('block_rss_feeds_add_edit', 'block_rss_client')?></a></center><br /><br />

<td colspan="3" align="center"><a href=" <?php echo $CFG->wwwroot; ?>/blocks/rss_client/block_rss_client_action.php?courseid=<?php echo $courseid; ?>"><?php print_string('block_rss_feeds_add_edit', 'block_rss_client')?></a></center><br /><br />
</td>
</table>
38 changes: 33 additions & 5 deletions blocks/rss_client/config_instance.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,37 @@
</td>
<td>
<?php
$selected = '';
$selectedarray = '';
if (isset($this->config) && isset($this->config->rssid)) {
$selected = $this->config->rssid;
if (blog_array_count($this->config->rssid)) {
// rssid is an array of rssids
$selectedarray = $this->config->rssid;
} else {
// rssid is a single rssid
$selectedarray = array($this->config->rssid);
}
}
if ($rssfeeds = get_records('block_rss_client')) {
foreach($rssfeeds as $rssfeed){
$feedoptions[$rssfeed->id] = $rssfeed->title;
}
choose_from_menu ($feedoptions, 'rssid', $selected);
$dropdownmenustring = choose_from_menu($feedoptions, 'rssid[]', '', '', '', '0', true);

//Daryl Hawes note:
// moodle's choose_from_menu() function does not support
// the "multiple" or "size" options, so before printing out the
// calculated drop down menu we insert the keyword "multiple"
$dropdownmenustring = preg_replace("|\"rssid\[\]\" >*|","\"rssid[]\" multiple>", $dropdownmenustring);

// since there may be multiple rssids to select
// we need to check for each
foreach ($selectedarray as $selected) {
$selected = intval($selected);
$dropdownmenustring = preg_replace("|\"$selected\">*|","\"$selected\" selected>", $dropdownmenustring);
}

print $dropdownmenustring;

} else {
print_string('block_rss_no_feeds', 'block_rss_client');
if ( isadmin() ){
Expand All @@ -61,8 +83,14 @@
</td>
</tr>
<tr valign="top">
<td align="right"><p><?php print_string('block_rss_word_title', 'block_rss_client'); ?>:</td>
<td><input type="text" name="title" size="30" value="<?php echo $this->config->title; ?>" />
<td align="right"><p><?php print_string('uploadlabel'); ?></td>
<?php
$title = '';
if (!empty($this->config) && !empty($this->config->title)) {
$title = $this->config->title;
}
?>
<td><input type="text" name="title" size="30" value="<?php echo $title; ?>" />
</td>
</tr>
<tr>
Expand Down
Loading

0 comments on commit 92ce1eb

Please sign in to comment.