<?php session_start(); 
require_once(dirname(__DIR__, 1) . '/config.php');
require_once(BASE_PATH . '/db/idl_connect.php');
echo '<meta charset="utf-8" />';
if(isset($_SESSION['user_droits'])) {
	if($_SESSION['user_droits'][4]!="2")
	{
		echo '<p style="text-align:center;font-size:1.4em;padding-top:20px;">'.NOMODULE.'<br><a href="../index.php">'.ACCUEIL.'</a></p>';
	}
	else {
		echo '<p style="text-align:center;font-size:1.4em;padding-top:20px;">'.INSTALLCOMP.'</p>'; flush(); ob_flush();
			if (($handle = fopen("emi_savoirs.csv", "r")) !== FALSE) {
				while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
					$att0 = mysqli_real_escape_string($bdd,$data[0]);
					$att1 = mysqli_real_escape_string($bdd,$data[1]);
					$att2 = mysqli_real_escape_string($bdd,$data[2]);
					$att3 = mysqli_real_escape_string($bdd,$data[3]);
					$att4 = mysqli_real_escape_string($bdd,$data[4]);
					$att5 = mysqli_real_escape_string($bdd,$data[5]);
					mysqli_query($bdd,"INSERT INTO ".$prefix."_savoirsmaj (s_id, nom_savoir, niveau, champs_id, notions, base) VALUES ('" . $att0 . "', '" . $att1 . "', '" . $att2 . "', '" . $att3 . "', '" . $att4 . "', '" . $att5 . "')");
				}
				fclose($handle);
			}
						
			$tabDebniveau = array('5', '4', '3');
			$tabNiveau = array('5ème', '4ème', '3ème');
			$i=0;
			foreach($tabDebniveau as $divdeb) {
				// Intégration dans la table ".$prefix."_savoirs* des savoirs pour le niveau
				echo '<p style="text-align:center;font-size:1.4em;padding-top:5px;">'.INSTALLNIVSAV.' '.$tabNiveau[$i].')...</p>'; flush(); ob_flush();
				$sql = mysqli_query($bdd,"SELECT * FROM ".$prefix."_savoirsmaj WHERE niveau = '".$divdeb."e'");
				while($sql_info=mysqli_fetch_assoc($sql)) {
					$att0 = mysqli_real_escape_string($bdd,$sql_info['s_id']);
					$att1 = mysqli_real_escape_string($bdd,$sql_info['nom_savoir']);
					$att2 = mysqli_real_escape_string($bdd,$sql_info['niveau']);
					$att3 = mysqli_real_escape_string($bdd,$sql_info['champs_id']);
					$att4 = mysqli_real_escape_string($bdd,$sql_info['notions']);
					$att5 = mysqli_real_escape_string($bdd,$sql_info['base']);
					mysqli_query($bdd,"INSERT INTO ".$prefix."_savoirs".$divdeb." (s".$divdeb."_id, nom_savoir, niveau, champs_id, notions, base) VALUES ('" . $att0 . "', '" . $att1 . "', '" . $att2 . "', '" . $att3 . "', '" . $att4 . "', '" . $att5 . "')");
				}
				$i++;
			}
						
			// Suppression des lignes de la table temporaire ".$prefix."_savoirsmaj
			echo '<p style="text-align:center;font-size:1.4em;padding-top:5px;">'.TEMPSUPPR.'</p>'; flush(); ob_flush();
			mysqli_query($bdd,"DELETE FROM ".$prefix."_savoirsmaj");
			
			// Paramétrage des savoirs dans ".$prefix."_parametres pour activer l'EMI (sans toucher aux autres si besoin)
			$verif_param_sav = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'savoirs'");
			$verif_param_sav_info = mysqli_fetch_array($verif_param_sav);
			if($verif_param_sav_info['valeur'] == "A1E0P0")
			{
				mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur='A1E1P0' WHERE param_nom='savoirs'");
			}
			else if($verif_param_sav_info['valeur'] == "A0E0P1")
			{
				mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur='A0E1P1' WHERE param_nom='savoirs'");
			}
			else if($verif_param_sav_info['valeur'] == "A1E0P1")
			{
				mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur='A1E1P1' WHERE param_nom='savoirs'");
			}
			
			// Fin de l'installation
			echo '<p style="text-align:center;font-size:1.4em;padding-top:5px;">'.INSTALLFINI.'<br><a href="../param.php?page=savoirs">'.BACKPARAM.'</a></p>';
	}
}
else {
	echo '<p style="text-align:center;font-size:1.4em;padding-top:20px;">'.NOACC2.'<br><a href="../index.php">'.ACCUEIL.'</a></p>';
}
mysqli_close($bdd);
?>
