<?php

echo '<p><b>Mise à jour base de données <i class="fas fa-angle-double-right"></i> Version 0.38</b></p>
<ul class="maxwidth">';

$event = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtevent event INNER JOIN ".$prefix."_userevent userevent ON event.uid = userevent.uid WHERE event.week > '202105'");
$nb_check = 0;
foreach($event as $uid_info) {
	// 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 WHERE event.dtstart <= '".$uid_info['dtstart']."' AND event.dtend >= '".$uid_info['dtstart']."' AND userevent.user_id = '".$uid_info['user_id']."' AND event.uid <> '".$uid_info['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_info['uid']."'");
		$nb_check++;
		$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 WHERE event.dtstart <= '".$uid_info['dtend']."' AND event.dtend >= '".$uid_info['dtend']."' AND event.uid <> '".$uid_info['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_info['uid']."'");
		if($test_nb==0) { $nb_check++; }
		$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_info['uid']."'"));
		$deb = substr($update_spe['dtstart'],9,4);
		$fin = substr($update_spe['dtend'],9,4);
		$duree1 = (round((($fin-$deb)/100),1))*10;
		if($duree1<=8) {
			$duree1 = 5;
		}
		else if($duree1>8&&$duree1<10) {
			$duree1 = 10;
		}
		else if(substr($duree1,1,1)<3) {
			$duree1 = substr($duree1,0,1).'0';
		}
		else if (substr($duree1,1,1)>=3&&substr($duree1,1,1)<=7) {
			$duree1 = substr($duree1,0,1).'5';
		}
		else if (substr($duree1,1,1)>7) {
			$duree1 = (substr($duree1,0,1)+1).'0';
		}
		mysqli_query($bdd,"UPDATE ".$prefix."_edtevent SET duration = '".$duree1."' WHERE uid = '".$uid_info['uid']."'");
	}
}
echo '<li>Correction d\'évenements intégrés sur des temps spécifiques ('.$nb_check.').</li>';

// version bdd :
mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '0.38' WHERE param_nom = 'version_bdd'");
echo '<li>Mise à jour de la base de données effectuée - version_bdd 0.38.</li></ul>';
?>