<?php

/* Displays all QCM in the database : finished QCM (and unfinished ones made by the connected user) */

//delete / desactivate a qcm
if(isset($_POST['deleteQcm']))
{
	$sDeletedQcmName = getTextFromId(QCM_TABLE, $_POST['deleteQcm']);
	deleteRow(QCM_TABLE, $_POST['deleteQcm']);
	addFeed('DELETE', $_POST['deleteQcm'], QCM_TABLE, $sDeletedQcmName);
}
elseif(isset($_POST['desactivateQcm']))
{
	setQcmValidity($_POST['desactivateQcm'], false);
}
elseif(isset($_POST['activateQcm']))
{
	if(checkQcm($_POST['activateQcm']))
	{
		setQcmValidity($_POST['activateQcm'], true);
	}
	else
	{
		echo getError(array('Il reste des erreurs dans ce Qcm.', 'Il est n&eacute;cessaire de les corriger avant d\'activer ce Qcm'));
	}
}

//set Input Box
$oDeleteInputBox = new InputBox('Suppression du QCM : <strong>#qcmName#</strong>', 'Voulez-vous vraiment #qcmAction# le QCM : <em>"#qcmName#"</em> ?');
$oEditInputBox = new InputBox(	'Modification du QCM : <strong>#qcmName#</strong>', 
								'Voulez-vous faire une copie du QCM : <em>"#qcmName#"</em> avant de le modifier ?', 
								array('Oui'=>'goTo(\'?page=qcmCreator&qcm=#idQcm#&copyQcm=1\')', 'Non'=>'goTo(\'?page=qcmCreator&qcm=#idQcm#&copyQcm=0\')'));

//display valid and not valid (unfinished) QCM
$asParams = array
			(
					'notValid' =>	array
									(
										'title' => 'QCM non finalis&eacute;s', 
										'valid' => '0',
										'displayConstraint' => $bAdmin?array():array('id_'.USER_TABLE=>$_SESSION['user']),
										'goTo' => 'Creator',
										'copyQcm' => false,
										'deleteAction' => 'deleteQcm',
										'deleteTitle' => 'Supprimer',
										'editTitle' => 'Activer'
									),
					'valid' => 		array
									(
										'title' => 'QCM disponibles', 
										'valid' => '1',
										'displayConstraint' => array(),
										'goTo' => 'Reader',
										'copyQcm' => true,
										'deleteAction' => 'desactivateQcm',
										'deleteTitle' => 'D&eacute;sactiver',
										'editTitle' => 'Editer'
									)
			);

foreach($asParams as $sParamName => $asParamValues)
{
	$asConstraints = array_merge(array('valid'=>$asParamValues['valid']), $asParamValues['displayConstraint']); 
	$asQcms = selectRows(array('from'=>QCM_TABLE, 'constraint'=>$asConstraints));
	$iTotal = 0;
	if(count($asQcms) > 0)
	{
		$asDisplay[$sParamName] = '<p>'.$asParamValues['title'].' :</p>
								 <table>
									<tr>
										<th width="35%" class="top-left-rounded">nom du QCM</th>
										<th width="20%">Auteur</th>
										<th width="15%">Derni&egrave;re modification</th>
										<th width="15%">'.$asParamValues['editTitle'].' le QCM</th>
										<th width="15%" class="top-right-rounded">'.$asParamValues['deleteTitle'].' le QCM</th>
									</tr>';

		foreach($asQcms as $asQcmRow)
		{
			$iQcmId = $asQcmRow['id_'.QCM_TABLE];
			
			//link to the QCM
			$asDisplay[$sParamName] .= '<tr><td class="qcmSelection" onclick="goTo(\'?page=qcm'.$asParamValues['goTo'].'&qcm='.$iQcmId.'\')"><p>'.$asQcmRow[QCM_TABLE].'</p></td>';
			
			//get author info
			$asAuthorInfo = selectRow(USER_TABLE, array($asQcmRow['id_'.USER_TABLE]), array(USER_TABLE.'_first_name', USER_TABLE.'_last_name'));
			$asDisplay[$sParamName] .= '<td><p>'.ucwords($asAuthorInfo[USER_TABLE.'_first_name']).' '.ucwords($asAuthorInfo[USER_TABLE.'_last_name']).'</p></td><td><p>'.date('d/m/Y H:i:s', strtotime($asQcmRow['led'])).'</p></td>';
			
			if($asQcmRow['id_user']==$_SESSION['user'] || $bAdmin)
			{				
				$sFormName = 'form'.$iQcmId;
				
				//set input box properties
				$oDeleteInputBox->setTags(array('formName'=>$sFormName, 'qcmName'=>$asQcmRow[QCM_TABLE], 'qcmAction'=>$asParamValues['deleteTitle']));
				$oEditInputBox->setTags(array('idQcm'=>$iQcmId, 'qcmName'=>$asQcmRow[QCM_TABLE]));
				
				
				if($sParamName=='valid')
				{
					$sEdition = '<input type="button" onClick="'.($asParamValues['copyQcm']?$oEditInputBox->getAction():'goTo(\'?page=qcmCreator&qcm='.$iQcmId.'\')').'" value="Editer">';
				}
				else
				{
					$sEdition = '<form method="POST" action="?page=frontal">
										<input type="hidden" value="'.$iQcmId.'" name="activateQcm" />
										<input type="submit" value="Activer">
									</form>';
				}
				
				
				$asDisplay[$sParamName] .= '<td>'.$sEdition.'</td>
											<td>
												<form class="no-margin" name="'.$sFormName.'" method="POST" action="?page=frontal">
													<input type="hidden" value="'.$iQcmId.'" name="'.$asParamValues['deleteAction'].'" />
													<input type="button" value="'.$asParamValues['deleteTitle'].'" onclick="'.$oDeleteInputBox->getAction().'" />
												</form>
											</td></tr>';
			}
			else
			{
				$asDisplay[$sParamName] .= '<td class="divDisabled"></td><td class="divDisabled"></td></tr>';
			}
			$iTotal++;
		}
		$asDisplay[$sParamName] .= '<tr><th colspan="5" class="bottom-left-rounded bottom-right-rounded">Un total de '.$iTotal.' QCM</th></tr></table>';
		if($iTotal==0)
		{
			unset($asDisplay[$sParamName]);
		}
		next($asParams);
	}
	elseif($sParamName == 'valid')
	{
		$asDisplay[$sParamName] = getNotice('Aucun Qcm n\'est disponible');
	}
}
echo implode("\n",$asDisplay);
echo $oDeleteInputBox->getHtml();
echo $oEditInputBox->getHtml();
?>