<?php

/* Check the questions and answers, and add the QCM into database */

if(!checkOrigin('qcmCreator') || !isset($_POST['user']))
{
	relocate(getError(array('Processus incorrect', 'Retour forc&eacute; &agrave; l\'accueil')));
}
else
{
	//reconnection in case of session time out
	setSession($_POST['user']);
	
	$sQcmName = ($_POST[QCM_TABLE] != '')?$_POST[QCM_TABLE]:DEFAULT_QCM_NAME;

	//Add / update the Qcm and unvalidate the qcm
	$bNewQcm = ($_POST['id_'.QCM_TABLE]=='new');
	$asData = array('id_user'=>$_POST['author'], QCM_TABLE=>$sQcmName);
	$iQcmId = $bNewQcm?insertRow(QCM_TABLE, $asData):updateRow(QCM_TABLE, $_POST['id_'.QCM_TABLE], $asData);
	setQcmValidity($iQcmId, false);
	
	//feed
	$iRefFeedId = addFeed($bNewQcm?'CREATE':'MODIFY', $iQcmId, QCM_TABLE, $sQcmName);
	
	//Add the QCM questions, images & answers to the database
	$asQuestionIds = $asFeedIds = array();
	foreach($_POST as $sKey => $sValue)
	{
		$asDataKeys = parseQuestion($sKey);
		if($asDataKeys && $sValue!=DEFAULT_QUESTION_INPUT && $sValue!=DEFAULT_ANSWER_INPUT) 
		{
			$bNew = (strpos($sKey, 'new') !== false);
			$iQuestionId = $asDataKeys[QUESTION_TABLE];	
			switch(count($asDataKeys))
			{
				case 1: //Add a question + image
					$asData = array($iQcmId, $sValue);
					if($bNew)
					{
						$asQuestionIds[$iQuestionId] = insertRow(QUESTION_TABLE, $asData);
					}
					else
					{
						$asQuestionIds[$iQuestionId] = updateRow(QUESTION_TABLE, $iQuestionId, $asData);
					}
					$asFeedIds[$iQuestionId] = addFeed($bNew?'CREATE':'MODIFY', $iRefFeedId, QUESTION_TABLE, $sValue, '', $iRefFeedId);
					
					//image
					$sImageName = $_FILES[$sKey.'image']['name'];
					if($sImageName!='')
					{
						$oError = uploadFile($_FILES[$sKey.'image'], IMAGE_FOLDER, array('jpg', 'jpeg', 'bmp', 'png'));
						if(is_string($oError))
						{
							addMessage($oError);
						}
						else
						{
							$sImageNewName = 'qcm'.$iQcmId.'question'.$asQuestionIds[$iQuestionId].'v'.date('dmYHis');
							resizeRenameImage(IMAGE_FOLDER.$sImageName, $sImageNewName);
							$bNewImage = (selectValue(QUESTION_TABLE, 'image', $asQuestionIds[$iQuestionId])=='');
							updateRow(QUESTION_TABLE, $asQuestionIds[$iQuestionId], array('image'=>$sImageNewName.'.'.getExtension($sImageName)));
							addFeed($bNewImage?'CREATE':'MODIFY', $asFeedIds[$iQuestionId], 'image', $sImageNewName, '', $iRefFeedId);
						}
					}
					break;
				case 2: //Add an answer
					//Add a question (empty) in the case of an answer without linked questions
					if(!array_key_exists($iQuestionId, $asQuestionIds))
					{
						$asQuestionIds[$iQuestionId] = insertRow(QUESTION_TABLE, array($iQcmId, ''));			
					}
					$bRightAnswer = isset($_POST[$sKey.RIGHT_ANSWER.$asDataKeys[ANSWER_TABLE]])?'1':'0';
					$asData = array($asQuestionIds[$iQuestionId], $iQcmId, $sValue, $bRightAnswer);
					if($bNew)
					{
						insertRow(ANSWER_TABLE, $asData);
					}
					else
					{
						updateRow(ANSWER_TABLE, $asDataKeys[ANSWER_TABLE], $asData);
					}
					addFeed($bNew?'CREATE':'MODIFY', $asFeedIds[$iQuestionId], ANSWER_TABLE, $sValue, '', $iRefFeedId);
					break;
			}
		} 
	}
	
	//delete questions / images / answers
	foreach(array_filter(explode(',', $_POST['deletedData'])) as $sDeletedItem)
	{
		$asDataKeys = parseQuestion($sDeletedItem);
		if(strpos($sDeletedItem, 'new') === false && $asDataKeys!=false && count($asDataKeys)>0)
		{
			if(strpos($sDeletedItem, 'image')!==false)
			{
				$sImagePath = IMAGE_FOLDER.selectValue(QUESTION_TABLE, 'image', $asDataKeys[QUESTION_TABLE]);
				updateRow(QUESTION_TABLE, $asDataKeys[QUESTION_TABLE], array('image'=>''));
				addFeed('DELETE', $asDataKeys[QUESTION_TABLE], 'image', $sImagePath, '', $iRefFeedId);
			}
			else
			{	
				$sTable = (count($asDataKeys)==1)?QUESTION_TABLE:ANSWER_TABLE;
				deleteRow($sTable, $asDataKeys[$sTable]);
				addFeed('DELETE', $asDataKeys[$sTable], $sTable, $sImagePath, '', $iRefFeedId);
			}
		}
	}
	
	//Check Qcm
	if(checkQcm($iQcmId))
	{
		setQcmValidity($iQcmId, true);
		relocate(getSuccess('Modifications enregistr&eacute;es'), 'qcmReader', array('qcm'=>$iQcmId));
	}
	else
	{
		relocate('', 'qcmCreator', array('qcm'=>$iQcmId));
	}
}
?>