first add of new rss client block. please take a moment to test its features and provide feedback

This commit is contained in:
dhawes
2004-12-14 18:09:20 +00:00
parent c7e65290e4
commit 72035d793e
9 changed files with 515 additions and 0 deletions
+132
View File
@@ -0,0 +1,132 @@
<?php //$Id$
class block_rss_client extends block_base {
function init() {
$this->title = get_string('block_rss_feeds_title', 'block_rss_client');
$this->content_type = BLOCK_TYPE_TEXT;
$this->version = 2004112000;
}
function specialization() {
if (!empty($this->config) && !empty($this->config->title)) {
$this->title = $this->config->title;
}
}
function get_content() {
global $CFG, $editing;
if($this->content !== NULL) {
return $this->content;
}
$this->content = new stdClass;
$this->content->footer = '';
if (empty($this->instance) || empty($CFG->blog_version)) {
// Either we're being asked for content without
// an associated instance of the Blog module has never been installed.
$this->content->text = '';
return $this->content;
}
require_once($CFG->dirroot .'/rss/templib.php');
$output = '';
$rssid = -1;
$display_description = false;
if (isset($CFG->block_rss_client_num_entries) && is_numeric($CFG->block_rss_client_num_entries) ) {
$shownumentries = intval($CFG->block_rss_client_num_entries);
} else {
$shownumentries = 5; //default to 5 entries is not specified by admin or instance
}
if (!empty($this->config)) {
if (!empty($this->config->rssid)) {
$rssid = intval($this->config->rssid);
}
if (!empty($this->config->display_description)) {
$display_description = intval($this->config->display_description);
}
if (!empty($this->config->shownumentries)) {
$shownumentries = intval($this->config->shownumentries);
}
}
if ($editing) {
$submitters = $CFG->block_rss_client_submitters;
$isteacher = false;
$courseid = '';
if ($this->instance->pagetype == MOODLE_PAGE_COURSE) {
$isteacher = isteacher($this->instance->pageid);
$courseid = $this->instance->pageid;
}
//if the user is an admin or course teacher then allow the user to
//assign categories to other uses than personal
if ( isadmin() || $submitters == 0 || ($submitters == 2 && $isteacher) ) {
$output .= '<center><a href="'. $CFG->wwwroot .'/blocks/rss_client/block_rss_client_action.php?courseid='. $courseid .'">'. get_string('block_rss_feeds_add_edit', 'block_rss_client') .'</a></center><br /><br />';
}
}
$rss_record = get_record('block_rss_client', 'id', $rssid);
if (isset($rss_record) && isset($rss_record->id)) {
$rss = rss_get_feed($rss_record->id, $rss_record->url, $rss_record->type);
// print_object($rss); //debug
if ($shownumentries > 0 && $shownumentries < count($rss->items) ) {
$count_to = $shownumentries;
} else {
$count_to = count($rss->items);
}
for ($y = 0; $y < $count_to; $y++) {
if ($rss->items[$y]['title'] == '') {
// $rss->items[$y]['description'] = blog_unhtmlentities($rss->items[$y]['description']);
//can define an additional instance/admin config item for this (20) - max_description_length
$rss->items[$y]['title'] = substr(strip_tags($rss->items[$y]['description']), 0, 20) . '...';
}
if ($rss->items[$y]['link'] == '') {
$rss->items[$y]['link'] = $rss->items[$y]['guid'];
}
$output .= '<a href="'. $rss->items[$y]['link'] .'" target=_new>'. $rss->items[$y]['title'] . '</a><br />' ."\n";
if ($display_description){
$output .= $rss->items[$y]['description'] . '<br />' ."\n";
}
}
$output .= '<br />';
// print_object($rss); //debug
$feedtitle = get_string('block_rss_remote_news_feed', 'block_rss_client');
if ( isset($rss->channel['link']) && isset($rss->channel['title']) ) {
$feedtitle = '<a href="'. $rss->channel['link'] .'">'. $rss->channel['title'] .'</a>';
}
}
//can we reset the title here?
if (isset($feedtitle) && $feedtitle != '') {
$this->title = $feedtitle;
}
$this->content->text = $output;
return $this->content;
}
function instance_allow_multiple() {
return true;
}
function has_config() {
return true;
}
function instance_allow_config() {
return true;
}
}
?>
@@ -0,0 +1,171 @@
<?php //$Id$
require_once('../../config.php');
global $USER, $CFG;
require($CFG->dirroot .'/rss/templib.php');
require_login();
//ensure that the logged in user is not using the guest account
if (isset($_SERVER['HTTP_REFERER'])){
$referrer = $_SERVER['HTTP_REFERER'];
} else {
$referrer = $CFG->wwwroot;
}
if (isguest()) {
error(get_string('noguestpost', 'forum'), $referrer);
}
optional_variable($act, 'none');
optional_variable($rssid, 'none');
optional_variable($courseid, 'none');
optional_variable($url);
optional_variable($rsstype);
optional_variable($item);
print_header('Add/Edit RSS Feeds','Add/Edit RSS Feeds','Add/Edit RSS Feeds' );
//check to make sure that the user is allowed to post new feeds
if (empty( $CFG->block_rss_client_submitters)) {
$CFG->block_rss_client_submitters = 0;
}
$submitters = $CFG->block_rss_client_submitters;
$isteacher = false;
if ($courseid != 'none'){
$isteacher = isteacher($courseid);
}
//if the user is an admin or course teacher then allow the user to
//assign categories to other uses than personal
if (! ( isadmin() || $submitters == 0 || ($submitters == 2 && $isteacher) ) ) {
error(get_string('noguestpost', 'forum'), $referrer);
}
if ($act == 'none') {
rss_display_feeds();
rss_get_form($act, $url, $rssid, $rsstype);
} else if ($act == 'updfeed') {
require_variable($url);
$rss = rss_get_feed($rssid, $url, $rsstype);
$dataobject->id = $rssid;
$dataobject->type = $rsstype;
$dataobject->description = addslashes($rss->channel['description']);
$dataobject->title = addslashes($rss->channel['title']);
$dataobject->url = addslashes($url);
if (!update_record('block_rss_client', $dataobject)) {
error('There was an error trying to update rss feed with id:'. $rssid);
}
rss_display_feeds($rssid);
print '<strong>'. get_string('block_rss_feed_updated', 'block_rss_client') .'</strong>';
rss_get_form($act, $url, $rssid, $rsstype);
} else if ($act == 'addfeed' ) {
require_variable($url);
require_variable($rsstype);
$dataobject->userid = $USER->id;
$dataobject->type = $rsstype;
$dataobject->description = '';
$dataobject->title = '';
$dataobject->url = addslashes($url);
$rssid = insert_record('block_rss_client', $dataobject);
if (!$rssid){
error('There was an error trying to add a new rss feed:'. $url);
}
$rss = rss_get_feed($rssid, $url, $rsstype);
$dataobject->id = $rssid;
if (!empty($rss->channel['description'])) {
$dataobject->description = addslashes($rss->channel['description']);
}
if (!empty($rss->channel['title'])) {
$dataobject->title = addslashes($rss->channel['title']);
}
if (!update_record('block_rss_client', $dataobject)) {
error('There was an error trying to update rss feed with id:'. $rssid);
}
rss_display_feeds();
print '<strong>'. get_string('block_rss_feed_added', 'block_rss_client') .'</strong>';
rss_get_form($act, $url, $rssid, $rsstype);
} else if ( $act == 'rss_edit') {
$rss_record = get_record('block_rss_client', 'id', $rssid);
$fname = stripslashes_safe($rss_record->title);
$url = stripslashes_safe($rss_record->url);
$rsstype = $rss_record->type;
rss_get_form($act, $url, $rssid, $rsstype);
} else if ($act == 'delfeed') {
$file = $CFG->dataroot .'/cache/rsscache/'. $rssid .'.xml';
if (file_exists($file)) {
unlink($file);
}
// echo "DEBUG: act = delfeed"; //debug
//Daryl Hawes note: convert this sql statement to a moodle function call
$sql = 'DELETE FROM '. $CFG->prefix .'block_rss_client WHERE id='. $rssid;
$res= $db->Execute($sql);
rss_display_feeds();
print '<strong>'. get_string('block_rss_feed_deleted', 'block_rss_client') .'</strong>';
rss_get_form($act, $url, $rssid, $rsstype);
} else if ($act == 'view') {
global $THEME;
// echo $sql; //debug
// print_object($res); //debug
$rss_record = get_record('block_rss_client', 'id', $rssid);
if (!$rss_record->id){
print '<strong>'. get_string('block_rss_could_not_find_feed', 'block_rss_client') .': '. $rssid .'</strong>';
} else {
// echo 'rssid = '. $rssid .', url ='. $rss_record->url .', type = '. $rss_record->type;
$rss = rss_get_feed($rssid, $rss_record->url, $rss_record->type);
// echo print_object($rss);
print '<table align=\"center\" width=\"50%\" cellspacing=\"1\">'."\n";
print '<tr><td colspan=\"2\"><strong>'. $rss->channel['title'] .'</strong></td></tr>'."\n";
for($y=0; $y < count($rss->items); $y++) {
// $rss->items[$y]['title'] = blog_unhtmlentities($rss->items[$y]['title']);
if ($rss->items[$y]['link'] == '') {
$rss->items[$y]['link'] = $rss->items[$y]['guid'];
}
if ($rss->items[$y]['title'] == '') {
$rss->items[$y]['title'] = '&gt;&gt;';
}
print '<tr bgcolor="'. $THEME->cellcontent .'"><td valign=\"middle\">'."\n";
print '<a href="'. $rss->items[$y]['link'] .'" target=_new><strong>'. $rss->items[$y]['title'];
print '</strong></a>'."\n";
print '</td>'."\n";
if (file_exists($CFG->dirroot .'/blog/lib.php')) {
print '<td align=\"right\">'."\n";
print '<img src="'. $CFG->pixpath .'/blog/blog.gif" alt="'. get_string('blog_blog_this', 'blog').'" title="'. get_string('blog_blog_this', 'blog') .'" border=\"0\" align=\"middle\" />'."\n";
print '<a href="'. $CFG->wwwroot .'/blog/blogthis.php?blogid='. $blogid .'&act=use&item='. $y .'&rssid='. $rssid .'"><small><strong>'. get_string('blog_blog_this', 'blog') .'</strong></small></a>'."\n";
} else {
print '<td>&nbsp;';
}
print '</td></tr>'."\n";
// $rss->items[$y]['description'] = blog_unhtmlentities($rss->items[$y]['description']);
print '<tr bgcolor="'. $THEME->cellcontent2 .'"><td colspan=2><small>';
print $rss->items[$y]['description'] .'</small></td></tr>'."\n";
}
print '</table>'."\n";
}
} else {
rss_display_feeds();
rss_get_form($act, $url, $rssid, $rsstype);
}
print_footer();
?>
+41
View File
@@ -0,0 +1,41 @@
<table cellpadding="9" cellspacing="0">
<tr valign="top">
<td align="right"><p>block_rss_client_num_entries:</td>
<td>
<input name="block" type="hidden" value="<?php echo intval($_REQUEST['block']); ?>" />
<input name="block_rss_client_num_entries" type="text" size="5" value="<?php
if(isset($CFG->block_rss_client_num_entries)) {
p($CFG->block_rss_client_num_entries);
} else {
p(5);
} ?>" />
</td>
<td>
<?php print_string('block_rss_client_num_entries', 'block_rss_client') ?>
</td>
</tr>
<tr valign="top">
<td align="right"><p>block_rss_client_submitters:</td>
<td>
<?php if (!empty($CFG->block_rss_client_submitters)) {
$selected = $CFG->block_rss_client_submitters;
} else {
$selected = '0';
$CFG->block_rss_client_submitters = 0;
}
$options = array ( '0' => 'All members',
'1' => 'Admins only',
'2' => 'Admins and teachers');
choose_from_menu ($options, 'block_rss_client_submitters', $selected);
?>
</td>
<td>
<?php print_string('block_rss_submitters', 'block_rss_client') ?>
</td>
</tr>
<tr>
<td colspan="3" align="center">
<input type="submit" value="<?php print_string('savechanges') ?>"></td>
</tr>
</table>
+73
View File
@@ -0,0 +1,73 @@
<table cellpadding="9" cellspacing="0">
<tr valign="top">
<td align="right">
<?php print_string('block_rss_display_description', 'block_rss_client') ?>
</td>
<td>
<?php
if(! isset($CFG->block_rss_client_display_description) ) {
$CFG->block_rss_client_display_description = '0';
}
$selected = $CFG->block_rss_client_display_description;
if (isset($this->config) && isset($this->config->display_description)) {
$selected = $this->config->display_description;
}
$options[0] = get_string('no');
$options[1] = get_string('yes');
choose_from_menu ($options, 'display_description', $selected);
?>
</td>
</tr>
<tr valign="top">
<td align="right">
<?php print_string('block_rss_shownumentries', 'block_rss_client') ?>
</td>
<td>
<input name="shownumentries" type="text" size="5" value="<?php
if(! isset($CFG->block_rss_client_num_entries) ) {
$CFG->block_rss_client_num_entries = '5';
}
$numentries = $CFG->block_rss_client_num_entries;
if (isset($this->config) && isset($this->config->shownumentries)) {
$numentries = intval($this->config->shownumentries);
}
p($numentries);
?>" />
</td>
</tr>
<tr valign="top">
<td>
<?php print_string('block_rss_choose_feed', 'block_rss_client') ?>
</td>
<td>
<?php
$selected = '';
if (isset($this->config) && isset($this->config->rssid)) {
$selected = $this->config->rssid;
}
if ($rssfeeds = get_records('block_rss_client')) {
foreach($rssfeeds as $rssfeed){
$feedoptions[$rssfeed->id] = $rssfeed->title;
}
choose_from_menu ($feedoptions, 'rssid', $selected);
} else {
print_string('block_rss_no_feeds', 'block_rss_client');
if ( isadmin() ){
print '&nbsp;<a href="'. $CFG->wwwroot .'/blocks/rss_client/block_rss_client_action.php"> '. get_string('block_rss_edit_news_feeds', 'block_rss_client') .'</a><br />';
}
}
?>
</td>
</tr>
<tr valign="top">
<td align="right"><p><?php print_string('block_rss_word_title', 'block_rss_client'); ?>:</td>
<td><input type="text" name="title" size="30" value="<?php echo $this->config->title; ?>" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="<?php print_string('savechanges') ?>">
</td>
</tr>
</table>
+16
View File
@@ -0,0 +1,16 @@
<?php //$Id$
function block_rss_client_upgrade($oldversion) {
/// This function does anything necessary to upgrade
/// older versions to match current functionality
global $CFG;
if ($oldversion < 2003111500) {
# Do something ...
}
return true;
}
?>
+21
View File
@@ -0,0 +1,21 @@
# This file contains a complete database schema for all the
# tables used by this module, written in SQL
# It may also contain INSERT statements for particular data
# that may be used, especially new entries in the table log_display
# --------------------------------------------------------
#
# Table structure for table `prefix_block_rss_client`
#
CREATE TABLE prefix_block_rss_client (
`id` int(11) NOT NULL auto_increment,
`userid` int(11) NOT NULL default '0',
`title` varchar(64) NOT NULL default '',
`description` varchar(128) NOT NULL default '',
`url` varchar(255) NOT NULL default '',
`type` char(1) NOT NULL default 'R',
PRIMARY KEY (`id`)
) TYPE=MyISAM COMMENT='Cached remote news feeds';
+13
View File
@@ -0,0 +1,13 @@
<?php
function block_rss_client_upgrade($oldversion) {
/// This function does anything necessary to upgrade
/// older versions to match current functionality
global $CFG;
return true;
}
?>
+12
View File
@@ -0,0 +1,12 @@
# This file contains a complete database schema for all the
# tables used by this module, written in SQL
# It may also contain INSERT statements for particular data
# that may be used, especially new entries in the table log_display
#
# Table structure for table `block_rss_client`
#
CREATE TABLE prefix_block_rss_client (
);
+36
View File
@@ -0,0 +1,36 @@
<?php // $Id$
$string['block_rss_feeds_title'] = 'Remote RSS Feeds';
$string['block_rss_feeds_add_edit'] = 'Add/Edit Feeds';
$string['block_rss_remote_news_feed'] = 'Remote News Feed';
$string['block_rss_feed_updated'] = 'News feed updated';
$string['block_rss_feed_added'] = 'News feed added';
$string['block_rss_feed_deleted'] = 'News feed deleted';
$string['block_rss_num_entries'] = 'Number of rss links to show per block.';
$string['block_rss_submitters'] = 'Who can define new rss feeds? Feeds are made available for any member of your site to add to their own pages.';
$string['block_rss_choose_feed'] = 'Choose an feed to display in this block:';
$string['block_rss_no_feeds'] = 'There are no RSS feeds defined for this site.';
$string['block_rss_edit_news_feeds'] = 'Edit news feeds';
$string['block_rss_shownumentries'] = 'Max number entries to show per block.';
$string['block_rss_display_description'] = 'Display each link\'s description?';
$string['block_rss_could_not_find_feed'] = 'Could not find feed with id';
$string['block_rss_feed'] = 'Feed';
$string['block_rss_find_more_feeds'] = 'Find more rss feeds';
$string['block_rss_delete_feed_confirm'] = 'Are you sure you want to delete this feed?';
$string['block_rss_timeout'] = 'block_rss_timeout';
$string['block_rss_timeout_desc'] = 'Time in minutes for an RSS feed to live in cache.';
$string['block_rss_add_headline_block'] = 'Add RSS headline block';
$string['block_rss_pick_feed'] = 'Pick a news feed';
$string['block_rss_feeds'] = 'News Feeds';
$string['block_rss_feed'] = 'News Feed';
$string['block_rss_edit_feeds'] = 'Edit, subscribe or unsubsribe from RSS/Atom news feeds';
$string['block_rss_see_all_feeds'] = 'See all feeds';
$string['block_rss_edit_rss_block'] = 'Edit RSS Headline Block';
$string['block_rss_add_new'] = 'Add New';
$string['block_rss_word_title'] = 'Title';
?>