<?php
// on regarde les événements proches pour ajuster si besoin : SUR LE DEBUT :
$verif = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtevent event INNER JOIN ".$prefix."_userevent userevent ON event.uid = userevent.uid INNER JOIN ".$prefix."_espaces_event espevent ON event.uid = espevent.uid WHERE event.dtstart <= '".$uid_info['dtstart']."' AND event.dtend > '".$uid_info['dtstart']."' AND userevent.user_id = '".$usercode."' AND espevent.espace_id = '".$espid."' AND event.uid <> '".$uid."'");
$nb_verif = mysqli_num_rows($verif);
$test_nb=0;
if($nb_verif==1) {
	$startmod = mysqli_fetch_assoc($verif);
	mysqli_query($bdd,"UPDATE ".$prefix."_edtevent SET dtstart = '".$startmod['dtend']."', specifique = '1' WHERE uid = '".$uid."'");
	$test_nb=1;
}
// on regarde les événements proches pour ajuster si besoin : SUR LA FIN :
$verif = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtevent event INNER JOIN ".$prefix."_userevent userevent ON event.uid = userevent.uid INNER JOIN ".$prefix."_espaces_event espevent ON event.uid = espevent.uid WHERE event.dtstart <= '".$uid_info['dtend']."' AND userevent.user_id = '".$usercode."' AND espevent.espace_id = '".$espid."' AND event.dtend >= '".$uid_info['dtend']."' AND event.uid <> '".$uid."'");
$nb_verif = mysqli_num_rows($verif);
if($nb_verif==1) {
	$endmod = mysqli_fetch_assoc($verif);
	mysqli_query($bdd,"UPDATE ".$prefix."_edtevent SET dtend = '".$endmod['dtstart']."', specifique = '1' WHERE uid = '".$uid."'");
	$test_nb=1;
}
if($test_nb==1) {
	// on ajuste la durée de l'événement :
	$update_spe = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT dtstart, dtend FROM ".$prefix."_edtevent WHERE uid = '".$uid."'"));
	$dteStart = new DateTime($update_spe['dtstart']);
	$dteEnd = new DateTime($update_spe['dtend']);
	$dteDiff  = $dteStart->diff($dteEnd); 
	$duree1 = $dteDiff->format("%H%I");
	if($duree1<=40) {
		$duree1 = 5;
	}
	else if($duree1>40&&$duree1<115) {
		$duree1 = 10;
	}
	else {
		$heu=substr($duree1,1,1);
		$min=substr($duree1,2,2);
		if($heu.$min>($heu-1).'40'&&$heu.$min<$heu.'15') {
			if($min>40) {
				$duree1 = ($heu+1).'0';
			}
			else { $duree1 = $heu.'0'; }
		}
		else {
			if($min>40) {
				$duree1 = ($heu+1).'0';
			}
			else { $duree1 = $heu.'5'; }
		}
	}
	mysqli_query($bdd,"UPDATE ".$prefix."_edtevent SET duration = '".$duree1."' WHERE uid = '".$uid."'");
}
?>