<?php
$hor = mysqli_query($bdd,"SELECT * FROM ".$prefix."_horaires");
$hor_nb = mysqli_num_rows($hor);

echo '<h2>'.TODAY.DOTS.' '.date("d/m/Y").'</h2>';

// Gestion de l'affichage des inscrits de la journée
$dday = date("Y-m-d");
// récupération de l'ordre d'affichage souhaité
$ordreaff = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_ordreaff'");
$ordreaff_info = mysqli_fetch_assoc($ordreaff);
if($ordreaff_info['valeur']=="div") {
	$ordre = "ORDER BY eleves.division ASC, eleves.nom ASC, eleves.prenom ASC";
}
else if($ordreaff_info['valeur']=="nom") {
	$ordre = "ORDER BY eleves.nom ASC, eleves.prenom ASC";
}
else if($ordreaff_info['valeur']=="hor") {
	$ordre = "ORDER BY GREATEST (coalesce(freq.h_inscription2,0), coalesce(freq.h_inscription3,0), coalesce(freq.h_inscription,0)) DESC";
}
else if($ordreaff_info['valeur']=="act") {
	$ordre = "ORDER BY freq.code_activ ASC";
}
else if($ordreaff_info['valeur']=="esp") {
	$ordre = "ORDER BY espaces.espace_nom ASC, eleves.division ASC, eleves.nom ASC, eleves.prenom ASC";
}
// affichage proprement dit
$inscrits_select = "SELECT freq.freq_id, freq.eleves_id, freq.date_freq, freq.code_activ, freq.hor_name, freq.hor_name, freq.code_activ, freq.cas, freq.h_inscription, freq.h_inscription2, freq.h_inscription3, freq.h_sortie, freq.h_sortie_2, freq.h_sortie_3, 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."'
".$ordre."";
$inscrits = mysqli_query($bdd,$inscrits_select);
$inscrits_info = mysqli_fetch_assoc($inscrits);
$nb_inscrits = mysqli_num_rows($inscrits);

echo '<h3>'.FREQ_INF3.' (<span id="nb_ins">'.$nb_inscrits.'</span>) :</h3>';

if($nb_inscrits == 0) {
	echo '<p>'.NONE.'.</p>';
	$printins=0;
}
else {
	$printins=1;
	$period=0;
	include('./db/proc/freq_affjou_equation.php');
}

// Si inscription possible par grappes :
$grappes = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_grappes'"));
if($grappes['valeur']==1) {
	$period=0;
	$grappes_ins = mysqli_query($bdd,"SELECT * FROM ".$prefix."_freqgrappes freq LEFT JOIN ".$prefix."_espaces espaces ON espaces.espace_id =  freq.espace_id WHERE freq.date_freq = '".$dday."' AND freq.passage = '0' ORDER BY freq.niveau DESC, freq.division ASC");
	$nb_inscrits = mysqli_num_rows($grappes_ins);
	echo '<h3>'.FREQ_INF4.'</h3>';
	if($nb_inscrits==0) {
		echo '<p>'.NONE.'.</p>';
	}
	else {
		$printins=1;
		$pass=0;
		include('./db/proc/freq_affjou_ensembles.php');
	}
	$passverif = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_passages'"));
	if($passverif['valeur']==1) {
		$grappes_ins = mysqli_query($bdd,"SELECT * FROM ".$prefix."_freqgrappes freq LEFT JOIN ".$prefix."_espaces espaces ON espaces.espace_id =  freq.espace_id WHERE freq.date_freq = '".$dday."' AND freq.passage = '1' ORDER BY freq.dtstart DESC");
		$nb_inscrits = mysqli_num_rows($grappes_ins);
		echo '<h3>'.FREQ_INF5.'</h3>';
		if($nb_inscrits==0) {
			echo '<p>'.NONE.'.</p>';
		}
		else {
			$pass=1;
			include('./db/proc/freq_affjou_ensembles.php');
		}
	}
}
?>