<?php
$exp = mysqli_query($bdd,"SELECT * FROM ".$prefix."_sequences WHERE seq_id = '$seq_id'");
$xml = '<?xml version="1.0" encoding="UTF-8"?>'."\n".'<seq_fiches>'."\n".'';
while ($row = mysqli_fetch_array($exp)) {
	// création des champs xml :
	$xml .= '<sequence>'."\n".'';
	$xml .= ''."\t".'<nom>'.$row['seq_nom'].'</nom>'."\n".'';
	$xml .= ''."\t".'<contexte>'.$row['contexte'].'</contexte>'."\n".'';
	$xml .= ''."\t".'<niveau>'.$row['niveau'].'</niveau>'."\n".'';
	$xml .= ''."\t".'<progression>'.$row['progression'].'</progression>'."\n".'';
	$xml .= ''."\t".'<intervs>'.$row['intervs'].'</intervs>'."\n".'';
	$xml .= ''."\t".'<enjeux>'.$row['enjeux'].'</enjeux>'."\n".'';
	$xml .= ''."\t".'<requis>'.$row['requis'].'</requis>'."\n".'';
	$xml .= ''."\t".'<notions>'.$row['notions'].'</notions>'."\n".'';
	$xml .= ''."\t".'<autres>'.$row['autres'].'</autres>'."\n".'';
	$xml .= ''."\t".'<production>'.$row['production'].'</production>'."\n".'';
	$xml .= ''."\t".'<calendrier>'.$row['calendrier'].'</calendrier>'."\n".'';
	$xml .= ''."\t".'<ressources>'.$row['ressources'].'</ressources>'."\n".'';
	$xml .= ''."\t".'<synopsis>'.$row['synopsis'].'</synopsis>'."\n".'';
	$xml .= ''."\t".'<outils1>'.$row['outils1'].'</outils1>'."\n".'';
	$xml .= ''."\t".'<outils2>'.$row['outils2'].'</outils2>'."\n".'';
	$xml .= ''."\t".'<eval>'.$row['eval'].'</eval>'."\n".'';
	$xml .= ''."\t".'<reinves>'.$row['reinves'].'</reinves>'."\n".'';
	$xml .= ''."\t".'<bilan>'.$row['bilan'].'</bilan>'."\n".'';
	$xml .= ''."\t".'<conseils>'.$row['conseils'].'</conseils>'."\n".'';
	$xml .= ''."\t".'<auteur>'.$row['auteur'].'</auteur>'."\n".'';
	$xml .= ''."\t".'<licence>'.$row['licence'].'</licence>'."\n".'';
	$xml .= ''."\t".'<url>'.$row['url'].'</url>'."\n".'';
	$xml .= ''."\t".'<date_crea>'.$row['date_crea'].'</date_crea>'."\n".'';
	$xml .= ''."\t".'<date_maj>'.$row['date_maj'].'</date_maj>'."\n".'';
	// ajout des notions :
	$xml .= ''."\t".'<seqnot_liste>'."\n".'';
	$seqnot = mysqli_query($bdd,"SELECT * FROM ".$prefix."_seqnot WHERE  seq_id = '$seq_id'");
	while ($not = mysqli_fetch_array($seqnot)) {
		$xml .= ''."\t".''."\t".'<seqnot>'.$not['notion_id'].'</seqnot>'."\n".'';
	}
	$xml .= ''."\t".'</seqnot_liste>'."\n".'';
	// ajout des savoirs :
	$xml .= ''."\t".'<seqsav_liste>'."\n".'';
	$seqsav = mysqli_query($bdd,"SELECT * FROM ".$prefix."_seqsav WHERE  seq_id = '$seq_id'");
	while ($sav = mysqli_fetch_array($seqsav)) {
		$xml .= ''."\t".''."\t".'<seqsav>'.$sav['s_id'].'</seqsav>'."\n".'';
	}
	$xml .= ''."\t".'</seqsav_liste>'."\n".'';
	// fin du fichier :
	$xml .= '</sequence>'."\n".'';
}
$xml .= '</seq_fiches>';

$exp2 = mysqli_query($bdd,"SELECT * FROM ".$prefix."_sequences WHERE seq_id = '$seq_id'");
$exp2_info = mysqli_fetch_assoc($exp2);
$encoding = mb_detect_encoding($exp2_info['seq_nom'], mb_detect_order(), true);
if ($encoding && $encoding !== 'UTF-8') {
    $seq_nom_utf8 = mb_convert_encoding($exp2_info['seq_nom'], 'UTF-8', $encoding);
} else {
    $seq_nom_utf8 = $exp2_info['seq_nom'];
}
$pathfile = "idlog-" . $exp2_info['niveau'] . "e-" . $seq_nom_utf8 . ".xml";
if(isset($_SESSION['chemin'])) {
	if($pathfile!=$_SESSION['chemin']) {
		// Suppression du fichier XML si différent (chgt de niveau et/ou de nom)
		if($pathinfo=="import") {
			$rep_xml = "../../xml/".$prefix;
		}
		else {
			$rep_xml = "xml/".$prefix;
		}
		$rep_xml_open = opendir($rep_xml);
		$chemin = $rep_xml.$_SESSION['chemin'];
		@unlink($chemin);
		closedir($rep_xml_open);
	}
}
if($pathinfo=="import") {
	$path = '../../xml/'.$prefix.'/'.$pathfile;
}
else {
	$path = 'xml/'.$prefix.'/'.$pathfile;
}
$fp = fopen(iconv("utf-8","iso-8859-1",$path), 'w+');
fputs($fp, $xml);
fclose($fp);
?>
