<?php 
/**
 * New Resource type for Moodle, which formats an Echo RSS feed for display in the browser
 *
 * Chris Fryer <c.j.fryer@lse.ac.uk> 2009-10
 * © London School of Economics and Political Science
 */
class resource_echofeed extends resource_base {
    public 
$version '201011241545';
    function 
resource_echofeed($cmid=0) {
        
parent::resource_base($cmid);

        global 
$CFG;
        require_once(
$CFG->libdir .'/magpie/rss_fetch.inc');

        if (!
defined('MAGPIE_OUTPUT_ENCODING')) {
            
define('MAGPIE_OUTPUT_ENCODING''utf-8'); // see MDL-3107
        
}
    }
    public function 
add_instance($resource) {
        if (
$success parent::add_instance($resource)) {
            global 
$USER;
            
add_to_log($this->course->id"resource""add""view.php?id={$this->cm->id}"$resource->id$this->cm->id);
        }
        return 
$success;
    }
    public function 
update_instance($resource) {
        if (
$success parent::update_instance($resource)) {
            global 
$USER;
            
add_to_log($this->course->id"resource""edit""view.php?id={$this->cm->id}"$resource->id$this->cm->id);
        }
        return 
$success;
    }
    
/**
     * @desc Displays an instance of the feed.  Called by /mod/resource/view.php
     * @param void
     * @return void
     */
    
public function display() {

        global 
$CFG$USER;
        
parent::display();

        
print_header($this->resource->name$this->course->fullnamebuild_navigation($this->navlinks$this->cm),
                    
""""trueupdate_module_button($this->cm->id$this->course->id$this->strresource),
                    
navmenu($this->course$this->cm));
        if (
$rss fetch_rss($this->resource->reference)) {
            echo 
"<h2>{$rss->channel['title']}</h2>";
            foreach (
$rss->items as $item) {
                if (
$this->resource->options == 'title') {
                    
$title $item['title'];
                } else {
                    
// Pick the description apart.  This is probably going to be expensive.
                    
if (preg_match('|Capture Date/Time:</strong>(.*)<br>|m'$item['description'], $matches)) {
                        
$capturedate userdate(strtotime($matches[1]), get_string('strftimedate')); // $item['pubdate'] != $matches[1]
                    
}
                    if (
preg_match('|Room:</strong>(.*)<br>|m'$item['description'], $matches)) {
                        
$room $matches[1];
                    }
                    
$title "$capturedate: $room";
                }
                echo 
"<h3>$title</h3><ul>";
                if (
preg_match('|Echo Link:</strong> <a href="(\S*)"|'$item['description'], $matches)) {
                    echo 
'<li><a href="' $matches[1] .'">Rich media presentation</a></li>';
                }
                if (
preg_match('|Enhanced Podcast URL: </strong> <a href="(\S*)"|'$item['description'], $matches)) {
                    echo 
'<li><a href="' $matches[1] . '">Enhanced podcast</a> (m4b file)</li>';
                }
                if (
preg_match('|Vodcast URL: </strong> <a href="(\S*)"|'$item['description'], $matches)) {
                    echo 
'<li><a href="' $matches[1] . '">Vodcast</a> (m4v file)</li>';
                }
                if (
preg_match('|Audio URL: </strong> <a href="(\S*)"|'$item['description'], $matches)) {
                    echo 
'<li><a href="' $matches[1] .'">Podcast</a> (mp3 file, audio only)</li>';
                }
                echo 
'</ul>';
            }

            echo 
'<a href="' $this->resource->reference .'"><img src="' $CFG->pixpath '/i/rss.gif"
                  alt="RSS" title="' 
get_string('subscriberss''resource_echofeed') . '" /></a>';
        } else {
            
notice('<p><strong>' get_string('echofeederror''resource_echofeed') . '</strong></p><p>' magpie_error() . '</p>',
                   
"$CFG->wwwroot/course/view.php?id={$this->course->id}");
        }

        
add_to_log($this->course->id"resource""view""view.php?id={$this->cm->id}"$this->resource->id$this->cm->id);
        
print_footer($this->course);
    }
    
/**
     * @desc This is called from parent::definition() in /mod/resource/mod_form.php
     * @param object &$mform An instance of a Moodle form
     * @link http://docs.moodle.org/en/Development:lib/formslib.php_Form_Definition
     */
    
public function setup_elements(&$mform) {
        if (
$cmid optional_param('update'nullPARAM_INT)) {
            
$cm get_coursemodule_from_id('resource'$cmid);
            
$resource get_record('resource''id'$cm->instance);
        }
        
$mform->addElement('text''reference'get_string('echofeedurl''resource_echofeed'), 'size="80"');
        
$mform->addRule('reference'null'required'null'client');
        
$options = array();
        
$options[] = $mform->createElement('radio''options'''get_string('echofeedtitle''resource_echofeed'), 'title');
        
$options[] = $mform->createElement('radio''options'''get_string('echofeeddatetime''resource_echofeed'), 'datetime');

        
$mform->addGroup($options'dateortitle'get_string('echofeedtitleformat''resource_echofeed'), array(' '), false);

        if (isset(
$resource->options)) {
            
$mform->setDefault('options'$resource->options);
        } else {
            
$mform->setDefault('options''datetime');
        }

        if (!empty(
$resource->reference)) $mform->setDefault('reference'$resource->reference);
    }
}
?>