<?php

/************************************************************************/
/* DOCEBO LMS - Learning Managment System                               */
/* ============================================                         */
/*                                                                      */
/* Copyright (c) 2004                                                   */
/* http://www.docebo.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.       */
/************************************************************************/

if(!defined('IN_DOCEBO')) die('You cannot access this file directly');

if($GLOBALS['current_user']->isAnonymous())  die("You can't access");

// XXX: save status in session
function saveTestStatus($save_this) {
	require_once($GLOBALS['where_framework'].'/lib/lib.sessionsave.php' );
	$save = new Session_Save();
	$save_name = $save->getName('test');
	
	$save->save($save_name, $save_this);
	return $save_name;
}

function &loadTestStatus($save_name) {
	require_once($GLOBALS['where_framework'].'/lib/lib.sessionsave.php' );
	$save = new Session_Save();
	
	return $save->load($save_name);
}

// XXX: addtest
function addtest( $object_test ) {
	checkPerm('view', false, 'storage');
	
	$lang =& DoceboLanguage::createInstance('test');
	if( !is_a($object_test, 'Learning_Test') ) {
		$_SESSION['last_error'] = $lang->def('_TEST_INCORRECTOBJECT');
		jumpTo( ''.$object_test->back_url.'&amp;create_result=0');
	}
	
	require_once($GLOBALS['where_framework'].'/lib/lib.form.php' );
	$url_encode = htmlentities(urlencode($object_test->back_url));
	
	
	$GLOBALS['page']->add(getTitleArea($lang->def('_TEST_SECTION'), 'test')
		.'<div class="std_block">'
		.getBackUi(ereg_replace('&', '&amp;', $object_test->back_url).'&amp;create_result=0', $lang->def('_BACK'))
		.Form::getFormHeader($lang->def('_TEST_ADD_FORM'))
		.Form::OpenForm('addtest_form', 'index.php?modname=test&amp;op=instest')
		
		.Form::openElementSpace()
		.Form::getHidden('back_url', 'back_url', htmlentities(urlencode($object_test->back_url)))
		.Form::getTextfield($lang->def('_TITLE'), 'title', 'title', '255')
		.Form::getTextarea($lang->def('_DESCRIPTION'), 'textof', 'textof')
		.Form::closeElementSpace()
		
		.Form::openButtonSpace()
		.Form::getButton('button_ins', 'button_ins', $lang->def('_TEST_INSERT'))
		.Form::closeButtonSpace()
		
		.Form::closeForm()
		.'</div>', 'content');
}

// XXX: instest
function instest() {
	checkPerm('view', false, 'storage');
	$lang =& DoceboLanguage::createInstance('test');
	
	if( trim($_POST['title']) == '' ) $_POST['title'] = $lang->def('_NOTITLE');
	
	$ins_query = "
	INSERT INTO ".$GLOBALS['prefix_lms']."_test 
	( author, title, description )
		VALUES 
	( '".(int)getLogUserId()."', '".$_POST['title']."', '".$_POST['textof']."' )";
	
	if( !mysql_query($ins_query) ) {
		
		$_SESSION['last_error'] = $lang->def('_TEST_ERR_INSERT');
		jumpTo( ''.urldecode($_POST['back_url']).'&create_result=0' );
	}
	
	list($id_test) = mysql_fetch_row(mysql_query("SELECT LAST_INSERT_ID()"));
	if($id_test > 0) jumpTo( ''.urldecode($_POST['back_url']).'&id_lo='.$id_test.'&create_result=1' );
	else jumpTo( ''.urldecode($_POST['back_url']).'&create_result=0' );
}

