2004-12-14 18:09:20 +00:00
< ? php //$Id$
2005-01-29 03:01:18 +00:00
// Developer's debug assistant - if true then the display string will not cache, only
// the magpie object's built in caching will be used
2005-01-29 03:08:55 +00:00
define ( 'BLOCK_RSS_SECONDARY_CACHE_ENABLED' , true );
2005-01-29 03:01:18 +00:00
2004-12-14 18:09:20 +00:00
class block_rss_client extends block_base {
function init () {
$this -> title = get_string ( 'block_rss_feeds_title' , 'block_rss_client' );
$this -> version = 2004112000 ;
}
function specialization () {
2004-12-30 18:08:38 +00:00
// After the block has been loaded we customize the block's title display
2004-12-14 18:09:20 +00:00
if ( ! empty ( $this -> config ) && ! empty ( $this -> config -> title )) {
2004-12-30 18:08:38 +00:00
// There is a customized block title, display it
2004-12-14 18:09:20 +00:00
$this -> title = $this -> config -> title ;
2004-12-30 18:08:38 +00:00
} else {
// No customized block title, use localized remote news feed string
$this -> title = get_string ( 'block_rss_remote_news_feed' , 'block_rss_client' );
2004-12-14 18:09:20 +00:00
}
}
function get_content () {
global $CFG , $editing ;
2005-01-25 06:09:31 +00:00
require_once ( $CFG -> libdir . '/rsslib.php' );
2004-12-31 07:42:22 +00:00
2004-12-14 18:09:20 +00:00
if ( $this -> content !== NULL ) {
return $this -> content ;
}
$this -> content = new stdClass ;
2004-12-30 18:08:38 +00:00
$this -> content -> footer = '' ;
2004-12-14 18:09:20 +00:00
2004-12-31 07:42:22 +00:00
if ( empty ( $this -> instance )) {
// We're being asked for content without an associated instance
2004-12-14 18:09:20 +00:00
$this -> content -> text = '' ;
return $this -> content ;
}
$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 )) {
2004-12-31 08:51:21 +00:00
if ( is_array ( $this -> config -> rssid )) {
2004-12-30 18:08:38 +00:00
$rssidarray = $this -> config -> rssid ;
2004-12-31 08:51:21 +00:00
} else { // Make an array of the single value
2004-12-30 18:08:38 +00:00
$rssidarray = array ( $this -> config -> rssid );
}
2004-12-14 18:09:20 +00:00
}
if ( ! empty ( $this -> config -> display_description )) {
$display_description = intval ( $this -> config -> display_description );
}
if ( ! empty ( $this -> config -> shownumentries )) {
$shownumentries = intval ( $this -> config -> shownumentries );
}
}
2005-01-23 16:16:28 +00:00
$submitters = $CFG -> block_rss_client_submitters ;
2004-12-14 18:09:20 +00:00
2005-01-23 16:16:28 +00:00
$isteacher = false ;
$courseid = '' ;
2005-01-31 02:18:15 +00:00
if ( $this -> instance -> pagetype == PAGE_COURSE_VIEW ) {
2005-01-23 16:16:28 +00:00
$isteacher = isteacher ( $this -> instance -> pageid );
$courseid = $this -> instance -> pageid ;
}
2005-01-29 03:01:18 +00:00
//if the user is an admin, course teacher, or all users are allowed
2005-01-23 16:16:28 +00:00
// then allow the user to add rss feeds
2005-01-29 05:06:43 +00:00
global $USER ;
$userisloggedin = false ;
if ( isset ( $USER ) && isset ( $USER -> id ) && $USER -> id && ! isguest ()) {
$userisloggedin = true ;
}
if ( $userisloggedin && ( isadmin () || $submitters == SUBMITTERS_ALL_ACCOUNT_HOLDERS || ( $submitters == SUBMITTERS_ADMIN_AND_TEACHER && $isteacher )) ) {
2005-01-24 03:24:19 +00:00
$output .= '<div align="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></div><br />' ;
2004-12-14 18:09:20 +00:00
}
2004-12-30 18:08:38 +00:00
// Daryl Hawes note: if count of rssidarray is greater than 1
// we should possibly display a drop down menu of selected feed titles
// so user can select a single feed to view (similar to RSSFeed)
2004-12-31 03:11:03 +00:00
if ( ! empty ( $rssidarray )) {
2005-01-21 03:23:26 +00:00
$numids = count ( $rssidarray );
$count = 0 ;
2004-12-31 03:11:03 +00:00
foreach ( $rssidarray as $rssid ) {
2005-01-23 16:16:28 +00:00
$rssfeedstring = $this -> get_rss_by_id ( $rssid , $display_description , $shownumentries , ( $numids > 1 ) ? true : false );
$output .= format_text ( $rssfeedstring );
2005-01-29 03:01:18 +00:00
if ( $numids > 1 && $count != $numids - 1 && ! empty ( $rssfeedstring )) {
2005-01-20 01:32:58 +00:00
$output .= '<hr width="80%" />' ;
}
$count ++ ;
2004-12-31 03:11:03 +00:00
}
2004-12-30 18:08:38 +00:00
}
$this -> content -> text = $output ;
return $this -> content ;
}
function instance_allow_multiple () {
return true ;
}
function has_config () {
return true ;
}
function instance_allow_config () {
return true ;
}
2005-01-21 03:23:26 +00:00
2005-01-19 23:59:21 +00:00
/**
2005-01-23 16:16:28 +00:00
* @ param int $rssid The feed to be displayed
* @ param bool $display_description Should the description information from the feed be displayed or simply the title ?
* @ param int $shownumentries The maximum number of feed entries to be displayed .
* @ param bool $showtitle True if the feed title should be displayed above the feed entries .
* @ return string | NULL
2005-01-19 23:59:21 +00:00
*/
2005-01-20 01:32:58 +00:00
function get_rss_by_id ( $rssid , $display_description , $shownumentries , $showtitle = false ) {
2005-01-19 23:59:21 +00:00
global $CFG ;
2005-01-23 16:16:28 +00:00
$returnstring = '' ;
$now = time ();
2005-01-25 06:10:38 +00:00
require_once ( $CFG -> libdir . '/rsslib.php' );
2005-01-23 16:16:28 +00:00
require_once ( MAGPIE_DIR . 'rss_fetch.inc' );
2005-01-21 03:23:26 +00:00
2005-01-23 16:16:28 +00:00
// Check if there is a cached string which has not timed out.
2005-01-29 03:01:18 +00:00
if ( BLOCK_RSS_SECONDARY_CACHE_ENABLED &&
isset ( $this -> config -> { 'rssid' . $rssid }) &&
isset ( $this -> config -> { 'rssid' . $rssid . 'timestamp' }) &&
$this -> config -> { 'rssid' . $rssid . 'timestamp' } >= $now - $CFG -> block_rss_timeout * 60 ) {
2005-01-23 16:16:28 +00:00
// If the cached string is not too stale
// use it rather than going any further
return stripslashes_safe ( $this -> config -> { 'rssid' . $rssid });
2005-01-21 03:23:26 +00:00
}
2004-12-14 18:09:20 +00:00
$rss_record = get_record ( 'block_rss_client' , 'id' , $rssid );
if ( isset ( $rss_record ) && isset ( $rss_record -> id )) {
2005-01-23 16:16:28 +00:00
// By capturing the output from fetch_rss this way
// error messages do not display and clutter up the moodle interface
// however, we do lose out on seeing helpful messages like "cache hit", etc.
2005-01-29 03:07:43 +00:00
ob_start ();
2005-01-23 16:16:28 +00:00
$rss = fetch_rss ( $rss_record -> url );
2005-01-29 03:07:43 +00:00
$rsserror = ob_get_contents ();
ob_end_clean ();
2005-01-29 03:01:18 +00:00
2005-01-23 16:16:28 +00:00
if ( $rss === false ) {
if ( $CFG -> debug && ! empty ( $rsserror )) {
// There was a failure in loading the rss feed, print link to full error text
2005-01-29 03:01:18 +00:00
return '<a href="' . $CFG -> wwwroot . '/blocks/rss_client/block_rss_client_error.php?error=' . urlencode ( $rsserror ) . '">Error loading a feed.</a><br />' ; //Daryl Hawes note: localize this line
2005-01-19 23:59:21 +00:00
}
2004-12-30 18:08:38 +00:00
}
2005-01-28 01:21:08 +00:00
2004-12-14 18:09:20 +00:00
if ( $shownumentries > 0 && $shownumentries < count ( $rss -> items ) ) {
2005-01-23 16:16:28 +00:00
$rss -> items = array_slice ( $rss -> items , 0 , $shownumentries );
2004-12-14 18:09:20 +00:00
}
2005-01-27 22:47:13 +00:00
if ( empty ( $rss_record -> preferredtitle )) {
$feedtitle = stripslashes_safe ( rss_unhtmlentities ( $rss -> channel [ 'title' ]));
2005-01-28 01:21:08 +00:00
} else {
$feedtitle = stripslashes_safe ( $rss_record -> preferredtitle );
2005-01-29 03:01:18 +00:00
}
// print_object($rss);
if ( isset ( $this -> config ) &&
isset ( $this -> config -> block_rss_client_show_channel_image ) &&
$this -> config -> block_rss_client_show_channel_image &&
isset ( $rss -> image ) && isset ( $rss -> image [ 'link' ]) && isset ( $rss -> image [ 'title' ]) && isset ( $rss -> image [ 'url' ]) ) {
2005-02-14 02:57:15 +00:00
$returnstring .= '<div class="rssclientimage"><a href="' . $rss -> image [ 'link' ] . '"><img src="' . $rss -> image [ 'url' ] . '" title="' . $rss -> image [ 'title' ] . '" alt="' . $rss -> image [ 'title' ] . '"/></a></div><br />' ;
2005-01-29 03:01:18 +00:00
}
2005-01-28 01:21:08 +00:00
if ( $showtitle ) {
2005-02-24 16:23:48 +00:00
$returnstring .= '<div class="rssclienttitle">' . $feedtitle . '</div><br />' ;
2005-01-28 01:21:08 +00:00
}
2005-01-23 16:16:28 +00:00
foreach ( $rss -> items as $item ) {
2005-01-27 22:47:13 +00:00
$item [ 'title' ] = stripslashes_safe ( rss_unhtmlentities ( $item [ 'title' ]));
$item [ 'description' ] = stripslashes_safe ( rss_unhtmlentities ( $item [ 'description' ]));
2005-01-23 16:16:28 +00:00
if ( $item [ 'title' ] == '' ) {
2005-01-29 03:01:18 +00:00
// no title present, use portion of description
2005-01-23 16:16:28 +00:00
$item [ 'title' ] = substr ( strip_tags ( $item [ 'description' ]), 0 , 20 ) . '...' ;
2004-12-14 18:09:20 +00:00
}
2005-01-23 16:16:28 +00:00
if ( $item [ 'link' ] == '' ) {
$item [ 'link' ] = $item [ 'guid' ];
2004-12-14 18:09:20 +00:00
}
2005-01-25 14:49:30 +00:00
$item [ 'link' ] = str_replace ( '&' , '&' , $item [ 'link' ]);
2005-01-23 16:16:28 +00:00
$returnstring .= '<div class="rssclientlink"><a href="' . $item [ 'link' ] . '" target="_new">' . $item [ 'title' ] . '</a></div>' . " \n " ;
2004-12-14 18:09:20 +00:00
2005-01-23 16:16:28 +00:00
if ( $display_description && ! empty ( $item [ 'description' ])){
$returnstring .= '<div class="rssclientdescription">' . clean_text ( $item [ 'description' ]) . '</div>' . " \n " ;
2004-12-14 18:09:20 +00:00
}
}
2005-01-27 22:47:13 +00:00
if ( ! empty ( $rss -> channel [ 'link' ])) {
if ( ! empty ( $this -> config ) && isset ( $this -> config -> block_rss_client_show_channel_link ) && $this -> config -> block_rss_client_show_channel_link ) {
$returnstring .= '<div class="rssclientchannellink"><br /><a href="' . $rss -> channel [ 'link' ] . '">' . get_string ( 'block_rss_client_channel_link' , 'block_rss_client' ) . '</a></div>' ;
}
if ( ! empty ( $feedtitle ) ) {
$feedtitle = '<a href="' . $rss -> channel [ 'link' ] . '">' . $feedtitle . '</a>' ;
}
2004-12-14 18:09:20 +00:00
}
}
2005-01-25 14:49:30 +00:00
if ( ! empty ( $feedtitle ) and ( $feedtitle != '<a href="' . $rss -> channel [ 'link' ] . '"></a>' )) {
2004-12-14 18:09:20 +00:00
$this -> title = $feedtitle ;
}
2005-01-21 03:23:26 +00:00
// store config setting for this rssid so we do not need to read from file each time
2005-01-23 16:16:28 +00:00
$this -> config -> { 'rssid' . $rssid } = addslashes ( $returnstring );
$this -> config -> { 'rssid' . $rssid . 'timestamp' } = $now ;
2005-01-21 03:23:26 +00:00
$this -> instance_config_save ( $this -> config );
2004-12-30 18:08:38 +00:00
return $returnstring ;
2004-12-14 18:09:20 +00:00
}
}
2005-01-30 04:55:27 +00:00
?>