From b7ca3d032191b70af2e7d575375fb1f5b557ebcf Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Wed, 25 Nov 2015 00:04:02 +0100 Subject: [PATCH] =?UTF-8?q?Int=C3=A9gration=20d'une=20liste=20d'=C3=A9l?= =?UTF-8?q?=C3=A8ves=20op=C3=A9rationnelle?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- manager/database.php | 40 ++++++++++++++++ manager/groups.php | 17 +++++++ manager/repo/group.php | 96 +++++++++++++++++++++++++++++++++++++++ manager/repo/semestre.php | 85 ++++++++++++++++++++++++++++++++++ manager/repo/user.php | 65 ++++++++++++++++++++++++-- test.php | 24 ++++++++-- 6 files changed, 318 insertions(+), 9 deletions(-) diff --git a/manager/database.php b/manager/database.php index b6521a6..9430207 100755 --- a/manager/database.php +++ b/manager/database.php @@ -149,6 +149,46 @@ class DataBase{ } + + + + + /* intègre une liste d'utilisateurs (pour un rang d'une année) à la BDD + * + * @rang le rang du semestre en question + * @annee l'année du semestre en question + * @formationList contient tous les étudiants par formations + * + */ + public function addUserlist($rang, $annee, $formationList){ + + foreach($formationList as $form){ + + $formationUID = semestreRepo::includeFormation($form['nom'], $form['libelle']); // on récupère l'UID de la formation en la créant si nécessaire + + if( is_numeric($formationUID) ){ // si la formation est ok, alors on cherche le semestre + + $semestreUID = semestreRepo::includeSemestre($rang, $annee, $formationUID); // on récupère l'UID du semestre en le créant si nécessaire + + if( is_numeric($semestreUID) ){ // si le semestre est ok, alors on ajoute les utilisateurs + + foreach($form['userlist'] as $user){ + // on récupère l'UID utilisateur en le créant si nécessaire + $utilisateurUID = userRepo::includeEtudiant($user['identifiant'], $user['prenom'], $user['nom'], $user['sexe'], $user['mail']); + + if( is_string($utilisateurUID) ) // si l'utilisateur est ok, alors on créé l'appartenance + groupRepo::includeAppartenance($utilisateurUID, $semestreUID); // on ajoute l'utilisateur au groupeVide de chaque semestre (s'il n'a pas déjà un groupe) + } + + } + + } + + } + + } + + /* créé un semestre/formation dans la BDD * * @codeFormation le code/nom de la formation diff --git a/manager/groups.php b/manager/groups.php index 6719175..527fb02 100755 --- a/manager/groups.php +++ b/manager/groups.php @@ -245,6 +245,23 @@ require_once __ROOT__.'/manager/database.php'; $answer->request = 'permission_error'; break; + + + /*******************************************/ + /* intégration des élèves pour un semestre */ + /*******************************************/ + case 'addUserlist': if( permission('admin') ){ + + if( isset($request->formationList) && isset($request->rang) && isset($request->annee) && is_numeric($request->rang) && is_numeric($request->annee) ){ // si tout les paramètres sont bons + DataBase::getInstance()->addUserlist($request->rang, $request->annee, $request->formationList); + $answer->request = 'success'; + }else + $answer->request = 'param_error'; + + }else + $answer->request = 'permission_error'; + break; + /***********/ /* DEFAULT */ /***********/ diff --git a/manager/repo/group.php b/manager/repo/group.php index d69844a..9e9009c 100755 --- a/manager/repo/group.php +++ b/manager/repo/group.php @@ -324,4 +324,100 @@ class groupRepo extends DBAccess{ } + + + + + + + + + /* retourne l'UID d'une appartenance et la créé en amont si elle n'existe pas + * + * @identifiant l'UID de l'étudiant en question + * @semestre l'UID du semestre en question + * + * + * @return UID retourne l'UID de l'appartenance + * @return FALSE retourne FALSE si une erreur occure + * + */ + public static function includeAppartenance($identifiant, $semestre){ + /* [1] On vérifie l'existence de l'appartenance + ======================================================*/ + $getAppartenanceUID = DataBase::getPDO()->prepare("SELECT id_appartenance as id + FROM appartenance + WHERE id_etudiant = :identifiant + AND id_semestre = :semestre"); + $getAppartenanceUID->execute(array( ':identifiant' => $identifiant, ':semestre' => $semestre )); + + /* [2] Cas où l'appartenance existe déjà, alors on retourne l'UID + ======================================================*/ + if( $appartenanceUID = $getAppartenanceUID->fetch()['id'] ) return $appartenanceUID; + + /* [3] Cas où l'appartenance n'existe pas, + /* alors on met l'étudiant dans le groupe de même + /* nom que la formation+semestre + ======================================================*/ + + + /* [3-1] On vérifie l'existence du groupe de même nom que la formation+semestre + ======================================================*/ + $getGroupeUID = DataBase::getPDO()->prepare("SELECT DISTINCT id_groupe as id + FROM groupe + WHERE nom = (SELECT concat(f.code, ' ', s.nom) as nom + FROM semestre as s, formation as f + WHERE s.id_formation = f.id_formation + AND s.id_semestre = :semestre)"); + $getGroupeUID->execute(array( ':semestre' => $semestre )); + + + /* [3-2] Le groupe n'existe pas, alors on le créé + ======================================================*/ + if( !($groupeUID=$getGroupeUID->fetch()['id']) ){ + + $creerGroupe = DataBase::getPDO()->prepare("INSERT INTO groupe(id_groupe, nom) + VALUES(DEFAULT, (SELECT concat(f.code, ' ', s.nom) as nom + FROM semestre as s, formation as f + WHERE s.id_formation = f.id_formation + AND s.id_semestre = :semestre) + )"); + $creerGroupe->execute(array( ':semestre' => $semestre )); + + + // si le groupe n'a pas été créé (erreur) on retourne false + $getGroupeUID = DataBase::getPDO()->prepare("SELECT DISTINCT id_groupe as id + FROM groupe + WHERE nom = (SELECT concat(f.code, ' ', s.nom) as nom + FROM semestre as s, formation as f + WHERE s.id_formation = f.id_formation + AND s.id_semestre = :semestre)"); + $getGroupeUID->execute(array( ':semestre' => $semestre )); + + if( !($groupeUID=$getGroupeUID->fetch()['id']) ) return false; + + } + + + + $creerAppartenance = DataBase::getPDO()->prepare("INSERT INTO appartenance(id_appartenance, id_etudiant, id_groupe, id_semestre) + VALUES(DEFAULT, :identifiant, :groupeUID, :semestre)"); + $creerAppartenance->execute(array( + ':identifiant' => $identifiant, + ':groupeUID' => $groupeUID, + ':semestre' => $semestre + )); + + /* [4] On vérifie que l'appartenance a été créé et retourne son UID + ======================================================*/ + $getAppartenanceUID = DataBase::getPDO()->prepare("SELECT id_appartenance as id + FROM appartenance + WHERE id_etudiant = :identifiant + AND id_semestre = :semestre"); + $getAppartenanceUID->execute(array( ':identifiant' => $identifiant, ':semestre' => $semestre )); + + return $appartenanceUID = $getAppartenanceUID->fetch()['id']; + } + + } \ No newline at end of file diff --git a/manager/repo/semestre.php b/manager/repo/semestre.php index 7544ace..6fad16c 100755 --- a/manager/repo/semestre.php +++ b/manager/repo/semestre.php @@ -63,6 +63,91 @@ class semestreRepo extends DBAccess{ + /* retourne l'UID d'une formation et la créé en amont si elle n'existe pas + * + * @code Le code de la formation + * @nom Le nom de la formation + * + * + * @return UID l'UID de la formation en question + * + */ + public static function includeFormation($code, $nom){ + /* [1] On vérifie l'existence de la formation + ======================================================*/ + $getFormationUID = DataBase::getPDO()->prepare("SELECT id_formation as id + FROM formation + WHERE code = :code + AND nom = :nom"); + $getFormationUID->execute(array( ':code' => $code, ':nom' => $nom )); + + /* [2] Cas où la formation existe déjà, alors on retourne l'UID + ======================================================*/ + if( $formationUID = $getFormationUID->fetch()['id'] ) return $formationUID; + + /* [3] Cas où la formation n'existe pas, alors on la créé + ======================================================*/ + $creerFormation = DataBase::getPDO()->prepare("INSERT INTO formation(id_formation, code, nom, nb_semestres) + VALUES(DEFAULT, :code, :nom, DEFAULT)"); + $creerFormation->execute(array( ':code' => $code, ':nom' => $nom )); + + /* [4] On vérifie que la formation a été créé et retourne son UID + ======================================================*/ + $getFormationUID = DataBase::getPDO()->prepare("SELECT id_formation as id + FROM formation + WHERE code = :code + AND nom = :nom"); + $getFormationUID->execute(array( ':code' => $code, ':nom' => $nom )); + + return $formationUID = $getFormationUID->fetch()['id']; + + } + + + + /* retourne l'UID d'un semestre le créé en amont si il n'existe pas + * + * @rang Le rang du semestre en question + * @annee L'année du semestre en question + * @formation L'UID de la formation en question + * + * + * @return UID l'UID du semestre en question + * + */ + public static function includeSemestre($rang, $annee, $formation){ + /* [1] On vérifie l'existence du semestre + ======================================================*/ + $getSemestreUID = DataBase::getPDO()->prepare("SELECT id_semestre as id + FROM semestre + WHERE rang = :rang + AND annee = :annee + AND id_formation = :formation"); + $getSemestreUID->execute(array( ':rang' => $rang, ':annee' => $annee, ':formation' => $formation )); + + /* [2] Cas où le semestre existe déjà, alors on retourne l'UID + ======================================================*/ + if( $semestreUID = $getSemestreUID->fetch()['id'] ) return $semestreUID; + + /* [3] Cas où le semestre n'existe pas, alors on le créé + ======================================================*/ + $creerSemestre = DataBase::getPDO()->prepare("INSERT INTO semestre(id_semestre, id_formation, nom, rang, annee) + VALUES(DEFAULT, :formation, :nom, :rang, :annee)"); // par défaut le nom du semestre = "Sn" où n est le rang + $creerSemestre->execute(array( ':formation' => $formation, ':nom' => 'S'.$rang, ':rang' => $rang, ':annee' => $annee )); + + /* [4] On vérifie que la formation a été créé et retourne son UID + ======================================================*/ + $getSemestreUID = DataBase::getPDO()->prepare("SELECT id_semestre as id + FROM semestre + WHERE rang = :rang + AND annee = :annee + AND id_formation = :formation"); + $getSemestreUID->execute(array( ':rang' => $rang, ':annee' => $annee, ':formation' => $formation )); + + return $getSemestreUID->fetch()['id']; + + } + diff --git a/manager/repo/user.php b/manager/repo/user.php index 04ecb27..0b46d65 100755 --- a/manager/repo/user.php +++ b/manager/repo/user.php @@ -351,7 +351,7 @@ class userRepo extends DBAccess{ * (*) Si aucun autre n'avait le même @identifiant * */ - public static function creer($identifiant, $prenom, $nom, $mail, $mdp, $droits){ + public static function creer($identifiant, $prenom, $nom, $sexe, $mail, $mdp, $droits){ /* [1] On normalise les données =================================================================================================*/ foreach($droits as $droit) @@ -362,8 +362,9 @@ class userRepo extends DBAccess{ $identifiant = strtolower($identifiant); // on met l'identifiant en minuscule $prenom = ucwords( strtolower($prenom) ); // majuscule à chaque mot sinon minuscule $nom = strtoupper($nom); // nom en majuscules + $sexe = boolval($sexe); // VRAI pour homme / FAUX pour femme $mail = strtolower($mail); // email en minuscules - $mdp = secure_sha1($mdp); // on hash le password (SHA1) + $mdp = secure_sha1($mdp); // on hash le password (SHA1) $droits = implode(',', $droits); // on met le droit sous forme de chaine /* [2] On vérifie que l'utilisateur n'est pas déjà créé @@ -373,12 +374,13 @@ class userRepo extends DBAccess{ /* [3] On créé le nouvel utilisateur =================================================================================================*/ - $creationUtilisateur = DataBase::getPDO()->prepare("INSERT INTO utilisateur(identifiant, prenom, nom, mail, mdp, droits) - VALUES(:identifiant, :prenom, :nom, :mail, :mdp, :droits)"); + $creationUtilisateur = DataBase::getPDO()->prepare("INSERT INTO utilisateur(identifiant, prenom, nom, sexe, mail, mdp, droits) + VALUES(:identifiant, :prenom, :nom, :sexe, :mail, :mdp, :droits)"); $creationUtilisateur->execute(array( ':identifiant' => $identifiant, ':prenom' => $prenom, ':nom' => $nom, + ':sexe' => $sexe, ':mail' => $mail, ':mdp' => $mdp, ':droits' => $droits @@ -393,4 +395,59 @@ class userRepo extends DBAccess{ + + + + + + + /* retourne l'UID d'un étudiant et le créé en amont s'il n'existe pas + * + * @identifiant l'UID de l'étudiant en question + * @prenom le prénom de l'étudiant en question + * @nom le nom de l'étudiant en question + * @sexe le sexe de l'étudiant en question + * @mail le mail de l'étudiant en question + * + * + * @return UID retourne l'UID de l'étudiant + * @return FALSE retourne FALSE si une erreur occure + * + */ + public static function includeEtudiant($identifiant, $prenom, $nom, $sexe, $mail){ + /* [1] On vérifie l'existence de l'étudiant (par identifiant uniquement) + ======================================================*/ + $getEtudiantUID = DataBase::getPDO()->prepare("SELECT identifiant as id + FROM utilisateur + WHERE identifiant = :identifiant"); + $getEtudiantUID->execute(array( ':identifiant' => $identifiant )); + + /* [2] Cas où l'étudiant existe déjà, alors on retourne l'UID + ======================================================*/ + if( $etudiantUID = $getEtudiantUID->fetch()['id'] ) return $etudiantUID; + + /* [3] Cas où l'étudiant n'existe pas, alors on le créé + ======================================================*/ + $creerSemestre = DataBase::getPDO()->prepare("INSERT INTO utilisateur(identifiant, prenom, nom, sexe, mail, mdp, droits) + VALUES(:identifiant, :prenom, :nom, :sexe, :mail, DEFAULT, :droits)"); // par défaut le nom du semestre = "Sn" où n est le rang + $creerSemestre->execute(array( + ':identifiant' => $identifiant, + ':prenom' => $prenom, + ':nom' => $nom, + ':sexe' => $sexe, + ':mail' => $mail, + ':droits' => 'student' + )); + + /* [4] On vérifie que la formation a été créé et retourne son UID + ======================================================*/ + $getEtudiantUID = DataBase::getPDO()->prepare("SELECT identifiant as id + FROM utilisateur + WHERE identifiant = :identifiant"); + $getEtudiantUID->execute(array( ':identifiant' => $identifiant )); + + return $etudiantUID = $getEtudiantUID->fetch()['id']; + } + + } \ No newline at end of file diff --git a/test.php b/test.php index 722df15..12dfeeb 100755 --- a/test.php +++ b/test.php @@ -46,14 +46,28 @@ require_once __ROOT__.'/manager/security.php'; debug(); require_once __ROOT__.'/manager/phpExcel.php'; +require_once __ROOT__.'/manager/groups.php'; -$request = new stdClass(); $answer = new stdClass(); -$request->level_1 = 'import_userlist_group'; -$request->docPath = __ROOT__.'/src/files/modele_import_inscrits.xlsx'; +$r1 = new stdClass(); $a1 = new stdClass(); +$r1->level_1 = 'import_userlist_group'; +$r1->docPath = __ROOT__.'/src/files/modele_import_inscrits.xlsx'; -xlsx_switch_lvl1($request, $answer); +xlsx_switch_lvl1($r1, $a1); + + +if( $a1->request == 'success' ){ + + $r2 = new stdClass(); $a2 = new stdClass(); + $r2->level_1 = 'addUserlist'; + $r2->rang = 1; + $r2->annee = 2018; + $r2->formationList = $a1->formationList; + + groups_switch_level_1($r2, $a2); + + var_dump( $a2 ); +} -var_dump( $answer->formationList ); // $request = new stdClass(); $answer = new stdClass(); // $request->level_1 = 'updateRole';