<?php
include('eval_select_common.php');
require_once('db/proc/caloffsco.php');

// Récupération des données :
$tabsav_select = 
"SELECT sav.nom_savoir nom_savoir, SUBSTR(sav.nom_savoir,1,40) savsub, SUBSTR(sav.s".$div."_id,7,3) sidsub, SUBSTR(sav.s".$div."_id,1,3) cadre, sav.s".$div."_id savdiv_id, sc.bgcolor bg
FROM ".$prefix."_savoirs".$div." sav
INNER JOIN ".$prefix."_savchamps sc
ON sav.champs_id = sc.champs_id 
".$savchoix." 
ORDER BY s".$div."_id ASC";
$tabsav = mysqli_query($bdd,$tabsav_select);
$tabsav_nb = mysqli_num_rows($tabsav);

if($divougr=="division") {
	$tabdiv_select = 
	"SELECT nom, prenom, eleves_id
	FROM ".$prefix."_eleves
	WHERE division='$diveval'
	GROUP BY nom, prenom";
}
else if($divougr=="groupe") {
	$tabdiv_select = 
	"SELECT eleves.nom, eleves.prenom, eleves.eleves_id
	FROM ".$prefix."_eleves eleves
	INNER JOIN ".$prefix."_groupele groupele
	ON groupele.eleves_id = eleves.eleves_id
	INNER JOIN ".$prefix."_groupes groupes
	ON groupele.groupe_id = groupes.groupe_id 
	WHERE groupe_nom = '$diveval'
	GROUP BY nom, prenom";
}

// Affichage des séances pour la classe
$eventdiv_select = "SELECT * FROM ".$prefix."_edtevent event
INNER JOIN ".$prefix."_edtbrik brik
ON event.brik_id = brik.brik_id
LEFT JOIN ".$prefix."_sequences seq
ON seq.seq_id = brik.seq_id
LEFT JOIN ".$prefix."_edtcode code
ON code.code = brik.code
WHERE brik.division = '$diveval' AND event.week >= $renweek 
ORDER BY event.dtstart";
$eventdiv = mysqli_query($bdd,$eventdiv_select);
$eventdiv_nb = mysqli_num_rows($eventdiv);

if($eventdiv_nb==0) {
	echo '<p>'.EDTEL_STATS1.'</p>';
}
else {
	echo '<div class="edtsuividiv"><p><b>'.EDTEL_STATS2.'</b></p><ul>';
	while($eventdiv_info = mysqli_fetch_assoc($eventdiv)) {
		$eventcount_select = "SELECT uid FROM ".$prefix."_edtevent event
			INNER JOIN ".$prefix."_edtbrik brik
			ON event.brik_id = brik.brik_id
			LEFT JOIN ".$prefix."_sequences seq
			ON seq.seq_id = brik.seq_id
			WHERE brik.division = '$diveval' AND event.seq_id = '".$eventdiv_info['seq_id']."' AND brik.code = '".$eventdiv_info['code']."' AND event.dtstart < '".$eventdiv_info['dtstart']."' AND event.week >= $renweek";
		$eventcount = mysqli_query($bdd,$eventcount_select);
		$eventcount_nb = mysqli_num_rows($eventcount);
		$dday = date("Ymd");
		echo '<li class="seanceitem" style="background-color:';
		if($eventdiv_info['bgcolor_code']!='') { echo $eventdiv_info['bgcolor_code'].';">'; }
		else { echo '#fff;">'; }
		if(substr($eventdiv_info['dtstart'],0,8)>=$dday) { echo '<b>'; }
		echo 'le '.substr($eventdiv_info['dtstart'],6,2).'/'.substr($eventdiv_info['dtstart'],4,2).'/'.substr($eventdiv_info['dtstart'],0,4).' : ';
		if($eventdiv_info['code']!='') { echo $eventdiv_info['code'].' - '; }
		if($eventdiv_info['progression']!='1000') { echo $eventdiv_info['progression'].'. '; }
		echo ''.$eventdiv_info['seq_nom'].' (Séance n°'.($eventcount_nb+1).' - Durée : ';
		if($eventdiv_info['duration']==5) { echo '0,5)'; }
		else { 
			echo substr($eventdiv_info['duration'],0,1);
			if(substr($eventdiv_info['duration'],1,1)!=0) {
				echo ','.substr($eventdiv_info['duration'],1,1);
			}
			echo ')';
		}
		if(substr($eventdiv_info['dtstart'],0,8)>=$dday) { echo '</b>'; }
		echo '</li>';
	}
	echo '</ul></div>';
}

