function validation($data, $files) { $errors = parent::validation($data, $files); $rss = new moodle_simplepie(); // set timeout for longer than normal to try and grab the feed $rss->set_timeout(10); $rss->set_feed_url($data['url']); $rss->set_autodiscovery_cache_duration(0); $rss->set_autodiscovery_level(SIMPLEPIE_LOCATOR_NONE); $rss->init(); if ($rss->error()) { $errors['url'] = get_string('errorloadingfeed', 'block_rss_client', $rss->error()); } else { $this->title = $rss->get_title(); $this->description = $rss->get_description(); } return $errors; }
public function definition_after_data() { global $CFG, $COURSE; $mform =& $this->_form; $name = trim($mform->getElementValue('name')); $description = trim($mform->getElementValue('description')); $url = $mform->getElementValue('url'); if (empty($name) || empty($description)) { $rss = new moodle_simplepie($url); if (empty($name) && $rss->get_title()) { $mform->setDefault('name', $rss->get_title()); } if (empty($description) && $rss->get_description()) { $mform->setDefault('description', $rss->get_description()); } } if ($id = $mform->getElementValue('id')) { $mform->setDefault('autotags', implode(',', tag_get_tags_array('blog_external', $id))); $mform->freeze('url'); $mform->freeze('filtertags'); // TODO change the filtertags element to a multiple select, using the tags of the external blog // Use $rss->get_channel_tags() } }
/** * 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; }
function test_redirect() { global $CFG; $feed = new moodle_simplepie(moodlesimplepie_test::REDIRECTURL); $this->assertFalse($feed->error()); $this->assertEqual($feed->get_title(), 'Moodle News'); $this->assertEqual($feed->get_link(), 'http://moodle.org/mod/forum/view.php?f=1'); }
debugging($rss->error()); print_error('errorfetchingrssfeed'); } $strviewfeed = get_string('viewfeed', 'block_rss_client'); $PAGE->set_title($strviewfeed); $PAGE->set_heading($strviewfeed); $managefeeds = new moodle_url('/blocks/rss_client/managefeeds.php', $urlparams); $PAGE->navbar->add(get_string('blocks')); $PAGE->navbar->add(get_string('pluginname', 'block_rss_client')); $PAGE->navbar->add(get_string('managefeeds', 'block_rss_client'), $managefeeds); $PAGE->navbar->add($strviewfeed); echo $OUTPUT->header(); if (!empty($rssrecord->preferredtitle)) { $feedtitle = $rssrecord->preferredtitle; } else { $feedtitle = $rss->get_title(); } echo '<table align="center" width="50%" cellspacing="1">' . "\n"; echo '<tr><td colspan="2"><strong>' . s($feedtitle) . '</strong></td></tr>' . "\n"; foreach ($rss->get_items() as $item) { echo '<tr><td valign="middle">' . "\n"; echo '<a href="' . $item->get_link() . '" target="_blank"><strong>'; echo s($item->get_title()); echo '</strong></a>' . "\n"; echo '</td>' . "\n"; echo '</tr>' . "\n"; echo '<tr><td colspan="2"><small>'; echo format_text($item->get_description(), FORMAT_HTML) . '</small></td></tr>' . "\n"; } echo '</table>' . "\n"; echo $OUTPUT->footer();
$newexternal->url = $data->url; $newexternal->filtertags = !empty($data->filtertags) ? $data->filtertags : null; $newexternal->timemodified = time(); $newexternal->id = $DB->insert_record('blog_external', $newexternal); core_tag_tag::set_item_tags('core', 'blog_external', $newexternal->id, context_user::instance($newexternal->userid), $data->autotags); blog_sync_external_entries($newexternal); // Log this action. $eventparms = array('context' => $context, 'objectid' => $newexternal->id, 'other' => array('url' => $newexternal->url)); $event = \core\event\blog_external_added::create($eventparms); $event->trigger(); break; case 'edit': if ($data->id && $DB->record_exists('blog_external', array('id' => $data->id))) { $rss = new moodle_simplepie($data->url); $external->id = $data->id; $external->name = empty($data->name) ? $rss->get_title() : $data->name; $external->description = empty($data->description) ? $rss->get_description() : $data->description; $external->userid = $USER->id; $external->url = $data->url; $external->filtertags = !empty($data->filtertags) ? $data->filtertags : null; $external->timemodified = time(); $DB->update_record('blog_external', $external); // Log this action. $eventparms = array('context' => $context, 'objectid' => $external->id, 'other' => array('url' => $external->url)); $event = \core\event\blog_external_updated::create($eventparms); $event->trigger(); core_tag_tag::set_item_tags('core', 'blog_external', $external->id, context_user::instance($external->userid), $data->autotags); } else { print_error('wrongexternalid', 'blog'); } break;
function test_redirect() { global $CFG; $feed = new moodle_simplepie(); $feed->set_timeout(self::TIMEOUT); $feed->set_feed_url(self::REDIRECTURL); $feed->init(); $this->assertNull($feed->error()); $this->assertEquals($feed->get_title(), 'Moodle News'); $this->assertEquals($feed->get_link(), 'http://moodle.org/mod/forum/view.php?f=1'); }
/** * 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; }
public function test_redirect() { $feed = new moodle_simplepie($this->getExternalTestFileUrl('/rss_redir.php'), self::TIMEOUT); $this->assertNull($feed->error()); $this->assertSame('Moodle News', $feed->get_title()); $this->assertSame('http://moodle.org/mod/forum/view.php?f=1', $feed->get_link()); }
/** * 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_plus_timeout)) { $feed->set_cache_duration($CFG->block_rss_plus_timeout * 60); } if (debugging() && $feed->error()) { return '<p>' . $feedrecord->url . ' Failed with code: ' . $feed->error() . '</p>'; } $r = ''; // return string if (empty($feedrecord->preferredtitle)) { $feedtitle = $this->format_title($feed->get_title()); } else { $feedtitle = $this->format_title($feedrecord->preferredtitle); } $r .= '<div class="rsswrap">'; $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>'; $r .= '</div>'; return $r; }