. /** * A block which displays Remote feeds * * @package rss_client * @copyright Daryl Hawes * @license http://www.gnu.org/copyleft/gpl.html GNU GPL */ class block_rss_client extends block_base { function init() { $this->title = get_string('feedstitle', 'block_rss_client'); $this->version = 2009072901; $this->cron = 300; /// Set min time between cron executions to 300 secs (5 mins) } function preferred_width() { return 210; } function applicable_formats() { return array('all' => true, 'tag' => false); // Needs work to make it work on tags MDL-11960 } 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('remotenewsfeed', 'block_rss_client'); } } function get_content() { global $CFG, $DB; if ($this->content !== NULL) { return $this->content; } if (!$CFG->enablerssfeeds) { $this->content->text = ''; if ($this->page->user_is_editing()) { $this->content->text = get_string('disabledrssfeeds', 'block_rss_client'); } return $this->content; } // initalise block content object $this->content = new stdClass; $this->content->text = ''; $this->content->footer = ''; if (empty($this->instance)) { return $this->content; } if (!isset($this->config)) { // The block has yet to be configured - just display configure message in // the block if user has permission to configure it if (has_capability('block/rss_client:manageanyfeeds', $this->context)) { $this->content->text = get_string('feedsconfigurenewinstance2', 'block_rss_client'); } return $this->content; } // How many feed items should we display? $maxentries = 5; if ( !empty($this->config->shownumentries) ) { $maxentries = intval($this->config->shownumentries); }elseif( isset($CFG->block_rss_client_num_entries) ) { $maxentries = intval($CFG->block_rss_client_num_entries); } /* --------------------------------- * Begin Normal Display of Block Content * --------------------------------- */ $output = ''; if (!empty($this->config->rssid)) { list($rss_ids_sql, $params) = $DB->get_in_or_equal($this->config->rssid); $rss_feeds = $DB->get_records_select('block_rss_client', "id $rss_ids_sql", $params); $showtitle = false; if (count($rss_feeds) > 1) { // when many feeds show the title for each feed $showtitle = true; } foreach($rss_feeds as $feed){ $output.= $this->get_feed_html($feed, $maxentries, $showtitle); } } $this->content->text = $output; return $this->content; } function instance_allow_multiple() { return true; } function has_config() { return true; } function instance_allow_config() { return true; } /** * Returns the html of a feed to be displaed in the block * * @param mixed feedrecord The feed record from the database * @param int maxentries The maximum number of entries to be displayed * @param boolean showtitle Should the feed title be displayed in html * @return string html representing the rss feed content */ function get_feed_html($feedrecord, $maxentries, $showtitle){ global $CFG; require_once($CFG->libdir.'/simplepie/moodle_simplepie.php'); $feed = new moodle_simplepie($feedrecord->url); if(isset($CFG->block_rss_client_timeout)){ $feed->set_cache_duration($CFG->block_rss_client_timeout*60); } if(debugging() && $feed->error()){ return '
'. $feedrecord->url .' Failed with code: '.$feed->error().'
'; } $r = ''; // return string if($this->config->block_rss_client_show_channel_image){ if($image = $feed->get_image_url()){ $imagetitle = s($feed->get_image_title()); $imagelink = $feed->get_image_link(); $r.='