From: tjhunt Date: Wed, 29 Jul 2009 11:18:55 +0000 (+0000) Subject: block_rss_client: MDL-13932 Clean up a lot of the crap. X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=f1a34d8f05887f89f4af5eb4a83c1a1fa771a7af;p=moodle.git 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 --- diff --git a/blocks/rss_client/block_rss_client.php b/blocks/rss_client/block_rss_client.php index 3dfc24b5e3..36c454b53d 100644 --- a/blocks/rss_client/block_rss_client.php +++ b/blocks/rss_client/block_rss_client.php @@ -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 .= '
'. get_string('feedsaddedit', 'block_rss_client') .'
'; - } 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 .= '
'. get_string('feedsconfigurenewinstance', 'block_rss_client') .'
'; - } + 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 'Error loading a feed.
'; //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; diff --git a/blocks/rss_client/block_rss_client_action.php b/blocks/rss_client/block_rss_client_action.php deleted file mode 100644 index 2f05664cd8..0000000000 --- a/blocks/rss_client/block_rss_client_action.php +++ /dev/null @@ -1,289 +0,0 @@ -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 .= '
'. 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 .= '

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 .= '
'. 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 ''. get_string('couldnotfindfeed', 'block_rss_client') .': '. $rssid .''; - } 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 ''."\n"; - print ''."\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'] = '>>'; - } - - print ''."\n"; - if (file_exists($CFG->dirroot .'/blog/lib.php')) { - //Blog module is installed - provide "blog this" link - print ''."\n"; - print ''."\n"; - } - print '
'. $feedtitle .'
'."\n"; - print ''. $rss->items[$y]['title']; - print ''."\n"; - print ''."\n"; - - /// MDL-9291, blog this feature needs further discussion/implementation - /// temporarily disabling for now. - - // print ''. get_string('blogthis', 'blog').''."\n"; - //print ''. get_string('blogthis', 'blog') .''."\n"; - } else { - print ' '; - } - print '
'; - print $rss->items[$y]['description'] .'
'."\n"; - } -} else { - rss_display_feeds($id, $USER->id, '', $context); - rss_print_form($act, $url, $rssid, $preferredtitle, $shared, $id, $context); -} -print_footer(); -?> diff --git a/blocks/rss_client/block_rss_client_error.php b/blocks/rss_client/block_rss_client_error.php deleted file mode 100644 index 14ab2aae27..0000000000 --- a/blocks/rss_client/block_rss_client_error.php +++ /dev/null @@ -1,14 +0,0 @@ - diff --git a/blocks/rss_client/config_instance.html b/blocks/rss_client/config_instance.html deleted file mode 100644 index 9de7b3b3de..0000000000 --- a/blocks/rss_client/config_instance.html +++ /dev/null @@ -1,177 +0,0 @@ -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(); -?> - - - - - - - - - - - - - - - - - - - config) && !empty($this->config->title)) { - $title = $this->config->title; - } - ?> - - - - - - - - - - - - - - - - -
- - - 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); - ?> -
- - - -
- - - 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 = ''; - print $checkbox . $feedtitle .'
'."\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 ' '. get_string('editnewsfeeds', 'block_rss_client') .'
'; - } - } - ?> -
-
- 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); - ?> -
- 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); - ?> -
- -
-\n"; -} else { - global $act, $url, $rssid, $preferredtitle, $shared; - print ''; // 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(); -} -?> diff --git a/blocks/rss_client/config_instance_tabs.php b/blocks/rss_client/config_instance_tabs.php deleted file mode 100644 index a7ac1d45d8..0000000000 --- a/blocks/rss_client/config_instance_tabs.php +++ /dev/null @@ -1,24 +0,0 @@ -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')); -} - -$script = $page->url->out(array('instanceid' => $this->instance->id, 'sesskey' => sesskey(), 'blockaction' => 'config', 'currentaction' => 'managefeeds', 'id' => $id, 'section' => 'rss')); -$row[] = new tabobject('managefeeds', $script, - get_string('managefeeds', 'block_rss_client')); - -$tabs[] = $row; - -/// Print out the tabs and continue! -print "\n".'
'."\n"; -print_tabs($tabs, $currentaction); -print '
' . print_location_comment(__FILE__, __LINE__, true); -?> diff --git a/blocks/rss_client/db/access.php b/blocks/rss_client/db/access.php index 9f77fdf38f..ab3905fa13 100644 --- a/blocks/rss_client/db/access.php +++ b/blocks/rss_client/db/access.php @@ -33,29 +33,6 @@ $block_rss_client_capabilities = array( - 'block/rss_client:createprivatefeeds' => array( - - 'captype' => 'write', - 'contextlevel' => CONTEXT_BLOCK, - 'legacy' => array( - 'teacher' => CAP_ALLOW, - 'editingteacher' => CAP_ALLOW, - 'admin' => CAP_ALLOW - ) - ), - - 'block/rss_client:createsharedfeeds' => array( - - 'riskbitmask' => RISK_SPAM, - - 'captype' => 'write', - 'contextlevel' => CONTEXT_BLOCK, - 'legacy' => array( - 'editingteacher' => CAP_ALLOW, - 'admin' => CAP_ALLOW - ) - ), - 'block/rss_client:manageownfeeds' => array( 'captype' => 'write', diff --git a/blocks/rss_client/edit_form.php b/blocks/rss_client/edit_form.php new file mode 100644 index 0000000000..2a605eb066 --- /dev/null +++ b/blocks/rss_client/edit_form.php @@ -0,0 +1,80 @@ +. + +/** + * Form for editing RSS client block instances. + * + * @package moodlecore + * @copyright 2009 Tim Hunt + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +/** + * Form for editing RSS client block instances. + * + * @copyright 2009 Tim Hunt + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class block_rss_client_edit_form extends block_edit_form { + protected function specific_definition($mform) { + global $CFG, $DB, $USER; + + // Fields for editing HTML block title and contents. + $mform->addElement('header', 'configheader', get_string('blocksettings', 'block')); + + $mform->addElement('selectyesno', 'config_display_description', get_string('displaydescriptionlabel', 'block_rss_client')); + $mform->setDefault('config_display_description', 0); + + $mform->addElement('text', 'config_shownumentries', get_string('shownumentrieslabel', 'block_rss_client'), array('size' => 5)); + $mform->setType('config_text', PARAM_INTEGER); + if (!empty($CFG->block_rss_client_num_entries)) { + $mform->setDefault('config_text', $CFG->block_rss_client_num_entries); + } else { + $mform->setDefault('config_text', 5); + } + + $rssfeeds = $DB->get_records_sql_menu(' + SELECT id, CASE WHEN preferredtitle = ? THEN title ELSE preferredtitle END AS acutaltitle + FROM {block_rss_client} + WHERE userid = ? OR shared = 1 + ORDER BY acutaltitle', + array($DB->sql_empty(), $USER->id)); + if ($rssfeeds) { + $select = $mform->addElement('select', 'config_rssid', get_string('choosefeedlabel', 'block_rss_client'), $rssfeeds); + $select->setMultiple(true); + + } else { + $mform->addElement('static', 'config_rssid', get_string('choosefeedlabel', 'block_rss_client'), + get_string('nofeeds', 'block_rss_client')); + } + + if (has_any_capability(array('block/rss_client:manageanyfeeds', 'block/rss_client:manageownfeeds'), $this->block->context)) { + $mform->addElement('static', 'nofeedmessage', '', + '' . + get_string('feedsaddedit', 'block_rss_client') . ''); + } + + $mform->addElement('selectyesno', 'config_title', get_string('uploadlabel')); + $mform->setType('config_title', PARAM_NOTAGS); + + $mform->addElement('selectyesno', 'config_block_rss_client_show_channel_link', get_string('clientshowchannellinklabel', 'block_rss_client')); + $mform->setDefault('config_block_rss_client_show_channel_link', 0); + + $mform->addElement('selectyesno', 'config_block_rss_client_show_channel_image', get_string('clientshowimagelabel', 'block_rss_client')); + $mform->setDefault('config_block_rss_client_show_channel_image', 0); + } +} \ No newline at end of file diff --git a/blocks/rss_client/editfeed.php b/blocks/rss_client/editfeed.php new file mode 100644 index 0000000000..9341974c25 --- /dev/null +++ b/blocks/rss_client/editfeed.php @@ -0,0 +1,197 @@ +. + +/** + * Script to let a user edit the properties of a particular RSS feed. + * + * @package moodlecore + * @copyright 2009 Tim Hunt + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + + +require_once(dirname(__FILE__) . '/../../config.php'); +require_once($CFG->libdir . '/formslib.php'); +require_once($CFG->libdir .'/rsslib.php'); +require_once(MAGPIE_DIR .'rss_fetch.inc'); + +class feed_edit_form extends moodleform { + protected $isadding; + protected $caneditshared; + protected $title = ''; + protected $description = ''; + + function __construct($actionurl, $isadding, $caneditshared) { + $this->isadding = $isadding; + $this->caneditshared = $caneditshared; + parent::moodleform($actionurl); + } + + function definition() { + $mform =& $this->_form; + + // Then show the fields about where this block appears. + $mform->addElement('header', 'header', get_string('feed', 'block_rss_client')); + + $mform->addElement('text', 'url', get_string('feedurl', 'block_rss_client'), array('size' => 60)); + $mform->setType('url', PARAM_URL); + $mform->addRule('url', null, 'required'); + + $validatejs = "window.open('http://feedvalidator.org/check.cgi?url='+" . + "getElementById('id_url').value, 'validate', " . + "'width=640,height=480,scrollbars=yes,status=yes,resizable=yes'); return true;"; + $validatelink = '' . get_string('validatefeed', 'block_rss_client') . ''; + $mform->addElement('static', 'validatelink', '', $validatelink); + + $mform->addElement('text', 'preferredtitle', get_string('customtitlelabel', 'block_rss_client'), array('size' => 60)); + $mform->setType('preferredtitle', PARAM_NOTAGS); + + if ($this->caneditshared) { + $mform->addElement('selectyesno', 'shared', get_string('sharedfeed', 'block_rss_client')); + $mform->setDefault('shared', 0); + } + + $submitlabal = null; // Default + if ($this->isadding) { + $submitlabal = get_string('addnewfeed', 'block_rss_client'); + } + $this->add_action_buttons(true, $submitlabal); + } + + function validation($data, $files) { + $errors = parent::validation($data, $files); + + ob_start(); + $rss = fetch_rss($data['url']); + $rsserrors = ob_get_clean(); + + if (!$rss) { + $errors['url'] = get_string('errorloadingfeed', 'block_rss_client', $rsserrors); + } else { + $this->title = $rss->channel['title']; + $this->description = $rss->channel['description']; + } + + return $errors; + } + + function get_data() { + $data = parent::get_data(); + if ($data) { + $data->title = $this->title; + $data->description = $this->description; + } + return $data; + } +} + +$returnurl = optional_param('returnurl', '', PARAM_LOCALURL); +$courseid = optional_param('courseid', 0, PARAM_INTEGER); +$rssid = optional_param('rssid', 0, PARAM_INTEGER); // 0 mean create new. + +if ($courseid = SITEID) { + $courseid = 0; +} +if ($courseid) { + $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST); + $PAGE->set_course($course); + $context = $PAGE->context; +} else { + $context = get_context_instance(CONTEXT_SYSTEM); + $PAGE->set_context($context); +} + +$managesharedfeeds = has_capability('block/rss_client:manageanyfeeds', $context); +if (!$managesharedfeeds) { + require_capability('block/rss_client:manageownfeeds', $context); +} + +$urlparams = array('rssid' => $rssid); +$manageparams = array(); +if ($courseid) { + $urlparams['courseid'] = $courseid; + $manageparams[] = 'courseid=$courseid'; +} +if ($returnurl) { + $urlparams['returnurl'] = $returnurl; + $manageparams = 'returnurl=' . $returnurl; +} +if ($manageparams) { + $manageparams = '?' . implode('&', $manageparams); +} else { + $manageparams = ''; +} +$PAGE->set_url('blocks/rss_client/editfeed.php', $urlparams); +$PAGE->set_generaltype('form'); + +if ($rssid) { + $isadding = false; + $rssrecord = $DB->get_record('block_rss_client', array('id' => $rssid), '*', MUST_EXIST); +} else { + $isadding = true; + $rssrecord = new stdClass; +} + +$mform = new feed_edit_form($PAGE->url, $isadding, $managesharedfeeds); +$mform->set_data($rssrecord); + +if ($mform->is_cancelled()) { + redirect($CFG->wwwroot . '/blocks/rss_client/managefeeds.php' . $manageparams); + +} else if ($data = $mform->get_data()) { + $data->userid = $USER->id; + if (!$managesharedfeeds) { + $data->shared = 0; + } + + if ($isadding) { + $DB->insert_record('block_rss_client', $data); + } else { + $data->id = $rssid; + $DB->update_record('block_rss_client', $data); + } + + redirect($CFG->wwwroot . '/blocks/rss_client/managefeeds.php' . $manageparams); + +} else { + if ($isadding) { + $strtitle = get_string('addnewfeed', 'block_rss_client'); + } else { + $strtitle = get_string('editafeed', 'block_rss_client'); + } + + $PAGE->set_title($strtitle); + $PAGE->set_heading($strtitle); + + $navlinks = array( + array('name' => get_string('administration'), 'link' => "$CFG->wwwroot/$CFG->admin/index.php", 'type' => 'misc'), + array('name' => get_string('managemodules'), 'link' => null, 'type' => 'misc'), + array('name' => get_string('blocks'), 'link' => null, 'type' => 'misc'), + array('name' => get_string('feedstitle', 'block_rss_client'), 'link' => "$CFG->wwwroot/$CFG->admin/settings.php?section=blocksettingrss_client", 'type' => 'misc'), + array('name' => get_string('managefeeds', 'block_rss_client'), 'link' => $CFG->wwwroot . '/blocks/rss_client/managefeeds.php' . $manageparams, 'type' => 'misc'), + array('name' => $strtitle, 'link' => null, 'type' => 'misc'), + ); + $navigation = build_navigation($navlinks); + + echo $OUTPUT->header($navigation); + echo $OUTPUT->heading($strtitle, 2); + + $mform->display(); + + echo $OUTPUT->footer(); +} + diff --git a/blocks/rss_client/managefeeds.php b/blocks/rss_client/managefeeds.php new file mode 100644 index 0000000000..9cad064665 --- /dev/null +++ b/blocks/rss_client/managefeeds.php @@ -0,0 +1,162 @@ +. + +/** + * Script to let a user manage their RSS feeds. + * + * @package moodlecore + * @copyright 2009 Tim Hunt + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require_once(dirname(__FILE__) . '/../../config.php'); +require_once($CFG->libdir . '/tablelib.php'); + +require_login(); + +$returnurl = optional_param('returnurl', '', PARAM_LOCALURL); +$courseid = optional_param('courseid', 0, PARAM_INTEGER); +$deleterssid = optional_param('deleterssid', 0, PARAM_INTEGER); + +if ($courseid == SITEID) { + $courseid = 0; +} +if ($courseid) { + $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST); + $PAGE->set_course($course); + $context = $PAGE->context; +} else { + $context = get_context_instance(CONTEXT_SYSTEM); + $PAGE->set_context($context); +} + +$managesharedfeeds = has_capability('block/rss_client:manageanyfeeds', $context); +if (!$managesharedfeeds) { + require_capability('block/rss_client:manageownfeeds', $context); +} + +$urlparams = array(); +$extraparams = ''; +if ($courseid) { + $urlparams['courseid'] = $courseid; + $extraparams = '&courseid=' . $courseid; +} +if ($returnurl) { + $urlparams['returnurl'] = $returnurl; + $extraparams = '&returnurl=' . $returnurl; +} +$PAGE->set_url('blocks/rss_client/managefeeds.php', $urlparams); + +// Process any actions +if ($deleterssid && confirm_sesskey()) { + $DB->delete_records('block_rss_client', array('id'=>$rssid)); + + $file = $CFG->dataroot . '/cache/rsscache/' . $rssid .'.xml'; + if (file_exists($file)) { + unlink($file); + } + + redirect($PAGE->url, get_string('feeddeleted', 'block_rss_client')); +} + +// Display the list of feeds. +if ($managesharedfeeds) { + $select = '(userid = ' . $USER->id . ' OR shared = 1)'; +} else { + $select = 'userid = ' . $USER->id; +} +$feeds = $DB->get_records_select('block_rss_client', $select, null, $DB->sql_order_by_text('title')); + +$strmanage = get_string('managefeeds', 'block_rss_client'); + +$PAGE->set_generaltype('form'); +$PAGE->set_title($strmanage); +$PAGE->set_heading($strmanage); + +$navlinks = array( + array('name' => get_string('administration'), 'link' => "$CFG->wwwroot/$CFG->admin/index.php", 'type' => 'misc'), + array('name' => get_string('managemodules'), 'link' => null, 'type' => 'misc'), + array('name' => get_string('blocks'), 'link' => null, 'type' => 'misc'), + array('name' => get_string('feedstitle', 'block_rss_client'), 'link' => "$CFG->wwwroot/$CFG->admin/settings.php?section=blocksettingrss_client", 'type' => 'misc'), + array('name' => get_string('managefeeds', 'block_rss_client'), 'link' => null, 'type' => 'misc'), +); +$navigation = build_navigation($navlinks); + +echo $OUTPUT->header($navigation); + +$table = new flexible_table('rss-display-feeds'); + +$table->define_columns(array('feed', 'actions')); +$table->define_headers(array(get_string('feed', 'block_rss_client'), get_string('actions', 'moodle'))); + +$table->set_attribute('cellspacing', '0'); +$table->set_attribute('id', 'rssfeeds'); +$table->set_attribute('class', 'generaltable generalbox'); +$table->column_class('feed', 'feed'); +$table->column_class('actions', 'actions'); + +$table->setup(); + +foreach($feeds as $feed) { + if (!empty($feed->preferredtitle)) { + $feedtitle = $feed->preferredtitle; + } else { + $feedtitle = $feed->title; + } + + $viewlink = new html_link(); + $viewlink->url = $CFG->wwwroot .'/blocks/rss_client/viewfeed.php?rssid=' . $feed->id . $extraparams; + $viewlink->text = $feedtitle; + + $feedinfo = '
' . $OUTPUT->link($viewlink) . '
' . + '
' . $OUTPUT->link($feed->url, $feed->url) .'
' . + '
' . $feed->description . '
'; + + $editaction = new action_icon(); + $editaction->link->url = $CFG->wwwroot .'/blocks/rss_client/editfeed.php?rssid=' . $feed->id . $extraparams; + $editaction->link->title = get_string('edit'); + $editaction->image->src = $OUTPUT->old_icon_url('t/edit'); + $editaction->image->alt = get_string('edit'); + + $deleteaction = new action_icon(); + $deleteaction->link->url = $CFG->wwwroot .'/blocks/rss_client/managefeeds.php?deleterssid=' . $feed->id . + '&sesskey=' . sesskey() . $extraparams; + $deleteaction->link->title = get_string('delete'); + $deleteaction->image->src = $OUTPUT->old_icon_url('t/delete'); + $deleteaction->image->alt = get_string('delete'); + $deleteaction->link->add_action('click', 'confirm_dialog', array(get_string('deletefeedconfirm', 'block_rss_client'))); + + $feedicons = $OUTPUT->action_icon($editaction) . ' ' . $OUTPUT->action_icon($deleteaction); + + $table->add_data(array($feedinfo, $feedicons)); +} + +$table->print_html(); + +$button = new html_form(); +$button->method = 'get'; +$button->url = $CFG->wwwroot . '/blocks/rss_client/editfeed.php?' . substr($extraparams, 1); +$button->showbutton = true; +$button->button->text = get_string('addnewfeed', 'block_rss_client'); +echo '
' . $OUTPUT->button($button) . '
'; + + +if ($returnurl) { + echo ''; +} + +echo $OUTPUT->footer(); diff --git a/blocks/rss_client/settings.php b/blocks/rss_client/settings.php index f18c8c232f..313f0b6117 100644 --- a/blocks/rss_client/settings.php +++ b/blocks/rss_client/settings.php @@ -14,8 +14,5 @@ $options = array (SUBMITTERS_ALL_ACCOUNT_HOLDERS => get_string('everybody'), $settings->add(new admin_setting_configselect('block_rss_client_submitters', get_string('submitters2', 'block_rss_client'), get_string('submitters', 'block_rss_client'), SUBMITTERS_ADMIN_ONLY, $options)); -$link =''.get_string('feedsaddedit', 'block_rss_client').''; +$link =''.get_string('feedsaddedit', 'block_rss_client').''; $settings->add(new admin_setting_heading('block_rss_addheading', '', $link)); - - -?> diff --git a/blocks/rss_client/viewfeed.php b/blocks/rss_client/viewfeed.php new file mode 100644 index 0000000000..a0085e6a15 --- /dev/null +++ b/blocks/rss_client/viewfeed.php @@ -0,0 +1,124 @@ +. + +/** + * Script to let a user edit the properties of a particular RSS feed. + * + * @package moodlecore + * @copyright 2009 Tim Hunt + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require_once(dirname(__FILE__) . '/../../config.php'); +require_once($CFG->libdir .'/rsslib.php'); +require_once(MAGPIE_DIR .'rss_fetch.inc'); + +require_login(); +if (isguest()) { + echo_error('guestsarenotallowed'); +} + +$returnurl = optional_param('returnurl', '', PARAM_LOCALURL); +$courseid = optional_param('courseid', 0, PARAM_INTEGER); +$rssid = required_param('rssid', PARAM_INTEGER); + +if ($courseid = SITEID) { + $courseid = 0; +} +if ($courseid) { + $course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST); + $PAGE->set_course($course); + $context = $PAGE->context; +} else { + $context = get_context_instance(CONTEXT_SYSTEM); + $PAGE->set_context($context); +} + +$urlparams = array('rssid' => $rssid); +$manageparams = array(); +if ($courseid) { + $urlparams['courseid'] = $courseid; + $manageparams[] = 'courseid=$courseid'; +} +if ($returnurl) { + $urlparams['returnurl'] = $returnurl; + $manageparams = 'returnurl=' . $returnurl; +} +if ($manageparams) { + $manageparams = '?' . implode('&', $manageparams); +} else { + $manageparams = ''; +} +$PAGE->set_url('blocks/rss_client/viewfeed.php', $urlparams); +$PAGE->set_generaltype('popup'); + +$rssrecord = $DB->get_record('block_rss_client', array('id' => $rssid), '*', MUST_EXIST); + +ob_start(); +$rss = fetch_rss($rssrecord->url); +$rsserrors = ob_get_clean(); + +if (!$rss) { + debugging($rsserrors); + print_error('errorfetchingrssfeed'); +} + +$strviewfeed = get_string('viewfeed', 'block_rss_client'); + +$PAGE->set_title($strviewfeed); +$PAGE->set_heading($strviewfeed); + +$navlinks = array( + array('name' => get_string('administration'), 'link' => "$CFG->wwwroot/$CFG->admin/index.php", 'type' => 'misc'), + array('name' => get_string('managemodules'), 'link' => null, 'type' => 'misc'), + array('name' => get_string('blocks'), 'link' => null, 'type' => 'misc'), + array('name' => get_string('feedstitle', 'block_rss_client'), 'link' => "$CFG->wwwroot/$CFG->admin/settings.php?section=blocksettingrss_client", 'type' => 'misc'), + array('name' => get_string('managefeeds', 'block_rss_client'), 'link' => $CFG->wwwroot . '/blocks/rss_client/managefeeds.php' . $manageparams, 'type' => 'misc'), + array('name' => $strviewfeed, 'link' => null, 'type' => 'misc'), +); +$navigation = build_navigation($navlinks); + +echo $OUTPUT->header($navigation); + + +if (!empty($rssrecord->preferredtitle)) { + $feedtitle = $rssrecord->preferredtitle; +} else { + $feedtitle = $rss->channel['title']; +} +echo ''."\n"; +echo ''."\n"; +foreach ($rss->items as $item) { + if ($item['link'] == '') { + $item['link'] = $item['guid']; + } + + if ($item['title'] == '') { + $item['title'] = '>>'; + } + + echo ''."\n"; + echo ''."\n"; + echo ''."\n"; +} +echo '
'. $feedtitle .'
'."\n"; + echo ''. $item['title']; + echo ''."\n"; + echo '
'; + echo $item['description'] .'
'."\n"; + +echo $OUTPUT->footer(); diff --git a/lang/en_utf8/block_rss_client.php b/lang/en_utf8/block_rss_client.php index 280fd944cc..2836fc10ae 100644 --- a/lang/en_utf8/block_rss_client.php +++ b/lang/en_utf8/block_rss_client.php @@ -5,6 +5,7 @@ $string['addfeed'] = 'Add a news feed URL:'; $string['addheadlineblock'] = 'Add RSS headline block'; $string['addnew'] = 'Add New'; +$string['addnewfeed'] = 'Add a new feed'; $string['blockname'] = 'RSS Client'; $string['cannotmakemodification'] = 'You are not allowed to make modifications to this RSS feed at this time.'; $string['choosefeedlabel'] = 'Choose the feeds which you would like to make available in this block:'; @@ -17,17 +18,21 @@ $string['couldnotfindfeed'] = 'Could not find feed with id'; $string['customtitlelabel'] = 'Custom title (leave blank to use title supplied by feed):'; $string['deletefeedconfirm'] = 'Are you sure you want to delete this feed?'; $string['displaydescriptionlabel'] = 'Display each link\'s description?'; +$string['editafeed'] = 'Edit a feed'; $string['editfeeds'] = 'Edit, subscribe or unsubsribe from RSS/Atom news feeds'; $string['editnewsfeeds'] = 'Edit news feeds'; $string['editrssblock'] = 'Edit RSS Headline Block'; +$string['errorloadingfeed'] = 'Error loading this RSS feed ($a)'; $string['feed'] = 'Feed'; $string['feedadded'] = 'News feed added'; $string['feeddeleted'] = 'News feed deleted'; $string['feeds'] = 'News Feeds'; $string['feedsaddedit'] = 'Add/Edit Feeds'; $string['feedsconfigurenewinstance'] = 'Click here to configure this block to display RSS feeds.'; +$string['feedsconfigurenewinstance2'] = 'Click the edit icon above to configure this block to display RSS feeds.'; $string['feedstitle'] = 'Remote RSS Feeds'; $string['feedupdated'] = 'News feed updated'; +$string['feedurl'] = 'Feed URL'; $string['findmorefeeds'] = 'Find more rss feeds'; $string['managefeeds'] = 'Manage all my feeds'; $string['numentries'] = 'Entries per feed'; @@ -48,5 +53,6 @@ $string['timeout2'] = 'Timeout'; $string['timeoutdesc'] = 'Time in minutes for an RSS feed to live in cache.'; $string['updatefeed'] = 'Update a news feed URL:'; $string['validatefeed'] = 'Validate feed'; +$string['viewfeed'] = 'View feed'; ?> diff --git a/lang/en_utf8/error.php b/lang/en_utf8/error.php index f025424d72..b2d842d600 100644 --- a/lang/en_utf8/error.php +++ b/lang/en_utf8/error.php @@ -193,6 +193,7 @@ $string['errorcopyingfiles'] = 'Error copying files'; $string['errorcreatingdirectory'] = 'Error creating directory \"$a\"'; $string['errorcreatingfile'] = 'Error creating file \"$a\"'; $string['errorcreatingrole'] = 'Error creating role'; +$string['errorfetchingrssfeed'] = 'Error fetching RSS feed.'; $string['erroronline'] = 'Error on line $a'; $string['errorreadingfile'] = 'Error reading file \"$a\"'; $string['errorsavingrequest'] = 'An error occurred when trying to save your request.'; diff --git a/lib/rsslib.php b/lib/rsslib.php index 852c87101d..8263ff5340 100644 --- a/lib/rsslib.php +++ b/lib/rsslib.php @@ -23,6 +23,10 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +if (!defined('MAGPIE_OUTPUT_ENCODING')) { + define('MAGPIE_OUTPUT_ENCODING', 'utf-8'); // see bug 3107 +} + /** * This function returns the icon (from theme) with the link to rss/file.php * @@ -404,188 +408,6 @@ define('SUBMITTERS_ALL_ACCOUNT_HOLDERS', 0); define('SUBMITTERS_ADMIN_ONLY', 1); define('SUBMITTERS_ADMIN_AND_TEACHER', 2); -/** - * @param int $courseid The id of the course the user is currently viewing - * @param int $userid We need this to know which feeds the user is allowed to manage - * @param int $rssid If present the rss entry matching this id alone will be displayed - * as long as the user is allowed to manage this feed - * @param object $context we need the context object to check what the user is allowed to do. - */ -function rss_display_feeds($courseid, $userid, $rssid='', $context) { - global $USER, $CFG, $DB, $OUTPUT; - global $blogid; //hackish, but if there is a blogid it would be good to preserve it - - require_once($CFG->libdir.'/tablelib.php'); - - $select = ''; - $managesharedfeeds = has_capability('block/rss_client:manageanyfeeds', $context); - $manageownfeeds = has_capability('block/rss_client:manageownfeeds', $context); - - if ($rssid != '') { - $select = 'id = '.$rssid.' AND '; - } - if ($managesharedfeeds) { - $select .= '(userid = '.$userid.' OR shared = 1)'; - } else if ($manageownfeeds) { - $select .= 'userid = '.$userid; - } - - $table = new flexible_table('rss-display-feeds'); - - $table->define_columns(array('feed', 'actions')); - $table->define_headers(array(get_string('feed', 'block_rss_client'), get_string('actions', 'moodle'))); - - $table->set_attribute('cellspacing', '0'); - $table->set_attribute('id', 'rssfeeds'); - $table->set_attribute('class', 'generaltable generalbox'); - $table->column_class('feed', 'feed'); - $table->column_class('actions', 'actions'); - - $table->setup(); - - $feeds = $DB->get_records_select('block_rss_client', $select, null, $DB->sql_order_by_text('title')); - - if(!empty($feeds)) { - foreach($feeds as $feed) { - - if (!empty($feed->preferredtitle)) { - $feedtitle = $feed->preferredtitle; - } else { - $feedtitle = $feed->title; - } - - if ( ($feed->userid == $USER->id && $manageownfeeds) - || ($feed->shared && $managesharedfeeds) ) { - - $feedicons = ''. - ''. get_string('edit').' '. - - ''. - ''. get_string('delete').''; - } - else { - $feedicons = ''; - } - - $feedinfo = ' -
- - '. $feedtitle .' -
-
- '. $feed->url .' -
-
'.$feed->description.'
'; - - $table->add_data(array($feedinfo, $feedicons)); - } - } - - $table->print_html(); -} - - -/** - * Wrapper function for rss_get_form - */ -function rss_print_form($act='none', $url='', $rssid='', $preferredtitle='', $shared=0, $courseid='', $context) { - print rss_get_form($act, $url, $rssid, $preferredtitle, $shared, $courseid, $context); -} - - -/** - * Prints or returns a form for managing rss feed entries. - * - * @global object - * @global object - * @global int - * @global string - * @param string $act The current action. If "rssedit" then and "update" button is used, otherwise "add" is used. - * @param string $url The url of the feed that is being updated or NULL - * @param int $rssid The dataabse id of the feed that is being updated or NULL - * @param string $preferredtitle The preferred title to display for this feed - * @param int $shared Whether this feed is to be shared or not - * @param int $courseid The id of the course that is currently being viewed if applicable - * @param object $context The context that we will use to check for permissions - * @return string Either the form is printed directly and nothing is returned or the form is returned as a string - */ -function rss_get_form($act='none', $url='', $rssid='', $preferredtitle='', $shared=0, $courseid='', $context) { - global $USER, $CFG, $blockid, $blockaction; - global $blogid; //hackish, but if there is a blogid it would be good to preserve it - - $stredit = get_string('edit'); - $stradd = get_string('add'); - $strupdatefeed = get_string('updatefeed', 'block_rss_client'); - $straddfeed = get_string('addfeed', 'block_rss_client'); - - $returnstring = ''; - - $returnstring .= '
'."\n"; - print_location_comment(__FILE__,__LINE__); - $returnstring .= '
'."\n"; - if ($act == 'rssedit') { - $returnstring .= $strupdatefeed; - } else { - $returnstring .= $straddfeed; - } - - $returnstring .= "\n".'
'."\n"; - $returnstring .= '
'. get_string('customtitlelabel', 'block_rss_client'); - $returnstring .= '
'."\n"; - - if (has_capability('block/rss_client:createsharedfeeds', $context)) { - $returnstring .= '
'. "\n"; - } - - $returnstring .= ''."\n"; - $returnstring .= ''."\n"; - $returnstring .= ''."\n"; - $returnstring .= '
". get_string('validatefeed', 'block_rss_client').""; - - if ($act == 'rssedit') { - $returnstring .= $stredit; - } else { - $returnstring .= $stradd; - } - - $returnstring .= '" /> '. $validatestring ."\n"; - // $returnstring .= '
'."\n"; // Avoiding nested forms... Ugly temporary hack #8922 - return $returnstring . print_location_comment(__FILE__, __LINE__, true); -} - - /** * Adds RSS Media Enclosures for "podcasting" by examining links to media files, * and attachments which are media files. Please note that the RSS that is