<?php

if(!class_exists('ServiceFactory')){
	require_once(dirname(__FILE__) . '/common/ddrsimpleapi/interface/serviceFactory.php');
}
if(!class_exists('Transcoder')){
	require_once( dirname(__FILE__) . '/common/transcoding/transcoder.php');
}
if(!class_exists('FileLogger')){
	require_once( dirname(__FILE__) . '/common/logger/implementation/filelogger/filelogger.php');
}


$GLOBALS['filterName'] = 'W3CMobileAuthoringToolLibrary';
try{
	init_filter();
	$error = false;
}
catch(Exception $e){
	$log =new FileLogger();
	$log->add_message(
		'Error while initializing the transcoder. Error ' . $e->getCode() .
		' occured in ' . $e->getFile() . ' line ' . $e->getLine() .
		'. ' . $e->getMessage(),
		'error'
	);
	$error = true;
}

function init_filter(){
	global $configuration, $service, $evidence, $filterName;
	
	if(!defined('dir')){
		define("dir", "http://localhost/moodle/");
	}
	
	if(!defined('cache_img_dir')){
		define("cache_img_dir", dir."filter/".$filterName."/common/transcoding/cache");
	}
	
	$implementation_name = get_config($filterName, 'ddrsimpleapi_implementation');
	
	if(!isset($configuration)){
		$configuration = array();
		if($implementation_name == 'WURFL'){
			$path = get_config($filterName, 'wurfl_path');
			$lastChar = substr($path, strlen($path) -1);
			if($lastChar != '/' && $lastChar != '\\'){
				$path .= '/';
			}
			if(!file_exists($path)){
				throw new Exception("The given path of the wurfl files doesn't exists.");
				exit(1);
			}
			$configuration['wurfl_path'] = $path;
		}
	}
	
	if(!isset($service)){
		$service = ServiceFactory::newService($implementation_name,
				'http://www.w3.org/2008/01/ddr-core-vocabulary',
				$configuration);
	}
	
	if(!isset($evidence)){
		$evidence = $service->newHTTPEvidenceM($_SERVER);
	}
}
/**
 * The filter deals with all the content of courses before being displayed.
 * We apply all the needed transcoding actions here.
 */
function W3CMobileAuthoringToolLibrary_filter($courseid, $text){
	global $service, $evidence, $error, $filterName, $COURSE;
	if(isset($error) && $error){
		return $text;
	}
	
	$COURSE->format = "weekscss";
	try{
		$transcoder = new Transcoder($service);
		
		if(get_config($filterName, "delete_script")){
			$trans = $transcoder->newTranscodingAction('DeleteScript');
			$transcoder->addTranscodingAction($trans);
		}
		if(get_config($filterName, "delete_popup")){
			$trans = $transcoder->newTranscodingAction('DeletePopup');
			$transcoder->addTranscodingAction($trans);
		}
		if(get_config($filterName, "resize_img")){
			$trans = $transcoder->newTranscodingAction('ResizeIMG');
			if(get_config($filterName, 'remote_resizing')){
				$trans->setOption('remote_resizing', true);
			}
			$transcoder->addTranscodingAction($trans);
		}
		if(get_config($filterName, "linear_tables")){
			$trans = $transcoder->newTranscodingAction('LinearTables');
			
			$layout = settype(
				get_config($filterName, 'linear_layout_tables'),
				'bool');
			$data_v = settype(
				get_config($filterName, 'linear_data_tables_vert'),
				'bool');
			$data_h = settype(
				get_config($filterName, 'linear_data_tables_horr'),
				'bool');
			
			$trans->setOption('layout', $layout);
			$trans->setOption('data_v', $data_v);
			$trans->setOption('data_h', $data_h);
			$transcoder->addTranscodingAction($trans);
		}
		if(get_config($filterName, 'pagination')){
			$trans = $transcoder->newTranscodingAction('Pagination');
			$trans->setOption('home', 
				get_config($filterName, 'homepage'));
			$trans->setOption('home_number', 
				intVal(get_config($filterName, 'homepage_number')));
			$trans->setOption('percent', 
				intVal(get_config($filterName, 'percent')));
			$trans->setOption('max_image_size', 
				intval(get_config($filterName, 'max_image_size')));
			$transcoder->addTranscodingAction($trans);
		}
	}
	catch(Exception $e){
		$log =new FileLogger();
		$log->add_message(
			'Error while initializing the transcoder. Error ' . $e->getCode() .
			' occured in ' . $e->getFile() . ' line ' . $e->getLine() .
			'. ' . $e->getMessage(),
			'error'
		);
		return $text;
	}
	
	try{
		$return = $transcoder->apply($text, $evidence);
	}
	catch(Exception $e){
		$log =new FileLogger();
		$log->add_message(
			'Error while appling the transcoder. Error ' . $e->getCode() .
			' occured in ' . $e->getFile() . ' line ' . $e->getLine() .
			'. ' . $e->getMessage(),
			'error'
		);
		return $text;
	}
	
	return $return;
}

?>