<?php //$Id: filter.php,v 1.2 2006/12/19 23:15:53 stronk7 Exp $

///////////////////////////////////////////////////////////////////////////
//                                                                       //
// NOTICE OF COPYRIGHT                                                   //
//                                                                       //
// Moodle - Modular Object-Oriented Dynamic Learning Environment         //
//          http://moodle.com                                            //
//                                                                       //
// Copyright (C) 2001-3001 Martin Dougiamas        http://dougiamas.com  //
//           (C) 2001-3001 Eloy Lafuente (stronk7) http://contiento.com  //
//                                                                       //
// 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:                          //
//                                                                       //
//          http://www.gnu.org/copyleft/gpl.html                         //
//                                                                       //
///////////////////////////////////////////////////////////////////////////

// This filter allows you to display videos from different sources (youtube,
// google...) inside any Moodle text (posts, resources...)
//                                                                         
// Syntax for videos is:
//     [[mm:source:reference|title]]
// where:
//     mm:        acronym of "MultiMovie", must be always present.
//     source:    one of these are supported: "youtube" or "google" (without
//                the quotes!).
//     reference: reference of the video to display (can be easily copied  
//                from the URL when displaying the video on the source site)
//     title:     free text to be displayed in the popup that will be
//                generated if more than one video is introduced.
//                                                                         
// That's all. Happy Christmas! Eloy Lafuente (stronk7) 20061219.

function multimovie_filter($courseid, $text) {

    $u = empty($CFG->unicodedb) ? '' : 'u'; //Unicode modifier

       preg_match_all('/\[\[mm:(youtube|google|teachertube|voicethread):(.*?)(\|(.*?))?\]\]/s'.$u, $text, $list_of_movies);

/// No multimovie links found. Return original text
    if (empty($list_of_movies[0])) {
        return $text;
    }

    foreach ($list_of_movies[0] as $key=>$item) {
        $replace = '';
    /// Extract info from the multimovie link
        $movie = new stdClass;
        $movie->type = $list_of_movies[1][$key];
        $movie->reference = $list_of_movies[2][$key];
        $movie->title = $list_of_movies[4][$key];
    /// Calculate footer text (it's optional in the filter)
        if ($movie->title) {
            $footertext = '<br /><span class="filtermultimovie-title">'.format_string($movie->title).'</span>';
        } else {
            $footertext = '';
        }
    /// Calculate the replacement
        if ($movie->type == 'youtube') {
            $replace = '<div class="filtermultimovie-container">'.
                       '<object type="application/x-shockwave-flash" '.
                       'data="http://www.youtube.com/v/'.$movie->reference.'" '.
                       'width="425" height="350">'.
                       '<param name="movie" value="http://www.youtube.com/v/'.$movie->reference.'" />'.
                       '<param name="wmode" value="transparent" />'.
                       '</object>'.$footertext.'</div>';
        } else if ($movie->type == 'google') {
            $replace = '<div id="filtermultimovie-container">'.
                       '<object type="application/x-shockwave-flash" '.
                       'data="http://video.google.com/googleplayer.swf?docId='.$movie->reference.'&amp;hl=en&amp;playerMode=embedded" '.
                       'width="400" height="326">'.
                       '<param name="movie" value="http://video.google.com/googleplayer.swf?docId='.$movie->reference.'&amp;hl=en&amp;playerMode=simple" />'.
                       '<param name="wmode" value="transparent" />'.
                       '</object>'.$footertext.'</div>';
        } else if ($movie->type == 'teachertube') {
            $replace = '<div class="filtermultimovie-container">'.
                       '<object type="application/x-shockwave-flash" '.
                       'data="http://www.teachertube.com/flvplayer.swf" '.
                       'width="450" height="350" >'.
                       '<param name="FlashVars" value="config=http://www.teachertube.com/flvplayer.php?viewkey='.$movie->reference.
                                                      '&amp;vimg=http://www.teachertube.com/thumb/10.jpg" />'.
                       '<param name="wmode" value="transparent" />'.
                       '</object>'.$footertext.'</div>';
        } else if ($movie->type == 'voicethread') {
            $replace = '<div id="filtermultimovie-container">'.
                       '<object width="480" height="360"> '.
                       '<param name="movie" value="http://voicethread.com/book.swf?b='.$movie->reference.'"></param> '.
                       '<param name="wmode" value="transparent"></param>'.
                       '<embed src="http://voicethread.com/book.swf?b='.$movie->reference.'" type="application/x-shockwave-flash" wmode="transparent" width="480" height="360"></embed>'.
                       '</object>'.$footertext.'</div>';
        }
    /// If replace found, do it
        if ($replace) {
            $text = str_replace($list_of_movies[0][$key], $replace, $text);
        }
    }

/// Finally, return the text
    return $text;
}
?>
