<?php
$ouverture = opendir("../img/nav/");
$lecture = readdir($ouverture);
$filename = '../img/nav/menuBase06.png';
if (file_exists($filename)) {
	unlink ("../img/nav/menuBase06.png");
}
$filename = '../img/nav/menuAide01.png';
if (file_exists($filename)) {
	unlink ("../img/nav/menuAide01.png");
}
$filename = '../img/nav/menuAide02.png';
if (file_exists($filename)) {
	unlink ("../img/nav/menuAide02.png");
}
$filename = '../img/nav/menuAide03.png';
if (file_exists($filename)) {
	unlink ("../img/nav/menuAide03.png");
}
$filename = '../img/nav/menuAide04.png';
if (file_exists($filename)) {
	unlink ("../img/nav/menuAide04.png");
}
$filename = '../img/nav/menuAide05.png';
if (file_exists($filename)) {
	unlink ("../img/nav/menuAide05.png");
}
$filename = '../img/nav/menuAide06.png';
if (file_exists($filename)) {
	unlink ("../img/nav/menuAide06.png");
}
$filename = '../img/nav/menuAide07.png';
if (file_exists($filename)) {
	unlink ("../img/nav/menuAide07.png");
}
$filename = '../img/nav/menuEdt02.png';
if (file_exists($filename)) {
	unlink ("../img/nav/menuEdt02.png");
}
$filename = '../img/nav/menuEval01.png';
if (file_exists($filename)) {
	unlink ("../img/nav/menuEval01.png");
}
closedir($ouverture);
$ouverture = opendir("../js/");
$lecture = readdir($ouverture);
$filename = '../js/jquery-3.2.1.js';
if (file_exists($filename)) {
	unlink ("../js/jquery-3.2.1.js");
}
$filename = '../js/jquery-3.3.1.js';
if (file_exists($filename)) {
	unlink ("../js/jquery-3.3.1.js");
}
closedir($ouverture);

// création de la table _userconnect
mysqli_query($bdd,"CREATE TABLE IF NOT EXISTS ".$base.".".$prefix."_userconnect (user_id INT ( 11 ) , connected INT ( 11 ) ) ENGINE = MYISAM DEFAULT CHARSET = utf8 COLLATE = utf8_unicode_ci;");

$users = mysqli_query($bdd, "SELECT id FROM ".$prefix."_user");

// Ajout d'une colonne todate dans la table ".$prefix."_user
mysqli_query($bdd,"ALTER TABLE ".$prefix."_user ADD connected_info INT ( 11 ) DEFAULT NULL");

// Changement dans les deux tables
foreach($users as $user) {
	mysqli_query($bdd,"INSERT INTO ".$prefix."_userconnect (user_id, connected) VALUES ('".$user['id']."', 0)");
	mysqli_query($bdd,"UPDATE ".$prefix."_user SET connected_info = 0 WHERE id = '".$user['id']."'");
}

// Ajout d'une colonne espace_default dans la table ".$prefix."_user
mysqli_query($bdd,"ALTER TABLE ".$prefix."_user ADD espace_default INT ( 11 ) DEFAULT NULL");

//ajout d'une option d'ajustement du seuil de séparation des stats par créneau
$verif_nb = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_parametres WHERE param_nom = 'freq_statcren_seuil'"));
if($verif_nb==0) {
	mysqli_query($bdd,"INSERT INTO ".$prefix."_parametres (param_nom, valeur, comments) VALUES ('freq_statcren_seuil', '20000', 'Option d\'ajustement du seuil de séparation des stats par créneau.')");
}

//ajout d'une option de notification de visites
$verif_nb = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_parametres WHERE param_nom = 'freq_notif_opt'"));
if($verif_nb==0) {
	mysqli_query($bdd,"INSERT INTO ".$prefix."_parametres (param_nom, valeur, comments) VALUES ('freq_notif_opt', '0', 'Option de notification de visites.')");
}
//gestion du seuil pour la notification de visites
$verif_nb = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_parametres WHERE param_nom = 'freq_notif_nb'"));
if($verif_nb==0) {
	mysqli_query($bdd,"INSERT INTO ".$prefix."_parametres (param_nom, valeur, comments) VALUES ('freq_notif_nb', '100', 'Nombre de visites pour la notification de visites.')");
}

//option de réservation à distance par espaces
$verif_nb = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_parametres WHERE param_nom = 'freq_reseleve_esp'"));
if($verif_nb==0) {
	mysqli_query($bdd,"INSERT INTO ".$prefix."_parametres (param_nom, valeur, comments) VALUES ('freq_reseleve_esp', '0', 'Option de réservation à distance par espaces.')");
}
//option de précision de l'activité lors de la réservation à distance
$verif_nb = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_parametres WHERE param_nom = 'freq_reseleve_activ'"));
if($verif_nb==0) {
	mysqli_query($bdd,"INSERT INTO ".$prefix."_parametres (param_nom, valeur, comments) VALUES ('freq_reseleve_activ', '0', 'Option de précision de l\'activité lors de la réservation à distance.')");
}
//option de précision de l'activité lors de la réservation
$verif_nb = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_parametres WHERE param_nom = 'freq_resactiv'"));
if($verif_nb==0) {
	mysqli_query($bdd,"INSERT INTO ".$prefix."_parametres (param_nom, valeur, comments) VALUES ('freq_resactiv', '0', 'Option de précision de l\'activité lors de la réservation.')");
}
// création de la colonne activ_id dans _freqres
mysqli_query($bdd,"ALTER TABLE ".$prefix."_freqres ADD activ_id INT ( 11 ) DEFAULT NULL");

// archivage des briques non archivées en doublon
$briks = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik WHERE textelibre = '' AND activ_brik < 2");
foreach($briks as $brik) {
	$tests = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik WHERE code = '".$brik['code']."' AND division = '".$brik['division']."' AND seq_id = '".$brik['seq_id']."' AND textelibre = '' AND activ_brik < 2");
	if(mysqli_num_rows($tests)>1) {
		$i=1;
		foreach($tests as $test) {
			if($i>1) { mysqli_query($bdd,"UPDATE ".$prefix."_edtbrik SET activ_brik = 2 WHERE brik_id = '".$test['brik_id']."'"); }
			$i++;
		}
	}
}
$briks = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik WHERE textelibre <> '' AND activ_brik < 2");
foreach($briks as $brik) {
	$tests = mysqli_query($bdd,"SELECT * FROM ".$prefix."_edtbrik WHERE code = '".$brik['code']."' AND textelibre = '".$brik['textelibre']."' AND activ_brik < 2");
	if(mysqli_num_rows($tests)>1) {
		$i=1;
		foreach($tests as $test) {
			if($i>1) { mysqli_query($bdd,"UPDATE ".$prefix."_edtbrik SET activ_brik = 2 WHERE brik_id = '".$test['brik_id']."'"); }
			$i++;
		}
	}
}

//ajout d'une option de désactivation des briques dans la page de création des EDT
$verif_nb = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_parametres WHERE param_nom = 'edt_brik_creadesactiv'"));
if($verif_nb==0) {
	mysqli_query($bdd,"INSERT INTO ".$prefix."_parametres (param_nom, valeur, comments) VALUES ('edt_brik_creadesactiv', '0', 'Option de désactivation des briques dans la page de création des EDT.')");
}

// version bdd :
mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = '0.30' WHERE param_nom = 'version_bdd'");
?>