<?php
if($_POST['ordredeb']!=$_POST['ordrefin']) {
	$resprint_query = $_SESSION['resprint_query'];
	if($_POST['ordrefin']=="cre") {
		$ordre = "ORDER BY eleves.division ASC, eleves.nom ASC, eleves.prenom ASC";
	}
	else if($_POST['ordrefin']=="crn") {
		$ordre = "ORDER BY eleves.division ASC, eleves.nom ASC, eleves.prenom ASC";
	}
	else if($_POST['ordrefin']=="div") {
		$ordre = "ORDER BY eleves.division ASC, eleves.nom ASC, eleves.prenom ASC";
	}
	else if($_POST['ordrefin']=="nom") {
		$ordre = "ORDER BY eleves.nom ASC, eleves.prenom ASC";
	}
	else if($_POST['ordrefin']=="hor") {
		$ordre = "ORDER BY GREATEST (coalesce(freq.h_inscription2,0), coalesce(freq.h_inscription3,0), coalesce(freq.h_inscription,0)) DESC";
	}
	else if($_POST['ordrefin']=="esp") {
		$ordre = "ORDER BY espaces.espace_nom ASC, eleves.division ASC, eleves.nom ASC, eleves.prenom ASC";
	}
	else if($_POST['ordrefin']=="esn") {
		$ordre = "ORDER BY espaces.espace_nom ASC, eleves.division ASC, eleves.nom ASC, eleves.prenom ASC";
	}
	else if($_POST['ordrefin']=="act") {
		$ordre = "ORDER BY activites.nom_activ ASC, eleves.division ASC, eleves.nom ASC, eleves.prenom ASC";
	}
	else { $ordre = "ORDER BY eleves.division ASC, eleves.nom ASC, eleves.prenom ASC"; }
	
	//freq_period :
	if(isset($_SESSION['groupe'])) {
		$inscrits_select = "SELECT freq.freq_id, freq.eleves_id, freq.date_freq, freq.code_activ, freq.hor_name, freq.h_inscription, freq.h_inscription2, freq.h_inscription3, freq.h_sortie, freq.h_sortie_2, freq.h_sortie_3, freq.cas, espaces.espace_id, espaces.espace_nom, eleves.eleves_id, eleves.nom, eleves.prenom, eleves.division 
		FROM ".$prefix."_freq freq 
		INNER JOIN ".$prefix."_eleves eleves
		ON freq.eleves_id = eleves.eleves_id
		INNER JOIN ".$prefix."_groupele groupele
		ON eleves.eleves_id = groupele.eleves_id 
		INNER JOIN ".$prefix."_groupes groupedesc 
		ON groupele.groupe_id = groupedesc.groupe_id 
		LEFT JOIN ".$prefix."_espaces espaces
		ON espaces.espace_id = freq.espace_id
		".$_SESSION['filtre1'].$_SESSION['filtre2'].$_SESSION['filtre3']."  
		".$ordre."";
		$inscrits = mysqli_query($bdd,$inscrits_select);
		$inscrits_info = mysqli_fetch_assoc($inscrits);
		$nb_inscrits = mysqli_num_rows($inscrits);
	}
	//sélection des élèves (hors groupes)
	else {
		$inscrits_select = "SELECT freq.freq_id, freq.eleves_id, freq.date_freq, freq.code_activ, freq.hor_name, freq.h_inscription, freq.h_inscription2, freq.h_inscription3, freq.h_sortie, freq.h_sortie_2, freq.h_sortie_3, freq.cas, espaces.espace_id, espaces.espace_nom, eleves.eleves_id, 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
		".$_SESSION['filtre1'].$_SESSION['filtre2'].$_SESSION['filtre3']."  
		".$ordre."";
		$inscrits = mysqli_query($bdd,$inscrits_select);
		$inscrits_info = mysqli_fetch_assoc($inscrits);
		$nb_inscrits = mysqli_num_rows($inscrits);
	}

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

	$period=1;
	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=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 >= '".$day1."' AND freq.date_freq <= '".$day2."' AND freq.passage = 0 ORDER BY freq.niveau DESC, freq.division ASC");
		$nb_inscrits = mysqli_num_rows($grappes_ins);
		$list_reservs .= '<h3>'.FREQ_INF4.'</h3>';
		if($nb_inscrits==0) {
			$list_reservs .= '<p>'.NONE.'.</p>';
		}
		else {
			$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 >= '".$day1."' AND freq.date_freq <= '".$day2."' AND freq.passage = 1 ORDER BY freq.dtstart DESC");
			$nb_inscrits = mysqli_num_rows($grappes_ins);
			$list_reservs .= '<h3>'.FREQ_INF5.'</h3>';
			if($nb_inscrits==0) {
				$list_reservs .= '<p>'.NONE.'.</p>';
			}
			else {
				$pass=1;
				include('./db/proc/freq_affjou_ensembles.php');
			}
		}
	}
}
?>