<?php
// 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 = '".$day."' AND hor_name = '".$creneau."'");
$verif_info = mysqli_fetch_assoc($verif);
$nb_verif = mysqli_num_rows($verif);
// Gestion si seuls les résas peuvent être inscrites
$onlyresas_check = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_onlyresas'");
$onlyresas_check_info = mysqli_fetch_assoc($onlyresas_check);
if(isset($_SESSION['freqonlyresas'])&&($onlyresas_check_info['valeur']!=@$_SESSION['freqonlyresas'])&&@$_SESSION['freqonlyresas']!='def') {
	$onlyresas_check_info['valeur']=@$_SESSION['freqonlyresas'];
}
if(@$onlyresas_check_info['valeur']=='1') {
	$onlyresa_check = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_freqres WHERE eleves_id = '".$eleve_id."' AND date_freqres = '".$day."' AND hor_name = '".$creneau."' AND statut = '0'"));
	if($onlyresa_check>0){
		$onlyresa_check = 0;
	}
	else {
		$onlyresa_check = 1;
	}
}
else {
	$onlyresa_check = 0;
}
// Vérification qu'il ne soit pas exclu :
$excluopt_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_excluopt'"));
if($excluopt_info['valeur']==1){
	$dday = date("Y-m-d");
	$exc_check = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_exclusions WHERE eleves_id ='".$eleve_id."' AND date_fin >= '".$dday."' AND excluactivs LIKE '%".$code_activ."%'"));
}
else { $exc_check=0; }
$limitopt_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_excluopt_limit'"));
if($limitopt_info['valeur']==1&&$espopt['valeur']==1){
	$dday = date("Y-m-d");
	$limit_check = mysqli_query($bdd,"SELECT * FROM ".$prefix."_limits limits LEFT JOIN ".$prefix."_limitstxt limt ON limits.limittxt_id = limt.limittxt_id WHERE limits.eleves_id ='".$eleve_id."' AND limits.date_fin >= '".$dday."' AND limt.espace_id = '".$espace_id."'");
	$lim_check = mysqli_num_rows($limit_check);
	if($lim_check>0) {
		$lim_details= mysqli_fetch_assoc($limit_check);
	}
}
else { $lim_check=0; }
//gestion du seuil
$crennum = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT quota_freq FROM ".$prefix."_horaires WHERE hor_name = '".$creneau."'"));
if($freq_gestion_jourscren['valeur']==0) {
	$quota_freq = @intval(substr($crennum['quota_freq'],0,3));
}
else {
	$quota_freq = @intval(substr($crennum['quota_freq'], strpos($crennum['quota_freq'], date('D')) + strlen(date('D')), 3));
}
if($espopt['valeur']==0) {
	$inscrits_select = "SELECT freq.eleves_id, freq.date_freq, freq.hor_name, freq.code_activ, freq.cas, eleves.nom, eleves.prenom, eleves.division 
	FROM (SELECT * 
		FROM ".$prefix."_freq 
		WHERE date_freq = '".$day."' AND hor_name =  '".$creneau."' AND cas NOT IN ('sor','exc') 
	) freq 
	INNER JOIN ".$prefix."_eleves eleves
	ON freq.eleves_id = eleves.eleves_id
	ORDER BY eleves.division ASC, eleves.nom DESC, eleves.prenom ASC";
	$inscrits = mysqli_query($bdd,$inscrits_select);
	$nb_inscrits = mysqli_num_rows($inscrits);
	if($quota_freq==0) {
		$sin = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_seuilnbi'");
		$sin_info = mysqli_fetch_assoc($sin);
	}
	else {
		$sin_info['valeur'] = $quota_freq;
	}
	$sir = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_seuilreaci'");
	$sir_info = mysqli_fetch_assoc($sir);
}
else {
	if($espace_id==0) {
		$esp_var = "AND (espace_id = '0' OR espace_id IS NULL) ";
	}
	else { $esp_var = "AND espace_id = '".$espace_id."'"; }
	$inscrits_select = "SELECT freq.eleves_id, freq.date_freq, freq.hor_name, freq.code_activ, freq.cas, espaces.espace_id, espaces.espace_nom, eleves.nom, eleves.prenom, eleves.division 
	FROM (SELECT * 
		FROM ".$prefix."_freq 
		WHERE date_freq = '".$day."' AND hor_name =  '".$creneau."' AND code_activ = '".$code_activ."'  AND cas NOT IN ('sor','exc') ".$esp_var."
	) 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
	ORDER BY eleves.division ASC, eleves.nom DESC, eleves.prenom ASC";
	$inscrits = mysqli_query($bdd,$inscrits_select);
	$nb_inscrits = mysqli_num_rows($inscrits);
	if($espace_id==0) {
		if($quota_freq==0) {
			$sin = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_seuilnbi'");
			$sin_info = mysqli_fetch_assoc($sin);
		}
		else {
			$sin_info['valeur'] = $quota_freq;
		}
		$sir = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_seuilreaci'");
		$sir_info = mysqli_fetch_assoc($sir);
	}
	else {
		$sin = mysqli_query($bdd,"SELECT seuilnbi FROM ".$prefix."_espaces WHERE espace_id = '".$espace_id."'");
		$sir = mysqli_query($bdd,"SELECT seuilreaci FROM ".$prefix."_espaces WHERE espace_id = '".$espace_id."'");
		$sin_info = mysqli_fetch_assoc($sin);
		$sir_info = mysqli_fetch_assoc($sir);
		$sin_info['valeur'] = $sin_info['seuilnbi'];
		$sir_info['valeur'] = $sir_info['seuilreaci'];
	}
}
if($sin_info['valeur']==0||$nb_inscrits<$sin_info['valeur']||$classe==1||$sir_info['valeur']=='alert') {
	if($nb_verif==0&&$exc_check==0&&$lim_check==0&&$eleve_id!=0&&$onlyresa_check==0) {
		$son_file = array('0.wav','0899.wav','0253.wav','0292.wav','0294.wav','0227.wav','0283.wav','0863.wav','0236.wav','2481.wav');
		// Dépassement du seuil de tolérance si différents espaces
		if($espopt['valeur']==1&&$sin_info['valeur']!=0&&$nb_inscrits>=$sin_info['valeur']&&$sir_info['valeur']=='alert') {
			if(@$seuilalert!=1) {
				echo '<p name="seuil" class="signal"><b>'.FREQ_SEUIL1.'</b></p>';
			}
			$seuilalert = 1;
		}
		// 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."', '".$day."', '".$creneau."', '".$code_activ."', '".$eleve_div."', '".$eleve_sex."', '".$h_exact."', '".$_SESSION['user_id']."', '".$espace_id."')");
			mysqli_query($bdd,"UPDATE ".$prefix."_freqres SET statut = '2' WHERE eleves_id = '".$eleve_id."' AND code_freqres = '".$day.$creneau."'");
		}
		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."', '".$day."', '".$creneau."', '".$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 code_freqres = '".$day.$creneau."'");
		}
		if(isset($_POST['seance'])) {
			if($_POST['seance']!="non"&&$_POST['seance']!="create") {
				mysqli_query($bdd,"INSERT INTO ".$prefix."_seqeleves (eleves_id, seq_id) VALUES ('".$eleve_id."', '".$_POST['seance']."')");
			}
			if($_POST['seance']=="create"&&$_POST['sequence']!="") {
				mysqli_query($bdd,"INSERT INTO ".$prefix."_seqeleves (eleves_id, seq_id) VALUES ('".$eleve_id."', '".$_POST['sequence']."')");				
			}
		}
		// Gestion message de validation
		$validmes_check = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_validmes'");
		$validmes_check_info = mysqli_fetch_assoc($validmes_check);
		if(isset($_SESSION['freqvalidmes'])&&($validmes_check_info['valeur']!=@$_SESSION['freqvalidmes'])&&@$_SESSION['freqvalidmes']!='def') {
			$validmes_check_info['valeur']=@$_SESSION['freqvalidmes'];
		}
		if(@$validmes_check_info['valeur']=='1') {
			$eleve_val = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT prenom, nom, division FROM ".$prefix."_eleves WHERE eleves_id = '".$eleve_id."'"));
			echo '<p class="signalok">'.FREQ_INF13.' '.$eleve_val['prenom'].' '.$eleve_val['nom'].' ('.$eleve_val['division'].').</p>';
		}
		// gestion des inscriptions sur plusieurs créneaux de suite
		$nb_cren = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_multiinscrit'"));
		if($nb_cren['valeur']!=$_SESSION['nbcren']&&$_SESSION['nbcren']!='def') {
			$nb_cren['valeur']=$_SESSION['nbcren'];
		}
		if($nb_cren['valeur']!=0) {
			for($i=1;$i<=$nb_cren['valeur'];$i++) {
				if($freq_gestion_jourscren['valeur']==0) {
					$crensuiv[$i] = mysqli_fetch_array(mysqli_query($bdd,"SELECT * FROM ".$prefix."_horaires WHERE num_creneau = ".($creneau_info['num_creneau']+$i)." AND hor_freq LIKE '001%'"));
				}
				else {
					$crensuiv[$i] = mysqli_fetch_array(mysqli_query($bdd,"SELECT * FROM ".$prefix."_horaires WHERE num_creneau = ".($creneau_info['num_creneau']+$i)." AND hor_freq LIKE '%".date('D')."001%'"));
				}
				if(isset($_POST['crensuivok'.$i.''])) {
					$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."', '".$day."', '".$crensuiv[$i]['hor_name']."', '".$code_activ."', '".$eleve_div."', '".$eleve_sex."', '".$h_exact."', '".$_SESSION['user_id']."', '".$espace_id."')");
						mysqli_query($bdd,"UPDATE ".$prefix."_freqres SET statut = '2' WHERE eleves_id = '".$eleve_id."' AND code_freqres = '".$day.$crensuiv[$i]['hor_name']."'");
					}
					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."', '".$day."', '".$crensuiv[$i]['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 code_freqres = '".$day.$crensuiv[$i]['hor_name']."'");
					}
				}
			}
		}
		$verif_bip_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_retards_bip'"));
		if($verif_bip_info['valeur']!=0) {
			$retard = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_retards WHERE eleves_id = '".$eleve_id."'"));
			if($retard>0) {
				if(in_array($verif_bip_info['valeur'],$son_file)) {
					echo '<audio autoplay><source src="./img/wav/'.$verif_bip_info['valeur'].'" type="audio/wav"></audio> ';
				}
				else {
					echo '<audio autoplay><source src="./img/wav/'.$prefix.'/'.$verif_bip_info['valeur'].'" type="audio/wav"></audio> ';
				}
			}
		}
		$verif_bip_resa = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_sigb_resabip'"));
		if($verif_bip_resa['valeur']!=0) {
			$resa = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_sigbresas WHERE eleves_id = '".$eleve_id."'"));
			if($resa>0) {
				if(in_array($verif_bip_resa['valeur'],$son_file)) {
					echo '<audio autoplay><source src="./img/wav/'.$verif_bip_resa['valeur'].'" type="audio/wav"></audio> ';
				}
				else {
					echo '<audio autoplay><source src="./img/wav/'.$prefix.'/'.$verif_bip_resa['valeur'].'" type="audio/wav"></audio> ';
				}
			}
		}
		$birthday = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT * FROM ".$prefix."_parametres WHERE param_nom = 'freq_birthday_img'"));
		$verif_bip_birth = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_birthday_bip'"));
		if($birthday['valeur']==1&&$verif_bip_birth['valeur']!=0) {
			if(substr($eleve_naissance,5,5)==date('m-d')) {
				if(in_array($verif_bip_birth['valeur'],$son_file)) {
					echo '<audio autoplay><source src="./img/wav/'.$verif_bip_birth['valeur'].'" type="audio/wav"></audio> ';
				}
				else {
					echo '<audio autoplay><source src="./img/wav/'.$prefix.'/'.$verif_bip_birth['valeur'].'" type="audio/wav"></audio> ';
				}
			}		
		}
		$nopt = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_notif_opt'"));
		if($nopt['valeur']==1) {
			include('caloffsco.php');
			$nnb = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_notif_nb'"));
			if($rentree>date('Y-m-d')) {
				$rentree = $rentreeprecedente;
			}
			$elevenb = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT freq.freq_id, COUNT(eleves.eleves_id) FROM ".$prefix."_freq freq 
				INNER JOIN ".$prefix."_eleves eleves
				ON freq.eleves_id = eleves.eleves_id
				WHERE eleves.eleves_id = '".$eleve_id."' AND freq.date_freq > '".$rentree."' GROUP BY eleves.eleves_id ORDER BY COUNT(eleves.eleves_id) DESC"));
			if(isset($elevenb['COUNT(eleves.eleves_id)'])) {
				if(@is_int($elevenb['COUNT(eleves.eleves_id)']/@$nnb['valeur'])) {
					$eleve_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT prenom, nom, division FROM ".$prefix."_eleves WHERE eleves_id = '".$eleve_id."'"));
					echo '<span class="signal">'.$elevenb['COUNT(eleves.eleves_id)'].'<sup>e</sup> visite de '.$eleve_info['prenom'].' '.$eleve_info['nom'].' ('.$eleve_info['division'].') !</span><br>'; 
					$verif_bip_nb = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_nbvisites_bip'"));
					if($verif_bip_nb['valeur']!="0") {
						if(in_array($verif_bip_nb['valeur'],$son_file)) {
							echo '<audio autoplay><source src="./img/wav/'.$verif_bip_nb['valeur'].'" type="audio/wav"></audio> ';
						}
						else {
							echo '<audio autoplay><source src="./img/wav/'.$prefix.'/'.$verif_bip_nb['valeur'].'" type="audio/wav"></audio> ';
						}
					}
				}
			}
		}
	}
	else { 
		$eleve_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT prenom, nom, division FROM ".$prefix."_eleves WHERE eleves_id = '".$eleve_id."'"));
		if($onlyresa_check==1) {
			echo '<p class="signal">'.FREQ_INF12.' '.$eleve_info['prenom'].' '.$eleve_info['nom'].' ('.$eleve_info['division'].').</p>';
		}
		else if($exc_check>0||$lim_check>0) {
			if($exc_check>0) {
				$_SESSION['forcing_reason']='exc';
			}
			else{
				$_SESSION['forcing_reason']='lim';
			}
			$h_exact = date('Ymd').'T'.date('His');
			if(isset($_SESSION['forcing_multi'])) { $_SESSION['forcing_multi']++; }
			else { $_SESSION['forcing_multi'] = 1; }
			if($espopt['valeur']==1) { echo '<input type="hidden" name="fespace_id" value="'.$espace_id.'" />'; }
			$_SESSION['forc'.$_SESSION['forcing_multi']]=array();
			array_push($_SESSION['forc'.$_SESSION['forcing_multi']], $eleve_info['prenom'], $eleve_info['nom'], $eleve_info['division'], $eleve_id, $day, $creneau, $code_activ, $eleve_div, $eleve_sex, $h_exact);
			if($espopt['valeur']==1) { array_push($_SESSION['forc'.$_SESSION['forcing_multi']], $espace_id); }
			if(isset($lim_details['limits_txt'])) { array_push($_SESSION['forc'.$_SESSION['forcing_multi']], $lim_details['limits_txt'], $lim_details['espace_id']); }
		}
		else if($verif_info['cas'] == "sor") {
			echo '<p class="signal">'.FREQ_INF6.' '.$eleve_info['prenom'].' '.$eleve_info['nom'].' ('.$eleve_info['division'].').</p>';
			if($espopt['valeur']==1) { $espsql = ", espace_id = '".$espace_id."' "; }
			else { $espsql = ""; }
			$hcheck = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT h_sortie_2, h_sortie_3 FROM ".$prefix."_freq WHERE eleves_id = '".$eleve_id."' AND date_freq = '".$day."' AND hor_name = '".$creneau."' AND code_activ = '".$code_activ."'"));
			$h_inscrit = date('Ymd').'T'.date('His');
			if(@strlen($hcheck['h_sortie_2'])==15) { $hs = 'h_inscription3'; }
			else { $hs = 'h_inscription2'; }
			mysqli_query($bdd,"UPDATE ".$prefix."_freq SET cas = ''".$espsql.", ".$hs." = '".$h_inscrit."' WHERE eleves_id = '".$eleve_id."' AND date_freq = '".$day."' AND hor_name = '".$creneau."' AND code_activ = '".$code_activ."'");
		}
		else if($verif_info['cas'] == "exc") {
			echo '<p class="signal">'.FREQ_INF7.' '.$eleve_info['prenom'].' '.$eleve_info['nom'].' ('.$eleve_info['division'].').</p>'; 
			if($espopt['valeur']==1) { $espsql = ", espace_id = '".$espace_id."' "; }
			else { $espsql = ""; }
			$hcheck = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT h_sortie_2, h_sortie_3 FROM ".$prefix."_freq WHERE eleves_id = '".$eleve_id."' AND date_freq = '".$day."' AND hor_name = '".$creneau."' AND code_activ = '".$code_activ."'"));
			$h_inscrit = date('Ymd').'T'.date('His');
			if(@strlen($hcheck['h_sortie_2'])==15) { $hs = 'h_inscription3'; }
			else { $hs = 'h_inscription2'; }
			mysqli_query($bdd,"UPDATE ".$prefix."_freq SET cas = ''".$espsql.", ".$hs." = '".$h_inscrit."' WHERE eleves_id = '".$eleve_id."' AND date_freq = '".$day."' AND hor_name = '".$creneau."' AND code_activ = '".$code_activ."'");
		}
		else {
			echo '<p class="signal">'.FREQ_INF8.' '.$eleve_info['prenom'].' '.$eleve_info['nom'].' '.$eleve_info['division'].'</p>'; 
		}
	}
}
else if($sir_info['valeur']=='stopp'&&$classe==0) {
	echo '<p name="seuil" class="signal"><b>'.FREQ_SEUIL2;
	if($espopt['valeur']==1) { echo ' '.FREQ_SEUIL3; }
	echo '!</b></p>';
}
?>
