<?php //$Id: filter.php,v 1.8 2010/01/16 19:56:49 dlnsk Exp $

///////////////////////////////////////////////////////////////////////////
//                                                                       //
// This program is part of Moodle - Modular Object-Oriented Dynamic      //
// Learning Environment - http://moodle.org                              //
//                                                                       //
// Copyright (C) 2008  Dmitry Pupinin <dlnsk@ngs.ru>                     //
//                                                                       //
// 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                         //
//                                                                       //
///////////////////////////////////////////////////////////////////////////

function hiddentext_filter($courseid, $text) {
    global $CFG;

    if (empty($text) or is_numeric($text)) {
        return $text;
    }
	
    //When <> tags has written in HTMLeditor, parameters can be sorted alphabetically. Is Typo3 do it?
    $search_span = '/<span((\s+filter="hiddentext"|\s+class=".*?"|\s+desc=".*?"){0,2})\s*>(.*?)<\/span>/is';
    $search_div  =  '/<div((\s+filter="hiddentext"|\s+class=".*?"|\s+desc=".*?"){0,2})\s*>(.*?)<\/div>/is';
    
    $result = preg_replace_callback($search_span, 'hiddentext_filter_callback', $text);
    $result = preg_replace_callback($search_div,  'hiddentext_filter_callback', $result);

    // For TinyMCE editor
    list($search_span, $search_div) = str_replace(array('<', '>'),
                                                  array('\[', '\]'),
                                                  array($search_span, $search_div));

    $result = preg_replace_callback($search_span, 'hiddentext_filter_callback', $result);
    $result = preg_replace_callback($search_div,  'hiddentext_filter_callback', $result);

    // For HTMLeditor
    list($search_span, $search_div) = str_replace(array('"'), 
                                                  array('&quot;'), 
                                                  array($search_span, $search_div));

    $result = preg_replace_callback($search_span, 'hiddentext_filter_callback_html', $result);
    $result = preg_replace_callback($search_div,  'hiddentext_filter_callback_html', $result);

    if (is_null($result)) {
        return "ERROR in filter HiddenText<br/>".$text; //error during regex processing (too many nested spans?)
    } else {
        return $result;
    }
}

function hiddentext_filter_callback($matches) {
    return hiddentext_filter_impl($matches, '"');
}

function hiddentext_filter_callback_html($matches) {
    return hiddentext_filter_impl($matches, '&quot;');
}

function hiddentext_filter_impl($matches, $quot) {
    global $CFG;
    
    if (strpos($matches[1], "filter={$quot}hiddentext{$quot}") === false) {
    	return $matches[0];
    }
    
    $output =  '<link rel="stylesheet" type="text/css" href="'.$CFG->wwwroot.'/filter/hiddentext/yuidomcollapse.css" />';
    $output .= '<script type="text/javascript" src="'.$CFG->wwwroot.'/lib/yui/yahoo-dom-event/yahoo-dom-event.js"></script>';
	$output .= '<script type="text/javascript" src="'.$CFG->wwwroot.'/lib/yui/animation/animation-min.js"></script>';
	$output .= '<script type="text/javascript" src="'.$CFG->wwwroot.'/filter/hiddentext/yuidomcollapse.js"></script>';
	$output .= '<script type="text/javascript" src="'.$CFG->wwwroot.'/filter/hiddentext/yuidomcollapse-css.js"></script>'."\n";
    		   
    
   	preg_match("/class={$quot}(.*?){$quot}/is", $matches[1], $m);
   	$class = empty($m[1]) ? '' : ' class="'.$m[1].'"';
   	
   	preg_match("/desc={$quot}(.*?){$quot}/is", $matches[1], $m);
   	$desc = empty($m[1]) ? '' : $m[1];
   	
    if (substr($matches[0], 1, 4) == 'span') {
    	$output .= "<span class=\"ht_trigger\"><img src=\"{$CFG->wwwroot}/filter/hiddentext/show_hidden.png\" alt=\"$desc\" />".
    	           (empty($desc) ? '' : "&nbsp;$desc") .'</span>'.
    			   "<span$class>&nbsp;".$matches[3].'</span>';
    } else {
	   	if (empty($desc)) {
	   		$desc = get_string('seemore', 'hiddentext');
	   	}
    	$output .=  "<div class=\"ht_trigger\"><img src=\"{$CFG->wwwroot}/filter/hiddentext/show_hidden.png\" alt=\"\" />&nbsp;$desc</div>".
    				"<div$class>".$matches[3].'</div>';
    }
    return $output;
}

?>
