<?php
// UPLOAD DU FICHIER CSV, vérification et insertion en BASE

if(isset($_SESSION['user_droits'])) {
	if($_SESSION['user_droits'][5]!="2") {
		echo '<p class="noaccess">'.NOMODULE.'<br><a href="../index.php">'.ACCUEIL.'</a>.</p>';
	}

	else {
		
		$validmaj = 0;
		function strToNoAccent($var) {
			$var = str_replace(
				array(
					'à', 'â', 'ä', 'á', 'ã', 'å',
					'î', 'ï', 'ì', 'í', 
					'ô', 'ö', 'ò', 'ó', 'õ', 'ø', 
					'ù', 'û', 'ü', 'ú', 
					'é', 'è', 'ê', 'ë', 
					'ç', 'ÿ', 'ñ',
					'À', 'Â', 'Ä', 'Á', 'Ã', 'Å',
					'Î', 'Ï', 'Ì', 'Í', 
					'Ô', 'Ö', 'Ò', 'Ó', 'Õ', 'Ø', 
					'Ù', 'Û', 'Ü', 'Ú', 
					'É', 'È', 'Ê', 'Ë', 
					'Ç', 'Ÿ', 'Ñ', '-', ' ','\''
				),
				array(
					'a', 'a', 'a', 'a', 'a', 'a', 
					'i', 'i', 'i', 'i', 
					'o', 'o', 'o', 'o', 'o', 'o', 
					'u', 'u', 'u', 'u', 
					'e', 'e', 'e', 'e', 
					'c', 'y', 'n', 
					'a', 'a', 'a', 'a', 'a', 'a', 
					'i', 'i', 'i', 'i', 
					'o', 'o', 'o', 'o', 'o', 'o', 
					'u', 'u', 'u', 'u', 
					'e', 'e', 'e', 'e', 
					'c', 'y', 'n', '','',''
				),$var);
			return $var;
		}
		function rand_nb($length = 8) {
		    $characters = '123456789';
		    $charactersLength = strlen($characters);
		    $randomString = '';
		    for ($i = 0; $i < $length; $i++) {
		        $randomString .= $characters[rand(0, $charactersLength - 1)];
		    }
		    return $randomString;
		}
		if (isset($_FILES['file_csv']) && $_FILES['file_csv']['error'] === UPLOAD_ERR_OK) {
		    $mimes = array(
		        'text/csv',
		        'text/plain',
		        'application/csv',
		        'text/comma-separated-values',
		        'application/excel',
		        'application/vnd.ms-excel',
		        'application/vnd.msexcel',
		        'text/anytext',
		        'application/octet-stream',
		        'application/txt'
		    );    
		    $method = 'csv';
		    $_FILES['file'] = $_FILES['file_csv'];
		}
		else if (isset($_FILES['file_xml']) && $_FILES['file_xml']['error'] === UPLOAD_ERR_OK) {
		    $mimes = array('application/xml', 'text/xml');
		    $method = 'xml';
		    $_FILES['file'] = $_FILES['file_xml'];
		    $xmlEleves = simplexml_load_file($_FILES['file_xml']['tmp_name']);
		    $xmlResponsables = null;
			if (isset($_FILES['file_xml2']) && $_FILES['file_xml2']['error'] === UPLOAD_ERR_OK) {
			    $xmlResponsables = simplexml_load_file($_FILES['file_xml2']['tmp_name']);
			}
		}

		// Vérifier que le fichier a bien été téléchargé
		if (isset($_FILES['file']) && in_array($_FILES['file']['type'], $mimes)) {		

			$_SESSION['annee_effectifs'] = $_POST['annee_effectifs'];
			$_SESSION['input_identifiant'] = isset($_POST['input_identifiant']) ? 1 : 0;
		    // Vérifier si le formulaire de confirmation est soumis
		    if (!isset($_POST['confirmmajdiv'])) {

		        // Si le fichier est valide, effectuer les actions nécessaires
		        mysqli_query($bdd, "DELETE FROM ".$prefix."_elevesmaj");

		        // Sauvegarder automatiquement si le bouton est activé
		        if (isset($_POST['input_save'])) {
		            $_SESSION['save_auto'] = '2';
		            include('./db/backup.php');
		        }

		        // Traitement spécifique selon le type de fichier
		        if ($method == 'csv') {
		            include('maj_elev_csv.php');
		        }
		        else if ($method == 'xml') {
		            include('maj_elev_xml.php');
		        }
		    }
		    
		    // Si le fichier est valide, marquer la mise à jour comme valide
		    $validmaj = 1;
		}
		if($validmaj==1||isset($_POST['confirmmajdiv'])) {
			
			// Vérification de la qualité de la table de MAJ
			$elevescheck = mysqli_query($bdd,"SELECT * FROM ".$prefix."_elevesmaj WHERE nom = '' OR nom = 'nom' OR nom = 'Nom' OR nom = 'Nom de famille' OR nom = 'nom de famille' OR prenom = '' OR division = '' OR division LIKE '%  %' OR sexe = '' OR (sexe <> 'M' AND sexe <> 'F') OR naissance ='0000-00-00'");
			$nb = mysqli_num_rows($elevescheck);
			$virgule = mysqli_fetch_assoc($elevescheck);
			if(@strpos($virgule['nom'], ',')!==false) {
				echo '<p class="signal">'.PBFILE3;
				mysqli_query($bdd,"DELETE FROM ".$prefix."_elevesmaj");
				echo '</p><p class="signal">'.PBFILE10.'</p>';
			}
			else if((@$datas_col==4&&@$cdata8==0)||(@$datas_col==3&&@$cdata7==0)||(@$datas_col==2&&@$cdata6==0)||(@$datas_col==1&&@$cdata5==0)||(@$datas_col==0&&@$cdata5==1)) {
				echo '<p>'.PBFILE2;
				mysqli_query($bdd,"DELETE FROM ".$prefix."_elevesmaj");
				echo '</p><p class="signal">'.PBFILE10.'</p>';
			}
			else if($nb>50) {
				echo '<p class="signal">'.PBFILE11.'</p><p>';
				mysqli_query($bdd,"DELETE FROM ".$prefix."_elevesmaj");
				echo '</p><p class="signal">'.PBFILE10.'</p>';
			}
			else if($nb>0) {
				echo '<p class="signal">'.PBFILE4.'</p><p>';
				foreach($elevescheck as $problemes) {
					if($problemes['nom']=='Nom'||$problemes['nom']=='nom'||$problemes['nom']=='Nom de famille'||$problemes['nom']=='nom de famille') {
						echo PBFILE5.'<br>';
					}
					else {
						echo PBFILE6.' '.$problemes['prenom'].' '.$problemes['nom'].' :';
						if($problemes['division']=='') { echo ' '.PBFILE71; }
						if(preg_match('#  #', $problemes['division'])) { echo ' '.PBFILE7; }
						if($problemes['sexe']==''||($problemes['sexe'] != 'M' AND $problemes['sexe'] != 'F')) { echo ' '.PBFILE8; }
						if($problemes['naissance']=='0000-00-00') { echo ' '.PBFILE9; }						
						echo '.<br>';
					}
				}
				mysqli_query($bdd,"DELETE FROM ".$prefix."_elevesmaj");
				echo '</p><p class="signal">'.PBFILE10.'</p>';
			}
			else {
				$_SESSION['eleve_insert_nb']=0;
				$_SESSION['eleve_update_nb']=0;
				$_SESSION['eleve_delete_nb']=0;
				// Vérification des divisions
				$nb_equal_divs = 0;
				$elevescheck = mysqli_query($bdd,"SELECT * FROM ".$prefix."_elevesmaj");
				if(!isset($_POST['confirmmajdiv'])) {
					foreach($elevescheck as $checkdivs) {
						$nb_equal_divs += mysqli_num_rows(mysqli_query($bdd,"SELECT division FROM ".$prefix."_eleves WHERE division = '".$checkdivs['division']."'"));
					}
				}
				if($nb_equal_divs == 0&&!isset($_POST['confirmmajdiv'])) {
					echo '<p class="signal">'.PBFILE12.'</p><form method="POST" action="database.php?page=maj" name="confirmmaj"><input name="maj_send" type="hidden" value="stopdiv" /><input name="confirmmajdiv" class="bouton boutdb" type="submit" value="'.MAJ_ORDER1.'" /> <input name="supprmajdiv" class="bouton boutdb" type="submit" value="'.MAJ_ORDER2.'" /></form>';
				}
				else {
					// Mise à jour depuis ".$prefix."_elevesmaj vers ".$prefix."_eleves
					$elevesmaj = mysqli_query($bdd,"SELECT * FROM ".$prefix."_elevesmaj");
					$eleves = mysqli_query($bdd,"SELECT * FROM ".$prefix."_eleves");
					
					// cas d'une base initialement installée sans date de naissance : 
					$badbase = mysqli_query($bdd,"SELECT * FROM ".$prefix."_eleves WHERE naissance ='0000-00-00'");
					$badbase_nb = mysqli_num_rows($badbase);
						
					// Trois opérations :
					
					// 1. Mise à jour (update) quand la ligne existe dans les deux tables
					
					while($elevesmaj_info=mysqli_fetch_assoc($elevesmaj)) {
						$majnom = mysqli_real_escape_string($bdd,$elevesmaj_info['nom']);
						$majprenom = mysqli_real_escape_string($bdd,$elevesmaj_info['prenom']);
						$majdiv = mysqli_real_escape_string($bdd,$elevesmaj_info['division']);
						$majnaissance = mysqli_real_escape_string($bdd,$elevesmaj_info['naissance']);
						$majsexe = mysqli_real_escape_string($bdd,$elevesmaj_info['sexe']);
						$majcode = mysqli_real_escape_string($bdd,$elevesmaj_info['code']);
						$majmdp = mysqli_real_escape_string($bdd,$elevesmaj_info['mdp']);
						$majsel = mysqli_real_escape_string($bdd,$elevesmaj_info['sel']);
						$majmail = mysqli_real_escape_string($bdd,$elevesmaj_info['mail']);
						$majid_perso = mysqli_real_escape_string($bdd,$elevesmaj_info['id_perso']);
						$resp_nom = mysqli_real_escape_string($bdd,$elevesmaj_info['resp_nom']);
						$add1 = mysqli_real_escape_string($bdd,$elevesmaj_info['add1']);
						$add2 = mysqli_real_escape_string($bdd,$elevesmaj_info['add2']);
						$cpostal = mysqli_real_escape_string($bdd,$elevesmaj_info['cpostal']);
						$ville = mysqli_real_escape_string($bdd,$elevesmaj_info['ville']);

					// 2. Ajout (insert) quand la ligne n'existe que dans elevesmaj
					
						$elevesold = mysqli_query($bdd,"SELECT * FROM ".$prefix."_eleves WHERE nom='".$majnom."' AND prenom='".$majprenom."' AND naissance='".$elevesmaj_info['naissance']."'");
						$elevesold_nb = mysqli_num_rows($elevesold);

						if($elevesold_nb == 0) {
							// on intègre l'élève
							if($majmdp=="") {
								$majmdp = mb_strtolower(substr(strToNoAccent($majprenom),0,1),'UTF-8').substr($majnaissance,8,2).substr($majnaissance,5,2).substr($majnaissance,0,4);	
								do {
								    $sel = bin2hex(random_bytes(16));
								} while ($sel === '0');
								$mdp_avec_sel = $majmdp.$sel;
								$hashed_password = hash('sha256', $mdp_avec_sel);
								$elevesnew = mysqli_query($bdd,"INSERT INTO ".$prefix."_eleves (eleves_id, nom, prenom, division, naissance, sexe, code, mdp, sel, mail, auth_mail, id_perso, lang, resp_nom, add1, add2, cpostal, ville, categ) VALUES('', '".$majnom."', '".$majprenom."', '".$majdiv."', '".$majnaissance."', '".$majsexe."', '".$majcode."', '".$hashed_password."', '".$sel."', '".$majmail."', '0', '".$majid_perso."', 'fr', '$resp_nom', '$add1', '$add2', '$cpostal', '$ville', '1')");				
							}
							else {
								$elevesnew = mysqli_query($bdd,"INSERT INTO ".$prefix."_eleves (eleves_id, nom, prenom, division, naissance, sexe, code, mdp, mail, auth_mail, id_perso, sel, lang, resp_nom, add1, add2, cpostal, ville, categ) VALUES('', '".$majnom."', '".$majprenom."', '".$majdiv."', '".$majnaissance."', '".$majsexe."', '".$majcode."', '".$majmdp."', '".$majmail."', '0', '".$majid_perso."', '".$majsel."', 'fr', '$resp_nom', '$add1', '$add2', '$cpostal', '$ville', '1')");
							}							
							$_SESSION['eleve_insert_nb']++;
						}
						else {
							$elevesold_info=mysqli_fetch_assoc($elevesold);
							// on update l'élève :
							if($majmdp==""&&$elevesold_info['mdp']=="") {
								$majmdp = mb_strtolower(substr(strToNoAccent($majprenom),0,1),'UTF-8').substr($majnaissance,8,2).substr($majnaissance,5,2).substr($majnaissance,0,4);
								do {
								    $sel = bin2hex(random_bytes(16));
								} while ($sel === '0');
								$mdp_avec_sel = $majmdp.$sel;
								$hashed_password = hash('sha256', $mdp_avec_sel);
								mysqli_query($bdd,"UPDATE ".$prefix."_eleves SET division='" .$majdiv. "', mdp = '".$hashed_password."', sel = '".$sel."', sexe = '".$majsexe."' WHERE nom='".$majnom."' AND prenom='".$majprenom."' AND naissance='".$elevesmaj_info['naissance']."'");	
							}
							else if ($elevesold_info['mdp']=="") {
								mysqli_query($bdd,"UPDATE ".$prefix."_eleves SET division='" .$majdiv. "', mdp = '".$majmdp."', sel = '".$majsel."', sexe = '".$majsexe."' WHERE nom='".$majnom."' AND prenom='".$majprenom."' AND naissance='".$elevesmaj_info['naissance']."'");
							}
							else {
								mysqli_query($bdd,"UPDATE ".$prefix."_eleves SET division='" .$majdiv. "', sexe = '".$majsexe."' WHERE nom='".$majnom."' AND prenom='".$majprenom."' AND naissance='".$elevesmaj_info['naissance']."'");
							}
							if($majcode!="") {
								mysqli_query($bdd,"UPDATE ".$prefix."_eleves SET code='" .$majcode. "' WHERE nom='".$majnom."' AND prenom='".$majprenom."' AND naissance='".$elevesmaj_info['naissance']."'");
							}
							if($majmail!="") {
								mysqli_query($bdd,"UPDATE ".$prefix."_eleves SET mail='" .$majmail. "' WHERE nom='".$majnom."' AND prenom='".$majprenom."' AND naissance='".$elevesmaj_info['naissance']."'");
							}
							if($majid_perso!="") {
								mysqli_query($bdd,"UPDATE ".$prefix."_eleves SET id_perso='" .$majid_perso. "' WHERE nom='".$majnom."' AND prenom='".$majprenom."' AND naissance='".$elevesmaj_info['naissance']."'");
							}
							if($add1!="") {
								mysqli_query($bdd,"UPDATE ".$prefix."_eleves SET resp_nom='" .$resp_nom. "', add1='" .$add1. "', add2='" .$add2. "', cpostal='" .$cpostal. "', ville='" .$ville. "' WHERE nom='".$majnom."' AND prenom='".$majprenom."' AND naissance='".$elevesmaj_info['naissance']."'");
							}
													
							$_SESSION['eleve_update_nb']++;
							// purge des évaluations n-3 :
							$elevetoclean = mysqli_query($bdd,"SELECT eleves_id, division FROM ".$prefix."_eleves WHERE nom='".$majnom."' AND prenom='".$majprenom."' AND naissance='".$elevesmaj_info['naissance']."'");
							$elevetoclean_info = mysqli_fetch_assoc($elevetoclean);
							if(substr($elevetoclean_info['division'],0,1)=="3") {
								$savtoclean = mysqli_query($bdd,"SELECT * FROM ".$prefix."_savele WHERE eleves_id = '".$elevetoclean_info['eleves_id']."' AND (s_id LIKE 'APD6%' OR s_id LIKE 'EMI6%' OR s_id LIKE 'PER6%')");
								$savtoclean_nb = mysqli_num_rows($savtoclean);
								if($savtoclean_nb!=0) {
									mysqli_query($bdd,"DELETE FROM ".$prefix."_savele WHERE eleves_id = '".$elevetoclean_info['eleves_id']."' AND (s_id LIKE 'APD6%' OR s_id LIKE 'EMI6%' OR s_id LIKE 'PER6%')"); 
								}
							}
							if(substr($elevetoclean_info['division'],0,1)=="2") {
								$savtoclean = mysqli_query($bdd,"SELECT * FROM ".$prefix."_savele WHERE eleves_id = '".$elevetoclean_info['eleves_id']."' AND (s_id LIKE 'APD5%' OR s_id LIKE 'EMI5%' OR s_id LIKE 'PER5%')");
								$savtoclean_nb = mysqli_num_rows($savtoclean);
								if($savtoclean_nb!=0) {
									mysqli_query($bdd,"DELETE FROM ".$prefix."_savele WHERE eleves_id = '".$elevetoclean_info['eleves_id']."' AND (s_id LIKE 'APD5%' OR s_id LIKE 'EMI5%' OR s_id LIKE 'PER5%')"); 
								}
							}
							if(substr($elevetoclean_info['division'],0,1)=="1") {
								$savtoclean = mysqli_query($bdd,"SELECT * FROM ".$prefix."_savele WHERE eleves_id = '".$elevetoclean_info['eleves_id']."' AND (s_id LIKE 'APD4%' OR s_id LIKE 'EMI4%' OR s_id LIKE 'PER4%')");
								$savtoclean_nb = mysqli_num_rows($savtoclean);
								if($savtoclean_nb!=0) {
									mysqli_query($bdd,"DELETE FROM ".$prefix."_savele WHERE eleves_id = '".$elevetoclean_info['eleves_id']."' AND (s_id LIKE 'APD4%' OR s_id LIKE 'EMI4%' OR s_id LIKE 'PER4%')"); 
								}
							}
							if(substr($elevetoclean_info['division'],0,1)=="t") {
								$savtoclean = mysqli_query($bdd,"SELECT * FROM ".$prefix."_savele WHERE eleves_id = '".$elevetoclean_info['eleves_id']."' AND (s_id LIKE 'APD3%' OR s_id LIKE 'EMI3%' OR s_id LIKE 'PER3%')");
								$savtoclean_nb = mysqli_num_rows($savtoclean);
								if($savtoclean_nb!=0) {
									mysqli_query($bdd,"DELETE FROM ".$prefix."_savele WHERE eleves_id = '".$elevetoclean_info['eleves_id']."' AND (s_id LIKE 'APD3%' OR s_id LIKE 'EMI3%' OR s_id LIKE 'PER3%')"); 
								}
							}
						}
						mysqli_query($bdd,"UPDATE ".$prefix."_eleves SET auth_mail = '0' WHERE auth_mail = ''");
					}

					// 3. Suppression (delete) quand la ligne n'existe que dans eleves
					
					while($eleves_info = mysqli_fetch_assoc($eleves)) {
						$nom = mysqli_real_escape_string($bdd,$eleves_info['nom']);
						$prenom = mysqli_real_escape_string($bdd,$eleves_info['prenom']);
						$div = mysqli_real_escape_string($bdd,$eleves_info['division']);
						if($badbase_nb>0) {
							$elevesnew_select = "SELECT * FROM ".$prefix."_elevesmaj WHERE nom='".$nom."' AND prenom='".$prenom."'";
						}
						else {
							$elevesnew_select = "SELECT * FROM ".$prefix."_elevesmaj WHERE nom='".$nom."' AND prenom='".$prenom."' AND naissance='".$eleves_info['naissance']."'";
						}
						$elevesnew = mysqli_query($bdd,$elevesnew_select);
						$elevesnew_nb = mysqli_num_rows($elevesnew);
						
						if($elevesnew_nb == 0) {
							// on supprime l'élève
							$elevestosup = mysqli_query($bdd,"SELECT * FROM ".$prefix."_eleves WHERE nom='".$nom."' AND prenom='".$prenom."' AND naissance='".$eleves_info['naissance']."'");
							$elevestosup_info = mysqli_fetch_assoc($elevestosup);
							$elevestosup_id = $elevestosup_info['eleves_id'];
							
							mysqli_query($bdd,"DELETE FROM ".$prefix."_eleves WHERE eleves_id='".$elevestosup_id."'");
							mysqli_query($bdd,"UPDATE ".$prefix."_freqres SET eleves_id = '0' WHERE eleves_id = '".$elevestosup_id."'");
							mysqli_query($bdd,"DELETE FROM ".$prefix."_freqresref WHERE eleves_id = '".$elevestosup_id."'");
							mysqli_query($bdd,"DELETE FROM ".$prefix."_savele WHERE eleves_id='".$elevestosup_id."'");
							mysqli_query($bdd,"DELETE FROM ".$prefix."_groupele WHERE eleves_id='".$elevestosup_id."'");
							$_SESSION['eleve_delete_nb']++;
						}
					}

					// Puis vider la table elevesmaj
					mysqli_query($bdd,"DELETE FROM ".$prefix."_elevesmaj");
					
					// Mise à jour ou création des effectifs dans _etab_effectifs
					$anneesco = $_SESSION['annee_effectifs'];
					$divisions = mysqli_query($bdd,"SELECT division FROM ".$prefix."_eleves GROUP BY division ORDER BY division ASC");
					if(mysqli_num_rows($divisions)>0) {
						$etab = mysqli_query($bdd,"SELECT etab_id FROM ".$prefix."_etab");
						if(mysqli_num_rows($etab)>0) {
							$etab_info = mysqli_fetch_assoc($etab);
							$etab_id = $etab_info['etab_id'];
						}
						else { $etab_id = ''; }
						foreach($divisions as $div) {
							$nbeleves = mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_eleves WHERE division = '".$div['division']."'"));
							if(mysqli_num_rows(mysqli_query($bdd,"SELECT * FROM ".$prefix."_etab_effectifs WHERE anneesco = '".$anneesco."' AND division = '".$div['division']."'"))>0) {
								mysqli_query($bdd,"UPDATE ".$prefix."_etab_effectifs SET effectif = '".$nbeleves."' WHERE anneesco = '".$anneesco."' AND division = '".$div['division']."'");
							}
							else {
								mysqli_query($bdd,"INSERT INTO ".$prefix."_etab_effectifs (etab_id, anneesco, division, effectif) VALUES ('".$etab_id."', '".$anneesco."', '".$div['division']."', '".$nbeleves."')");
							}
						}
					}
					// Identifiants persos par défaut si importés
					if($_SESSION['input_identifiant']==1) {
						mysqli_query($bdd,"UPDATE ".$prefix."_parametres SET valeur = 'perso' WHERE param_nom = 'freq_resmodauth'");
					}
					else {						
						$auth_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_resmodauth'"));
						if($auth_info['valeur']=="prenomnom"||$auth_info['valeur']=="pnom") {
							$eleves = mysqli_query($bdd,"SELECT eleves_id, nom, prenom FROM ".$prefix."_eleves");
							foreach($eleves as $eleve) {
								if($auth_info['valeur']=="prenomnom") {
									$ident = substr(strToNoAccent($eleve['prenom']),0,20).'.'.substr(strToNoAccent($eleve['nom']),0,20);
								}
								else if($auth_info['valeur']=="pnom") {
									$ident = substr(strToNoAccent($eleve['prenom']),0,1).substr(strToNoAccent($eleve['nom']),0,20);
								}
								$checke = mysqli_query($bdd,"SELECT id_constr FROM ".$prefix."_eleves WHERE id_constr = '".mysqli_real_escape_string($bdd,strtolower($ident))."' AND eleves_id <> '".$eleve['eleves_id']."'");
								$checku = mysqli_query($bdd,"SELECT login FROM ".$prefix."_user WHERE login = '".mysqli_real_escape_string($bdd,strtolower($ident))."'");
								if(mysqli_num_rows($checke)>0||mysqli_num_rows($checku)>0) {
									$ident = $ident.rand_nb(2);
								}
								mysqli_query($bdd,"UPDATE ".$prefix."_eleves SET id_constr = '".strtolower($ident)."' WHERE eleves_id = '".$eleve['eleves_id']."'");
							}
						}
					}
					// on crée les identifiants id_constr si besoin quand ils sont vides :
					$reserv_could=mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_reseleve'"));
					$auth_info = mysqli_fetch_assoc(mysqli_query($bdd,"SELECT valeur FROM ".$prefix."_parametres WHERE param_nom = 'freq_resmodauth'"));
					if($reserv_could['valeur']==2&&($auth_info['valeur']=="prenomnom"||$auth_info['valeur']=="pnom")) {
						$eleves = mysqli_query($bdd,"SELECT eleves_id, nom, prenom FROM ".$prefix."_eleves WHERE id_constr = ''");
						foreach($eleves as $eleve) {
							if($auth_info['valeur']=="prenomnom") {
								$ident = substr(strToNoAccent($eleve['prenom']),0,20).'.'.substr(strToNoAccent($eleve['nom']),0,20);
							}
							else if($auth_info['valeur']=="pnom") {
								$ident = substr(strToNoAccent($eleve['prenom']),0,1).substr(strToNoAccent($eleve['nom']),0,20);
							}
							$checke = mysqli_query($bdd,"SELECT id_constr FROM ".$prefix."_eleves WHERE id_constr = '".mysqli_real_escape_string($bdd,strtolower($ident))."' AND eleves_id <> '".$eleve['eleves_id']."'");
							$checku = mysqli_query($bdd,"SELECT login FROM ".$prefix."_user WHERE login = '".mysqli_real_escape_string($bdd,strtolower($ident))."'");
							if(mysqli_num_rows($checke)>0||mysqli_num_rows($checku)>0||$ident=='reserver') {
								$ident = $ident.rand_nb(2);
							}
							mysqli_query($bdd,"UPDATE ".$prefix."_eleves SET id_constr = '".strtolower($ident)."' WHERE eleves_id = '".$eleve['eleves_id']."'");
						}
					}
					$_SESSION['import']="maj";
					echo '<script language="Javascript">
					<!--
					document.location.replace("database.php?page=consulter");
					// -->
					</script>';
				}
			}
		}
		else {
			die(ERROR3);
		}
	}
}
else {
	include('include/noaccess.php');
	echo '<br><a href="../index.php">'.ACCUEIL.'</a>.</p>';
}
?>
