<?php
//typo
$jtypo = $_POST['jtypo'];
if($jtypo=='ins') { $freqp = 'freq'; }
else if($jtypo=='res') { $freqp = 'freqres'; }
else { $freqp = 'freq'; }
//date
$day = date('Y-m-d', strtotime(str_replace("/","-",$_POST['datepick1'])));
$dayaff = date('d/m/Y', strtotime(str_replace("/","-",$_POST['datepick1'])));
//créneaux
if($_POST['crenpicker1']!="") {
	$cren1 = $_POST['crenpicker1'];
}
if($_POST['crenpicker2']!="") {
	$cren2 = $_POST['crenpicker2'];
}
//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;
		$division = "";
	}
}
//niveau
else if($_POST['niveau']!="") {
	$niveau = $_POST['niveau'];
}

echo '<h2>'.$dayaff.' : ';
if(isset($cren1)&&isset($cren2)) {
	echo 'de '.$cren1.' à '.$cren2.'</h2>';
}
else if(isset($cren1)) {
	echo ''.$cren1.'</h2>';
}
else {
	echo '</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 ".$freqp.".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;

if(isset($cren1)&&isset($cren2)) {
	// requêtes s'il y a deux créneaux
	// récupération des créneaux concernés
	$hor1 = mysqli_query($bdd,"SELECT num_creneau FROM ".$prefix."_horaires WHERE hor_name = '".$cren1."'");
	$hor1_info = mysqli_fetch_assoc($hor1);
	$hor2 = mysqli_query($bdd,"SELECT num_creneau FROM ".$prefix."_horaires WHERE hor_name = '".$cren2."'");
	$hor2_info = mysqli_fetch_assoc($hor2);
	if($hor2_info['num_creneau'] <= $hor1_info['num_creneau']) {
		echo '<p>'.FREQ_HORERR1.'</p>';
	}
	else {
		//sélection des créneaux concernés
		$period = mysqli_query($bdd,"SELECT hor_name FROM ".$prefix."_horaires WHERE num_creneau >= ".$hor1_info['num_creneau']." AND num_creneau <= ".$hor2_info['num_creneau']." ORDER BY num_creneau");
		//filtrage avec boucle
		if(isset($activite)) {
			$crenv = 0;
			while($period_info=mysqli_fetch_assoc($period)) {
				if($crenv==0) {
					$filtre1 = "WHERE ( (freq.date_".$freqp." = '".$day."' AND freq.hor_name = '".$period_info['hor_name']."' AND freq.code_activ = '".$activite."') ";
					$crenv = 1;
				}
				if($crenv==1) {
					$filtre1 = "".$filtre1." OR (freq.date_".$freqp." = '".$day."' AND freq.hor_name = '".$period_info['hor_name']."' AND freq.code_activ = '".$activite."') ";					
				}
			}
			$filtre1 .= ") ";
		}
		else {
			$crenv = 0;
			while($period_info=mysqli_fetch_assoc($period)) {
				if($crenv==0) {
					$filtre1 = "WHERE (freq.date_".$freqp." = '".$day."' AND freq.hor_name = '".$period_info['hor_name']."') ";
					$crenv = 1;
				}
				if($crenv==1) {
					$filtre1 = "".$filtre1." OR (freq.date_".$freqp." = '".$day."' AND freq.hor_name = '".$period_info['hor_name']."') ";
				}
			}
		}
		$_SESSION['filtre1']=$filtre1;
		include('freq_select_liste.php');
	}
}
else if(isset($cren1)) { // si un seul créneau
	if(isset($activite)) {
		$filtre1 = "WHERE (freq.date_".$freqp." = '".$day."' AND freq.hor_name = '".$cren1."' AND freq.code_activ = '".$activite."') ";
	}
	else {
		$filtre1 = "WHERE (freq.date_".$freqp." = '".$day."' AND freq.hor_name = '".$cren1."') ";
	}
	$_SESSION['filtre1']=$filtre1;
	include('freq_select_liste.php');
}

else { //si aucun créneau ou seulement le 2ème, alors on affiche la journée
	if(isset($activite)) {
		$filtre1 = "WHERE freq.date_".$freqp." = '".$day."' AND freq.code_activ = '".$activite."'";

	}
	else {
		$filtre1 = "WHERE freq.date_".$freqp." = '".$day."'";
	}
	$_SESSION['filtre1']=$filtre1;
	include('freq_select_liste.php');
}

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

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

else {
	$period=0;
	$printins=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&&$jtypo=='ins') {
	$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 = '".$day."' 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 = '".$day."' AND freq.passage = '1' ORDER BY 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');
		}
	}
}
?>
