<?php

/* Displays the qcm form */

//Edition or creation selector
$bEdition = false;
$iNbQuestions = 0;
$asDisplayQuestions = $asAnswersCurrentState = array();
$sAdditionalBoxesScript = 'addQuestion();';

//get The Qcm in the database
if(isset($_GET['qcm']))
{
	$iQcmId = $_GET['qcm'];
	
	/*
	if($_GET['copyQcm'])
	{
		$oQcm = new qcm($iQcmId);
		$iQcmId = $oQcm->cloneQcm()->iQcmId;
	}
	*/
	
	$bEdition=true;
	$asQcm = getQcm($iQcmId);
	$sQcmName = isset($asQcm[QCM_TABLE])?$asQcm[QCM_TABLE]:'';
	$sQcmAuthorId = isset($asQcm['id_'.USER_TABLE])?$asQcm['id_'.USER_TABLE]:'';
	$asQuestions = isset($asQcm[QUESTION_TABLE])?$asQcm[QUESTION_TABLE]:array();
	 
	foreach($asQuestions as $iQuestionId => $asAnswers)
	{
		$asDisplayAnswers = $asDisplayRightAnswers = array();
		$iNbAnswers = 0;
		$iNbQuestions++;
		$sQuestion = $asAnswers[QUESTION_TABLE];
		
		//image
		$sImageName = $asAnswers['image'];
		if($sImageName!='')
		{
			$sImage = getImage(IMAGE_FOLDER.$sImageName, '', '', '', array('id'=>'question'.$iQuestionId.'imageSrc'));
			$sXButton = '<a href="#" id="question'.$iQuestionId.'ImageDelete" class="deleteImage" title="Supprimer cette image" onclick="deleteImage(\'question'.$iQuestionId.'\');return false;">X</a>';
			$sImage = $sXButton.getHtml($sImage, 'div', 'qcmReader', '', array('id'=>'question'.$iQuestionId.'imageDiv'));
			$sImageDiv = getInputHtml('Image', array($sImage, 'Remplacer par une nouvelle image', '', $iQuestionId));
		}
		else
		{
			$sImageDiv = getInputHtml('Image', array('', ADD_IMAGE_TEXT, '', $iQuestionId));
		}
		
		if(array_key_exists(ANSWER_TABLE, $asAnswers) && count($asAnswers[ANSWER_TABLE])>0)
		{
			foreach($asAnswers[ANSWER_TABLE] as $iAnswerId => $asAnswer)
			{
				$iNbAnswers++;
				$asDisplayAnswers[$iAnswerId] = getInputHtml('Answer', array($iQuestionId, $iAnswerId, '', $asAnswer[ANSWER_TABLE]));
				$asDisplayRightAnswers[$iAnswerId] = getInputHtml('RightAnswer', array($iQuestionId, $iAnswerId, '', $asAnswer[RIGHT_ANSWER]?' CHECKED':''));
			}
		}
		$asAnswersCurrentState[] = getInputHtml('CurrentAnswer', array($iQuestionId, $iNbAnswers));
		$asDisplayQuestions[] = getInputHtml('Question', array($iNbQuestions, '', $iQuestionId, $sQuestion, implode($asDisplayAnswers), implode($asDisplayRightAnswers), $sImageDiv));
		$sAdditionalBoxesScript .= 'addAnswer('.$iQuestionId.');';
	}
	
	//check Qcm
	if(!checkOrigin('qcmCreator'))
	{
		checkQcm($iQcmId);
	}
}
?>
<script type="text/javascript">

	temp = new Array();

	function save(formId)
	{
		for(var i in document.forms[formId].elements)
		{
			var elem = document.forms[formId].elements[i];
			if(elem.type == 'text' || elem.type == 'textarea')
			{
				temp[elem.name] = elem.value;
			}
			else if(elem.type == 'checkbox')
			{
				temp[elem.name] = elem.checked;
			}
		}
	}
	
	function load(formId)
	{
		for(var i in document.forms[formId].elements)
		{
			var elem = document.forms[formId].elements[i];
			if(temp[elem.name])
			{
				if(elem.type == 'checkbox')
				{
					document.forms[formId].elements[elem.name].checked = temp[elem.name];
				}
				else
				{
					document.forms[formId].elements[elem.name].value = temp[elem.name];
				}
			}
		}
	}
	
	function addQuestion(idRequestingQuestion)
	{
		var questionId = parseInt(document.forms['temp'].elements['currentquestion'].value);
		if(!idRequestingQuestion || idRequestingQuestion==questionId)
		{
			save('qcmCreator');
			questionId++;
			newQuestion = '<?php echo getInputHtml('Question'); ?>';
			newQuestion = newQuestion.replace(/\#questionId\#/g, questionId);
			newQuestion = newQuestion.replace(/\#answerId\#/g, 1);
			document.getElementById("questionpanel").innerHTML += newQuestion;
			document.getElementById("temp").innerHTML += '<?php echo getInputHtml('CurrentAnswer'); ?>';
	
			document.forms['temp'].elements['currentquestion'].value = questionId;
			load('qcmCreator');

			if(idRequestingQuestion)
			{
				document.forms['qcmCreator'].elements['newquestion'+(questionId-1)].focus();
			}
		}
	}
	
	function addAnswer(questionId, idRequestingAnswer)
	{
		var answerId = parseInt(document.forms['temp'].elements["question"+questionId+"currentanswer"].value);
		if(!idRequestingAnswer || idRequestingAnswer==answerId)
		{
			save('qcmCreator');
			answerId++;
			
			newAnswer = '<?php echo getInputHtml('Answer'); ?>';
			newAnswer = newAnswer.replace(/\#answerId\#/g, answerId);
			newAnswer = newAnswer.replace(/\#questionId\#/g, questionId);
			newRightAnswer = '<?php echo getInputHtml('RightAnswer'); ?>';
			newRightAnswer = newRightAnswer.replace(/\#answerId\#/g, answerId);
			newRightAnswer = newRightAnswer.replace(/\#questionId\#/g, questionId);
			document.getElementById("question"+questionId+"answerpanel").innerHTML += newAnswer;
			document.getElementById("question"+questionId+"rightanswerpanel").innerHTML += newRightAnswer;
			
			document.forms['temp'].elements["question"+questionId+"currentanswer"].value = answerId;
			load('qcmCreator');

			if(idRequestingAnswer)
			{
				document.forms['qcmCreator'].elements['newquestion'+questionId+'answer'+(answerId-1)].focus();
			}
		}
	}

	function setDeletedData(id, forceSet)
	{
		var deletedData = document.forms['qcmCreator'].elements['deletedData'].value;
		var index = deletedData.indexOf(id);
		if(index === -1 && (!forceSet || forceSet=='y'))
		{
			deletedData += id+',';
		}
		else if(index !== -1 && (!forceSet || forceSet=='n'))
		{
			deletedData = deletedData.replace(new RegExp(id+',', 'i'), '');
		}
		document.forms['qcmCreator'].elements['deletedData'].value = deletedData;
	}

	function deleteQuestion(id)
	{
		var action = disableBox('qcmCreator', id);
		disableBox('qcmCreator', id+'image', action);
		disableImage(id, action);
		setDeletedData(id);
		for(var i in document.forms['qcmCreator'].elements)
		{
			var elem = document.forms['qcmCreator'].elements[i];
			if((elem.type == 'text') && elem.id.indexOf(id) !== -1)
			{
				deleteAnswer(elem.id, action);
			}
		}
	}

	function deleteAnswer(id, forceSet)
	{
		var action = disableBox('qcmCreator', id, forceSet);
		setDeletedData(id, forceSet);
		
		var index = id.length-1;
		var rightAnswerId = '';
		while(id.charAt(index).toString().search(/^[0-9]+$/) == 0)
		{
			rightAnswerId += id.charAt(index);
			index--;
		}
		disableBox('qcmCreator', id+'<?php echo RIGHT_ANSWER; ?>'+rightAnswerId, forceSet);
		
		//enable related question
		var relatedQuestion = id.substring(0, id.indexOf('<?php echo ANSWER_TABLE; ?>'));
		if(!document.forms['qcmCreator'].elements[relatedQuestion])
		{
			relatedQuestion = relatedQuestion.substring(3);
		}
		if(action == 'n' && document.forms['qcmCreator'].elements[relatedQuestion].disabled)
		{
			deleteQuestion(relatedQuestion);
		}
	}

	function deleteImage(id, forceSet)
	{
		action = disableImage(id, forceSet);
		setDeletedData(id+'image');
		if(action == 'n' && document.forms['qcmCreator'].elements[id].disabled)
		{
			deleteQuestion(id);
		}
	}

	function disableImage(id, forceSet)
	{
		//image
		var action = disableDiv(id+"imageDiv", forceSet);
		document.getElementById(id+"imageSrc").style.opacity = (action=='y')?0.5:1;
		hover(id+'ImageDelete', action);

		//input
		disableBox("qcmCreator", id+"image", action);
		disableDiv(id+"imageText", forceSet);
		
		return action;
	}
</script>
<form action="?page=qcmProcess" method="post" name="qcmCreator" enctype="multipart/form-data">
	<table class="maxWidth" id="questionpanel">
		<tr>
			<td colspan="3" class="rounded"><p class="important">Nom du QCM : <input type="text" name="qcm" value="<?php echo $bEdition?$sQcmName:''; ?>" /></p></td>
		</tr>
		<tr><td colspan="3" class="blank">&nbsp;</td></tr>
		<tr>
			<th width="50%" class="top-left-rounded">Libell&eacute; de la question</th>
			<th width="30%">R&eacute;ponses possibles</th>
			<th width="10%" class="top-right-rounded">Bonne(s) r&eacute;ponse(s)</th>
		</tr>
		<?php echo implode($asDisplayQuestions); ?>
	</table>
	<table class="maxWidth joinedTable">
		<tr>
			<th colspan="3" class="bottom-left-rounded bottom-right-rounded">une fois les questions compl&eacute;t&eacute;es, cliquez sur le bouton 'Cr&eacute;er le QCM'</th>
		</tr>
		<tr>
			<td class="blank" colspan="4">
				<input type="hidden" name="user" value="<?php echo $_SESSION['user']; ?>" />
				<input type="hidden" name="author" value="<?php echo $bEdition?$sQcmAuthorId:$_SESSION['user']; ?>" />
				<input type="hidden" name="id_<?php echo QCM_TABLE; ?>" value="<?php echo $bEdition?$iQcmId:'new'; ?>" />
				<input type="hidden" name="deletedData" id="deletedData" value="">
				<input type="button" name="annuler" onclick="goTo('?page=frontal');" value="Annuler">
				&nbsp;
				<input type="submit" value="<?php echo $bEdition?"Modifier le QCM":"Cr&eacute;er le QCM"; ?>" />
			</td>
		</tr>
	</table>
</form>
<form id="temp" name="temp" style="display:none;">
	<input type="hidden" name="currentquestion" id="currentquestion" value="<?php echo getMaxIncrementedValue(QUESTION_TABLE); ?>">
	<?php echo implode($asAnswersCurrentState); ?>
</form>
<script type="text/javascript">
	<?php echo $sAdditionalBoxesScript; ?>
</script>