<?php
if($droits!=2&&$droits!=6&&$droits!=4) {
	// Gestion de l'absence de droits sur créneau et classe
	$_POST['datepick'] = "";
	$_POST['crenpicker'] = "";
	$_POST['classe'] = "";
}

$code_activ = $_POST['selectactiv'];
if($espopt['valeur']==1) { 
	$espace_id = $_POST['selectespace']; $_SESSION['espace_id']=$espace_id; }
$_SESSION['code_activ'] = $code_activ;
if($_POST['divgrap']!="") {
	$_SESSION['divgrap'] = $_POST['divgrap'];
	if(isset($_SESSION['nivgrap'])) { unset($_SESSION['nivgrap']); }
	$divgrap = $_POST['divgrap'];
	$nivgrap = substr($_POST['divgrap'],0,1);
}
else { 
	$_SESSION['nivgrap'] = $_POST['nivgrap']; 
	if(isset($_SESSION['divgrap'])) { unset($_SESSION['divgrap']); }
	$nivgrap = $_POST['nivgrap'];
	$divgrap = "";
}
if($crenchoix_info['valeur']==1&&$_POST['datepick']!=""&&$_POST['crenpicker']!="") {
	$_SESSION['datepickchoix'] = $_POST['datepick'];
	$_SESSION['crenpickerchoix'] = $_POST['crenpicker'];
	$day = date('Y-m-d', strtotime(str_replace("/","-",$_POST['datepick'])));
	$jouraff = date('Ymd', strtotime(str_replace("/","-",$_POST['datepick'])));
	$creneau = $_POST['crenpicker'];
	$heure = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT debut FROM ".$prefix."_horaires WHERE hor_name = '".$_POST['crenpicker']."'"));
	$heurep = substr($heure['debut'],0,2).substr($heure['debut'],3,2).substr($heure['debut'],6,2);
	if($_POST['seance']=="create"&&$_POST['sequence']=="") {
		echo FREQ_SEQU1;
	}
	else {
		if(isset($_POST['passgrap'])) {
			$_SESSION['passgrap']=1;
			$check = mysqli_query($bdd,"SELECT * FROM ".$prefix."_freqgrappes WHERE division = '$divgrap' AND niveau = '$nivgrap' AND dtstart = '".$jouraff."T".$heurep."'");
			if(mysqli_num_rows($check)==1) {
				$pastnb = mysqli_fetch_assoc($check);
				mysqli_query($bdd,"UPDATE ".$prefix."_freqgrappes SET nombre = '".($pastnb['nombre']+$_POST['nbgrap'])."' WHERE division = '$divgrap' AND niveau = '$nivgrap' AND dtstart = '".$jouraff."T".$heurep."'");
			}
			else {
				if($espopt['valeur']==1) { 
					mysqli_query($bdd,"INSERT ".$prefix."_freqgrappes (freqgrap_id, nombre, division, niveau, date_freq, dtstart, passage, user_id, espace_id) VALUES ('', '".$_POST['nbgrap']."', '".$divgrap."', '".$nivgrap."', '".$day."', '".$jouraff."T".$heurep."', '1', '".$_SESSION['user_id']."', '".$espace_id."')");
				}
				else {
					mysqli_query($bdd,"INSERT ".$prefix."_freqgrappes (freqgrap_id, nombre, division, niveau, date_freq, dtstart, passage, user_id) VALUES ('', '".$_POST['nbgrap']."', '".$divgrap."', '".$nivgrap."', '".$day."', '".$jouraff."T".$heurep."', '1', '".$_SESSION['user_id']."')");
				}
			}
		}
		else {
			$_SESSION['passgrap']=0;
			$check = mysqli_query($bdd,"SELECT * FROM ".$prefix."_freqgrappes WHERE division = '$divgrap' AND niveau = '$nivgrap' AND date_freq = '".$day."' AND hor_name = '".$creneau."' AND code_activ = '".$code_activ."'");
			if(mysqli_num_rows($check)==1) {
				$pastnb = mysqli_fetch_assoc($check);
				mysqli_query($bdd,"UPDATE ".$prefix."_freqgrappes SET nombre = '".($pastnb['nombre']+$_POST['nbgrap'])."' WHERE division = '$divgrap' AND niveau = '$nivgrap' AND date_freq = '".$day."' AND hor_name = '".$creneau."' AND code_activ = '".$code_activ."'");
			}
			else {
				if($espopt['valeur']==1) { 
					mysqli_query($bdd,"INSERT ".$prefix."_freqgrappes (freqgrap_id, nombre, division, niveau, date_freq, hor_name, code_activ, passage, user_id, espace_id) VALUES ('', '".$_POST['nbgrap']."', '".$divgrap."', '".$nivgrap."', '".$day."', '".$creneau."', '".$code_activ."', '0', '".$_SESSION['user_id']."', '".$espace_id."')");
				}
				else {
					mysqli_query($bdd,"INSERT ".$prefix."_freqgrappes (freqgrap_id, nombre, division, niveau, date_freq, hor_name, code_activ, passage, user_id) VALUES ('', '".$_POST['nbgrap']."', '".$divgrap."', '".$nivgrap."', '".$day."', '".$creneau."', '".$code_activ."', '0', '".$_SESSION['user_id']."')");
				}
			}
		}
	}
}
else {
	if(isset($_SESSION['datepickchoix'])) {
		unset($_SESSION['datepickchoix']);
	}
	if(isset($_SESSION['crenpickerchoix'])) {
		unset($_SESSION['crenpickerchoix']);
		$_POST['soluce']==1;
	}
	if(@$creneau_info['hor_freq']==0||@$_POST['soluce']==0) { 
		echo '<p><b>'.FREQ_NO1.'</b></p>';
	}
	else {
		if(isset($_SESSION['nextday'])) {
			$day = $_SESSION['nextday'];
		}
		else {
			$day = date("Y-m-d");
		}
		$jouraff = substr($day,0,4).substr($day,5,2).substr($day,8,2);
		$heurep = date("Hi").'00';
		$creneau = $_SESSION['ccren'];	
	}
	if(isset($_POST['passgrap'])) {
		$_SESSION['passgrap']=1;
		$check = mysqli_query($bdd,"SELECT * FROM ".$prefix."_freqgrappes WHERE division = '$divgrap' AND niveau = '$nivgrap' AND dtstart = '".$jouraff."T".$heurep."'");
		if(mysqli_num_rows($check)==1) {
			$pastnb = mysqli_fetch_assoc($check);
			mysqli_query($bdd,"UPDATE ".$prefix."_freqgrappes SET nombre = '".($pastnb['nombre']+$_POST['nbgrap'])."' WHERE division = '$divgrap' AND niveau = '$nivgrap' AND dtstart = '".$jouraff."T".$heurep."'");
		}
		else {
			if($espopt['valeur']==1) { 
				mysqli_query($bdd,"INSERT ".$prefix."_freqgrappes (freqgrap_id, nombre, division, niveau, date_freq, dtstart, passage, user_id, espace_id) VALUES ('', '".$_POST['nbgrap']."', '".$divgrap."', '".$nivgrap."', '".$day."', '".$jouraff."T".$heurep."', '1', '".$_SESSION['user_id']."', '".$espace_id."')");
			}
			else {
				mysqli_query($bdd,"INSERT ".$prefix."_freqgrappes (freqgrap_id, nombre, division, niveau, date_freq, dtstart, passage, user_id) VALUES ('', '".$_POST['nbgrap']."', '".$divgrap."', '".$nivgrap."', '".$day."', '".$jouraff."T".$heurep."', '1', '".$_SESSION['user_id']."')");
			}
		}
	}
	else {
		$_SESSION['passgrap']=0;
		$check = mysqli_query($bdd,"SELECT * FROM ".$prefix."_freqgrappes WHERE division = '$divgrap' AND niveau = '$nivgrap' AND date_freq = '".$day."' AND hor_name = '".$creneau."' AND code_activ = '".$code_activ."'");
		if(mysqli_num_rows($check)==1) {
			$pastnb = mysqli_fetch_assoc($check);
			mysqli_query($bdd,"UPDATE ".$prefix."_freqgrappes SET nombre = '".($pastnb['nombre']+$_POST['nbgrap'])."' WHERE division = '$divgrap' AND niveau = '$nivgrap' AND date_freq = '".$day."' AND hor_name = '".$creneau."' AND code_activ = '".$code_activ."'");
		}
		else {
			if($espopt['valeur']==1) { 
				mysqli_query($bdd,"INSERT ".$prefix."_freqgrappes (freqgrap_id, nombre, division, niveau, date_freq, hor_name, code_activ, passage, user_id, espace_id) VALUES ('', '".$_POST['nbgrap']."', '".$divgrap."', '".$nivgrap."', '".$day."', '".$creneau."', '".$code_activ."', '0', '".$_SESSION['user_id']."', '".$espace_id."')");
			}
			else {
				mysqli_query($bdd,"INSERT ".$prefix."_freqgrappes (freqgrap_id, nombre, division, niveau, date_freq, hor_name, code_activ, passage, user_id) VALUES ('', '".$_POST['nbgrap']."', '".$divgrap."', '".$nivgrap."', '".$day."', '".$creneau."', '".$code_activ."', '0', '".$_SESSION['user_id']."')");
			}
		}
	}
}
?>
