<?php 
session_start();
require_once(dirname(__DIR__, 2) . '/config.php');
require_once(BASE_PATH . '/db/idl_connect.php');
if(isset($_SESSION['user_droits'])) {
	$inscript = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT * FROM ".$prefix."_freqres fs LEFT JOIN ".$prefix."_freqactiv fa ON fs.activ_id = fa.activ_id WHERE fs.freqres_id = '".$_REQUEST['freqres_id']."' AND statut = '0'"));
	$dday = $inscript['date_freqres'];
	$eleve_id = $inscript['eleves_id'];	
	$code_freqres = $inscript['code_freqres'];
	$hor_name = $inscript['hor_name'];
	$code_activ = $inscript['code_activ'];
	// Vérification qu'il ne soit pas déjà inscrit :
	$verif = mysqli_query($bdd,"SELECT * FROM ".$prefix."_freq WHERE eleves_id = '".$eleve_id."' AND date_freq = '".$inscript['date_freqres']."' AND hor_name = '".substr($code_freqres,10,2)."'");
	$nb_verif = mysqli_num_rows($verif);
	// Seuils si différents espaces :
	$espopt = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'espaces_gestion'"));
	if($espopt['valeur']==1) {
		if($inscript['espace_id']==0) {
			$esp_var = "AND (freq.espace_id = '0' OR freq.espace_id IS NULL) ";
		}
		else { $esp_var = "AND freq.espace_id = '".$inscript['espace_id']."' "; }
		$inscrits_select = "SELECT freq.eleves_id, freq.date_freq, freq.hor_name, freq.cas, freq.h_inscription, 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 = '".$hor_name."' AND freq.cas != 'sor' AND freq.cas != 'exc' ".$esp_var." 
		ORDER BY eleves.division ASC, eleves.nom DESC, eleves.prenom ASC";
		$inscrits = mysqli_query($bdd,$inscrits_select);
		$nb_inscrits = mysqli_num_rows($inscrits);
		if($inscript['espace_id']!=0) {
			$sin_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT seuilnbi FROM ".$prefix."_espaces WHERE espace_id = '".$inscript['espace_id']."'"));
			$sir_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT seuilreaci FROM ".$prefix."_espaces WHERE espace_id = '".$inscript['espace_id']."'"));
			$sin_valeur = $sin_info['seuilnbi'];
			$sir_valeur = $sir_info['seuilreaci'];
		}
		else {
			$sin_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_seuilnbi'"));
			$sin_valeur = $sin_info['valeur'];
			$sir_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_seuilreaci'"));
			$sir_valeur = $sir_info['valeur'];
		}
	}
	if(($nb_verif==0&&$espopt['valeur']==0)||($nb_verif==0&&$espopt['valeur']==1&&($nb_inscrits<$sin_valeur||$sir_valeur=="alert"))) {
		// Récupération de la division :
		$diveleve = mysqli_query($bdd,"SELECT division, sexe FROM ".$prefix."_eleves WHERE eleves_id = '".$eleve_id."'");
		$diveleve_nb = mysqli_num_rows($diveleve);
		if($diveleve_nb!=0) {
			$diveleve_info = mysqli_fetch_assoc($diveleve);
		}
		$eleve_div = $diveleve_info['division'];
		$eleve_sex = $diveleve_info['sexe'];
		// Inscription :
		$h_exact = date('Ymd').'T'.date('His');
		if($espopt['valeur']==1) {
			mysqli_query($bdd,"INSERT INTO ".$prefix."_freq (eleves_id, date_freq, hor_name, code_activ, division, sexe, h_inscription, user_id, espace_id) VALUES ('".$eleve_id."', '".$dday."', '".$hor_name."', '".$code_activ."', '".$eleve_div."', '".$eleve_sex."', '".$h_exact."', '".$_SESSION['user_id']."', '".$inscript['espace_id']."')"); }
		else { mysqli_query($bdd,"INSERT INTO ".$prefix."_freq (eleves_id, date_freq, hor_name, code_activ, division, sexe, h_inscription, user_id, espace_id) VALUES ('".$eleve_id."', '".$dday."', '".$hor_name."', '".$code_activ."', '".$eleve_div."', '".$eleve_sex."', '".$h_exact."', '".$_SESSION['user_id']."', '0')"); }
		mysqli_query($bdd,"UPDATE ".$prefix."_freqres SET statut = '2' WHERE eleves_id = '".$eleve_id."' AND freqres_id = '".$_REQUEST['freqres_id']."'");
	}
	else if($nb_verif>0) { // si déjà inscrit on supprime de la réservation
		mysqli_query($bdd,"UPDATE ".$prefix."_freqres SET statut = '2' WHERE eleves_id = '".$eleve_id."' AND freqres_id = '".$_REQUEST['freqres_id']."'");
	}
}
mysqli_close($bdd);
?>