Commit 6a9091e5 authored by Penny Leach's avatar Penny Leach
Browse files

New blocktype - external feed. Changed the 'rss' blocktype category to 'feeds' to match.

parent 7759f738
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="lib/db" VERSION="20060926" COMMENT="XMLDB file for core Mahara tables"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd"
>
<TABLES>
<TABLE NAME="blocktype_externalfeed_data">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" SEQUENCE="true" NOTNULL="true" />
<FIELD NAME="url" TYPE="text" NOTNULL="true" />
<FIELD NAME="link" TYPE="text" NOTNULL="false" />
<FIELD NAME="title" TYPE="text" NOTNULL="false" />
<FIELD NAME="description" TYPE="text" NOTNULL="false" />
<FIELD NAME="content" TYPE="text" NOTNULL="false" />
<FIELD NAME="lastupdate" TYPE="datetime" NOTNULL="false" />
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" />
<KEY NAME="urluk" TYPE="unique" FIELDS="url" />
</KEYS>
</TABLE>
</TABLES>
</XMLDB>
<?php
/**
* This program is part of Mahara
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package mahara
* @subpackage blocktype-externalfeeds
* @author Penny Leach <penny@catalyst.net.nz>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
*
*/
defined('INTERNAL') || die();
$string['title'] = 'External Feed';
$string['description'] = 'Embed an external RSS or ATOM feed';
$string['feedlocation'] = 'Feed location';
$string['feedlocationdesc'] = 'URL of a valid RSS or ATOM feed';
$string['invalidurl'] = 'That URL is either invalid or the feed there won\'t parse';
$string['lastupdatedon'] = 'Last updated on %s';
?>
<?php
/**
* This program is part of Mahara
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package mahara
* @subpackage blocktype-externalfeed
* @author Penny Leach <penny@catalyst.net.nz>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
*
*/
defined('INTERNAL') || die();
class PluginBlocktypeExternalfeed extends SystemBlocktype {
public static function get_title() {
return get_string('title', 'blocktype.externalfeed');
}
public static function get_description() {
return get_string('description', 'blocktype.externalfeed');
}
public static function get_categories() {
return array('feeds');
}
public static function render_instance(BlockInstance $instance) {
$configdata = $instance->get('configdata');
if ($configdata['feedid']) {
$data = get_record('blocktype_externalfeed_data', 'id', $configdata['feedid']);
unserialize($data->content);
$smarty = smarty_core();
$smarty->assign('title', $data->title);
$smarty->assign('description', $data->description);
$smarty->assign('link', $data->link);
$smarty->assign('entries', unserialize($data->content));
$smarty->assign('lastupdated', get_string('lastupdatedon', 'blocktype.externalfeed', format_date(time($data->lastupdate))));
return $smarty->fetch('blocktype:externalfeed:feed.tpl');
}
return '';
}
public static function has_instance_config() {
return true;
}
public static function instance_config_form($instance) {
$configdata = $instance->get('configdata');
if (isset($configdata['feedid'])) {
$url = get_field('blocktype_externalfeed_data', 'url', 'id', $configdata['feedid']);
}
else {
$url = '';
}
return array(
'url' => array(
'type' => 'text',
'title' => get_string('feedlocation', 'blocktype.externalfeed'),
'description' => get_string('feedlocationdesc', 'blocktype.externalfeed'),
'width' => '90%',
'defaultvalue' => $url,
'rules' => array(
'required' => true
),
),
);
}
/**
* Optional method. If exists, allows this class to decide the title for
* all blockinstances of this type
*/
public static function get_instance_title(BlockInstance $bi) {
$configdata = $bi->get('configdata');
if (!empty($configdata['feedid'])) {
if ($title = get_field('blocktype_externalfeed_data', 'title', 'id', $configdata['feedid'])) {
return $title;
}
}
return '';
}
public static function instance_config_validate(Pieform $form, $values) {
if (strpos($values['url'], '://') == false) {
// try add on http://
$values['url'] = 'http://' . $values['url'];
}
else {
$proto = substr($values['url'], 0, strpos($values['url'], '://'));
if (!in_array($proto, array('http', 'https'))) {
$form->set_error('url', get_string('invalidurl', 'blocktype.externalfeed'));
}
}
if (!record_exists('blocktype_externalfeed_data', 'url', $values['url']) && !self::parse_feed($values['url'])) {
$form->set_error('url', get_string('invalidurl', 'blocktype.externalfeed'));
}
}
public static function instance_config_save($values) {
// we need to turn the feed url into an id in the feed_data table..
if (strpos($values['url'], '://') == false) {
// try add on http://
$values['url'] = 'http://' . $values['url'];
}
if ($exists = get_record('blocktype_externalfeed_data', 'url', $values['url'])) {
$values['feedid'] = $exists->id;
unset($values['url']);
return $values;
}
$data = self::parse_feed($values['url']);
$data->content = serialize($data->content);
$data->lastupdate = db_format_timestamp(time());
$id = ensure_record_exists('blocktype_externalfeed_data', array('url' => $values['url']), $data, 'id', true);
$values['feedid'] = $id;
unset($values['url']);
return $values;
}
public static function get_cron() {
$refresh = new StdClass;
$refresh->callfunction = 'refresh_feeds';
$refresh->hour = '*';
$refresh->minute = '0';
$cleanup = new StdClass;
$cleanup->callfunction = 'cleanup_feeds';
$cleanup->hour = '3';
$cleanup->minute = '0';
return array($refresh, $cleanup);
}
public static function refresh_feeds() {
if (!$feeds = get_records_select_array('blocktype_externalfeed_data',
'lastupdate < ?', array(db_format_timestamp(strtotime('-30 minutes'))))) {
return;
}
foreach ($feeds as $feed) {
$data = self::parse_feed($feed->url);
$data->id = $feed->id;
$data->lastupdate = db_format_timestamp(time());
$data->content = serialize($data->content);
update_record('blocktype_externalfeed_data', $data);
}
}
public static function cleanup_feeds() {
$ids = array();
if ($instances = get_records_array('block_instance', 'blocktype', 'externalfeed')) {
foreach ($instances as $block) {
$data = unserialize($block->configdata);
$ids[$data['feedid']] = true;
}
}
if (count($ids) == 0) {
delete_records('blocktype_externalfeed_data'); // just delete it all
return;
}
$usedids = implode(', ', array_map('db_quote', array_keys($ids)));
delete_records_select('blocktype_externalfeed_data', 'id NOT IN ( ' . $usedids . ' )');
}
public static function parse_feed($source) {
static $cache;
if (empty($cache)) {
$cache = array();
}
if (array_key_exists($source, $cache)) {
return $cache[$source];
}
require_once('XML/Feed/Parser.php');
require_once('snoopy/Snoopy.class.php');
$snoopy = new Snoopy();
$success = true;
if (!$snoopy->fetch($source)) {
$cache[$source] = false;
return false;
}
try {
$feed = new XML_Feed_Parser($snoopy->results, false, true, false);
}
catch (XML_Feed_Parser_Exception $e) {
$cache[$source] = false;
return false;
}
$data = new StdClass;
$data->title = $feed->title;
$data->url = $source;
$data->link = $feed->link;
$data->description = $feed->description;
$data->content = array();
foreach ($feed as $count => $item) {
if ($count == 11) { // it starts at one!
break;
}
$data->content[] = (object)array('title' => $item->title, 'link' => $item->link);
}
$cache[$source] = $data;
return $data;
}
}
?>
<div id="blocktype_externalfeed_feed">
<div id="blocktype_externalfeed_title">
{if $link}<a href="{$link|escape}">{/if}
{$title|escape}
{if $link}</a>{/if}
</div>
<div id="blocktype_externalfeed_desc">{$description|escape}</a></div>
<div id="blocktype_externalfeed_entries">
{foreach from=$entries item=entry}
<p>
{if $entry->link}<a href="{$entry->link|escape}">{/if}
{$entry->title|escape}
{if $entry->link}</a>{/if}
</p>
{/foreach}
</div>
<div id="blocktype_externalfeed_lastupdate">
{$lastupdated}
</div>
</div>
<?php
/**
* This program is part of Mahara
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package mahara
* @subpackage blocktype-rssfeed
* @author Penny Leach <penny@catalyst.net.nz>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
*
*/
defined('INTERNAL') || die();
$config = new StdClass;
$config->version = 2007110500;
$config->release = '0.9';
?>
......@@ -108,7 +108,7 @@ $string['15,70,15'] = 'Much larger centre column';
$string['20,30,30,20'] = 'Larger centre columns';
$string['noviewlayouts'] = 'There are no view layouts for a %s column view';
$string['blocktypecategory.rss'] = 'RSS';
$string['blocktypecategory.feeds'] = 'External feeds';
$string['blocktypecategory.images'] = 'Images';
$string['blocktypecategory.general'] = 'General';
$string['blocktypecategory.multimedia'] = 'Multimedia';
......
......@@ -796,7 +796,7 @@ function sort_upgrades($k1, $k2) {
* (eg not tied to artefact plugins)
*/
function get_core_blocktype_categories() {
return array('general', 'images', 'multimedia', 'rss');
return array('general', 'images', 'multimedia', 'feeds');
}
function install_blocktype_categories_for_plugin($blocktype) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment