<?php 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'][1]!="2")
	{
		echo '<p class="noaccess">'.NOMODULE.'<br><a href="./index.php">'.ACCUEIL.'</a></p>';
	}
	else {
		?><style type="text/css">
		#med2 { background-color: #fff; }
		#med2 td { border: 3px #DEBB22 solid; }
		#med1 td { border: 0; }
		</style><?php
		echo '<script language=javascript>
		function OuvrirPopup(page,nom,option) {
		  window.open(page,nom,option);
		}
		</script>';
		
		echo EDITC_CON3;
		
		// Vérification de la présence de grilles : EMI et PERSO
		$verif_emi = mysqli_query($bdd,"SELECT * FROM ".$prefix."_savoirs3 WHERE s3_id LIKE 'EMI%'");
		$nb_sav_emi = mysqli_num_rows($verif_emi);
		$verif_per = mysqli_query($bdd,"SELECT * FROM ".$prefix."_savoirs6 WHERE s6_id LIKE 'PER%'");
		$nb_sav_per = mysqli_num_rows($verif_per);
		// Affichage du formulaire
		echo EDITC_CON4.'
		<p>'.EDITC_CON5;
		if($nb_sav_emi>0) {
			echo EDITC_CON6;
		}
		if($nb_sav_per>0) {
			echo EDITC_CON7;
		}
		echo '</p>';

		// TRAITEMENT :
		if(isset($_POST['seq_crea'])) {
			// INSERT SEQ DANS BASE
			$seq_nom = mysqli_real_escape_string($bdd,$_POST['seq_nom']);
			$_SESSION['crea_seq_nom'] = $seq_nom;
			$niveau = mysqli_real_escape_string($bdd,$_POST['niveau']);
			$_SESSION['crea_niveau'] = $niveau;
			$progression = $_POST['progression'];
			if($_POST['progression']=='') { $progression='1000'; }
			$_SESSION['crea_progression'] = $progression;
			$date_crea = $_POST['date_crea'];
			$_SESSION['date_crea'] = $date_crea;
				
			// vérification des champs obligatoires :
			if($seq_nom==""||$niveau=="-1"||$date_crea=="") {
				echo EDITC_CON8;
			}
			// sinon vérification du nom (pas de doublon) et insertion:
			else {
				//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) {
					// insertion si pas de doublon :
					mysqli_query($bdd,"INSERT INTO ".$prefix."_sequences (seq_id, seq_nom, niveau, date_crea, seq_actif, progression) VALUES ('', '". $seq_nom."', '".$niveau."', '".$date_crea."', '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'");
					$seq_info = mysqli_fetch_assoc($seq_consult);
					$seq_id = $seq_info['seq_id'];
					// création des liens entre séquence et savoirs :
					if(isset($_POST['savoirsapd'])) {
						$savoirsapd = $_POST['savoirsapd'];
						foreach($savoirsapd as $savapd) {
							if($savapd!=""&&$savapd!="-1") {
								// 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 = '$savapd'");
								$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."', '".$savapd."')");
								}	
							}
						}
					}
					// création des liens entre séquence et savoirs :
					if(isset($_POST['savoirsemi'])) {
						$savoirsemi = $_POST['savoirsemi'];
						echo $_POST['savoirsemi'];
						foreach($savoirsemi as $savemi) {
							if($savemi!=""&&$savemi!="-1") {
								// 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 = '$savemi'");
								$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."', '".$savemi."')");
								}	
							}
						}
					}
					// création des liens entre séquence et savoirs :
					if(isset($_POST['savoirsper'])) {
						$savoirsper = $_POST['savoirsper'];
						foreach($savoirsper as $savper) {
							if($savper!=""&&$savper!="-1") {
								// 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 = '$savper'");
								$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."', '".$savper."')");
								}	
							}
						}
					}
					// Enregistrement du fichier XML correspondant :
					$pathinfo="creation";
					include('db/proc/edit_xml_create.php');
					// Aller vers la modification :
					echo '<script language="Javascript">
					<!--
					document.location.replace("edit.php?page=consult&seq_id='.$seq_id.'&modif");
					// -->
					</script>';
				}
				else {
					echo EDITC_CON9;
				}
			}
		}
		
		// FORMULAIRE :
		echo '<form method="POST" name="seqform" action="" class="padd2">';
			$order   = array("\r\n", "\n", "\r");
			$replace = '<br />';
			echo '<hr class="hr1n"><h3>'.EDITC_CON10.'</h3>
			<hr class="hr2n"><h4>'.NAME.' <span class="signal">*</span>'.DOTS.' &nbsp; <input type="text" class="inptxt" id="nom" name="seq_nom" maxlength="100" value="';
				if(isset($_SESSION['crea_seq_nom'])) { echo ''.$_SESSION['crea_seq_nom'].''; }
				echo'" /></h4>
			<h4>'.LEVEL.' <span class="signal">*</span>'.DOTS.' &nbsp; ';?>
				<div class="box"><select name='niveau' id='niveaugo' onchange='go()'>
					<?php
					echo '<option value="-1">'.NONE.'</option>';
					include('db/proc/freq_stats_nivcolors.php');
					for($i=0;$i<count($tabDebniveau);$i++) {
						echo '<option value="'.$tabDebniveau[$i].'">'.$tabNiveau[$i].'</option>';
					}
					?>
				</select></div>
				<?php
				echo '
				</h4>
			<h4>'.EDITC_CON11.DOTS.' &nbsp; 
				<input type="number" name="progression" min="1" max="100" class="number inptxtb" value="';
				if(isset($_SESSION['crea_progression'])) { echo $_SESSION['crea_progression']; }
				echo '" /><div id=\'progress\' class="divprogress">
			</div></h4>
			<h4>'.$txt_typen[4].' <span class="signal">*</span>'.DOTS.' &nbsp; ';
				echo '<input type="date" class="inptxt" name="date_crea" value="';
				if(isset($_SESSION['crea_date'])) { echo $_SESSION['crea_date']; }
				else { echo date('Y-m-d'); }
				echo '" />';				
			echo '</h4>
			<h4>'.EDIT_SAVID1.'<br>';
			echo '<div id=\'savoir1\' style=\'display:inline\'>';
			echo '</div>';
			if($nb_sav_emi>0) {
				echo '<br>&nbsp; <br>'.EDIT_SAVID2.'<br>';
				echo '<div id=\'emi1\' style=\'display:inline\'>';				
				echo '</div>';
			}
			if($nb_sav_per>0) {
				echo '<br>&nbsp; <br>'.EDIT_SAVID3.'<br>';
				$ccr = mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'eval_perso'");
				$ccr_info = mysqli_fetch_assoc($ccr);
				if($ccr_info['valeur']=="niveau"&&isset($_SESSION['seq_id'])) {
					$tabDebniveau = array('6', '5', '4', '3', '2', '1', 't');
					foreach($tabDebniveau as $divdeb) {
						$seqper_select = 
						"SELECT seqsav.s_id, sav.nom_savoir savoir, SUBSTR(seqsav.s_id,4,1) niv, SUBSTR(seqsav.s_id,-3) sidsub FROM ".$prefix."_seqsav seqsav 
						INNER JOIN ".$prefix."_savoirs".$divdeb." sav
						ON seqsav.s_id = sav.s".$divdeb."_id
						WHERE seqsav.seq_id = '".$_SESSION['seq_id']."' AND seqsav.s_id LIKE 'PER%'
						ORDER BY seqsav.s_id";
						$seqper = mysqli_query($bdd,$seqper_select);
						$seqper_nb = mysqli_num_rows($seqper);
					}
				}
				echo '<div id=\'per1\' style=\'display:inline\'>
				</div>';
			}
			echo '<br><input class="bouton boutseq" type="submit" value="'.VALID.'" name="seq_crea" />
		</form>';
		echo "<script type=\"text/javascript\">
				document.getElementById('nom').focus();
			</script>";
		echo "<script language='javascript'>
			window.onmousedown = function (e) {
		    var el = e.target;
		    if (el.tagName.toLowerCase() == 'option' && el.parentNode.hasAttribute('multiple')) {
		        e.preventDefault();

		        if (el.hasAttribute('selected')) el.removeAttribute('selected');
		        else el.setAttribute('selected', '');

		        var select = el.parentNode.cloneNode(true);
		        el.parentNode.parentNode.replaceChild(select, el.parentNode);
		    }
		}
		</script>";
	}
}
else {
	include('include/noaccess.php');
	echo '<br><a href="./index.php>'.ACCUEIL.'</a></p>';
}
?>