<?php
//typo
$jtypo = $_POST['jtypo'];
if($jtypo=='ins') { $freqp = 'freq'; }
else if($jtypo=='res') { $freqp = 'freqres'; }
else { $freqp = 'freq'; }
//date
$day1 = date('Y-m-d', strtotime(str_replace("/","-",$_POST['datepick1'])));
$dayaff1 = date('d/m/Y', strtotime(str_replace("/","-",$_POST['datepick1'])));
$day2 = date('Y-m-d', strtotime(str_replace("/","-",$_POST['datepick2'])));
$dayaff2 = date('d/m/Y', strtotime(str_replace("/","-",$_POST['datepick2'])));
//activité
if($_POST['activite']!="") {
	$activite = $_POST['activite'];
}
//espace
if($espopt['valeur']==1) {
	if($_POST['espace']!="") {
		$espace = $_POST['espace'];
	}
}
//élève
if($_POST['eleve']!="") {
	include('./db/proc/eleve_select.php');
}
//division
else if($_POST['division']!="") {
	$division = $_POST['division'];
	// gestion des divisions et groupes :
	$divisionq = mysqli_query($bdd,"SELECT division FROM ".$prefix."_eleves WHERE division = '".$division."' GROUP BY division");
	$div_num = mysqli_num_rows($divisionq);
	if($div_num==0) {
		$groupe = $division;
		$_SESSION['groupe'] = $groupe;
		$division = "";
	}
}
//niveau
else if($_POST['niveau']!="") {
	$niveau = $_POST['niveau'];
}

echo '<h2>'.FROM2.' '.$dayaff1.' '.TO2.' '.$dayaff2.' :</h2>';

// 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']=="hor"&&$jtypo=="res") {
	$ordreaff_info['valeur']=="nom";
}
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";
}
//filtre2 :
if(isset($seleve)) {
	$filtre2 = " AND eleves.eleves_id = '".$eleve_id."'";
}
else if(isset($groupe)) {
	$filtre2 = " AND groupedesc.groupe_nom = '".$groupe."'";
}
else if(isset($division)) {
	$filtre2 = " AND eleves.division = '".$division."'";
}
else if(isset($niveau)) {
	$filtre2 = " AND eleves.division LIKE '".$niveau."%'";
}
else {
	$filtre2 = "";
}
$_SESSION['filtre2']=$filtre2;

//filtre3 (espace) :
if(isset($espace)) {
	if($espace==0) {
		$filtre3 =" AND (freq.espace_id = 0 OR freq.espace_id IS NULL)";
	}
	else {
		$filtre3 = " AND freq.espace_id = '".$espace."'";
	}
}
else {
	$filtre3 = "";
}
$_SESSION['filtre3']=$filtre3;

// récupération des dates :
if(isset($activite)) {
	$filtre1 = "WHERE freq.date_".$freqp." >= '".$day1."' AND freq.date_".$freqp." <= '".$day2."' AND freq.code_activ = '".$activite."'";
}
else {
	$filtre1 = "WHERE freq.date_".$freqp." >= '".$day1."' AND freq.date_".$freqp." <= '".$day2."'";
}
$_SESSION['filtre1']=$filtre1;
// requête :
if(!isset($freqp)) { $freqp = 'freq'; }
if($freqp == 'freq' ) {
	$addfreq = 'freq.h_inscription, freq.h_inscription2, freq.h_inscription3, freq.h_sortie, freq.h_sortie_2, freq.h_sortie_3, freq.cas, ';
}
else { $addfreq = 'freq.statut, '; }
//sélection des élèves (si groupe)
if(isset($groupe)) {
	$inscrits_select = "SELECT freq.".$freqp."_id, freq.eleves_id, freq.date_".$freqp.", freq.code_activ, freq.hor_name, ".$addfreq." espaces.espace_id, espaces.espace_nom, eleves.eleves_id, eleves.nom, eleves.prenom, eleves.division 
	FROM ".$prefix."_".$freqp." 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
	".$filtre1.$filtre2.$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.".$freqp."_id, freq.eleves_id, freq.date_".$freqp.", freq.code_activ, freq.hor_name, ".$addfreq." espaces.espace_id, espaces.espace_nom, eleves.eleves_id, eleves.nom, eleves.prenom, eleves.division 
	FROM ".$prefix."_".$freqp." 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
	".$filtre1.$filtre2.$filtre3."  
	".$ordre."";
	$inscrits = mysqli_query($bdd,$inscrits_select);
	$inscrits_info = mysqli_fetch_assoc($inscrits);
	$nb_inscrits = mysqli_num_rows($inscrits);
}

echo '<h3>';
if(@$jtypo=='res') { echo RESA6; }
else  { echo FREQ_INF3; }
echo ' (<span id="nb_ins">'.$nb_inscrits.'</span>) :</h3>';

if($nb_inscrits == 0) {
	echo '<p>'.NONE.'.</p>';
	$printins=0;
}

else {
	$printins=1;
	$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&&$jtype=='ins') {
	$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);
	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 >= '".$day1."' AND freq.date_freq <= '".$day2."' 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');
		}
	}
}
?>
