<?php
$freq_excluopt_raisons = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_excluopt_raisons'"));
$freq_excluopt_signal = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_excluopt_signal'"));
$freq_excluopt_signalduree = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_excluopt_signalduree'"));
$freq_excluopt_exc = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_excluopt_exc'"));
$freq_excluopt_excduree = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_excluopt_excduree'"));
$freq_excluopt_limit = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_excluopt_limit'"));
$freq_excluopt_limitduree = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_excluopt_limitduree'"));
$dday = date('Y-m-d');
if(isset($_REQUEST['eleve_id'])) {
	$check_exc = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_exclusions WHERE eleves_id ='".$_REQUEST['eleve_id']."' AND (date_fin >= '".$dday."' OR date_info >= '".$dday."')"));
}
else {
	include('eleve_select.php');
	$check_exc = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_exclusions WHERE eleves_id ='".$eleve_id."' AND (date_fin >= '".$dday."' OR date_info >= '".$dday."')"));
}
if($check_exc==0||$yetexcluded==1) {
	if($freq_excluopt_raisons['valeur']==1) {
		if($_REQUEST['cause']!=""&&$_REQUEST['cause']!=FREQ_EXC1&&$_REQUEST['infocauseslisting']=="0") {
			$txt = mysqli_real_escape_string($bdd,$_POST['cause']);
			if(mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_exclucauses WHERE exclucause_txt = '".$txt."'"))==0) {
				mysqli_query($bdd,"INSERT ".$prefix."_exclucauses (exclucause_id, exclucause_txt) VALUES ('', '".$txt."')");
			}
			$cause_id_check = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT exclucause_id FROM ".$prefix."_exclucauses WHERE exclucause_txt = '".$txt."'"));
			if(mysqli_num_rows($cause_id_check)>0) { 
				$cause_id = $cause_id_check['exclucause_id'];
			}
			else { $cause_id = 0; }
		}
		else if($_REQUEST['infocauseslisting']!="0") {
			$cause_id = $_REQUEST['infocauseslisting'];
		}
		else { $cause_id = 0; }
	}
	else { $cause_id = 0; }
	if($freq_excluopt_signal['valeur']==1) { $date_info = $_REQUEST['signal']; }
	else { $date_info = '0000-00-00'; }
	if($freq_excluopt_exc['valeur']==1) { $date_fin = $_REQUEST['duree']; }
	else { $date_fin = '0000-00-00'; }
	$activ = mysqli_query($bdd,"SELECT * FROM ".$prefix."_freqactiv WHERE code_activ <> 'ZERO' ORDER BY nom_activ ASC");
	$string_activs = "";
	if(isset($_REQUEST['eleve_id'])) {
		foreach($activ as $check_activ) {
			if($_REQUEST['t'.$check_activ['code_activ']]!='') {
				$string_activs .= $_REQUEST['t'.$check_activ['code_activ']].'-';
			}
		}
		if($check_exc==0) {
			mysqli_query($bdd,"INSERT ".$prefix."_exclusions (exclusion_id, eleves_id, exclucause_id, date_exclusion, date_info, date_fin, excluactivs) VALUES ('', '".$_REQUEST['eleve_id']."', '".$cause_id."', '".$_REQUEST['creaexclu']."', '".$date_info."', '".$date_fin."', '".$string_activs."')");
		}
	}
	else {
		foreach($activ as $check_activ) {
			if(isset($_POST[$check_activ['code_activ']])) {
				$string_activs .= $check_activ['code_activ'].'-';
			}
		}
		if($check_exc==0) {
			mysqli_query($bdd,"INSERT ".$prefix."_exclusions (exclusion_id, eleves_id, exclucause_id, date_exclusion, date_info, date_fin, excluactivs) VALUES ('', '".$eleve_id."', '".$cause_id."', '".$_POST['creaexclu']."', '".$date_info."', '".$date_fin."', '".$string_activs."')");
		}
	}
}
/*else if($check_exc>0&&$yetexcluded==0) {
	echo '<p class="signal">Elève actuellement exclu(e).</p>';
}*/
else if($yetexcluded==1&&$check_exc==0&&mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_exclusions WHERE eleves_id ='".$eleve_id."' AND date_info >= '".$dday."'"))>0) {
	if(isset($_REQUEST['eleve_id'])) {
		$exclus_id = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT * FROM ".$prefix."_exclusions WHERE eleves_id ='".$_REQUEST['eleve_id']."' AND date_info >= '".$dday."'"));
	}
	else {
		$exclus_id = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT * FROM ".$prefix."_exclusions WHERE eleves_id ='".$eleve_id."' AND date_info >= '".$dday."'"));
	}
	mysqli_query($bdd,"UPDATE ".$prefix."_exclusions SET date_info = '' WHERE exclusion_id = '".$exclus_id['exclusion_id']."'");
}
?>