<?php

/* Display students grades, by QCM and classes. Add a download button (csv format) */

//TODO lorsque le mask sera installé, remplir la page et le csv en meme temps

//Results
$asResultsDisplay = '';
$asExportCsv = array();
$iClass= 0;
if(isset($_POST['qcm']))
{
	$bDisableExport = false;
	$iOutOf = $_POST['outofvalue'];
	list($iGrade, $iClass) = explode(GRADE_CLASS_SEPARATOR, $_POST['gradeClass']);
	$asResultsConstraints = array();
	$asOutputOrder = array('id_'.QCM_TABLE, 'grade', 'class', 'id_'.USER_TABLE);
	$asConstraintColumns = array('grade'=>$iGrade, 'class'=>$iClass, USER_TABLE=>$_POST[USER_TABLE], QCM_TABLE=>$_POST[QCM_TABLE], 'outputOrder'=>$asOutputOrder);
	foreach($asConstraintColumns as $sConstraintColumn=>$sConstraintValue)
	{
		if($sConstraintValue > 0)
		{
			$asResultsConstraints[$sConstraintColumn] = $sConstraintValue;
		}
	}
	$asResults = getResults($asResultsConstraints);
	
	//Display results & building export file
	if(!$asResults)
	{
		$asResultsDisplay = getWarning('Aucune note disponible');
	}
	else
	{
		foreach($asResults as $iQcmId=>$asQcmInfo)
		{
			$sQcmName = getQcmName($iQcmId);
			$asResultsDisplay .= '<table class="maxWidth stats rounded"><tr><td colspan="3" class="rounded"><p class="tresimportant">QCM <em>"'.$sQcmName.'"</em></p></td></tr>';
			$asExportCsv[] = array('QCM', $sQcmName);
			foreach($asQcmInfo as $iGradeId=>$asGradeInfo)
			{
				foreach($asGradeInfo as $iClassId=>$asClassInfo)
				{
					$sClass = $iGradeId.GRADE_CLASS_SEPARATOR.$iClassId;
					$asResultsDisplay .= '<tr><td colspan="3" class="blank"></td></tr>';
					$asResultsDisplay .= '<tr><td colspan="3" class="top-left-rounded top-right-rounded"><p class="important">'.$sClass.'</p></td></tr>';
					$asExportCsv[] = array('');
					$asExportCsv[] = array('Classe', $sClass, 'Note sur', $iOutOf);
					$dClassAvgMark = getResultsAverage($iOutOf, array('grade' => $iGradeId, 'class' => $iClassId, QCM_TABLE=>$iQcmId));
					$asResultsDisplay .=	'<tr>
												<th>Eleve</th>
												<th>Note</th>
												<th>Moyenne</th>
											</tr>';
					$asExportCsv[] = array('Eleve', 'Note', 'Moyenne de l\'eleve');
					foreach($asClassInfo as $iUserId=>$asUserInfo)
					{
						$dUserAvgMark = getResultsAverage($iOutOf, array('grade' => $iGradeId, 'class' => $iClassId, USER_TABLE=>$iUserId, QCM_TABLE=>array_keys($asResults)));
						$sUserLastName = ucwords($asUserInfo[USER_TABLE.'_last_name']);
						$sUserFirstName = ucwords($asUserInfo[USER_TABLE.'_first_name']);
						$sUserName = '<strong>'.$sUserLastName.'</strong> '.$sUserFirstName;
						$asResultsDisplay .=	'<tr>
													<td style="text-align:left;"><strong>'.$sUserLastName.'</strong> '.$sUserFirstName.'</td>
													<td>'.roundMark($iOutOf, $asUserInfo[RESULT_TABLE]).' / '.$iOutOf.'</td>
													<td>'.$dUserAvgMark.' / '.$iOutOf.'</td>
												</tr>';
						$asExportCsv[] = array($sUserLastName.' '.$sUserFirstName, roundMark($iOutOf, $asUserInfo[RESULT_TABLE]), $dUserAvgMark);
					}
					$asResultsDisplay .= '<tr><td colspan="3" class="bottom-left-rounded bottom-right-rounded">Moyenne de la '.$sClass.' : '.$dClassAvgMark.' / '.$iOutOf.'</td></tr>';
					$asExportCsv[] = array('Moyenne de la '.$sClass, $dClassAvgMark);
				}
			}
			$asResultsDisplay .= '</table><a href="#top" class="toTheTop rounded">Haut de page</a>';
			$asExportCsv[] = array('');
		}
	}
}
else
{
	$bDisableExport = true;
}

//QCM selection
$asQcms = getListe(array(	'select'=>array(getId(QCM_TABLE), QCM_TABLE), 	
							'from'=>QCM_TABLE,
							'orderBy'=>QCM_TABLE));
$sSelectQcm = getSelect($asQcms, 'id_'.QCM_TABLE, QCM_TABLE, array(0=>'Tous'), isset($_POST['qcm'])?$_POST['qcm']:'');

//Classes selection
$asClasses = getListe(array('select'=>array( "CONCAT(grade, '".GRADE_CLASS_SEPARATOR."', class) AS gradeClass", 'grade', 'class'), 
							'from'=>USER_TABLE,
							'constraint'=>"admin=0",
							'groupBy' => 'gradeClass',
							'orderBy'=>array('grade', 'class')));
$sSelectClasses = getSelect($asClasses, 'gradeClass', '', array('0'.GRADE_CLASS_SEPARATOR.'0'=>'Toutes'), isset($_POST['gradeClass'])?$_POST['gradeClass']:'');

//Students selection
$asStudents = getListe(array(	'select'=>array(getId(USER_TABLE), "CONCAT(user_last_name, ' ',  user_first_name) AS name"),
								'from'=>USER_TABLE,
								'constraint'=>"admin=0",
								'orderBy'=>'user_last_name'));
$sSelectStudents = getSelect($asStudents, 'id_'.USER_TABLE, 'name', array(0=>'Tous'), isset($_POST['user'])?$_POST['user']:'');

$sSelectOutOf = getOutOfForm(isset($_POST['outofvalue'])?$_POST['outofvalue']:20);

//pre($_POST);
//<select multiple="multiple" name="test" ><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option></select>
?>
<p>Choisissez l'affichage :</p>
<form method="post" action="?page=statsAdmin" name="stats">
	<table class="maxWidth">
		<tr>
			<th class="top-left-rounded">QCM</th>
			<th>Classe</th>
			<th>&Eacute;l&egrave;ve</th>
			<th>D&eacute;nominateur</th>
			<th class="top-right-rounded">Validation</th>
		</tr>
		<tr>
			<td class="bottom-left-rounded"><?php echo $sSelectQcm; ?></td>
			<td><?php echo $sSelectClasses; ?></td>
			<td><?php echo $sSelectStudents; ?></td>
			<td><?php echo $sSelectOutOf; ?></td>
			<td class="bottom-right-rounded"><input type="submit" value="Ok" /></td>
		</tr>
		<tr><td colspan="5" class="blank"><input type="button"<?php echo $bDisableExport?' disabled':''; ?> value="T&eacute;l&eacute;charger ces notes" onclick="document.forms['getCsv'].submit();" /></td></tr>
	</table>
	<?php echo $asResultsDisplay; ?>
	<br /><br />
</form>
<form action="?page=download" method="post" name="getCsv">
	<input type="hidden" name="generate" value="1" />
	<input type="hidden" name="file" value="<?php echo 'notes'.($iClass>0?$_POST['gradeClass']:''); ?>" />
	<input type="hidden" name="content" value="<?php echo urlencode(serialize($asExportCsv)); ?>" />
</form>