<?php

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

$ouverture = opendir("./include/");
$lecture = readdir($ouverture);
@unlink ("./include/showalltooltips.php");
closedir($ouverture);
echo '<li>Suppression d\'un fichier obsolète de statistiques showalltooltips.php.</li>';

$ouverture = opendir("./js/");
$lecture = readdir($ouverture);
@unlink ("./js/Chart.js");
closedir($ouverture);
echo '<li>Suppression d\'un fichier obsolète de statistiques Chart.js</li>';

$nbc=0;
$nbd=0;
$tabevent_select = 
"SELECT event.dtstart, event.dtend, event.week, event.hor_id, event.seq_id, event.hor_name, event.duration, event.specifique, brik.brik_id, brik.code, brik.seq_id, brik.division, SUBSTR(brik.division,1,9), brik.textelibre, brik.summary, brik.close, userevent.uid, userevent.user_id, espevent.uid, espevent.espace_id 
FROM ".$prefix."_edtevent event
INNER JOIN ".$prefix."_edtbrik brik
ON event.brik_id = brik.brik_id
INNER JOIN ".$prefix."_userevent userevent 
ON event.uid = userevent.uid 
INNER JOIN ".$prefix."_espaces_event espevent 
ON event.uid = espevent.uid 
WHERE specifique = '1'";
$tabevent_infos = mysqli_query($bdd,$tabevent_select);
foreach($tabevent_infos as $tabevent_info) {
	$checkevent = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtevent edt INNER JOIN ".$prefix."_espaces_event eve ON edt.uid = eve.uid WHERE eve.espace_id = '".$tabevent_info['espace_id']."' AND (edt.dtstart < '".$tabevent_info['dtend']."' AND edt.dtstart > '".$tabevent_info['dtstart']."') AND edt.uid <> '".$tabevent_info['uid']."'");
	if(mysqli_num_rows($checkevent)==1) {
		$info = mysqli_fetch_assoc($checkevent);
		if($info['dtend']<=$tabevent_info['dtend']) {
			$nbd++;
			mysqli_query($bdd,"DELETE FROM ".$prefix."_userevent WHERE uid = '".$info['uid']."'");
			mysqli_query($bdd,"DELETE FROM ".$prefix."_espaces_event WHERE uid = '".$info['uid']."'");
			mysqli_query($bdd,"DELETE FROM ".$prefix."_edtevent WHERE uid = '".$info['uid']."'");
		}
		else {
			$nbc++;
			$dteStart = new DateTime($tabevent_info['dtend']);
			$dteEnd = new DateTime($info['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 specifique = '1', dtstart = '".$tabevent_info['dtend']."', dtend = '".$info['dtend']."', duration = '".$duree1."' WHERE uid = '".$info['uid']."'");
		}
	}
	$checkevent = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtevent edt INNER JOIN ".$prefix."_espaces_event eve ON edt.uid = eve.uid WHERE eve.espace_id = '".$tabevent_info['espace_id']."' AND (edt.dtend > '".$tabevent_info['dtstart']."' AND edt.dtend < '".$tabevent_info['dtend']."') AND edt.uid <> '".$tabevent_info['uid']."'");
	if(mysqli_num_rows($checkevent)==1) {
		$info = mysqli_fetch_assoc($checkevent);
		if($info['dtstart']>=$tabevent_info['dtstart']) {
			$nbd++;
			mysqli_query($bdd,"DELETE FROM ".$prefix."_userevent WHERE uid = '".$info['uid']."'");
			mysqli_query($bdd,"DELETE FROM ".$prefix."_espaces_event WHERE uid = '".$info['uid']."'");
			mysqli_query($bdd,"DELETE FROM ".$prefix."_edtevent WHERE uid = '".$info['uid']."'");
		}
		else {
			$nbc++;
			$dteStart = new DateTime($info['dtstart']);
			$dteEnd = new DateTime($tabevent_info['dtstart']);
			$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 specifique = '1', dtstart = '".$info['dtstart']."', dtend = '".$tabevent_info['dtstart']."', duration = '".$duree1."' WHERE uid = '".$info['uid']."'");
		}
	}
}
echo '<li>Correction d\'événements avec chevauchements<br>('.$nbc.' événement.s corrigé.s, '.$nbd.' événement.s supprimé.s)</li></ul>';

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