Commit f1a34d8f authored by tjhunt's avatar tjhunt
Browse files

block_rss_client: MDL-13932 Clean up a lot of the crap.

This was all in order to do MDL-19889!

* The old tabbed interface is gone.
* The manage feeds UI has been completely rewritten to be a few
separate sane scripts like managefeeds.php, editfeed.php and viewfeed.php
* This is used from both Admin -> plugins -> blocks ... and as a link from the block edit_form.php
* new edit_form.php for the block replacing config_istance.php.
* A lot of stuff to do with UI specific to this block has been removed from rsslib.php
parent 166e3bd1
......@@ -19,7 +19,7 @@
function init() {
$this->title = get_string('feedstitle', 'block_rss_client');
$this->version = 2007101511;
$this->version = 2009072900;
$this->cron = 300; /// Set min time between cron executions to 300 secs (5 mins)
}
......@@ -80,52 +80,23 @@
$shownumentries = 5; //default to 5 entries is not specified in admin section or instance
}
if (!empty($this->config)) {
if (!empty($this->config->rssid)) {
if (is_array($this->config->rssid)) {
$rssidarray = $this->config->rssid;
} else { // Make an array of the single value
$rssidarray = array($this->config->rssid);
}
}
if (!empty($this->config->display_description)) {
$display_description = intval($this->config->display_description);
}
if (!empty($this->config->shownumentries)) {
$shownumentries = intval($this->config->shownumentries);
}
if (empty($this->config)) {
$this->content->text = get_string('feedsconfigurenewinstance2', 'block_rss_client');
return $this->content;
}
if (has_any_capability(array('block/rss_client:createsharedfeeds', 'block/rss_client:createprivatefeeds'), $this->context)) {
$page = $this->page;
//if ($page->user_allowed_editing()) { // for SUBMITTERS_ALL_ACCOUNT_HOLDERS we're going to run into trouble later if we show it and then they don't have write access to the page.
if (isset($this->config)) {
// This instance is configured - show Add/Edit feeds link.
$script = $page->url->out(
array('instanceid' => $this->instance->id,
'sesskey' => sesskey(),
'blockaction' => 'config',
'currentaction' => 'managefeeds',
'id' => $this->courseid,
'section' => 'rss'
));
$output .= '<div class="info"><a title="'. get_string('feedsaddedit', 'block_rss_client') .'" href="'. $script .'">'. get_string('feedsaddedit', 'block_rss_client') .'</a></div>';
} else {
// This instance has not been configured yet - show configure link?
if (has_capability('block/rss_client:manageanyfeeds', $this->context)) {
$script = $page->url->out(
array('instanceid' => $this->instance->id,
'sesskey' => sesskey(),
'blockaction' => 'config',
'currentaction' => 'configblock',
'id' => $this->page->course->id,
'section' => 'rss'
));
$output .= '<div class="info"><a title="'. get_string('feedsconfigurenewinstance', 'block_rss_client') .'" href="'. $script.'">'. get_string('feedsconfigurenewinstance', 'block_rss_client') .'</a></div>';
}
if (!empty($this->config->rssid)) {
if (is_array($this->config->rssid)) {
$rssidarray = $this->config->rssid;
} else { // Make an array of the single value
$rssidarray = array($this->config->rssid);
}
//}
}
if (!empty($this->config->display_description)) {
$display_description = intval($this->config->display_description);
}
if (!empty($this->config->shownumentries)) {
$shownumentries = intval($this->config->shownumentries);
}
// Daryl Hawes note: if count of rssidarray is greater than 1
......@@ -172,9 +143,6 @@
$now = time();
require_once($CFG->libdir .'/rsslib.php');
require_once(MAGPIE_DIR .'rss_fetch.inc');
if (!defined('MAGPIE_OUTPUT_ENCODING')) {
define('MAGPIE_OUTPUT_ENCODING', 'utf-8'); // see bug 3107
}
$rss_record = $DB->get_record('block_rss_client', array('id'=>$rssid));
if (isset($rss_record) && isset($rss_record->id)) {
......@@ -187,10 +155,7 @@
ob_end_clean();
if ($rss === false) {
if (debugging() && !empty($rsserror)) {
// There was a failure in loading the rss feed, print link to full error text
return '<a href="'. $CFG->wwwroot .'/blocks/rss_client/block_rss_client_error.php?error='. urlencode($rsserror) .'">Error loading a feed.</a><br />'; //Daryl Hawes note: localize this line
}
debugging($rsserror);
}
// first we must verify that the rss feed is loaded
......@@ -310,10 +275,6 @@
require_once($CFG->libdir .'/rsslib.php');
require_once(MAGPIE_DIR .'rss_fetch.inc');
if (!defined('MAGPIE_OUTPUT_ENCODING')) {
define('MAGPIE_OUTPUT_ENCODING', 'utf-8'); // see bug 3107
}
/// Fetch all site feeds.
$rs = $DB->get_recordset('block_rss_client');
$counter = 0;
......
<?php //$Id$
/*******************************************************************
* This file contains no classes. It will display a list of existing feeds
* defined for the site and allow add/edit/delete of site feeds.
*
* @author Daryl Hawes
* @version $Id$
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package base
******************************************************************/
require_once('../../config.php');
require_once($CFG->libdir .'/rsslib.php');
require_once(MAGPIE_DIR .'rss_fetch.inc');
require_login();
if (isset($_SERVER['HTTP_REFERER'])) {
$referrer = $_SERVER['HTTP_REFERER'];
} else {
$referrer = $CFG->wwwroot.'/';
}
// Ensure that the logged in user is not using the guest account
if (isguest()) {
print_error('noguestpost', 'forum', $referrer);
}
$url = optional_param('url','',PARAM_URL);
if (!empty($url)) {
// attempting to replace feed and rss url types with http
// it appears that the rss feed validator will validate these url types but magpie will not load them $url = str_replace ("feed://", "http://", "$url");
// Shifting this forward since PARAM_URL rejects these feed types as invalid entries!
$url = str_replace ("feed://", "http://", "$url");
$url = str_replace ("FEED://", "http://", "$url");
$url = str_replace ("rss://", "http://", "$url");
$url = str_replace ("RSS://", "http://", "$url");
}
$act = optional_param('act', NULL, PARAM_ALPHA);
$rssid = optional_param('rssid', NULL, PARAM_INT);
$id = optional_param('id', SITEID, PARAM_INT);
//$url = clean_param($url, PARAM_URL);
$preferredtitle = optional_param('preferredtitle', '', PARAM_TEXT);
$shared = optional_param('shared', 0, PARAM_INT);
if (!defined('MAGPIE_OUTPUT_ENCODING')) {
define('MAGPIE_OUTPUT_ENCODING', 'utf-8'); // see bug 3107
}
if (!empty($id)) {
// we get the complete $course object here because print_header assumes this is
// a complete object (needed for proper course theme settings)
if ($course = $DB->get_record('course', array('id'=>$id))) {
$context = get_context_instance(CONTEXT_COURSE, $id);
}
} else {
$context = get_context_instance(CONTEXT_SYSTEM);
}
$straddedit = get_string('feedsaddedit', 'block_rss_client');
$link = $CFG->wwwroot.'/course/view.php?id='.$id;
if (empty($course)) {
$link = '';
}
$navlinks = array();
$navlinks = array(array('name' => get_string('administration'), 'link' => "$CFG->wwwroot/$CFG->admin/index.php", 'type' => 'misc'));
$navlinks[] = array('name' => get_string('managemodules'), 'link' => null, 'type' => 'misc');
$navlinks[] = array('name' => get_string('blocks'), 'link' => null, 'type' => 'misc');
$navlinks[] = array('name' => get_string('feedstitle', 'block_rss_client'), 'link' => "$CFG->wwwroot/$CFG->admin/settings.php?section=blocksettingrss_client", 'type' => 'misc');
$navlinks[] = array('name' => get_string('addnew', 'block_rss_client'), 'link' => null, 'type' => 'misc');
$navigation = build_navigation($navlinks);
print_header($straddedit, $straddedit, $navigation);
if ( !isset($act) ) {
rss_display_feeds($id, $USER->id, '', $context);
rss_print_form($act, $url, $rssid, $preferredtitle, $shared, $id, $context);
print_footer();
die();
}
if ( isset($rssid) ) {
$rss_record = $DB->get_record('block_rss_client', array('id'=>$rssid));
}
if (isset($rss_record)) {
$managefeeds = ($rss_record->userid == $USER->id && has_capability('block/rss_client:manageownfeeds', $context))
|| ($rss_record->userid != $USER->id && has_capability('block/rss_client:manageanyfeeds', $context));
}
if ($act == 'updfeed') {
if (!$managefeeds) {
print_error('cannotmakemodification', 'rss_client', $referrer);
}
if (empty($url)) {
print_error('urlnotdefinerss');
}
// By capturing the output from fetch_rss this way
// error messages do not display and clutter up the moodle interface
// however, we do lose out on seeing helpful messages like "cache hit", etc.
$message = '';
ob_start();
$rss = fetch_rss($url);
if (debugging()) {
$message .= ob_get_contents();
}
ob_end_clean();
$canaddsharedfeeds = has_capability('block/rss_client:createsharedfeeds', $context);
$dataobject->id = $rssid;
if ($rss === false) {
$dataobject->description = '';
$dataobject->title = '';
$dataobject->preferredtitle = '';
$dataobject->shared = 0;
} else {
$dataobject->description = $rss->channel['description'];
$dataobject->title = $rss->channel['title'];
$dataobject->preferredtitle = $preferredtitle;
if ($shared == 1 && $canaddsharedfeeds) {
$dataobject->shared = 1;
} else {
$dataobject->shared = 0;
}
}
$dataobject->url = $url;
$DB->update_record('block_rss_client', $dataobject);
$message .= '<br />'. get_string('feedupdated', 'block_rss_client');
redirect($referrer, $message);
} else if ($act == 'addfeed' ) {
$canaddprivfeeds = has_capability('block/rss_client:createprivatefeeds', $context);
$canaddsharedfeeds = has_capability('block/rss_client:createsharedfeeds', $context);
if (!$canaddprivfeeds && !$canaddsharedfeeds) {
print_error('cannotaddrss', 'error');
}
if (empty($url)) {
print_error('urlnotdefinerss', 'error');
}
$dataobject->userid = $USER->id;
$dataobject->description = '';
$dataobject->title = '';
$dataobject->url = $url;
$dataobject->preferredtitle = $preferredtitle;
if ($shared == 1 && $canaddsharedfeeds) {
$dataobject->shared = 1;
} else {
$dataobject->shared = 0;
}
$rssid = $DB->insert_record('block_rss_client', $dataobject);
// By capturing the output from fetch_rss this way
// error messages do not display and clutter up the moodle interface
// however, we do lose out on seeing helpful messages like "cache hit", etc.
$message = '';
ob_start();
$rss = fetch_rss($url);
if (debugging()) {
$message .= ob_get_contents();
}
ob_end_clean();
if ($rss === false) {
$message .= '<br /><br />There was an error loading this rss feed. You may want to verify the url you have specified before using it.'; //Daryl Hawes note: localize this line
} else {
$dataobject->id = $rssid;
if (!empty($rss->channel['description'])) {
$dataobject->description = $rss->channel['description'];
}
if (!empty($rss->channel['title'])) {
$dataobject->title = $rss->channel['title'];
}
$DB->update_record('block_rss_client', $dataobject);
$message .= '<br />'. get_string('feedadded', 'block_rss_client');
}
redirect($referrer, $message);
/*
rss_display_feeds($id, $USER->id, '', $context);
rss_print_form($act, $dataobject->url, $dataobject->id, $dataobject->preferredtitle, $shared, $id, $context);
*/
} else if ( isset($rss_record) && $act == 'rssedit' ) {
$preferredtitle = $rss_record->preferredtitle;
if (empty($preferredtitle)) {
$preferredtitle = $rss_record->title;
}
$url = $rss_record->url;
$shared = $rss_record->shared;
rss_display_feeds($id, $USER->id, $rssid, $context);
rss_print_form($act, $url, $rssid, $preferredtitle, $shared, $id, $context);
} else if ($act == 'delfeed') {
if (!$managefeeds) {
print_error('cannotmakemodification', 'rss_client', $referrer);
}
$file = $CFG->dataroot .'/cache/rsscache/'. $rssid .'.xml';
if (file_exists($file)) {
unlink($file);
}
// echo "DEBUG: act = delfeed"; //debug
$DB->delete_records('block_rss_client', array('id'=>$rssid));
redirect($referrer, get_string('feeddeleted', 'block_rss_client') );
} else if ( isset($rss_record) && $act == 'view' ) {
// echo $sql; //debug
// print_object($res); //debug
if (!$rss_record->id) {
print '<strong>'. get_string('couldnotfindfeed', 'block_rss_client') .': '. $rssid .'</strong>';
} else {
// By capturing the output from fetch_rss this way
// error messages do not display and clutter up the moodle interface
// however, we do lose out on seeing helpful messages like "cache hit", etc.
ob_start();
$rss = fetch_rss($rss_record->url);
ob_end_clean();
if (empty($rss_record->preferredtitle)) {
$feedtitle = $rss_record->preferredtitle;
} else {
$feedtitle = $rss->channel['title'];
}
print '<table align="center" width="50%" cellspacing="1">'."\n";
print '<tr><td colspan="2"><strong>'. $feedtitle .'</strong></td></tr>'."\n";
for($y=0; $y < count($rss->items); $y++) {
if ($rss->items[$y]['link'] == '') {
$rss->items[$y]['link'] = $rss->items[$y]['guid'];
}
if ($rss->items[$y]['title'] == '') {
$rss->items[$y]['title'] = '&gt;&gt;';
}
print '<tr><td valign="middle">'."\n";
print '<a href="'. $rss->items[$y]['link'] .'" target="_blank"><strong>'. $rss->items[$y]['title'];
print '</strong></a>'."\n";
print '</td>'."\n";
if (file_exists($CFG->dirroot .'/blog/lib.php')) {
//Blog module is installed - provide "blog this" link
print '<td align="right">'."\n";
/// MDL-9291, blog this feature needs further discussion/implementation
/// temporarily disabling for now.
// print '<img src="'. $OUTPUT->old_icon_url('blog/blog') . '" alt="'. get_string('blogthis', 'blog').'" title="'. get_string('blogthis', 'blog') .'" border="0" align="middle" />'."\n";
//print '<a href="'. $CFG->wwwroot .'/blog/blogthis.php?userid='. $USER->id .'&act=use&item='. $y .'&rssid='. $rssid .'"><small><strong>'. get_string('blogthis', 'blog') .'</strong></small></a>'."\n";
} else {
print '<td>&nbsp;';
}
print '</td></tr>'."\n";
print '<tr><td colspan=2><small>';
print $rss->items[$y]['description'] .'</small></td></tr>'."\n";
}
print '</table>'."\n";
}
} else {
rss_display_feeds($id, $USER->id, '', $context);
rss_print_form($act, $url, $rssid, $preferredtitle, $shared, $id, $context);
}
print_footer();
?>
<?php //$Id$
// Print an error page condition
require_once('../../config.php');
$error = required_param('error', PARAM_CLEAN);
print_header(get_string('error'),
get_string('error'),
get_string('error') );
print clean_text(urldecode($error));
print_footer();
?>
<?php // $Id$ //
require_once($CFG->libdir .'/rsslib.php');
$id = optional_param('id', SITEID, PARAM_INT);
//create a page object for url_get_full()
$page = $this->page;
/// Print tabs at top
$currentaction = optional_param('currentaction', '', PARAM_ALPHA);
if (empty($currentaction) || $currentaction == 'configblock') {
$currentaction = 'configblock';
} else {
$currentaction = 'managefeeds';
}
include('config_instance_tabs.php');
if ($currentaction == 'configblock') {
print_box_start();
?>
<table cellpadding="9" cellspacing="0" class="blockconfigtable">
<tr valign="top">
<td class="label">
<?php print_string('displaydescriptionlabel', 'block_rss_client') ?>
</td>
<td class="value">
<?php
if(! isset($CFG->block_rss_client_display_description) ) {
$CFG->block_rss_client_display_description = '0';
}
$selected = $CFG->block_rss_client_display_description;
if (isset($this->config) && isset($this->config->display_description)) {
$selected = $this->config->display_description;
}
$options[0] = get_string('no');
$options[1] = get_string('yes');
choose_from_menu ($options, 'display_description', $selected);
?>
</td>
</tr>
<tr valign="top">
<td class="label">
<?php print_string('shownumentrieslabel', 'block_rss_client') ?>
</td>
<td class="value">
<input name="shownumentries" type="text" size="5" value="<?php
if(! isset($CFG->block_rss_client_num_entries) ) {
$CFG->block_rss_client_num_entries = '5';
}
$numentries = $CFG->block_rss_client_num_entries;
if (isset($this->config) && isset($this->config->shownumentries)) {
$numentries = intval($this->config->shownumentries);
}
p($numentries);
?>" />
</td>
</tr>
<tr valign="top">
<td class="label">
<?php print_string('choosefeedlabel', 'block_rss_client') ?>
</td>
<td class="value">
<?php
$selectedarray = array();
if (isset($this->config) && isset($this->config->rssid)) {
if (is_array($this->config->rssid)) {
// rssid is an array of rssids
$selectedarray = $this->config->rssid;
} else {
// rssid is a single rssid
$selectedarray = array($this->config->rssid);
}
}
global $USER;
if ($rssfeeds = $DB->get_records_select('block_rss_client', 'userid = ? OR shared = 1', array($USER->id))) {
foreach($rssfeeds as $rssfeed) {
if (!empty($rssfeed->preferredtitle)) {
$feedtitle = $rssfeed->preferredtitle;
} else {
$feedtitle = $rssfeed->title;
}
$checked = '';
if (in_array($rssfeed->id, $selectedarray)) {
$checked = 'checked="checked"';
}
$checkbox = '<input type="checkbox" name="rssid[]" id="rssid" value="'. $rssfeed->id .'" '. $checked .' />';
print $checkbox . $feedtitle .'<br />'."\n";
}
} else {
print_string('nofeeds', 'block_rss_client');
if (has_capability('block/rss_client:createprivatefeeds', $this->context)
|| has_capability('block/rss_client:createsharedfeeds', $this->context)) {
$addrsspage = $page->url->out(array('instanceid' => $this->instance->id,
'sesskey' => sesskey(),
'blockaction' => 'config',
'currentaction' => 'managefeeds',
'id' => $id));
print '&nbsp;<a href="'.$addrsspage.'">'. get_string('editnewsfeeds', 'block_rss_client') .'</a><br />';
}
}
?>
</td>
</tr>
<tr valign="top">
<td class="label"><?php print_string('uploadlabel'); ?></td>
<?php
$title = '';
if (!empty($this->config) && !empty($this->config->title)) {
$title = $this->config->title;
}
?>
<td class="value"><input type="text" name="title" size="30" value="<?php echo $title; ?>" />
</td>
</tr>
<tr valign="top">
<td class="label"><?php print_string('clientshowchannellinklabel', 'block_rss_client'); ?></td>
<td class="value">
<?php
if (isset($this->config) && isset($this->config->block_rss_client_show_channel_link)) {
$selected = $this->config->block_rss_client_show_channel_link;
} else {
$selected = '0';
}
$options = array ( '0' => get_string('no'),
'1' => get_string('yes') );
choose_from_menu ($options, 'block_rss_client_show_channel_link', $selected);
?>
</td>
</tr>
<tr valign="top">
<td class="label"><?php print_string('clientshowimagelabel', 'block_rss_client'); ?></td>
<td class="value">
<?php
if (isset($this->config) && isset($this->config->block_rss_client_show_channel_image)) {
$selected = $this->config->block_rss_client_show_channel_image;
} else {
$selected = '0';
}
$options = array ( '0' => get_string('no'),
'1' => get_string('yes') );
choose_from_menu ($options, 'block_rss_client_show_channel_image', $selected);
?>
</td>
</tr>
<tr>
<td colspan="2" class="submit">
<input type="submit" value="<?php print_string('savechanges') ?>" />
</td>
</tr>
</table>
<?php
print_box_end();
print "<!-- code came from " . __FILE__ . " at line " . __LINE__ . ". -->\n";
} else {
global $act, $url, $rssid, $preferredtitle, $shared;
print '</div></form></div>'; // Closes off page form
print_box_start();
rss_display_feeds($id, $USER->id, '', $context);
rss_print_form($act, $url, $rssid, $preferredtitle, $shared, $id, $this->context);
print_box_end();
}
?>
<?php // $Id$
/// This file to be included so we can assume config.php has already been included.
/// We also assume that $inactive, $activetab and $currentaction have been set
global $USER;
$tabs = $row = array();
if (has_capability('moodle/site:manageblocks', $this->context)) {
$script = $page->url->out(array('instanceid' => $this->instance->id, 'sesskey' => sesskey(), 'blockaction' => 'config', 'currentaction' => 'configblock', 'id' => $id, 'section' => 'rss'));
$row[] = new tabobject('configblock', $script,
get_string('configblock', 'block_rss_client'));
}