<?php
$nb_creneau = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_horaires"));
for($i=$creneau_info['num_creneau'];$i<$nb_creneau;$i++) {
	$crencheck[$i] = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT * FROM ".$prefix."_horaires WHERE num_creneau = ".($i+1).""));
	// affichage proprement dit
	$inscrits_select = "SELECT freq.freq_id, freq.eleves_id, freq.date_freq, freq.hor_name, freq.code_activ, freq.cas, espaces.espace_id, espaces.espace_nom, eleves.nom, eleves.prenom, eleves.division 
	FROM ".$prefix."_freq freq 
	INNER JOIN ".$prefix."_eleves eleves
	ON freq.eleves_id = eleves.eleves_id
	LEFT JOIN ".$prefix."_espaces espaces
	ON espaces.espace_id = freq.espace_id 
	WHERE freq.date_freq = '".$dday."' AND freq.hor_name = '".$crencheck[$i]['hor_name']."' AND freq.cas != 'sor' AND freq.cas != 'exc'
	".$ordre."";
	$inscrits = mysqli_query($bdd,$inscrits_select);
	$nb_inscrits = mysqli_num_rows($inscrits);
	if($nb_inscrits>0) {
		echo '<div><h2>'.FREQ_INF9.' '.EN.' ';
		$hor = mysqli_query($bdd,"SELECT * FROM ".$prefix."_horaires WHERE hor_name = '".$crencheck[$i]['hor_name']."'");
		while($liste = mysqli_fetch_assoc($hor)) {
			$cren = $liste['hor_name'];
			$deb_cren = substr("$cren", 0, 1);
			echo $liste['hor_name'].' - ';
			echo MOMENT[$deb_cren];
			$deb = $liste['debut'];
			$dh = substr("$deb", 0, 2);
			$dm = substr("$deb", 3, 2);
			echo ' '.FROM.' '.$dh.'h'.$dm.' '.TO.' ';
			$fin = $liste['fin'];
			$fh = substr("$fin", 0, 2);
			$fm = substr("$fin", 3, 2);
			echo ''.$fh.'h'.$fm.'';
		}
		echo '</h2><table>';
		foreach($inscrits as $liste) {
			$activite = $liste['code_activ'];
			$activ = mysqli_query($bdd,"SELECT nom_activ FROM ".$prefix."_freqactiv WHERE code_activ = '".$activite."'");
			$activ_info = mysqli_fetch_assoc($activ);
			if($droits==2||$droits==4||$droits==6) {
				echo '<form method="POST" name="inscrit" action="" class="padd3">';
			}
			echo '<tr><td>'.$liste['division'].' - '.$liste['nom'].' '.$liste['prenom'].'</td>';
			if(@$activ_info['nom_activ']=="") {
				echo '<td></td>';
			}
			else {
				echo '<td>'.$activ_info['nom_activ'].'</td>';
			}
			if($espopt['valeur']==1) {
				if($liste['espace_id']!=0) { 
					echo '<td>'.$liste['espace_nom'].'</td>'; }
				else { echo '<td>'.$esp0['valeur'].'</td>'; }
			}
			if($droits==2||$droits==4||$droits==6) {
				echo '<td name="noprint"><input class="boutontab" type="submit" value="'.FREQJ_ORDER8.'" name="sor'.$liste['freq_id'].'" /> <input class="boutontab" type="submit" value="'.FREQJ_ORDER9.'" name="exc'.$liste['freq_id'].'" /></td>';
				$retards = mysqli_query($bdd,"SELECT * FROM ".$prefix."_retards WHERE eleves_id = '".$liste['eleves_id']."' ORDER BY retour_date ASC");
				$retards_nb = mysqli_num_rows($retards);
				if($retards_nb!=0) {
					echo '<td class="noborder" name="noprint"><span style="font-size: 1.5em; color: #D90606;"><i class="fa-solid fa-stopwatch" title="';
					if($retards_nb>1) { $i=1; }
					while($retards_info = mysqli_fetch_assoc($retards)) {
						if($retards_nb>1) { echo ''.$i.'. '; }
						if($retards_info['titre']!="") {
							echo $retards_info['titre'].'. ';
						}
						if($retards_info['retour_date']!="0000-00-00") {
							$date = date('d/m/Y', strtotime(str_replace("/","-",$retards_info['retour_date'])));
							echo FREQ_GEST1.' '.$date.'. ';
						}
						if($retards_info['niv_alerte']!=0) {
							echo FREQ_GEST2;
							if($retards_info['niv_alerte']=="1") { echo FREQ_GEST3; }
							else if($retards_info['niv_alerte']=="2") { echo FREQ_GEST4; }
							else if($retards_info['niv_alerte']=="3") { echo FREQ_GEST5; }
							echo '. ';
						}
						if($retards_nb>1) { echo ''; $i++; }
					}
					echo '"></i></span></td>';
				}
			}
			echo '</tr>';
			if($droits==2||$droits==4||$droits==6) {
					'</form>';
				if(isset($_POST['sor'.$liste['freq_id'].''])) {
					$eleve_id = $liste['eleves_id'];
					mysqli_query($bdd,"UPDATE ".$prefix."_freq SET cas = 'sor' WHERE eleves_id = '".$eleve_id."' AND freq_id = '".$liste['freq_id']."'");
					echo '<script language="Javascript">
					<!--
					document.location.replace("freq.php");
					// -->
					</script>';	
				}
				if(isset($_POST['exc'.$liste['freq_id'].''])) {
					$eleve_id = $liste['eleves_id'];
					mysqli_query($bdd,"UPDATE ".$prefix."_freq SET cas = 'exc' WHERE eleves_id = '".$eleve_id."' AND freq_id = '".$liste['freq_id']."'");
					echo '<script language="Javascript">
					<!--
					document.location.replace("freq.php");
					// -->
					</script>';	
				}
			}
		}
		echo '</table></div>';
	}
}
?>