2004-12-14 18:09:20 +00:00
< ? php //$Id$
2005-06-28 21:21:59 +00:00
/*******************************************************************
2005-08-11 15:27:47 +00:00
* This file contains one class which defines a block for display on
* any Moodle page . This block can be configured to display the contents
* of a remote RSS news feed in your web site .
2005-06-28 21:21:59 +00:00
*
* @ author Daryl Hawes
* @ version $Id $
* @ license http :// www . gnu . org / copyleft / gpl . html GNU Public License
* @ package base
******************************************************************/
2005-08-11 15:27:47 +00:00
/**
* This class is for a block which defines a block for display on
* any Moodle page .
*/
class block_rss_client extends block_base {
2004-12-14 18:09:20 +00:00
function init () {
2005-05-17 15:21:32 +00:00
$this -> title = get_string ( 'feedstitle' , 'block_rss_client' );
2007-03-05 11:27:01 +00:00
$this -> version = 2006100102 ;
2004-12-14 18:09:20 +00:00
}
2005-05-10 05:39:32 +00:00
function preferred_width () {
return 210 ;
}
2006-04-05 08:35:40 +00:00
function applicable_formats () {
return array ( 'all' => true );
}
2004-12-14 18:09:20 +00:00
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
2005-05-17 15:21:32 +00:00
$this -> title = get_string ( 'remotenewsfeed' , 'block_rss_client' );
2004-12-14 18:09:20 +00:00
}
}
2005-08-11 12:45:38 +00:00
2004-12-14 18:09:20 +00:00
function get_content () {
2006-09-12 06:57:05 +00:00
global $CFG , $editing , $COURSE , $USER ;
if ( ! empty ( $COURSE )) {
$this -> courseid = $COURSE -> id ;
}
2004-12-14 18:09:20 +00:00
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 ;
2005-07-06 00:51:20 +00:00
$this -> content -> text = '' ;
$this -> content -> footer = '' ;
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
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 {
2006-09-12 06:57:05 +00:00
$shownumentries = 5 ; //default to 5 entries is not specified in admin section or instance
2004-12-14 18:09:20 +00:00
}
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 ;
2006-09-12 06:57:05 +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 );
}
}
2006-09-04 08:45:08 +00:00
2006-09-12 06:57:05 +00:00
$context = get_context_instance ( CONTEXT_BLOCK , $this -> instance -> id );
2006-09-13 04:10:41 +00:00
if ( has_capability ( 'block/rss_client:createsharedfeeds' , $context )
|| has_capability ( 'block/rss_client:createprivatefeeds' , $context )) {
2005-05-15 14:35:41 +00:00
2005-05-17 14:43:00 +00:00
$page = page_create_object ( $this -> instance -> pagetype , $this -> instance -> pageid );
2006-09-12 06:57:05 +00:00
//if ($page->user_allowed_editing()) { // for SUBMITTERS_ALL_ACCOUNT_HOLDERS we're going to run into trouble later if we show it and then they don't have write access to the page.
if ( isset ( $this -> config )) {
// This instance is configured - show Add/Edit feeds link.
$script = $page -> url_get_full (
array ( 'instanceid' => $this -> instance -> id ,
'sesskey' => $USER -> sesskey ,
'blockaction' => 'config' ,
'currentaction' => 'managefeeds' ,
2006-10-17 06:25:50 +00:00
'id' => $this -> courseid ,
'section' => 'rss'
2006-09-12 06:57:05 +00:00
));
2007-01-09 23:45:24 +00:00
$output .= '<div class="info"><a title="' . get_string ( 'feedsaddedit' , 'block_rss_client' ) . '" href="' . $script . '">' . get_string ( 'feedsaddedit' , 'block_rss_client' ) . '</a></div>' ;
2006-09-12 06:57:05 +00:00
} else {
// This instance has not been configured yet - show configure link?
if ( has_capability ( 'block/rss_client:manageanyfeeds' , $context )) {
$script = $page -> url_get_full (
array ( 'instanceid' => $this -> instance -> id ,
'sesskey' => $USER -> sesskey ,
'blockaction' => 'config' ,
'currentaction' => 'configblock' ,
2006-10-17 06:25:50 +00:00
'id' => $this -> courseid ,
'section' => 'rss'
2006-09-12 06:57:05 +00:00
));
2007-01-09 23:45:24 +00:00
$output .= '<div class="info"><a title="' . get_string ( 'feedsconfigurenewinstance' , 'block_rss_client' ) . '" href="' . $script . '">' . get_string ( 'feedsconfigurenewinstance' , 'block_rss_client' ) . '</a></div>' ;
2006-01-16 03:02:42 +00:00
}
2005-05-15 14:35:41 +00:00
}
2006-09-12 06:57:05 +00:00
//}
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 ) {
2006-10-24 22:40:07 +00:00
$output .= clean_text ( $this -> get_rss_by_id ( $rssid , $display_description , $shownumentries , ( $numids > 1 ) ? true : false ), FORMAT_HTML );
2005-01-29 03:01:18 +00:00
if ( $numids > 1 && $count != $numids - 1 && ! empty ( $rssfeedstring )) {
2007-01-09 23:45:24 +00:00
$output .= '<hr style="width=:80%" />' ;
2005-01-20 01:32:58 +00:00
}
$count ++ ;
2004-12-31 03:11:03 +00:00
}
2004-12-30 18:08:38 +00:00
}
2005-08-11 12:45:38 +00:00
2004-12-30 18:08:38 +00:00
$this -> content -> text = $output ;
return $this -> content ;
}
2005-08-11 12:45:38 +00:00
2004-12-30 18:08:38 +00:00
function instance_allow_multiple () {
return true ;
}
function has_config () {
return true ;
}
function instance_allow_config () {
return true ;
}
2005-08-11 12:45:38 +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-05-04 14:28:46 +00:00
if ( ! defined ( 'MAGPIE_OUTPUT_ENCODING' )) {
2006-11-11 17:23:20 +00:00
define ( 'MAGPIE_OUTPUT_ENCODING' , 'utf-8' ); // see bug 3107
2005-05-04 14:28:46 +00:00
}
2005-08-11 12:45:38 +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-08-11 12:45:38 +00:00
2005-01-23 16:16:28 +00:00
if ( $rss === false ) {
2006-09-18 13:24:45 +00:00
if ( debugging () && ! empty ( $rsserror )) {
2005-01-23 16:16:28 +00:00
// 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
}
2006-09-12 06:57:05 +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 )) {
2006-01-05 13:13:06 +00:00
$feedtitle = $this -> format_title ( $rss -> channel [ 'title' ]);
2005-01-28 01:21:08 +00:00
} else {
2006-01-05 13:13:06 +00:00
$feedtitle = $this -> format_title ( $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' ]) ) {
2006-12-01 10:10:56 +00:00
$returnstring .= " \n " . '<div class="image" title="' . $rss -> image [ 'title' ] . '"><a href="' . $rss -> image [ 'link' ] . '"><img src="' . $rss -> image [ 'url' ] . '" alt="' . $rss -> image [ 'title' ] . '" /></a></div>' ;
2005-01-29 03:01:18 +00:00
}
2005-01-28 01:21:08 +00:00
if ( $showtitle ) {
2005-05-17 17:52:34 +00:00
$returnstring .= '<div class="title">' . $feedtitle . '</div>' ;
2006-12-01 10:10:56 +00:00
}
2005-03-08 17:46:51 +00:00
$formatoptions -> para = false ;
2005-01-28 01:21:08 +00:00
2005-06-28 21:21:59 +00:00
// first we must verify that the rss feed is loaded
// by checking $rss and $rss->items exist before using them
if ( empty ( $rss ) || empty ( $rss -> items )) {
return '' ;
}
2005-08-11 12:45:38 +00:00
2006-09-22 15:15:48 +00:00
/// Accessibility: markup as a list.
$returnstring .= '<ul class="list">' . " \n " ;
2005-01-23 16:16:28 +00:00
foreach ( $rss -> items as $item ) {
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 ) . '...' ;
2005-05-14 07:38:57 +00:00
} else {
$item [ 'title' ] = break_up_long_words ( $item [ 'title' ], 30 );
2004-12-14 18:09:20 +00:00
}
2005-08-11 12:45:38 +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' ]);
2006-09-22 15:15:48 +00:00
$returnstring .= '<li><div class="link"><a href="' . $item [ 'link' ] . '" target="_blank">' . $item [ 'title' ] . " </a></div> \n " ;
2005-03-08 17:46:51 +00:00
2005-05-17 14:43:00 +00:00
if ( $display_description && ! empty ( $item [ 'description' ])) {
2005-05-14 07:38:57 +00:00
$item [ 'description' ] = break_up_long_words ( $item [ 'description' ], 30 );
2005-03-25 16:28:58 +00:00
$returnstring .= '<div class="description">' .
2005-03-08 17:46:51 +00:00
format_text ( $item [ 'description' ], FORMAT_MOODLE , $formatoptions , $this -> courseid ) .
2006-09-22 15:15:48 +00:00
'</div>' ;
2004-12-14 18:09:20 +00:00
}
2006-09-22 15:15:48 +00:00
$returnstring .= " </li> \n " ;
2004-12-14 18:09:20 +00:00
}
2006-09-22 15:15:48 +00:00
$returnstring .= " </ul> \n " ;
2004-12-14 18:09:20 +00:00
2005-01-27 22:47:13 +00:00
if ( ! empty ( $rss -> channel [ 'link' ])) {
2006-09-22 15:15:48 +00:00
$rss -> channel [ 'link' ] = str_replace ( '&' , '&' , $rss -> channel [ 'link' ]);
2005-01-27 22:47:13 +00:00
if ( ! empty ( $this -> config ) && isset ( $this -> config -> block_rss_client_show_channel_link ) && $this -> config -> block_rss_client_show_channel_link ) {
2005-05-17 15:21:32 +00:00
$this -> content -> footer = '<a href="' . $rss -> channel [ 'link' ] . '">' . get_string ( 'clientchannellink' , 'block_rss_client' ) . '</a>' ;
2005-01-27 22:47:13 +00:00
}
if ( ! empty ( $feedtitle ) ) {
$feedtitle = '<a href="' . $rss -> channel [ 'link' ] . '">' . $feedtitle . '</a>' ;
}
2004-12-14 18:09:20 +00:00
}
}
2005-05-18 20:09:29 +00:00
// if block has no custom title
if ( empty ( $this -> config ) || ( ! empty ( $this -> config ) && empty ( $this -> config -> title ))) {
// if the feed has a title
if ( ! empty ( $feedtitle ) and ( $feedtitle != '<a href="' . $rss -> channel [ 'link' ] . '"></a>' )) {
// set the block's title to the feed's title
$this -> title = $feedtitle ;
}
2004-12-14 18:09:20 +00:00
}
2004-12-30 18:08:38 +00:00
return $returnstring ;
2004-12-14 18:09:20 +00:00
}
2005-11-13 22:50:45 +00:00
// just strips the title down and adds ... for excessively long titles.
function format_title ( $title , $max = 64 ) {
2006-01-05 13:13:06 +00:00
/// Loading the textlib singleton instance. We are going to need it.
$textlib = textlib_get_instance ();
2006-11-11 17:23:20 +00:00
if ( $textlib -> strlen ( $title ) <= $max ) {
2005-11-13 22:50:45 +00:00
return $title ;
}
else {
2006-11-11 17:23:20 +00:00
return $textlib -> substr ( $title , 0 , $max - 3 ) . '...' ;
2005-11-13 22:50:45 +00:00
}
}
2004-12-14 18:09:20 +00:00
}
2006-12-01 10:10:56 +00:00
?>