/** * 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 '<p>' . $feedrecord->url . ' Failed with code: ' . $feed->error() . '</p>'; } $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 .= '<div class="image" title="' . $imagetitle . '">' . "\n"; if ($imagelink) { $r .= '<a href="' . $imagelink . '">'; } $r .= '<img src="' . $image . '" alt="' . $imagetitle . '" />' . "\n"; if ($imagelink) { $r .= '</a>'; } $r .= '</div>'; } } if (empty($feedrecord->preferredtitle)) { $feedtitle = $this->format_title($feed->get_title()); } else { $feedtitle = $this->format_title($feedrecord->preferredtitle); } if ($showtitle) { $r .= '<div class="title">' . $feedtitle . '</div>'; } $r .= '<ul class="list no-overflow">' . "\n"; $feeditems = $feed->get_items(0, $maxentries); foreach ($feeditems as $item) { $r .= $this->get_item_html($item); } $r .= '</ul>'; if ($this->config->block_rss_client_show_channel_link) { $channellink = $feed->get_link(); if (!empty($channellink)) { //NOTE: this means the 'last feed' display wins the block title - but //this is exiting behaviour.. $this->content->footer = '<a href="' . htmlspecialchars(clean_param($channellink, PARAM_URL)) . '">' . get_string('clientchannellink', 'block_rss_client') . '</a>'; } } if (empty($this->config->title)) { //NOTE: this means the 'last feed' displayed wins the block title - but //this is exiting behaviour.. $this->title = strip_tags($feedtitle); } return $r; }
/** * 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 block_rss_client\output\feed|null The renderable feed or null of there is an error */ public function get_feed($feedrecord, $maxentries, $showtitle) { global $CFG; require_once $CFG->libdir . '/simplepie/moodle_simplepie.php'; $simplepiefeed = new moodle_simplepie($feedrecord->url); if (isset($CFG->block_rss_client_timeout)) { $simplepiefeed->set_cache_duration($CFG->block_rss_client_timeout * 60); } if ($simplepiefeed->error()) { debugging($feedrecord->url . ' Failed with code: ' . $simplepiefeed->error()); return null; } if (empty($feedrecord->preferredtitle)) { $feedtitle = $this->format_title($simplepiefeed->get_title()); } else { $feedtitle = $this->format_title($feedrecord->preferredtitle); } if (empty($this->config->title)) { //NOTE: this means the 'last feed' displayed wins the block title - but //this is exiting behaviour.. $this->title = strip_tags($feedtitle); } $feed = new \block_rss_client\output\feed($feedtitle, $showtitle, $this->config->block_rss_client_show_channel_image); if ($simplepieitems = $simplepiefeed->get_items(0, $maxentries)) { foreach ($simplepieitems as $simplepieitem) { try { $item = new \block_rss_client\output\item($simplepieitem->get_id(), new moodle_url($simplepieitem->get_link()), $simplepieitem->get_title(), $simplepieitem->get_description(), new moodle_url($simplepieitem->get_permalink()), $simplepieitem->get_date('U'), $this->config->display_description); $feed->add_item($item); } catch (moodle_exception $e) { // If there is an error with the RSS item, we don't // want to crash the page. Specifically, moodle_url can // throw an exception of the param is an extremely // malformed url. debugging($e->getMessage()); } } } // Feed image. if ($imageurl = $simplepiefeed->get_image_url()) { try { $image = new \block_rss_client\output\channel_image(new moodle_url($imageurl), $simplepiefeed->get_image_title(), new moodle_url($simplepiefeed->get_image_link())); $feed->set_image($image); } catch (moodle_exception $e) { // If there is an error with the RSS image, we don'twant to // crash the page. Specifically, moodle_url can throw an // exception if the param is an extremely malformed url. debugging($e->getMessage()); } } return $feed; }