// Si aucun savoir à évaluer :
if($tabsav_nb == 0) {
	echo EDTEL_STATS3;
}
// Sinon :
else {
echo '<p><b>'.EDTEL_STATS4.'</b></p>';
// Affichage par élève :
$tabdiv = mysqli_query($bdd,$tabdiv_select);
?><table class="eval">
	<thead class="eval_thead">
		<tr>
		<th class="eval_th"><?php echo SAV1;?><br><i><?php echo SAV2;?></i></th>
		<?php foreach($tabdiv as $eleve) {
			echo '<th class="eleve"';
			if (preg_match("#Chrome#", getenv("HTTP_USER_AGENT"))) { 
				echo ' style="min-width:60px;max-width:60px;" ';
			}
			else {
				echo ' style="min-width:52px;max-width:52px;" ';
			}
			echo '><div class="elechrome">'.$eleve['nom'].' '.$eleve['prenom'].'</div></th>';
		}
		echo '</tr>';
	?>
	</thead>

	<tbody class="eval_tbody">
	<?php
	foreach ($tabsav as $savoir)
	{
		echo '<tr>';
		echo '<td class="eval_sav" style="background-color:'.$savoir['bg'].';">
		<span title="'.$savoir['nom_savoir'].'">'.$savoir['cadre'].'-'.$savoir['sidsub'].'. ';
		echo mb_ereg_replace("[?]","",mb_convert_encoding($savoir['savsub'],"utf-8"));
		echo '...';
		echo '</td>';
		
		foreach ($tabdiv as $eleve)
		{
			$tabeval_select = "SELECT count(seqe.eleves_id) FROM ".$prefix."_seqeleves seqe
			INNER JOIN ".$prefix."_seqsav seq
			ON seqe.seq_id = seq.seq_id 
			INNER JOIN ".$prefix."_savoirs".$div." sav
			ON sav.s".$div."_id = seq.s_id
			WHERE seqe.eleves_id = '".$eleve['eleves_id']."' AND seq.s_id='".$savoir['savdiv_id']."'";
			$tabeval = mysqli_query($bdd,$tabeval_select);
			$tabeval_info = mysqli_fetch_assoc($tabeval);
			$savid_opt = ''.$eleve['eleves_id'].':'.$savoir['savdiv_id'].'';
			if($tabeval_info['count(seqe.eleves_id)']==0) {
				echo '<td class="evalc" style="background-color:#eee;color:#eee;" id='.$savid_opt.'>';
			}
			/*else if($tabeval_info['count(seqe.eleves_id)']==1) {
				echo '<td class="evalc" style="background-color:red;text-align:center;" id='.$savid_opt.'>';
			}*/
			else if($tabeval_info['count(seqe.eleves_id)']==1) {
				echo '<td class="evalc niv1" id='.$savid_opt.'><b>'.$tabeval_info['count(seqe.eleves_id)'].'</b>';
			}
			else if($tabeval_info['count(seqe.eleves_id)']==2) {
				echo '<td class="evalc niv2" id='.$savid_opt.'><b>'.$tabeval_info['count(seqe.eleves_id)'].'</b>';
			}
			else if($tabeval_info['count(seqe.eleves_id)']>2) {
				echo '<td class="evalc niv3" id='.$savid_opt.'><b>'.$tabeval_info['count(seqe.eleves_id)'].'</b>';
			}
			$_SESSION['eid'] = $eleve['eleves_id'];
			$_SESSION['sid'] = $savoir['savdiv_id'];
			echo '</td>';
		}
		echo '<td class="eval_sav" style="background-color:'.$savoir['bg'].';">
		<span title="'.$savoir['nom_savoir'].'">'.$savoir['cadre'].'-'.$savoir['sidsub'].'. ';
		echo mb_ereg_replace("[?]","",mb_convert_encoding($savoir['savsub'],"utf-8"));
		echo '...';
		echo '</td><!--<td style="padding-right:360px;"></td>-->';
		echo '</tr>';
	}
	
	?>
	</tbody>
</table>
<?php
}
?>
