<?php session_start();
require_once(dirname(__DIR__, 2) . '/config.php');
require_once(BASE_PATH . '/db/idl_connect.php');
echo '<meta charset="utf-8" />';
if(isset($_SESSION['user_droits'])) {
	if($_SESSION['user_droits'][1]!="2")
	{
		echo '<p class="noaccess">'.NOMODULE.'<br><a href="../../index.php">'.ACCUEIL.'</a></p>';
	}
	else {
		$mimes = array('text/xml',
		'application/xml',);
			
		if(in_array($_FILES['file']['type'],$mimes)){
			// Intégration dans la table ".$prefix."_sequences
			echo EDIT_INFO7;
			if (($handle = fopen($_FILES['file']['tmp_name'], "r")) !== FALSE) {
				$content = file_get_contents($_FILES['file']['tmp_name']);
				$xml = simplexml_load_string($content);
				foreach($xml as $sequence){
					$seq_nom = mysqli_real_escape_string($bdd,$sequence->nom);
					$contexte = htmlspecialchars(mysqli_real_escape_string($bdd,$sequence->contexte));
					$niveau = mysqli_real_escape_string($bdd,$sequence->niveau);
					$progression = $sequence->progression;
					$date_crea = $sequence->date_crea;
					$date_maj = $sequence->date_maj;
					$intervs = htmlspecialchars(mysqli_real_escape_string($bdd,$sequence->intervs));
					$enjeux = htmlspecialchars(mysqli_real_escape_string($bdd,$sequence->enjeux));
					$requis = htmlspecialchars(mysqli_real_escape_string($bdd,$sequence->requis));
					$notions = htmlspecialchars(mysqli_real_escape_string($bdd,$sequence->notions));
					$autres = htmlspecialchars(mysqli_real_escape_string($bdd,$sequence->autres));
					$production = htmlspecialchars(mysqli_real_escape_string($bdd,$sequence->production));
					$calendrier = htmlspecialchars(mysqli_real_escape_string($bdd,$sequence->calendrier));
					$ressources = htmlspecialchars(mysqli_real_escape_string($bdd,$sequence->ressources));
					$synopsis = htmlspecialchars(mysqli_real_escape_string($bdd,$sequence->synopsis));
					$outils1 = htmlspecialchars(mysqli_real_escape_string($bdd,$sequence->outils1));
					$outils2 = htmlspecialchars(mysqli_real_escape_string($bdd,$sequence->outils2));
					$eval = htmlspecialchars(mysqli_real_escape_string($bdd,$sequence->eval));
					$reinves = htmlspecialchars(mysqli_real_escape_string($bdd,$sequence->reinves));
					$bilan = htmlspecialchars(mysqli_real_escape_string($bdd,$sequence->bilan));
					$conseils = htmlspecialchars(mysqli_real_escape_string($bdd,$sequence->conseils));
					$auteur = htmlspecialchars(mysqli_real_escape_string($bdd,$sequence->auteur));
					$url = mysqli_real_escape_string($bdd,$sequence->url);
					$licence = mysqli_real_escape_string($bdd,$sequence->licence);
					//vérification du nom (pas de doublon)
					$seq_nom_verif = mysqli_query($bdd,"SELECT * FROM ".$prefix."_sequences WHERE seq_nom = '$seq_nom' AND niveau = '$niveau' AND seq_actif = 1");
					$nb_verif = mysqli_num_rows($seq_nom_verif);
					if($nb_verif>0) {
						$seq_nom .= ' (2)';
					}
					// insertion si pas de doublon :
					mysqli_query($bdd,"INSERT INTO ".$prefix."_sequences (seq_id, seq_nom, contexte, niveau, intervs, enjeux, requis, notions, autres, production, calendrier, ressources, synopsis, outils1, outils2, eval, reinves, bilan, conseils, auteur, licence, url, date_crea, date_maj, seq_actif, progression) VALUES ('', '". $seq_nom."', '".$contexte."', '".$niveau."', '".$intervs."', '".$enjeux."', '".$requis."', '".$notions."', '".$autres."', '".$production."', '".$calendrier."', '".$ressources."', '".$synopsis."', '".$outils1."', '".$outils2."', '".$eval."', '".$reinves."', '".$bilan."', '".$conseils."', '".$auteur."', '".$licence."', '".$url."', '".$date_crea."', '".$date_maj."', 1, '".$progression."')");
					// Récupération de l'id
					$seq_consult = mysqli_query($bdd,"SELECT seq_id FROM ".$prefix."_sequences WHERE seq_nom = '$seq_nom' AND niveau = '$niveau' AND seq_actif = 1");
					$seq_info = mysqli_fetch_assoc($seq_consult);
					$seq_id = $seq_info['seq_id'];
					// création des liens entre séquence et savoirs/notions :
					// ajout notions
					foreach($sequence->seqnot_liste->seqnot as $seqnot_liste) {
						$notion_id = mysqli_real_escape_string($bdd,$seqnot_liste);
						// vérification qu'un lien n'existe pas déjà
						$not_verif = mysqli_query($bdd,"SELECT * FROM ".$prefix."_seqnot WHERE seq_id = '$seq_id' AND notion_id = '$notion_id'");
						$notnb_verif = mysqli_num_rows($not_verif);
						if($notnb_verif==0) {
							// insertion le cas échéant
							mysqli_query($bdd,"INSERT INTO ".$prefix."_seqnot (seq_id, notion_id) VALUES ('".$seq_id."', '".$notion_id."')");
						}
					}
					// ajout savoirs
					foreach($sequence->seqsav_liste->seqsav as $seqsav_liste) {
						$s_id = mysqli_real_escape_string($bdd,$seqsav_liste);
						// vérification qu'un lien n'existe pas déjà
						$seq_verif = mysqli_query($bdd,"SELECT * FROM ".$prefix."_seqsav WHERE seq_id = '$seq_id' AND s_id = '$s_id'");
						$seqnb_verif = mysqli_num_rows($seq_verif);
						if($seqnb_verif==0) {
							// insertion le cas échéant
							mysqli_query($bdd,"INSERT INTO ".$prefix."_seqsav (seq_id, s_id) VALUES ('".$seq_id."', '".$s_id."')");
						}
					}
									
					// Enregistrement du fichier XML correspondant :
					$pathinfo="import";
					include('edit_xml_create.php');
					// Aller vers la consultation :
					echo '<script language="Javascript">
					<!--
					document.location.replace("../../edit.php?page=consult&seq_id='.$seq_id.'");
					// -->
					</script>';
					
				}
				
			}
		}
		else {
			die(NOXML);
		}
	}
}
else {
	include('include/noaccess.php');
	echo '<a href="../../index.php">'.ACCUEIL.'</a></p>';
}
mysqli_close($bdd);
