Commit 88f92567 authored by Nigel McNie's avatar Nigel McNie
Browse files
parents 90aeecae eb0789c3
<?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-recentposts
* @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'] = 'Recent Blog Posts';
$string['description'] = 'Display the 10 most recent blog posts';
$string['postedin'] = 'in';
$string['postedon'] = 'on';
?>
<?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-recentposts
* @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 PluginBlocktypeRecentposts extends PluginBlocktype {
public static function get_title() {
return get_string('title', 'blocktype.blog/recentposts');
}
public static function get_description() {
return get_string('description', 'blocktype.blog/recentposts');
}
public static function get_categories() {
return array('blog');
}
public static function render_instance(BlockInstance $instance) {
$configdata = $instance->get('configdata');
$result = '';
if (!empty($configdata['artefactids'])) {
$artefactids = implode(', ', array_map('db_quote', $configdata['artefactids']));
if (!$mostrecent = get_records_sql_array(
'SELECT a.title, a.ctime, p.title AS parenttitle, a.id, a.parent
FROM {artefact} a
JOIN {artefact} p ON a.parent = p.id
WHERE a.artefacttype = \'blogpost\'
AND a.parent IN ( ' . $artefactids . ' )
AND a.owner = (SELECT owner from {view} WHERE id = ?)
ORDER BY a.ctime DESC
LIMIT 10', array($instance->get('view')))) {
$mostrecent = array();
}
// format the dates
foreach ($mostrecent as &$data) {
$data->displaydate = format_date(time($data->ctime));
}
$smarty = smarty_core();
$smarty->assign('mostrecent', $mostrecent);
$smarty->assign('view', $instance->get('view'));
$result = $smarty->fetch('blocktype:recentposts:recentposts.tpl');
}
return $result;
}
// TODO: Implement in parent class, saving us a lot of hassle
public static function get_artefacts(BlockInstance $instance) {
$configdata = $instance->get('configdata');
if (!empty($configdata['artefactids'])) {
return $configdata['artefactids'];
}
return false;
}
public static function has_instance_config() {
return true;
}
public static function instance_config_form($instance) {
$configdata = $instance->get('configdata');
return array(
self::artefactchooser_element((isset($configdata['artefactids'])) ? $configdata['artefactids'] : null),
);
}
public static function artefactchooser_element($default=null) {
return array(
'name' => 'artefactids',
'type' => 'artefactchooser',
'title' => get_string('blogs', 'artefact.blog'),
'defaultvalue' => $default,
'rules' => array(
'required' => true,
),
'blocktype' => 'recentposts',
'limit' => 5,
'selectone' => false,
'artefacttypes' => array('blog'),
'template' => 'artefact:blog:artefactchooser-element.tpl',
);
}
/**
* Optional method. If specified, changes the order in which the artefacts are sorted in the artefact chooser.
*
* This is a valid SQL string for the ORDER BY clause. Fields you can sort on are as per the artefact table
*/
public static function artefactchooser_get_sort_order() {
return 'title';
}
}
?>
<ul>
{foreach from=$mostrecent item=post}
<li>
<a href="{$WWWROOT}view/view.php?view={$view}&artefact={$post->id}">{$post->title|escape}</a>
{str tag='postedin' section='blocktype.blog/recentposts'}
<a href="{$WWWROOT}view/view.php?view={$view}&artefact={$post->parent}">{$post->parenttitle|escape}</a>
{str tag='postedon' section='blocktype.blog/recentposts'}
{$post->displaydate}
</li>
{/foreach}
</ul>
<?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-recentposts
* @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';
?>
......@@ -539,7 +539,10 @@ class ArtefactTypeBlogPost extends ArtefactType {
$data->artefact = $artefactid;
$data->parent = $this->get('parent');
$data->dirty = 0;
insert_record_if_not_exists('artefact_parent_cache', $data);
$where = $data;
unset($where->dirty);
ensure_record_exists('artefact_parent_cache', $where, $data);
}
public function detach_file($artefactid) {
......
......@@ -4,7 +4,7 @@
{else}<input type="checkbox" id="{$elementname}_{$artefact->id}" name="{$elementname}[{$artefact->id}]"{if $value && in_array($artefact->id, $value)} checked="checked"{/if}>
<input type="hidden" name="{$elementname}_onpage[]" value="{$artefact->id}">
{/if}</td>
<th><label for="{$elementname}_{$artefact->id}">{$artefact->blog|escape}: {$artefact->title|escape}</label></th>
<th><label for="{$elementname}_{$artefact->id}">{if $artefact->blog}{$artefact->blog|escape}: {/if}{$artefact->title|escape}</label></th>
</tr>
<tr>
<td>{if $artefact->description}{$artefact->description}{/if}</td>
......
<?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';
......
......@@ -1032,46 +1032,57 @@ function insert_record($table, $dataobject, $primarykey=false, $returnpk=false)
}