// XXX: modtest
function modtest() {
	checkPerm('view', false, 'storage');
	$lang =& DoceboLanguage::createInstance('test');
	
	require_once($GLOBALS['where_framework'].'/lib/lib.form.php');
	$id_test = importVar('idTest', true, 0);
	$back_url = urldecode(importVar('back_url'));
	$url_encode = htmlentities(urlencode($back_url));
	
	list($test_title, $textof) = mysql_fetch_row( mysql_query("
	SELECT title, description
	FROM ".$GLOBALS['prefix_lms']."_test
	WHERE idTest = '".$id_test."'") );
	
	$GLOBALS['page']->add(
		getTitleArea($lang->def('_TEST_SECTION'), 'test')
		.'<div class="std_block">'
		.getBackUi('index.php?modname=test&amp;op=modtestgui&amp;idTest='.$id_test.'&amp;back_url='.$url_encode, $lang->def('_BACK'))
		.Form::getFormHeader($lang->def('_TEST_MOD_FORM'))
		.Form::OpenForm('addtest_form', 'index.php?modname=test&amp;op=uptest')
		
		.Form::openElementSpace()
		.Form::getHidden('idTest', 'idTest', $id_test)
		.Form::getHidden('back_url', 'back_url', $url_encode)
		.Form::getTextfield($lang->def('_TITLE'), 'title', 'title', '255', $test_title)
		.Form::getTextarea( $lang->def('_DESCRIPTION'), 'textof', 'textof', $textof)
		.Form::closeElementSpace()
	
		.Form::openButtonSpace()
		.Form::getButton('button_ins', 'button_ins', $lang->def('_SAVE'))
		.Form::closeButtonSpace()
	
		.Form::closeForm()
		.'</div>', 'content');
}

// XXX: uptest
function uptest() {
	checkPerm('view', false, 'storage');
	$lang =& DoceboLanguage::createInstance('test');
	
	if( trim($_POST['title']) == '' ) $_POST['title'] = $lang->def('_NOTITLE');
	
	$id_test = importVar('idTest', true, 0);
	$back_url = urldecode(importVar('back_url'));
	$url_encode = htmlentities(urlencode($back_url));
	
	$mod_query = "
	UPDATE ".$GLOBALS['prefix_lms']."_test
	SET title = '".$_POST['title']."', 
		description = '".$_POST['textof']."' 
	WHERE idTest = '".$id_test."'";
	
	if( !mysql_query($mod_query) ) {
		
		errorCommunication($lang->def('_ERR_UPD_TEST')
			.getBackUi('index.php?modname=test&amp;op=modtest&amp;idTest='.$id_test.'&amp;back_url='.$url_encode));
		return;
	}
	require_once($GLOBALS['where_lms'].'/class.module/track.object.php');
	Track_Object::updateObjectTitle($id_test, 'test', $_POST['title']);
	
	jumpTo( 'index.php?modname=test&op=modtestgui&idTest='.$id_test.'&back_url='.$url_encode );
}

// XXX: modtestgui
function modtestgui( $object_test ) {
	checkPerm('view', false, 'storage');
	$lang =& DoceboLanguage::createInstance('test');
	
	cout(
		'<!--CSS file (default YUI Sam Skin) -->'."\n"
		.'<link type="text/css" rel="stylesheet" href="'.$GLOBALS['where_framework_relative'].'/addons/yui/button/assets/skins/sam/button.css">'."\n"
		.'<link type="text/css" rel="stylesheet" href="'.$GLOBALS['where_framework_relative'].'/addons/yui/menu/assets/skins/sam/menu.css">'."\n"
		.'<link type="text/css" rel="stylesheet" href="'.$GLOBALS['where_framework_relative'].'/addons/yui/container/assets/skins/sam/container.css"> '."\n"
		
		.'<!-- Dependencies -->'."\n"
		.'<script type="text/javascript" src="'.$GLOBALS['where_framework_relative'].'/addons/yui/yahoo-dom-event/yahoo-dom-event.js"></script>'."\n"
		.'<script type="text/javascript" src="'.$GLOBALS['where_framework_relative'].'/addons/yui/element/element-beta-min.js"></script>'."\n"

		.'<script type="text/javascript" src="'.$GLOBALS['where_framework_relative'].'/addons/yui/container/container_core-min.js"></script>'."\n"
		.'<script type="text/javascript" src="'.$GLOBALS['where_framework_relative'].'/addons/yui/menu/menu-min.js"></script>'."\n"
		
		.'<script type="text/javascript" src="'.$GLOBALS['where_framework_relative'].'/addons/yui/button/button-min.js"></script>'."\n"
	
		.'<!-- OPTIONAL: Animation (not required if not enabling animation) -->'."\n"
		.'<script type="text/javascript" src="'.$GLOBALS['where_framework_relative'].'/addons/yui/animation/animation-min.js"></script>'."\n"

		.'<!-- Source file -->'."\n"
		.'<script type="text/javascript" src="'.$GLOBALS['where_framework_relative'].'/addons/yui/container/container-min.js"></script>'."\n"
		
		.'<script type="text/javascript" src="'.$GLOBALS['where_lms_relative'].'/modules/quest_bank/ajax.quest_bank.js"></script>'."\n"
	
	, 'page_head');
	addCss('style_yui_docebo');
	
	// ----------------------------------------------------------------------------------------
	
	if( !is_a($object_test, 'Learning_Test') ) {
		$_SESSION['last_error'] = $lang->def('_TEST_INCORRECTOBJECT');
		jumpTo( ''.$object_test->back_url.'&amp;create_result=0');
	}
	
	require_once($GLOBALS['where_framework'].'/lib/lib.newtypeone.php');
	require_once($GLOBALS['where_framework'].'/lib/lib.form.php');
	$url_encode = htmlentities(urlencode($object_test->back_url));
	
	list($test_title) = mysql_fetch_row( mysql_query("
	SELECT title 
	FROM ".$GLOBALS['prefix_lms']."_test 
	WHERE idTest = '".$object_test->getId()."'") );
	
	$re_quest = mysql_query("
	SELECT idQuest, type_quest, title_quest, sequence, page 
	FROM ".$GLOBALS['prefix_lms']."_testquest 
	WHERE idTest = '".$object_test->getId()."'
	ORDER BY sequence");
	
	$num_quest = mysql_num_rows($re_quest);
	list($num_page) = mysql_fetch_row(mysql_query("
	SELECT MAX(page) 
	FROM ".$GLOBALS['prefix_lms']."_testquest 
	WHERE idTest = '".$object_test->getId()."'"));
	$num_page = (int)$num_page;
	
	$GLOBALS['page']->add(
		getTitleArea($lang->def('_TEST_SECTION'), 'test')
		.'<div class="std_block yui-skin-docebo yui-skin-sam">'
		.getBackUi(ereg_replace('&','&amp;', $object_test->back_url), $lang->def('_BACK')), 'content');
	if(isset($_GET['mod_operation'])) {
		if($_GET['mod_operation']) $GLOBALS['page']->add(getResultUi($lang->def('_QUEST_MOD_OK')), 'content');
		else $GLOBALS['page']->add(getResultUi($lang->def('_QUEST_ERR_MODIFY')), 'content');
	}
	//other areas
	$GLOBALS['page']->add('<span class="text_bold">'.$lang->def('_TITLE').' : </span>'.$test_title.'<br />'
		.'<div class="mod_container">'
		.'<a href="index.php?modname=test&amp;op=modtest&amp;idTest='
		.$object_test->getId().'&amp;back_url='.$url_encode.'" title="'.$lang->def('_REG_MODTEST_TITLE').'">'
		.'<img src="'.getPathImage().'standard/mod.gif" alt="'.$lang->def('_MOD').'" />&nbsp;'.$lang->def('_REG_MODTEST').'</a>'
		.'</div>'
		
		.'<br /><div class="align_left">'
		//test modality
		.'<div class="link_button">'
		.'<a href="index.php?modname=test&amp;op=defmodality&amp;idTest='
		.$object_test->getId().'&amp;back_url='.$url_encode.'" title="'.$lang->def('_TEST_MODALITY').'">'
			.$lang->def('_TEST_MODALITY').'</a>'
		.'</div>&nbsp;&nbsp;'
		//test times
		.'<div class="link_button">'
		.'<a href="index.php?modname=test&amp;op=deftime&amp;idTest='
		.$object_test->getId().'&amp;back_url='.$url_encode.'" title="'.$lang->def('_TEST_COMPILE_TIME').'">'
			.$lang->def('_TEST_COMPILE_TIME').'</a>'
		.'</div>&nbsp;&nbsp;'
		//test point assign
		.'<div class="link_button">'
		.'<a href="index.php?modname=test&amp;op=defpoint&amp;idTest='
		.$object_test->getId().'&amp;back_url='.$url_encode.'" title="'.$lang->def('_TEST_POINT_ASSIGNEMENT').'">'
			.$lang->def('_TEST_POINT_ASSIGNEMENT').'</a>'
		.'</div>'
		.'</div><br />', 'content');
	
	$caption = str_replace('%tot_page%' , $num_page, str_replace('%tot_element%' ,$num_quest , $lang->def('_TEST_CAPTION')));
	
	$tab = new TypeOne( 0, $caption, $lang->def('_TEST_SUMMARY'));
 
	$tab->setColsStyle(array('image', 'image', '', 'image', 'image', 'image', 'image', 'image'));
	$tab->addHead(
		array($lang->def('_QUEST'), $lang->def('_TYPE'), $lang->def('_TEST_QUEST_TITLE'), $lang->def('_TEST_QUEST_ORDER'), 
		'<img src="'.getPathImage().'standard/down.gif" alt="'.$lang->def('_DOWN').'" longdesc="'.$lang->def('_MOVE_DOWN').'" />', 
		'<img src="'.getPathImage().'standard/up.gif" alt="'.$lang->def('_UP').'" longdesc="'.$lang->def('_MOVE_UP').'" />', 
		'<img src="'.getPathImage().'standard/mod.gif" alt="'.$lang->def('_MOD').'" longdesc="'.$lang->def('_TEST_MODTEST').'" />', 
		'<img src="'.getPathImage().'standard/rem.gif" alt="'.$lang->def('_DEL').'" longdesc="'.$lang->def('_TEST_REMTEST').'" />' ));
	
	$i = 0;
	$correct_sequence = 1;
	$seq_error_detected = false;
	
	$quest_num = 1;
	$title_num = 1;
	$last_type = '';
	$uri_back = '&amp;back_url='.$url_encode;
	while(list($id_quest, $type, $title, $sequence, $page) = mysql_fetch_row($re_quest)) {
		
		$last_type = $type;
		$content = array(
		( (($type != 'break_page') && ($type != 'title')) ? '<span class="text_bold">'.($quest_num++).'</span>' : '' ),
		$lang->def('_QUEST_ACRN_'.strtoupper($type)),
		$title,
		$sequence, 
		( ($i != ($num_quest - 1)) ? 
			'<a href="index.php?modname=test&amp;op=movedown&amp;idQuest='.$id_quest.$uri_back.'" title="'.$lang->def('_MOVE_DOWN').'">'
				.'<img src="'.getPathImage().'standard/down.gif" alt="'.$lang->def('_DOWN').' : '.$lang->def('_TEST_ROWS').' '.($i + 1).'" /></a>' : '' ),
		( ($i != 0) ? 
			'<a href="index.php?modname=test&amp;op=moveup&amp;idQuest='.$id_quest.$uri_back.'" title="'.$lang->def('_MOVE_UP').'">'
				.'<img src="'.getPathImage().'standard/up.gif" alt="'.$lang->def('_UP').' : '.$lang->def('_TEST_ROWS').' '.($i + 1).'" /></a>' : '' ),
				
		( $type != 'break_page' ? '<a href="index.php?modname=test&amp;op=modquest&amp;idQuest='.$id_quest.$uri_back.'" title="'.$lang->def('_TEST_MODTEST').'">'
			.'<img src="'.getPathImage().'standard/mod.gif" alt="'.$lang->def('_MOD').' : '.$lang->def('_TEST_ROWS').' '.($i + 1).'" longdesc="'.$lang->def('_TEST_MODTEST').'" /></a>' : '' ),
		'<a href="index.php?modname=test&amp;op=delquest&amp;idQuest='.$id_quest.$uri_back.'" title="'.$lang->def('_TEST_REMTEST').'">'
			.'<img src="'.getPathImage().'standard/rem.gif" alt="'.$lang->def('_DEL').' : '.$lang->def('_TEST_ROWS').' '.($i + 1).'" longdesc="'.$lang->def('_TEST_REMTEST').'" /></a>',
		);
		
		$tab->addBody($content);
		if($sequence != $correct_sequence) $seq_error_detected = true;
		$correct_sequence++;
		++$i;
	}
	
	//------------------------------------------------------------------
	if($num_quest > 1) {
		$move_quest = '<form class="align_right" method="post" action="index.php?modname=test&amp;op=movequest">'
			.'<div>'
			.'<input type="hidden" name="back_url" value="'.$url_encode.'" />'
			.'<input type="hidden" name="idTest" value="'.$object_test->getId().'" />';
		$move_quest .= '<label class="text_bold" for="source_quest">'.$lang->def('_TEST_MOVE_QUEST').'</label>&nbsp;'
			.'<select id="source_quest" name="source_quest">';
		for( $opt = 1; $opt <= $i; $opt++ ) {
			$move_quest .= '<option value="'.$opt.'"'
				.( $opt == 1 ? ' selected="selected"' : '' ).'>'.$lang->def('_TEST_MOVEQUEST').' '.$opt.'</option>';
		}
		$move_quest .= '</select>';
		$move_quest .= '<label class="text_bold" for="dest_quest"> '.$lang->def('_TEST_LABEL_AFTER_QUEST').'</label>&nbsp;'
			.'<select id="dest_quest" name="dest_quest">'
			.'<option value="1" selected="selected">'.$lang->def('_TEST_FIRST_QUEST').'</option>';
		for( $opt = 1; $opt < $i; $opt++ ) {
			$move_quest .= '<option value="'.($opt + 1).'">'.$lang->def('_TEST_AFTER_QUEST').' '.$opt.'</option>';
		}
		$move_quest .= '<option value="'.($i + 1).'">'.$lang->def('_TEST_LAST_QUEST').'</option>';
		$move_quest .= '</select>';
		$move_quest .= '&nbsp;<input class="button_nowh" type="submit" name="move_quest" value="'.$lang->def('_ALT_MOVE').'" />'
			.'</div>'
			.'</form>';
		$tab->addActionAdd( $move_quest );
	}
	//------------------------------------------------------------------
/*	$re_type = mysql_query("
	SELECT type_quest 
	FROM ".$GLOBALS['prefix_lms']."_quest_type
	ORDER BY sequence");
	$add_quest = '<form method="post" action="index.php?modname=test&amp;op=addquest">'
		.'<div>'
		.'<input type="hidden" name="back_url" value="'.$url_encode.'" />'
		.'<input type="hidden" name="idTest" value="'.$object_test->getId().'" />';
	$add_quest .= '<label class="text_bold" for="add_test_quest">'.$lang->def('_TEST_ADDQUEST').'</label>&nbsp;'
		.'<select id="add_test_quest" name="add_test_quest">';
	while(list($type_quest) = mysql_fetch_row($re_type)) {
		$add_quest .= '<option value="'.$type_quest.'"'
		.( $last_type == $type_quest ? ' selected="selected"' : '' ).'>'
		.$lang->def('_QUEST_ACRN_'.strtoupper($type_quest)).' - '.$lang->def('_QUEST_'.strtoupper($type_quest)).'</option>';
	}
	$add_quest .= '</select>';
	$add_quest .= '&nbsp;<input class="button_nowh" type="submit" name="add_quest" value="'.$lang->def('_ADD').'" />'
		.'</div>'
		.'</form>';*/
	
	$re_type = mysql_query("
	SELECT type_quest 
	FROM ".$GLOBALS['prefix_lms']."_quest_type
	ORDER BY sequence");
	
	$add_quest = '<form method="post" action="index.php?modname=test&amp;op=addquest">'
		.'<input type="hidden" name="back_url" value="'.$url_encode.'" />'
		.'<input type="hidden" name="idTest" value="'.$object_test->getId().'" />'
		
		.'<input type="submit" id="add_quest" name="add_quest" value="'.$lang->def('_TEST_ADDQUEST').'">
		<select id="add_test_quest" name="add_test_quest">';         
	while(list($type_quest) = mysql_fetch_row($re_type)) {
		
		$add_quest .= '<option value="'.$type_quest.'">'
			.$lang->def('_QUEST_ACRN_'.strtoupper($type_quest)).' - '.$lang->def('_QUEST_'.strtoupper($type_quest))
			.'</option>';
	}
	$add_quest .= '</select>'
		.'</form>';
			
	//------------------------------------------------------------------
	//$tab->addActionAdd( $add_quest, '' );
	$GLOBALS['page']->add(
		$tab->getTable()
		.$add_quest
		.getBackUi(ereg_replace('&','&amp;', $object_test->back_url), $lang->def('_BACK')), 'content');
	
	/*
	$GLOBALS['page']->add(
		Form::openForm('add_question', 'index.php?modname=test&amp;op=importquest', false, false, 'multipart/form-data')
		
		.Form::openElementSpace()
		.Form::getOpenFieldset($lang->def('_IMPORT_FROM_XML'))
		.Form::getHidden('back_url', 'back_url', $url_encode)
		.Form::getHidden('idTest', 'idTest', $object_test->getId())
		.Form::getFilefield($lang->def('_FILE'), 'xml_file', 'xml_file')
		.Form::getCloseFieldset()
		.Form::closeElementSpace()
		
		.Form::openButtonSpace()
		.form::getButton('import', 'import', $lang->def('_IMPORT'))
		.Form::closeButtonSpace()
		
		.Form::closeForm()
	, 'content');
	*/
	
	
	if($seq_error_detected) {
		
		$GLOBALS['page']->add(
			' <a href="index.php?modname=test&amp;op=fixsequence&amp;idTest='.$object_test->getId().$uri_back.'" title="'.$lang->def('_FIX_SEQ_ERROR').'">'
			.$lang->def('_SEQUENCE_ERROR').'</a>'
		, 'content');
	}
	
	require_once($GLOBALS['where_lms'].'/lib/lib.quest_bank.php');
	$qb_man 	= new QuestBankMan();
	$supported_format = $qb_man->supported_format();
	
	$GLOBALS['page']->add(
		'<form method="post" action="index.php?modname=test&amp;op=modtestgui">'
		.'<input type="hidden" name="back_url" value="'.$url_encode.'" />'
		.'<input type="hidden" name="idTest" value="'.$object_test->getId().'" />'
		
		.'<div class="align_right">
			<input type="submit" id="export_quest" name="export_quest" value="'.$lang->def('_EXPORT').'">
			<select id="export_quest_select" name="export_quest_select">', 'content');
		foreach($supported_format as $id_exp => $def) {
			
			cout('<option value="'.$id_exp.'">'.$def.'</option>', 'content');
		}           
		cout('</select>
	
			<input type="submit" id="import_quest" name="import_quest" value="'.$lang->def('_IMPORT').'">
		</div>'
		.'</form>'
	, 'content');
	
	$GLOBALS['page']->add('
	<script type="text/javascript">
	YAHOO.util.Event.addListener(window, "load", function() {
		var oSplitExport = new YAHOO.widget.Button("export_quest", { type: "menu", menu: "export_quest_select" });
		var oPushImport = new YAHOO.widget.Button("import_quest");
		var oSplitAddQuest = new YAHOO.widget.Button("add_quest", { type: "menu", menu: "add_test_quest" });
	});
	</script>', 'content');
	
	$GLOBALS['page']->add('</div>', 'content');
	//fixPageSequence($object_test->getId());
}

// XXX: movequestion
function movequestion($direction) {
	checkPerm('view', false, 'storage');
	$lang =& DoceboLanguage::createInstance('test');
	
	
	$idQuest = importVar('idQuest', true, 0);
	$back_url = urldecode(importVar('back_url'));
	$back_coded = htmlentities(urlencode($back_url));
	
	list($seq, $idTest) = mysql_fetch_row(mysql_query("
	SELECT sequence, idTest 
	FROM ".$GLOBALS['prefix_lms']."_testquest 
	WHERE idQuest = '$idQuest'"));
	
	if($direction == 'up') {
		mysql_query("
		UPDATE ".$GLOBALS['prefix_lms']."_testquest 
		SET sequence = '$seq' 
		WHERE idTest = '$idTest' AND sequence = '".($seq - 1)."'");
		mysql_query("
		UPDATE ".$GLOBALS['prefix_lms']."_testquest  
		SET sequence = sequence - 1 
		WHERE idTest = '$idTest' AND idQuest = '$idQuest'");
		
	}
	if($direction == 'down') {
		mysql_query("
		UPDATE ".$GLOBALS['prefix_lms']."_testquest 
		SET sequence = '$seq' 
		WHERE idTest = '$idTest' AND sequence = '".($seq + 1)."'");
		mysql_query("
		UPDATE ".$GLOBALS['prefix_lms']."_testquest 
		SET sequence = '".($seq + 1)."' 
		WHERE idTest = '$idTest' AND idQuest = '$idQuest'");
	}
	fixPageSequence($idTest);
	jumpTo( 'index.php?modname=test&op=modtestgui&idTest='.$idTest.'&back_url='.$back_coded);
}

// XXX: movequestion from to
function movequest() {
	checkPerm('view', false, 'storage');
	$lang =& DoceboLanguage::createInstance('test');
	
	
	$idTest = importVar('idTest', true, 0);
	$back_url = urldecode(importVar('back_url'));
	$back_coded = htmlentities(urlencode($back_url));
	$source_seq = importVar('source_quest', true, 0);
	$dest_seq = importVar('dest_quest', true, 0);
	
	list($idQuest) = mysql_fetch_row(mysql_query("
	SELECT idQuest 
	FROM ".$GLOBALS['prefix_lms']."_testquest 
	WHERE idTest = '$idTest' AND sequence = '$source_seq'"));
	
	if($source_seq < $dest_seq) {
		
		mysql_query("
		UPDATE ".$GLOBALS['prefix_lms']."_testquest 
		SET sequence = sequence - 1 
		WHERE idTest = '$idTest' AND sequence > '".($source_seq)."'  AND sequence < '".($dest_seq)."'");
		mysql_query("
		UPDATE ".$GLOBALS['prefix_lms']."_testquest 
		SET sequence = '".($dest_seq - 1)."' 
		WHERE idQuest = '$idQuest'");
	} else {
		
		mysql_query("
		UPDATE ".$GLOBALS['prefix_lms']."_testquest 
		SET sequence = sequence + 1 
		WHERE idTest = '$idTest' AND sequence >= '".($dest_seq)."'  AND sequence < '".($source_seq)."'");
		
		mysql_query("
		UPDATE ".$GLOBALS['prefix_lms']."_testquest 
		SET sequence = '$dest_seq' 
		WHERE idQuest = '$idQuest'");
	}
	fixPageSequence($idTest);
	jumpTo( 'index.php?modname=test&op=modtestgui&idTest='.$idTest.'&back_url='.$back_coded);
}

function fixQuestSequence() {
	checkPerm('view', false, 'storage');
	
	$idTest = importVar('idTest', true, 0);
	$back_url = urldecode(importVar('back_url'));
	$back_coded = htmlentities(urlencode($back_url));
	
	$re_quest = mysql_query("
	SELECT idQuest, sequence 
	FROM ".$GLOBALS['prefix_lms']."_testquest 
	WHERE idTest = '$idTest' 
	ORDER BY page, sequence");
	$seq = 1;
	while(list($id_quest) = mysql_fetch_row($re_quest)) {
		
		mysql_query("
		UPDATE ".$GLOBALS['prefix_lms']."_testquest 
		SET sequence = '$seq' 
		WHERE idQuest = '$id_quest'");
		$seq++;
	}
	fixPageSequence($idTest);
	jumpTo( 'index.php?modname=test&op=modtestgui&idTest='.$idTest.'&back_url='.$back_coded);
}

// XXX: fixPageSequence
function fixPageSequence($id_test) {
	checkPerm('view', false, 'storage');
	$lang =& DoceboLanguage::createInstance('test');
	
	list($tot_quest) = mysql_fetch_row(mysql_query("
	SELECT COUNT(*) 
	FROM ".$GLOBALS['prefix_lms']."_testquest 
	WHERE idTest = '".$id_test."'"));
	
	$re_break_page = mysql_query("
	SELECT sequence 
	FROM ".$GLOBALS['prefix_lms']."_testquest 
	WHERE idTest = '".$id_test."' AND type_quest = 'break_page'
	ORDER BY sequence");
	
	$page_num = 1;
	//first page 
	$ini_seq = 0;
	while(list($break_sequence) = mysql_fetch_row($re_break_page)) {
		
		mysql_query("
		UPDATE ".$GLOBALS['prefix_lms']."_testquest
		SET page = '".(int)$page_num."'
		WHERE idTest = '".(int)$id_test."' AND 
			sequence > '".(int)$ini_seq."' AND sequence <= '".(int)$break_sequence."'");
		$ini_seq = $break_sequence;
		++$page_num;
	}
	mysql_query("
	UPDATE ".$GLOBALS['prefix_lms']."_testquest
	SET page = '".(int)$page_num."'
	WHERE idTest = '".(int)$id_test."' AND 
		sequence > '".(int)$ini_seq."' AND sequence <= '".(int)$tot_quest."'");
}

function &istanceQuest( $type_of_quest, $id ) {
	checkPerm('view', false, 'storage');
	$lang =& DoceboLanguage::createInstance('test');
	
	
	$re_quest = mysql_query("
	SELECT type_file, type_class 
	FROM ".$GLOBALS['prefix_lms']."_quest_type 
	WHERE type_quest = '".$type_of_quest."'");
	if( !mysql_num_rows($re_quest) ) return;
	list($type_file, $type_class) = mysql_fetch_row($re_quest);
	
	require_once( $GLOBALS['where_lms'].'/modules/question/'.$type_file);
	$quest_obj = eval("return new $type_class ( $id );");
	
	return $quest_obj;
}

// XXX: addquest 
function addquest() {
	checkPerm('view', false, 'storage');
	$lang =& DoceboLanguage::createInstance('test');
	
	
	$idTest = importVar('idTest', true, 0);
	
	if(isset($_POST['add_test_quest'])) {
		//first enter
		$type_quest = importVar('add_test_quest');
		$var_to_safe = array(
			'idQuest' => 0,
			'type_quest' => $type_quest,
			'idTest' => $idTest,
			'back_url' => urldecode(importVar('back_url'))
		);
		$var_save = saveTestStatus($var_to_safe);
	}
	else {
		//other enter
		$var_save = importVar('test_saved');
		$var_loaded = loadTestStatus($var_save);
		
		$idTest = $var_loaded['idTest'];
		$type_quest = $var_loaded['type_quest'];
	}
	
	require_once($GLOBALS['where_lms'].'/modules/question/question.php');
	
	quest_create($type_quest, $idTest, 'index.php?modname=test&op=modtestgui&test_saved='.$var_save);
}

// XXX: modquest 
function modquest() {
	checkPerm('view', false, 'storage');
	$lang =& DoceboLanguage::createInstance('test');
	
	
	$idQuest = importVar('idQuest', true, 0);
	
	list($idTest, $type_quest) = mysql_fetch_row(mysql_query("
	SELECT idTest, type_quest 
	FROM ".$GLOBALS['prefix_lms']."_testquest 
	WHERE idQuest = '".$idQuest."'"));
	
	if(!isset($_POST['back_url'])) {
		//first enter
		$var_to_safe = array(
			'idQuest' => $idQuest,
			'type_quest' => $type_quest,
			'idTest' => $idTest,
			'back_url' => urldecode(importVar('back_url'))
		);
		$var_save = saveTestStatus($var_to_safe);
	}
	else {
		//other enter
		$var_save = importVar('test_saved');
		$var_loaded = loadTestStatus($var_save);
		
		$idQuest = $var_loaded['idQuest'];
		$type_quest = $var_loaded['type_quest'];
	}
	
	require_once($GLOBALS['where_lms'].'/modules/question/question.php');
	
	quest_edit($type_quest, $idQuest, 'index.php?modname=test&op=modtestgui&test_saved='.$var_save);
}

// XXX: deletequest
function delquest() {
	checkPerm('view', false, 'storage');
	
	$lang =& DoceboLanguage::createInstance('test');
	
	$idQuest = importVar('idQuest', true, 0);
	$back_url = urldecode(importVar('back_url'));
	$url_coded = htmlentities(urlencode($back_url));
	
	list($idTest, $title_quest, $type_quest, $seq) = mysql_fetch_row(mysql_query("
	SELECT idTest, title_quest, type_quest, sequence 
	FROM ".$GLOBALS['prefix_lms']."_testquest 
	WHERE idQuest = '".$idQuest."'"));
	
	if(isset($_GET['confirm'])) {
		
		$quest_obj = istanceQuest($type_quest, $idQuest);
		if(!$quest_obj->del()) {

			errorCommunication($lang->def('_TEST_ERR_QUESTREM').'index.php?modname=test&amp;op=delquest&amp;idTest='.$idTest.'&amp;back_url='
				.$url_coded, $lang->def("_BACK") );
			return;
		}
		mysql_query("
		UPDATE ".$GLOBALS['prefix_lms']."_testquest 
		SET sequence = sequence -1 
		WHERE sequence > '$seq'");
		fixPageSequence($idTest);
		jumpTo( 'index.php?modname=test&op=modtestgui&idTest='.$idTest.'&back_url='.$url_coded);
	}
	else {
		$GLOBALS['page']->add(
			'<div class="std_block">'
			.getDeleteUi(	$lang->def('_TEST_AREYOUSURE'), 
							'<span class="text_bold">'.$lang->def('_TYPE').' : </span>'
							.$lang->def('_QUEST_ACRN_'.strtoupper($type_quest)).' - '.$lang->def('_QUEST_'.strtoupper($type_quest)).'<br />'
							.'<span class="text_bold">'.$lang->def('_TEST_QUEST_TITLE').' : </span>'.$title_quest, 
							
							true,
							'index.php?modname=test&amp;op=delquest&amp;idQuest='.$idQuest.'&amp;back_url='.$url_coded.'&amp;confirm=1', 
							'index.php?modname=test&amp;op=modtestgui&amp;idTest='.$idTest.'&amp;back_url='.$url_coded
						)
			.'</div>', 'content');
			/*
			.'<div class="boxinfo_title">'.$lang->def('_TEST_AREYOUSURE').'</div>'
			.'<div class="boxinfo_container">'
			.'<span class="text_bold">'.$lang->def('_TYPE').' : </span>'
			.$lang->def('_QUEST_ACRN_'.strtoupper($type_quest)).' - '.$lang->def('_QUEST_'.strtoupper($type_quest)).'<br />'
			.'<span class="text_bold">'.$lang->def('_TEST_QUEST_TITLE').' : </span>'.$title_quest
			.'</div>'
			.'<div class="del_container">'
			.'<a href="index.php?modname=test&amp;op=delquest&amp;idQuest='.$idQuest.'&amp;back_url='
				.$url_coded.'&amp;confirm=1">'
				.'<img src="'.getPathImage().'standard/rem.gif" alt="'.$lang->def('_CONFIRM').'" />&nbsp;'
				.$lang->def('_CONFIRM').'</a>&nbsp;&nbsp;'
			.'<a href="index.php?modname=test&amp;op=modtestgui&amp;idTest='.$idTest.'&amp;back_url='.$url_coded.'">'
				.'<img src="'.getPathImage().'standard/undo.gif" alt="'.$lang->def('_UNDO').'" />&nbsp;'
				.$lang->def('_UNDO').' </a>'
			.'</div>'*/
	}
}

// XXX: defmodality
function defmodality() {
	checkPerm('view', false, 'storage');
	
	$lang =& DoceboLanguage::createInstance('test');
	
	require_once($GLOBALS['where_framework'].'/lib/lib.form.php');
	
	$idTest = importVar('idTest', true, 0);
	$back_url = urldecode(importVar('back_url'));
	$url_coded = htmlentities(urlencode($back_url));
	
	list($title, $description, $display_type, $order_type, $shuffle_answer, $question_random_number, 
		$save_keep, $mod_doanswer, $can_travel, 
		$show_score,  $show_score_cat, $show_doanswer, $show_solution, 
		$max_attempt, $hide_info) = mysql_fetch_row( mysql_query("
	SELECT title, description, display_type, order_type, shuffle_answer, question_random_number, 
		save_keep, mod_doanswer, can_travel, 
		show_score, show_score_cat, show_doanswer, show_solution, 
		max_attempt, hide_info
	FROM ".$GLOBALS['prefix_lms']."_test 
	WHERE idTest = '".$idTest."'"));
	
	list($tot_quest) = mysql_fetch_row(mysql_query("
	SELECT COUNT(*) 
	FROM ".$GLOBALS['prefix_lms']."_testquest 
	WHERE idTest = '".(int)$idTest."' AND type_quest <> 'title' AND type_quest <> 'break_page'"));
	
	$GLOBALS['page']->add(
		getTitleArea($lang->def('_TEST_SECTION'), 'test')
		.'<div class="std_block">'
		.'<div class="title_big">'.$lang->def('_TEST_MODALITY').'</div>'
		.getBackUi('index.php?modname=test&amp;op=modtestgui&amp;idTest='.$idTest.'&amp;back_url='.$url_coded, $lang->def('_BACK'))
		
		.Form::openForm('defmodality', 'index.php?modname=test&amp;op=updatemodality')
		
		.'<fieldset class="configuration">'."\n"
		.'<legend>'.$lang->def('_TEST_MM_ONE').'</legend>'."\n"
		.'<div class="field_content">'
		.Form::getHidden('idTest', 'idTest', $idTest)
		.Form::getHidden('back_url', 'back_url', $url_coded)
		.Form::getRadio($lang->def('_TEST_MM1_GROUPING'), 'display_type_page', 'display_type', 0, !$display_type)
		.Form::getRadio($lang->def('_TEST_MM1_ONEFORPAGE'), 'display_type_one', 'display_type', 1, $display_type)
		.'<br />'
		//-order-----------------------------------------------------
		.'<div class="text_bold">'.$lang->def('_TEST_MM1_ORDER').'</div>'
		.Form::getRadio($lang->def('_TEST_MM1_SEQUENCE'), 'order_type_seq', 'order_type', 0, $order_type == 0)
		.Form::getRadio($lang->def('_TEST_MM1_RANDOM'), 'order_type_random', 'order_type', 1, $order_type == 1)
	, 'content');
	//-random question
	$input_field =  Form::getInputTextfield('textfield_nowh', 'question_random_number', 'question_random_number',$question_random_number, 4, '', '');
	$label = str_replace('[random_quest]', '</label>'.$input_field.'<label for="question_random_number">', $lang->def('_TEST_MM1_QUESTION_RANDOM_NUMBER'));
	
	$GLOBALS['page']->add(
		Form::openFormLine()
		.Form::getInputRadio('order_type_random_quest', 'order_type', 2, $order_type == 2, '')
		.'<label for="order_type_random_quest">'.$lang->def('_ORDER_TYPE_RANDOM').'</label> - '
		.'<label for="question_random_number">'
		.str_replace('[tot_quest]', $tot_quest, $label)
		.'</label>'
		.Form::closeFormLine()
		.'<br />'
		//-order-answer----------------------------------------------
		.'<div class="text_bold">'.$lang->def('_TEST_MM1_ANSWER_ORDER').'</div>'
		.Form::getRadio($lang->def('_TEST_MM1_ANSWER_SEQUENCE'), 'shuffle_answer_seq', 'shuffle_answer', 0, !$shuffle_answer)
		.Form::getRadio($lang->def('_TEST_MM1_ANSWER_RANDOM'), 'shuffle_answer_random', 'shuffle_answer', 1, $shuffle_answer)
		.'</div>'
		
		.'</fieldset>'
		
		.'<fieldset class="configuration">'."\n"
		.'<legend>'.$lang->def('_TEST_MM_TWO').'</legend>'."\n"
		.'<div class="field_content">'
			//visualization of the info
			.$lang->def('_TEST_MM2_HIDE_INFO').'<br />'
			.'<input class="valign_middle" type="radio" id="mod_hide_info_no" name="mod_hide_info" value="0"'
				.( !$hide_info ? '  checked="checked"' : '' ).' /> '
			.'<label for="mod_doanswer_no">'.$lang->def('_NO').'</label>&nbsp;&nbsp;'
			.'<input class="valign_middle" type="radio" id="mod_hide_info_yes" name="mod_hide_info" value="1"'
				.( $hide_info ? '  checked="checked"' : '' ).' /> '
			.'<label for="mod_doanswer_yes">'.$lang->def('_YES').'</label>'
			.'<br /><br />'
			//can modify answer
			.$lang->def('_TEST_MM2_MODANSWER').'<br />'
			.'<input class="valign_middle" type="radio" id="mod_doanswer_no" name="mod_doanswer" value="0"'
				.( !$mod_doanswer ? '  checked="checked"' : '' ).' /> '
			.'<label for="mod_doanswer_no">'.$lang->def('_NO').'</label>&nbsp;&nbsp;'
			.'<input class="valign_middle" type="radio" id="mod_doanswer_yes" name="mod_doanswer" value="1"'
				.( $mod_doanswer ? '  checked="checked"' : '' ).' /> '
			.'<label for="mod_doanswer_yes">'.$lang->def('_YES').'</label>'
			.'<br /><br />'
			// can travel trought page
			.$lang->def('_TEST_MM2_CANTRAVEL').'<br />'
			.'<input class="valign_middle" type="radio" id="can_travel_no" name="can_travel" value="0"'
				.( !$can_travel ? '  checked="checked"' : '' ).' /> '
			.'<label for="can_travel_no">'.$lang->def('_NO').'</label>&nbsp;&nbsp;'
			.'<input class="valign_middle" type="radio" id="can_travel_yes" name="can_travel" value="1"'
				.( $can_travel ? '  checked="checked"' : '' ).' /> '
			.'<label for="can_travel_yes">'.$lang->def('_YES').'</label>'
			.'<br /><br />'
			//can freeze
			.$lang->def('_TEST_MM2_SAVEKEEP').'<br />'
			.'<input class="valign_middle" type="radio" id="save_keep_no" name="save_keep" value="0"'
				.( $save_keep == 0 ? '  checked="checked"' : '' ).' /> '
			.'<label for="save_keep_no">'.$lang->def('_NO').'</label>&nbsp;&nbsp;'
			.'<input class="valign_middle" type="radio" id="save_keep_yes" name="save_keep" value="1"'
				.( $save_keep == 1 ? '  checked="checked"' : '' ).' /> '
			.'<label for="save_keep_yes">'.$lang->def('_YES').'</label>'
			.'<br /><br />'
			
			.Form::getTextfield($lang->def('_MAX_ATTEMPT'), 'max_attempt', 'max_attempt', 3, $max_attempt)
			
			.'<br /><br />'
		.'</div>'
		.'</fieldset>'
		.'<fieldset class="configuration">'."\n"
		.'<legend>'.$lang->def('_TEST_MM_FOUR').'</legend>'."\n"
		.'<div class="field_content">'
			//can modify answer
			.$lang->def('_TEST_MM4_SHOWTOT').'<br />'
			.'<input class="valign_middle" type="radio" id="show_tot_no" name="show_tot" value="0"'
				.( !$show_score ? '  checked="checked"' : '' ).' /> '
			.'<label for="show_tot_no">'.$lang->def('_NO').'</label>&nbsp;&nbsp;'
			.'<input class="valign_middle" type="radio" id="show_tot_yes" name="show_tot" value="1"'
				.( $show_score ? '  checked="checked"' : '' ).' /> '
			.'<label for="show_tot_yes">'.$lang->def('_YES').'</label>'
			.'<br /><br />'
			.$lang->def('_TEST_MM4_SHOWCAT').'<br />'
			.'<input class="valign_middle" type="radio" id="show_cat_no" name="show_cat" value="0"'
				.( !$show_score_cat ? '  checked="checked"' : '' ).' /> '
			.'<label for="show_cat_no">'.$lang->def('_NO').'</label>&nbsp;&nbsp;'
			.'<input class="valign_middle" type="radio" id="show_cat_yes" name="show_cat" value="1"'
				.( $show_score_cat ? '  checked="checked"' : '' ).' /> '
			.'<label for="show_cat_yes">'.$lang->def('_YES').'</label>'
			.'<br /><br />'
			.$lang->def('_TEST_MM4_SHOWDOANSWER').'<br />'
			.'<input class="valign_middle" type="radio" id="show_doanswer_no" name="show_doanswer" value="0"'
				.( $show_doanswer == 0 ? '  checked="checked"' : '' ).' /> '
			.'<label for="show_doanswer_no">'.$lang->def('_NO').'</label>&nbsp;&nbsp;'
			.'<input class="valign_middle" type="radio" id="show_doanswer_yes" name="show_doanswer" value="1"'
				.( $show_doanswer == 1 ? '  checked="checked"' : '' ).' /> '
			.'<label for="show_doanswer_yes">'.$lang->def('_YES').'</label>&nbsp;&nbsp;'
			.'<input class="valign_middle" type="radio" id="show_doanswer_yes_if_passed" name="show_doanswer" value="2"'
				.( $show_doanswer == 2 ? '  checked="checked"' : '' ).' /> '
			.'<label for="show_doanswer_yes_if_passed">'.$lang->def('_YES_IF_PASSED').'</label>'
			.'<br /><br />'
			.$lang->def('_TEST_MM4_SHOWSOL').'<br />'
			.'<input class="valign_middle" type="radio" id="show_solution_no" name="show_solution" value="0"'
				.( $show_solution == 0 ? '  checked="checked"' : '' ).' /> '
			.'<label for="show_solution_no">'.$lang->def('_NO').'</label>&nbsp;&nbsp;'
			.'<input class="valign_middle" type="radio" id="show_solution_yes" name="show_solution" value="1"'
				.( $show_solution == 1 ? '  checked="checked"' : '' ).' /> '
			.'<label for="show_solution_yes">'.$lang->def('_YES').'</label>&nbsp;&nbsp;'
			.'<input class="valign_middle" type="radio" id="show_solution_yes_if_passed" name="show_solution" value="2"'
				.( $show_solution == 2 ? '  checked="checked"' : '' ).' /> '
			.'<label for="show_solution_yes_if_passed">'.$lang->def('_YES_IF_PASSED').'</label>'
			.'<br /><br />'
		.'</div>'
		.'</fieldset>'
		.'<div class="align_right">'
			.'<input class="button" type="submit" value="'.$lang->def('_SAVE').'" />'
		.'</div>'
		.Form::closeForm()
		.getBackUi('index.php?modname=test&amp;op=modtestgui&amp;idTest='.$idTest.'&amp;back_url='.$url_coded, $lang->def('_BACK'))
		.'</div>', 'content');
}

// XXX: updatemodality
function updatemodality() {
	checkPerm('view', false, 'storage');
	
	$lang =& DoceboLanguage::createInstance('test');
	
	$idTest = importVar('idTest', true, 0);
	$back_url = urldecode(importVar('back_url'));
	$url_coded = htmlentities(urlencode($back_url));
	
	list($time_dependent) = mysql_fetch_row( mysql_query("
	SELECT time_dependent 
	FROM ".$GLOBALS['prefix_lms']."_test 
	WHERE idTest = '".$idTest."'"));
	
	if(!mysql_query("
	UPDATE ".$GLOBALS['prefix_lms']."_test 
	SET display_type = '".( $_POST['display_type'] ? 1 : 0 )."', 
		order_type = '".$_POST['order_type']."', 
		shuffle_answer = '".( $_POST['shuffle_answer'] ? 1 : 0 )."', 
		question_random_number = '".( $_POST['order_type'] == 2 ? $_POST['question_random_number'] : 0 )."', 
		save_keep = '".( $_POST['save_keep'] ? 1 : 0 )."',
		hide_info = '".( $_POST['mod_hide_info'] ? 1 : 0 )."',
		mod_doanswer = '".( $_POST['mod_doanswer'] ? 1 : 0 )."', 
		can_travel = '".( $_POST['can_travel'] ? 1 : 0 )."', 
		show_score = '".( $_POST['show_tot'] ? 1 : 0 )."', 
		show_score_cat = '".( $_POST['show_cat'] ? 1 : 0 )."', 
		show_doanswer = '".$_POST['show_doanswer']."', 
		show_solution = '".$_POST['show_solution']."',
		max_attempt = '".(int)$_POST['max_attempt']."'"
	.( $time_dependent == 2 && $_POST['display_type'] == 0 ? " ,time_dependent = 0 " : "" )
	." WHERE idTest = '$idTest'")) {
		errorCommunication($lang->def('_ERR_SAVE')
			.getBackUi('index.php?modname=test&amp;op=deftime&amp;idTest='.$idTest.'&amp;back_url='.$url_coded, $lang->def('_BACK')));
		return;
	}
	
	jumpTo( 'index.php?modname=test&op=modtestgui&idTest='.$idTest.'&back_url='.$url_coded);
}

// XXX: deftime
function deftime() {
	checkPerm('view', false, 'storage');
	
	$lang =& DoceboLanguage::createInstance('test');
	
	require_once($GLOBALS['where_framework'].'/lib/lib.form.php');
	
	$idTest = importVar('idTest', true, 0);
	$back_url = urldecode(importVar('back_url'));
	$url_coded = htmlentities(urlencode($back_url));
	
	list($time_dependent, $time_assigned, 
		$penality_test, $penality_time_test, $penality_quest, $penality_time_quest) = mysql_fetch_row(mysql_query("
	SELECT time_dependent, time_assigned, 
		penality_test, penality_time_test, penality_quest, penality_time_quest 
	FROM ".$GLOBALS['prefix_lms']."_test 
	WHERE idTest = '$idTest'"));
	
	
	if(isset($_POST['undo'])) {
		
		jumpTo('index.php?modname=test&op=modtestgui&idTest='.$idTest.'&back_url='.$url_coded);
	}
	if(isset($_POST['settime_button'])) {
		
		// second step, ask for time
		switch($_POST['time_limit']) {
			case 0 : {
				
				$update_query = "
				UPDATE ".$GLOBALS['prefix_lms']."_test 
				SET time_dependent = 0, 
					time_assigned = '".$_POST['time_assigned']."' 
				WHERE idTest = '$idTest'";
				if(!mysql_query($update_query)) {
					errorCommunication($lang->def('_ERR_SAVE')
						.getBackUi('index.php?modname=test&amp;op=deftime&amp;idTest='.$idTest.'&amp;back_url='.$url_coded, $lang->def('_BACK')));
					return;
				}
				jumpTo('index.php?modname=test&op=modtestgui&idTest='.$idTest.'&back_url='.$url_coded.'&mod_operation=1');
			};break;
			case 1 : {
				
				$GLOBALS['page']->add(
					getTitleArea($lang->def('_TEST_SECTION'), 'test')
					.'<div class="std_block">'
					.'<div class="title_big">'.$lang->def('_TEST_TIME_MANAGEMENT').'</div>'
					.getBackUi('index.php?modname=test&amp;op=modtestgui&amp;idTest='.$idTest.'&amp;back_url='.$url_coded, $lang->def('_BACK'))
					//-------------------------------------------------------------
					.Form::openForm('deftime', 'index.php?modname=test&amp;op=updatetime')
					
					.Form::getHidden('idTest', 'idTest', $idTest)
					.Form::getHidden('back_url', 'back_url', $url_coded)
					.Form::getHidden('time_limit', 'time_limit', $_POST['time_limit'])
					
					.Form::getOpenFieldset($lang->def('_TEST_TM_TWO'))
					.Form::getLineBox($lang->def('_TEST_TM2_TIMETOTAL'), $time_assigned.' '.$lang->def('_SECONDS'))
					.Form::getTextfield(	$lang->def('_TEST_TM2_NEWTIMETOTAL'), 
											'time_assigned', 
											'time_assigned', 
											5, 
											$time_assigned, 
											$lang->def('_TEST_TM2_NEWTIMETOTAL'), 
											$lang->def('_SECONDS') )
					.Form::getCloseFieldset()
					
					.Form::openButtonSpace()
					.Form::getButton('settime_button', 'settime_button', $lang->def('_SAVE'))
					.Form::getButton('undo', 'undo', $lang->def('_UNDO'))
					.Form::closeButtonSpace()
					
					.Form::closeForm()
					
					.'</div>', 'content');
			};break;
			case 2 : {
				
				list($actual_tot_time) = mysql_fetch_row(mysql_query("
				SELECT SUM(time_assigned) 
				FROM ".$GLOBALS['prefix_lms']."_testquest 
				WHERE idTest = '$idTest'"));
				
				$GLOBALS['page']->add(
					getTitleArea($lang->def('_TEST_SECTION'), 'test')
					.'<div class="std_block">'
					.'<div class="title_big">'.$lang->def('_TEST_TIME_MANAGEMENT').'</div>'
					.getBackUi('index.php?modname=test&amp;op=modtestgui&amp;idTest='.$idTest.'&amp;back_url='.$url_coded, $lang->def('_BACK'))
					//-------------------------------------------------------------
					.Form::openForm('deftime', 'index.php?modname=test&amp;op=updatetime')
					
					.Form::getHidden('idTest', 'idTest', $idTest)
					.Form::getHidden('back_url', 'back_url', $url_coded)
					.Form::getHidden('time_limit', 'time_limit', $_POST['time_limit'])
					
					.Form::getOpenFieldset($lang->def('_TEST_TM_THREE'))
					.Form::getLineBox($lang->def('_TEST_TM2_ACTUALTIME'), $actual_tot_time.' '.$lang->def('_SECONDS'))
					.Form::getTextfield($lang->def('_TEST_TM2_NEWTIME'), 'new_time', 'new_time', 10, $actual_tot_time, 
						$lang->def('_TEST_TM2_NEWTIME'), $lang->def('_SECONDS') )
					.Form::getOpenCombo($lang->def('_TEST_TM2_SUBD_BY'))
					.Form::getRadio($lang->def('_TEST_TM2_DIFFICULT'), 'point_diffcult', 'point_assignement',0)
					.Form::getRadio($lang->def('_TEST_TM2_EQUALTOALL'), 'point_equaltoall', 'point_assignement', 1 )
					.Form::getRadio($lang->def('_TEST_TM2_MANUAL'), 'point_manual', 'point_assignement', 2, true)
					.Form::getCloseCombo()
					.Form::getCloseFieldset()
					
					.Form::openButtonSpace()
					.Form::getButton('settime_button', 'settime_button', $lang->def('_TEST_TM2_SETTIME'))
					.Form::getButton('undo', 'undo', $lang->def('_UNDO'))
					.Form::closeButtonSpace()
					
					.Form::closeForm()
					
					.'</div>', 'content');
			};break;
		}
	} else {
		
		$GLOBALS['page']->add(
			getTitleArea($lang->def('_TEST_SECTION'), 'test')
			.'<div class="std_block">'
			.'<div class="title_big">'.$lang->def('_TEST_TIME_MANAGEMENT').'</div>'
			.getBackUi('index.php?modname=test&amp;op=modtestgui&amp;idTest='.$idTest.'&amp;back_url='.$url_coded, $lang->def('_BACK'))
			//-------------------------------------------------------------
			.Form::openForm('deftime', 'index.php?modname=test&amp;op=deftime')
			
			.Form::getHidden('idTest', 'idTest', $idTest)
			.Form::getHidden('back_url', 'back_url', $url_coded)
			
			.Form::getOpenFieldset($lang->def('_TEST_TM_ONE'))
			.Form::getRadio($lang->def('_TEST_TIME_NO'), 'time_limit_no', 'time_limit', 0, $time_dependent == 0)
			.Form::getRadio($lang->def('_TEST_TIME_YES'), 'time_limit_yes', 'time_limit', 1, $time_dependent == 1)
			.Form::getRadio($lang->def('_TEST_TIME_YES_QUEST'), 'time_limit_yes_quest', 'time_limit', 2, $time_dependent == 2)
			.Form::getCloseFieldset()
			
			.Form::openButtonSpace()
			.Form::getButton('settime_button', 'settime_button', $lang->def('_TEST_TM2_SETTIME'))
			.Form::getButton('undo', 'undo', $lang->def('_UNDO'))
			.Form::closeButtonSpace()
			
			.Form::closeForm()
			
			.getBackUi('index.php?modname=test&amp;op=modtestgui&amp;idTest='.$idTest.'&amp;back_url='.$url_coded, $lang->def('_BACK'))
			.'</div>', 'content');
	}
	
}

// XXX: updatetime
function updatetime() {
	
	$lang =& DoceboLanguage::createInstance('test');
	
	$idTest = importVar('idTest', true, 0);
	$back_url = urldecode(importVar('back_url'));
	$url_coded = htmlentities(urlencode($back_url));
	
	if(isset($_POST['undo'])) jumpTo('index.php?modname=test&op=modtestgui&idTest='.$idTest.'&back_url='.$url_coded);
	
	$update_query = "
	UPDATE ".$GLOBALS['prefix_lms']."_test 
	SET time_dependent = '".$_POST['time_limit']."', 
		time_assigned = '".$_POST['time_assigned']."' "
	.( $_POST['time_limit'] == 2 ? " ,display_type = 1 " : "" )
	." WHERE idTest = '$idTest'";
	
	if(!mysql_query($update_query)) {
		errorCommunication($lang->def('_ERR_SAVE')
			.getBackUi('index.php?modname=test&amp;op=deftime&amp;idTest='.$idTest.'&amp;back_url='.$url_coded, $lang->def('_BACK')));
		return;
	}
	
	if($_POST['time_limit'] == 2) {
		
		jumpTo('index.php?modname=test&op=modassigntime&idTest='.$idTest.'&back_url='.$url_coded
			.'&point_assignement='.$_POST['point_assignement'].'&new_time='.$_POST['new_time']);
	}
	jumpTo('index.php?modname=test&op=modtestgui&idTest='.$idTest.'&back_url='.$url_coded.'&mod_operation=1');
}

// XXX: modassignedtime
function modassigntime() {
	checkPerm('view', false, 'storage');
	
	$lang =& DoceboLanguage::createInstance('test');
	
	require_once($GLOBALS['where_framework'].'/lib/lib.form.php');
	require_once($GLOBALS['where_framework'].'/lib/lib.newtypeone.php');
	
	$idTest 	= importVar('idTest', true, 0);
	$back_url 	= urldecode(importVar('back_url'));
	$url_coded 	= htmlentities(urlencode($back_url));
	
	//save new time -------------------------------------------------
	if(isset($_POST['saveandexit'])) {
		
		$re = mysql_query("
		UPDATE ".$GLOBALS['prefix_lms']."_test
		SET display_type = '1'
		WHERE idTest = '$idTest'");
		if($re) {
			while(list($idQuest, $difficult) = each($_POST['new_difficult_quest'])) {
				$re &= mysql_query("
				UPDATE ".$GLOBALS['prefix_lms']."_testquest 
				SET difficult = '".$difficult."', 
					time_assigned = '".$_POST['new_time_quest'][$idQuest]."' 
				WHERE idTest = '$idTest' AND idQuest = '".(int)$idQuest."'");
			}
		}
		jumpTo('index.php?modname=test&op=modtestgui&idTest='.$idTest.'&back_url='.$url_coded.'&mod_operation='.($re ? 1 : 0));
	}
	
	list($test_title) = mysql_fetch_row( mysql_query("
	SELECT title 
	FROM ".$GLOBALS['prefix_lms']."_test 
	WHERE idTest = '".$idTest."'") );
	
	list($tot_quest, $tot_difficult, $actual_tot_time) = mysql_fetch_row(mysql_query("
	SELECT COUNT(*), SUM(difficult), SUM(time_assigned) 
	FROM ".$GLOBALS['prefix_lms']."_testquest 
	WHERE idTest = '$idTest' AND type_quest <> 'break_page' AND type_quest <> 'title'"));
	
	$re_quest = mysql_query("
	SELECT idQuest, type_quest, title_quest, difficult, time_assigned 
	FROM ".$GLOBALS['prefix_lms']."_testquest 
	WHERE idTest = '".$idTest."' 
	ORDER BY sequence");
	
	$GLOBALS['page']->add(
		getTitleArea($lang->def('_TEST_SECTION'), 'test')
		.'<div class="std_block">'
		.getBackUi('index.php?modname=test&amp;op=deftime&amp;idTest='.$idTest.'&amp;back_url='.$url_coded, $lang->def('_BACK'))
		
		.'<form method="post" action="index.php?modname=test&amp;op=modassigntime">'
		
		.'<fieldset class="fieldset_std">'
		.'<legend>'.$lang->def('_TEST_TM2_CAPTIONSETTIME').'</legend>'
		.'<input type="hidden" name="idTest" value="'.$idTest.'" />'
		.'<input type="hidden" name="back_url" value="'.$url_coded.'" />', 'content');
		
	//table header---------------------------------------------------
	$tab_quest = new typeOne(0, $lang->def('_TEST_SUMMARY'), $lang->def('_TEST_SUMMARY'));
	$tab_quest->setColsStyle(array('image', 'image', '', 'image', 'image'));
	$tab_quest->addHead(array(
		$lang->def('_TEST_QUEST_ORDER'), $lang->def('_TYPE'), $lang->def('_TEST_QUEST_TITLE'), $lang->def('_TEST_QUEST_DIFFICULT'), 
		$lang->def('_TEST_QUEST_TIME_ASSIGNED').' ('.$lang->def('_SECONDS').')' ) );
	
	$i = 1;
	$effective_time = $effective_difficult = 0;
	//tabel body--------------------------------------------------------
	while(list($idQuest, $type_quest, $title_quest, $difficult, $time_assigned) = mysql_fetch_row($re_quest)) {
		
		if(isset($_POST['new_difficult_quest'][$idQuest])) {
			
			//loading new time form previous page
			$difficult = $_POST['new_difficult_quest'][$idQuest];
			$new_time = $_POST['new_time_quest'][$idQuest];
		} elseif(isset($_GET['point_assignement'])) {
			
			//calculate new time from deftime page
			switch($_GET['point_assignement']) {
				case "0" : {
					$new_time = (int)( ( $_GET['new_time'] / $tot_difficult ) * $difficult );
				};break;
				case "1" : {
					$new_time = (int)( $_GET['new_time'] / $tot_quest );
				};break;
				case "2" : {
					$new_time = (int)( $time_assigned );
				};break;
			}
		}
		
		$content = array(
			$i++, 
			$lang->def('_QUEST_ACRN_'.strtoupper($type_quest)), 
			$title_quest, 
			( $difficult ? 
				'<label for="new_difficult_quest_'.$idQuest.'">'.$lang->def('_QUEST_TM2_SETDIFFICULT').'</label>'
				.Form::getInputDropdown(	'', 
							'new_difficult_quest_'.$idQuest, 
							'new_difficult_quest['.$idQuest.']',
							array(1 => 1, 2, 3, 4, 5),
							$difficult, 
							'' ) :  
				'&nbsp;' ), 
			( $difficult ? 
				'<label for="new_time_quest_'.$idQuest.'">'.$lang->def('_QUEST_TM2_SETTIME').'</label>'.
				'<input type="text" id="new_time_quest_'.$idQuest.'" name="new_time_quest['.$idQuest.']" value="'.$new_time.'" size="5" maxlength="4" alt="'.$lang->def('_QUEST_TM2_SETTIME').'" />' : 
				'&nbsp;' )
		);
		if( $difficult != 0 ) {
			$effective_time += $new_time;
			$effective_difficult += $difficult;
		}
		$tab_quest->addBody($content);
	}
	$tab_quest->addBodyCustom('<tr class="line-top-bordered">'
		.'<td colspan="3" class="align_right">'.$lang->def('_QUEST_TM2_TOTAL').'</td>'
		.'<td class="align_center">'.$effective_difficult.'</td>'
		.'<td class="align_center">'.$effective_time.'</td>'
		.'</tr>');
	
	$GLOBALS['page']->add($tab_quest->getTable(), 'content');
	//command for this page---------------------------------------------
	if(isset($_GET['new_time'])) $previous_time = $_GET['new_time'];
	else $previous_time = $previous_time = $_POST['previous_time'];
	$time_difference = $effective_time - $previous_time;
	echo $effective_time;
	$GLOBALS['page']->add('</fieldset>'
		.'<div class="set_time_row">'
			.'<input type="hidden" name="previous_time" value="'.$effective_time.'">'
			.str_replace('[time_difference]', $time_difference, $lang->def('_QUEST_TM2_DIFFERENCE_FROM_PREVIOUS'))
			.'&nbsp;&nbsp;&nbsp;'
			.'<input class="button_nowh" type="submit" name="settime" value="'.$lang->def('_TEST_TM2_SETTIME').'" />'
		.'</div><br />'
		.'<div class="align_right">'
			.'<input class="button" type="submit" name="saveandexit" value="'.$lang->def('_SAVE').'" />'
		.'</div>'
		.'</form>'
		.'</div>', 'content');
}

// XXX: defpoint
function defpoint() {
	checkPerm('view', false, 'storage');
	
	$lang =& DoceboLanguage::createInstance('test');
	
	require_once($GLOBALS['where_framework'].'/lib/lib.form.php');
	
	$idTest = importVar('idTest', true, 0);
	$back_url = urldecode(importVar('back_url'));
	$url_coded = htmlentities(urlencode($back_url));
	
	list($title, $description, $point_type, $point_required) = mysql_fetch_row( mysql_query("
	SELECT title, description, point_type, point_required 
	FROM ".$GLOBALS['prefix_lms']."_test 
	WHERE idTest = '".$idTest."'"));
	
	$GLOBALS['page']->add(
		getTitleArea($lang->def('_TEST_SECTION'), 'test')
		.'<div class="std_block">'
		.'<div class="title_big">'.$lang->def('_TEST_POINT_MANAGEMENT').'</div>'
		.getBackUi('index.php?modname=test&amp;op=modtestgui&amp;idTest='.$idTest.'&amp;back_url='.$url_coded, $lang->def('_BACK'))
		
		.Form::openForm('defpoint', 'index.php?modname=test&amp;op=updatepoint')
	
		.'<fieldset class="configuration">'."\n"
		.'<legend>'.$lang->def('_TEST_PM_MINIMUM').'</legend>'."\n"
		.'<div class="field_content">'."\n"
		.Form::getHidden('idTest', 'idTest', $idTest)
		.Form::getHidden('back_url', 'back_url', $url_coded)
		.Form::getTextfield($lang->def('_TEST_PMM_REQUIREDSCORE_POINT'), 'point_required', 'point_required', 30, 
			( $point_required ?$point_required : '0.0' ) )
		.'</div>'
		.'</fieldset>'
	
		.'<fieldset class="configuration">'."\n"
		.'<legend>'.$lang->def('_TEST_PM_ONE').'</legend>'."\n"
		.'<div class="field_content">'."\n"
		.Form::getRadio($lang->def('_TEST_PM1_POINT'), 'point_type_point', 'point_type', 0, ($point_type == 0))
		.Form::getRadio($lang->def('_TEST_PM1_PERC'), 'point_type_perc', 'point_type', 1, ($point_type == 1))
		.'</div>'
		.'</fieldset>'
		
		.'<div class="align_right">'
		.Form::getButton('defpoint_submit', 'defpoint_submit', $lang->def('_SAVE'))
		.'</div>'
		.Form::closeForm()
	
		.Form::openForm('assignpoint', 'index.php?modname=test&amp;op=modassignpoint')
		.'<fieldset class="configuration">'."\n"
		.'<legend>'.$lang->def('_TEST_PM_TWO').'</legend>'."\n"
		.'<div class="field_content">'
		.Form::getHidden('idTest_assign', 'idTest', $idTest)
		.Form::getHidden('back_url_assign', 'back_url', $url_coded), 'content');
	
	$query_question = "
	SELECT q.idQuest, q.type_quest, t.type_file, t.type_class, q.title_quest, q.difficult 
	FROM ".$GLOBALS['prefix_lms']."_testquest AS q JOIN ".$GLOBALS['prefix_lms']."_quest_type AS t 
	WHERE q.idTest = '".(int)$idTest."' AND q.type_quest = t.type_quest";
	$query_question .= " ORDER BY q.sequence";
	$re_quest = mysql_query($query_question);
	
	$max_score = 0;
	while(list($idQuest, $type_quest, $type_file, $type_class, $title_quest, $difficult) = mysql_fetch_row($re_quest)) {
		
		require_once(dirname(__FILE__).'/../question/'.$type_file);
		$quest_obj = eval("return new $type_class( $idQuest );");
		
		$max_score += $quest_obj->getMaxScore();
	}
	
	$GLOBALS['page']->add(
		'<div class="form_line_l">'
		.'<div class="label_effect">'.$lang->def('_TEST_QUEST_MAXTESTSCORE').'</div>'.$max_score.' '.$lang->def('_TEST_QUEST_POINT').'</div>'
		.Form::getTextfield($lang->def('_TEST_QUEST_NEWMAXTESTSCORE'), 'new_assigned_score', 'new_assigned_score', 255, 
			$max_score, $lang->def('_TEST_QUEST_NEWMAXTESTSCORE'), $lang->def('_TEST_QUEST_POINT'))
			
		.Form::getOpenCombo($lang->def('_TEST_PM_SUBD_BY'))
		
		.Form::getRadio($lang->def('_TEST_PM_DIFFICULT'), 'point_diffcult', 'point_assignement', 0)
		.Form::getRadio($lang->def('_TEST_PM_EQUALTOALL'), 'point_equaltoall', 'point_assignement', 1)
		.Form::getRadio($lang->def('_TEST_PM_MANUAL'), 'point_manual', 'point_assignement', 2, true)
		
		.Form::getCloseCombo()
		.'</div>'
		.'</fieldset>'
	
		.'<div class="align_right">'
		.Form::getButton('assignpoint_submit', 'assignpoint_submit', $lang->def('_TEST_PM_SETPOINT'))
		.'</div>'
		.Form::closeForm()
	
		.getBackUi('index.php?modname=test&amp;op=modtestgui&amp;idTest='.$idTest.'&amp;back_url='.$url_coded, $lang->def('_BACK'))
		.'</div>', 'content');
}

// XXX: updatemodality
function updatepoint() {
	checkPerm('view', false, 'storage');
	
	$lang =& DoceboLanguage::createInstance('test');
	
	$idTest = importVar('idTest', true, 0);
	$back_url = urldecode(importVar('back_url'));
	$url_coded = htmlentities(urlencode($back_url));
	
	if(!mysql_query("
	UPDATE ".$GLOBALS['prefix_lms']."_test 
	SET point_required = '".$_POST['point_required']."',
		point_type = '".( $_POST['point_type'] ? $_POST['point_type'] : 0 )."' 
	WHERE idTest = '$idTest'")) {
		errorCommunication($lang->def('_ERR_SAVE')
			.getBackUi('index.php?modname=test&amp;op=deftime&amp;idTest='.$idTest.'&amp;back_url='.$url_coded, $lang->def('_BACK')));
		return;
	}
	
	jumpTo( 'index.php?modname=test&op=modtestgui&idTest='.$idTest.'&back_url='.$url_coded);
}


// XXX: modassignpoint
function modassignpoint() {
	checkPerm('view', false, 'storage');
	
	$lang =& DoceboLanguage::createInstance('test');
	
	require_once($GLOBALS['where_framework'].'/lib/lib.newtypeone.php');
	require_once($GLOBALS['where_framework'].'/lib/lib.form.php');
	
	$idTest 	= importVar('idTest', true, 0);
	$back_url 	= urldecode(importVar('back_url'));
	$url_coded 	= htmlentities(urlencode($back_url));
	
	//jump back
	if(isset($_POST['back_to_home'])) {
		jumpTo('index.php?modname=test&op=modtestgui&idTest='.$idTest.'&back_url='.$url_coded);
	}
	
	//save new score ------------------------------------------------
	if(isset($_POST['saveandexit'])) {
		
		$query_question = "
		SELECT q.idQuest, q.type_quest, t.type_file, t.type_class 
		FROM ".$GLOBALS['prefix_lms']."_testquest AS q JOIN ".$GLOBALS['prefix_lms']."_quest_type AS t 
		WHERE q.idTest = '".(int)$idTest."' AND q.type_quest = t.type_quest";
		$query_question .= " ORDER BY q.sequence";
		$re_quest = mysql_query($query_question);
		
		$score_assign = array();
		while(list($idQuest, $type_quest, $type_file, $type_class) = mysql_fetch_row($re_quest)) {
			
			
			mysql_query("
			UPDATE ".$GLOBALS['prefix_lms']."_testquest 
			SET difficult = '".(int)$_POST['new_difficult_quest'][$idQuest]."' 
			WHERE idTest = '".$idTest."' AND idQuest = '".(int)$idQuest."'");
			
			require_once(dirname(__FILE__).'/../question/'.$type_file);
			$quest_obj = eval("return new $type_class( $idQuest );");
			$score_assign[$idQuest] = $quest_obj->setMaxScore($_POST['new_score_quest'][$idQuest]);
		}
	}
	
	list($test_title) = mysql_fetch_row( mysql_query("
	SELECT title 
	FROM ".$GLOBALS['prefix_lms']."_test 
	WHERE idTest = '".$idTest."'") );
	
	list($tot_quest, $tot_difficult) = mysql_fetch_row(mysql_query("
	SELECT COUNT(*), SUM(difficult) 
	FROM ".$GLOBALS['prefix_lms']."_testquest 
	WHERE idTest = '$idTest' AND type_quest <> 'break_page' AND type_quest <> 'title'"));
	
	$query_question = "
	SELECT q.idQuest, q.type_quest, t.type_file, t.type_class, q.title_quest, q.difficult 
	FROM ".$GLOBALS['prefix_lms']."_testquest AS q JOIN ".$GLOBALS['prefix_lms']."_quest_type AS t 
	WHERE q.idTest = '".(int)$idTest."' AND q.type_quest = t.type_quest";
	$query_question .= " ORDER BY q.sequence";
	$re_quest = mysql_query($query_question);
	
	$GLOBALS['page']->add(getTitleArea($lang->def('_TEST_SECTION'), 'test')
		.'<div class="std_block">'
		.getBackUi('index.php?modname=test&amp;op=defpoint&amp;idTest='.$idTest.'&amp;back_url='.$url_coded, $lang->def('_BACK'))
		
		.'<form method="post" action="index.php?modname=test&amp;op=modassignpoint">'
		
		.'<fieldset class="fieldset_std">'
		.'<legend>'.$lang->def('_TEST_TM2_CAPTIONSETTIME').'</legend>'
		.'<input type="hidden" name="idTest" value="'.$idTest.'" />'
		.'<input type="hidden" name="back_url" value="'.$url_coded.'" />', 'content');
		
	//table header---------------------------------------------------
	$tab_quest = new typeOne(0, $lang->def('_TEST_SUMMARY'), $lang->def('_TEST_SUMMARY'));
	$tab_quest->setColsStyle(array('image', 'image', '', 'image', 'image'));
	$tab_quest->addHead(array($lang->def('_TEST_QUEST_ORDER'), $lang->def('_TYPE'), $lang->def('_TEST_QUEST_TITLE'), 
		$lang->def('_TEST_QUEST_DIFFICULT'), $lang->def('_TEST_QUEST_MAXSCORE') ) );
	
	$i = 1;
	$effective_tot_score = $effective_difficult = 0;
	//table body--------------------------------------------------------
	
	while(list($idQuest, $type_quest, $type_file, $type_class, $title_quest, $difficult) = mysql_fetch_row($re_quest)) {
		
		require_once(dirname(__FILE__).'/../question/'.$type_file);
		$quest_obj = eval("return new $type_class( $idQuest );");
		
		if(isset($_POST['new_score_quest'][$idQuest])) {
			
			//loading new time form previous page
			$difficult = $_POST['new_difficult_quest'][$idQuest];
			$quest_score = $quest_obj->getRealMaxScore($_POST['new_score_quest'][$idQuest], true);

		} elseif(isset($_POST['point_assignement'])) {
			
			//calculate new time from deftime page
			switch($_POST['point_assignement']) {
				case "0" : {
					
					$quest_score = $quest_obj->getRealMaxScore(round(round($_POST['new_assigned_score'] / $tot_difficult, 2) * $difficult), 2);
					//$quest_score = (( $_POST['new_assigned_score'] / $tot_difficult ) * $difficult ), 2;
				};break;
				case "1" : {
					$quest_score = $quest_obj->getRealMaxScore(round($_POST['new_assigned_score'] / $tot_quest, 2));
					//$quest_score = round(( $_POST['new_assigned_score'] / $tot_quest ), 2);
				};break;
				case "2" : {
					$quest_score = $quest_obj->getMaxScore();
				};break;
			}
		}
		
		$content = array(
			$i++, 
			$lang->def('_QUEST_ACRN_'.strtoupper($type_quest)), 
			$title_quest );
		
		if(isset($score_assign)) {
			$content[] = ( $difficult ? $difficult : '&nbsp;' );
			
			if($difficult ) {
				$content[] = ( $score_assign[$idQuest] != $_POST['new_score_quest'][$idQuest] ? $score_assign[$idQuest].'&nbsp;<span class="font_red">*</span>' : $score_assign[$idQuest]);
			} else {
				$content[] = '&nbsp;';
			}
		} else {
			$content[] = ( $difficult ? 
			'<label for="new_difficult_quest_'.$idQuest.'">'.$lang->def('_QUEST_TM2_SETDIFFICULT').'</label>'
			.Form::getInputDropdown(	'', 
										'new_difficult_quest_'.$idQuest, 
										'new_difficult_quest['.$idQuest.']',
										array(1 => 1, 2, 3, 4, 5),
										$difficult, 
										'' ) : 
			'&nbsp;' );
			$content[] = ( $difficult ? 
			'<label for="new_difficult_quest_'.$idQuest.'">'.$lang->def('_QUEST_TM2_SETSCORE').'</label>'
			.'<input type="text" id="new_score_quest_'.$idQuest.'" name="new_score_quest['.$idQuest.']" value="'
			.$quest_score.'" size="5" maxlength="200" alt="'.$lang->def('_QUEST_TM2_SETSCORE').'" />' : 
			'&nbsp;' );
		}
		if( $difficult != 0 ) {
			$effective_difficult += $difficult;
			
			if(isset($score_assign)) $effective_tot_score = round($effective_tot_score + $score_assign[$idQuest], 2);
			else $effective_tot_score = round($effective_tot_score + $quest_score, 2);
		}
		$tab_quest->addBody($content);
	}
	$tab_quest->addBodyCustom('<tr class="line-top-bordered">'
		.'<td colspan="3" class="align_right">'.$lang->def('_QUEST_TM2_TOTAL').'</td>'
		.'<td class="align_center">'.$effective_difficult.'</td>'
		.'<td class="align_center">'.$effective_tot_score.'</td>'
		.'</tr>');
	$GLOBALS['page']->add($tab_quest->getTable(), 'content');
	
	//command for this page---------------------------------------------
	if(isset($_POST['new_assigned_score'])) $previous_score = $_POST['new_assigned_score'];
	else $previous_score = $_POST['previous_score'];
	$score_difference = round($effective_tot_score - $previous_score, 2);
	
	if($score_difference < 0) $score_difference = '<strong class="font_red">'.$score_difference.'<strong>';
	else $score_difference = '<strong>'.$score_difference.'<strong>';
	
	$GLOBALS['page']->add('</fieldset>', 'content');
	if(!isset($score_assign)) {
		
		$GLOBALS['page']->add(
			'<div class="set_time_row">'
				.Form::getHidden('previous_score', 'previous_score', $effective_tot_score)
				.str_replace('[score_difference]', $score_difference, $lang->def('_QUEST_TM2_SCORE_DIFFERENCE_FROM_PREVIOUS'))
				.Form::getButton('setpoint', 'setpoint', $lang->def('_TEST_TM2_SETSCORE'), 'button_nowh')
			.'</div>'
			.Form::openButtonSpace()
				.Form::getButton('saveandexit', 'saveandexit', $lang->def('_SAVE'), 'button')
				.Form::getButton('back_to_home', 'back_to_home', $lang->def('_UNDO'), 'button')
			.Form::closeButtonSpace()
			.'</form>', 'content');
	} else {
		$GLOBALS['page']->add(
			'<div class="set_time_row">'
				.Form::getHidden('previous_score', 'previous_score', $effective_tot_score)
				.str_replace('[score_difference]', $score_difference, $lang->def('_QUEST_TM2_SCORE_DIFFERENCE_FROM_PREVIOUS'))
			.'</div>'
			.Form::openButtonSpace()
				.Form::getHidden('point_manual', 'point_assignement', 2)
				.Form::getButton('setpoint', 'setpoint', $lang->def('_TEST_BACK_TO_SETTIME'), 'button')
				.Form::getButton('back_to_home', 'back_to_home', $lang->def('_SAVE'), 'button')
			.Form::closeButtonSpace()
		, 'content');
	}
	
	$GLOBALS['page']->add('</div>', 'content');
}

function importquest() {
	checkPerm('view', false, 'storage');
	$lang =& DoceboLanguage::createInstance('test');
	
	$idTest = importVar('idTest', true, 0);
	$back_url = urldecode(importVar('back_url'));
	$back_coded = htmlentities(urlencode($back_url));
	
	require_once($GLOBALS['where_framework'].'/lib/lib.form.php');
	
	$form = new Form();
	require_once($GLOBALS['where_lms'].'/lib/lib.quest_bank.php');
	$qb_man 	= new QuestBankMan();
	$supported_format = $qb_man->supported_format();
	
	
	$title = array('index.php?modname=test&op=modtestgui&idTest='.$idTest.'&back_url='.$back_coded => $lang->def('_QUEST_BANK'), $lang->def('_IMPORT'));
	cout(
		getTitleArea($title, 'quest_bank')
		.'<div class="std_block">'

		.$form->openForm('import_form', 'index.php?modname=test&op=doimportquest', false, false, 'multipart/form-data')
		
		.$form->openElementSpace()
		.'<input type="hidden" name="idTest" value="'.$idTest.'" />'
		.'<input type="hidden" name="back_url" value="'.$back_coded.'" />'
		.$form->getFilefield($lang->def('_FILE'), 'import_file', 'import_file')
		.$form->getRadioSet($lang->def('_FILE_FORMAT'), 'file_format', 'file_format', $supported_format, 0)
		.$form->getTextfield($lang->def('_FILE_ENCODE'), 'file_encode', 'file_encode', 255, 'utf-8')
		.$form->closeElementSpace()
		
		.$form->openButtonSpace()
		.$form->getButton('undo','undo',$lang->def('_UNDO'))
		.$form->getButton('quest_search','quest_search',$lang->def('_IMPORT') )
		.$form->closeButtonSpace()
		.$form->closeForm()
		
		.'</div>');
	
}

function doimportquest() {
	checkPerm('view', false, 'storage');
	$lang =& DoceboLanguage::createInstance('test');
	
	$idTest = importVar('idTest', true, 0);
	$back_url = urldecode(importVar('back_url'));
	$back_coded = htmlentities(urlencode($back_url));
	
	require_once($GLOBALS['where_lms'].'/lib/lib.quest_bank.php');
	
	$qb_man = new QuestBankMan();
	
	$file_format = get_req('file_format', DOTY_INT, 0);
	$file_encode = get_req('file_encode', DOTY_ALPHANUM, 'utf-8');
	$file_readed = file($_FILES['import_file']['tmp_name']);
	
	cout(
		'<!--CSS file (default YUI Sam Skin) -->'."\n"
		.'<link type="text/css" rel="stylesheet" href="'.$GLOBALS['where_framework_relative'].'/addons/yui/datatable/assets/skins/sam/datatable.css">'."\n"
		
		.'<!-- Dependencies -->'."\n"
		.'<script type="text/javascript" src="'.$GLOBALS['where_framework_relative'].'/addons/yui/yahoo-dom-event/yahoo-dom-event.js"></script>'."\n"
		.'<script type="text/javascript" src="'.$GLOBALS['where_framework_relative'].'/addons/yui/element/element-beta-min.js"></script>'."\n"
		.'<script type="text/javascript" src="'.$GLOBALS['where_framework_relative'].'/addons/yui/datasource/datasource-beta-min.js"></script>'."\n"
		
		.'<!-- Source files -->'."\n"
		.'<script type="text/javascript" src="'.$GLOBALS['where_framework_relative'].'/addons/yui/datatable/datatable-beta-min.js"></script>'."\n"
	
	, 'page_head');
	
	addCss('style_yui_docebo');
	$title = array('index.php?modname=test&op=modtestgui&idTest='.$idTest.'&back_url='.$back_coded => $lang->def('_QUEST_BANK'), $lang->def('_IMPORT'));
	cout( getTitleArea($title, 'quest_bank')
		.'<div class="std_block yui-skin-docebo">'
		.getBackUi('index.php?modname=test&amp;op=defpoint&amp;idTest='.$idTest.'&amp;back_url='.$back_coded, $lang->def('_BACK'))
	);
	
	$import_result = $qb_man->import_quest($file_readed, $file_format, $idTest);
	fixQuestSequence($idTest);
	
	cout('<table id="import_result"><caption>'.$lang->def('_IMPORT').'</caption>');
	cout('<tr>'
		.'<td>'.$lang->def('_QUEST_TYPE').'</td>'
		.'<td>'.$lang->def('_SUCCESS').'</td>'
		.'<td>'.$lang->def('_FAIL').'</td>'
		.'</tr>' );
	foreach($import_result as $type_quest => $i_result) {
		
		cout('<tr>'
			.'<td>'.$lang->def('_QUEST_'.strtoupper($type_quest)).'</td>'
			.'<td>'.$i_result['success'].'</td>'
			.'<td>'.$i_result['fail'].'</td>'
			.'</tr>' );
	}
	cout('</table>');
	
	cout('</div>');
}

function exportquest() {
	checkPerm('view', false, 'storage');
	$lang =& DoceboLanguage::createInstance('test');
	
	$idTest = importVar('idTest', true, 0);
	$back_url = urldecode(importVar('back_url'));
	$back_coded = htmlentities(urlencode($back_url));
	
	require_once($GLOBALS['where_lms'].'/lib/lib.quest_bank.php');
	$qb_man = new QuestBankMan();
	
	$file_format 		= get_req('export_quest_select', DOTY_INT, 0);
	
	
	$quests = array(); 
	
	$re_quest = mysql_query("
	SELECT idQuest, type_quest 
	FROM ".$GLOBALS['prefix_lms']."_testquest 
	WHERE idTest = '$idTest' 
	ORDER BY page, sequence");
	while(list($id_quest, $type_quest) = mysql_fetch_row($re_quest)) {
		
		$quests[$id_quest] = $type_quest;
	}
	
	$quest_export = $qb_man->export_quest($quests, $file_format);
	
	require_once($GLOBALS['where_framework'].'/lib/lib.download.php');
	sendStrAsFile( $quest_export, 'export_'.date("Y-m-d").'.txt' );
	
}

// XXX: switch

if(isset($_POST['import_quest'])) $GLOBALS['op'] = 'importquest';
if(isset($_POST['export_quest'])) $GLOBALS['op'] = 'exportquest';

switch($GLOBALS['op']) {
	case "instest" : {
		instest();
	};break;

	case "modtest" : {
		modtest();
	};break;
	case "uptest" : {
		uptest();
	};break;

	case "modtestgui" : {
		$GLOBALS['page']->add('<script type="text/javascript" src="'.$GLOBALS['where_framework_relative'].'/lib/lib.elem_selector.js"></script>', 'page_head');
		if( isset($_GET['test_saved']) || isset($_POST['test_saved']) ) {
			//other enter
			$var_save = importVar('test_saved');
			$var_loaded = loadTestStatus($var_save);
			
			$idTest = $var_loaded['idTest'];
			$back_url = urlencode($var_loaded['back_url']);
			fixPageSequence($idTest);
		}
		else {
			$idTest = importVar('idTest', true, 0);
			$back_url = importVar('back_url');
		}
		$object_test = createLO('test', $idTest);
		$object_test->edit($idTest, urldecode($back_url));
	};break;
	
	case "movequest" : {
		movequest();
	};break;
	
	case "movedown" : {
		movequestion('down');
	};break;
	case "moveup" : {
		movequestion('up');
	};break;
	case "fixsequence" : {
		fixQuestSequence();
	};break;
	
	case "addquest" : {
		addquest();
	};break;
	case "modquest" : {
		modquest();
	};break;
	case "delquest" : {
		delquest();
	};break;
	
	//modality setting
	case "defmodality" : {
		defmodality();
	};break;
	case "updatemodality" : {
		updatemodality();
	};break;
	
	//time setting
	case "deftime" : {
		deftime();
	};break;
	case "updatetime" : {
		updatetime();
	};break;
	case "modassigntime" : {
		modassigntime();
	};break;
	
	
	//point setting
	case "defpoint" : {
		defpoint();
	};break;
	case "updatepoint" : {
		updatepoint();
	};break;
	case "modassignpoint" : {
		modassignpoint();
	};break;
	
	case "importquest" : {
		importquest();
	};break;
	case "doimportquest" : {
		doimportquest();
	};break;
	
	case "exportquest" : {
		exportquest();
	};break;
		
	
}

?>