From 587884d4378859e95642d20b1ad7bd7cebe4a8d8 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sun, 4 Mar 2018 11:58:19 +0100 Subject: [PATCH] [repo.professor] get (gets by id if given, else getAll) | getWithVH (same as 'get' but with VH data) --- build/api/module/professorController.php | 54 +++++++++--- build/database/repo/professor.php | 107 ++++++++++++++++------- config/modules.json | 3 +- 3 files changed, 118 insertions(+), 46 deletions(-) diff --git a/build/api/module/professorController.php b/build/api/module/professorController.php index f2c8150..4a45714 100644 --- a/build/api/module/professorController.php +++ b/build/api/module/professorController.php @@ -17,6 +17,33 @@ use error\core\Err; class professorController{ + private static function proccess_vh(array &$vh_prof){ + + /* (1) If not in category 1 nor 3 -> equivalentTD = TD + 2/3.TP + 1.5.COURS */ + if( !in_array($vh_prof['idCat'], [1,3]) ) + return $vh_prof['equiTD'] = $vh_prof['VHTd'] + (2/3)*$vh_prof['VHTp'] + 1.5*$vh_prof['VHCours']; + // exited because of 'return' statement + + + /* (2) Else (cat = 1 or 3) -> equivalentTD = TD + TP + 1.5.COURS */ + $vh_prof['equiTD'] = $vh_prof['VHTd'] + $vh_prof['VHTp'] + 1.5*$vh_prof['VHCours']; + + /* (3) If equivalentTD exceeds HeuresDues */ + if($vh_prof['equiTD'] > $vh_prof['du']){ + + /* (3.1) @valTP = HeuresDues.(TP / equivalentTD) */ + $valTP = $vh_prof['du'] * ( $vh_prof['VHTp'] / $vh_prof['equiTD'] ); + /* (3.2) equivalentTD = 1.5*COURS + TD + @valTP + (TP-@valTP) */ + $vh_prof['equiTD'] = round(1.5*$vh_prof['VHCours'] + $vh_prof['VHTd'] + $valTP + ($vh_prof['VHTp'] - $valTP)*(2/3), 2); + + } + + /* (4) VH comp */ + $vh_prof['VHComp'] = round($vh_prof['equiTD'] - $vh_prof['du'], 2); + $vh_prof['VHComp'] = ( $vh_prof['VHComp'] < 0 ) ? 0 : $vh_prof['VHComp']; + } + + /* (1) Returns 1 or all professors * * @prof_id [OPT] The professor UID @@ -26,6 +53,7 @@ class professorController{ ---------------------------------------------------------*/ public static function get($args){ $prof_id = null; + $with_vh = null; extract($args); /* Get the professor repo */ @@ -33,26 +61,30 @@ class professorController{ $prof_repo = Repo::getRepo('professor'); - /* (1) If @prof_id is set -> getById() + /* (1) If with VH data ---------------------------------------------------------*/ - if( !is_null($prof_id) ){ + if( is_string($with_vh) && $with_vh == '1' ){ - /* (1) Only get one professor by its id */ - $fetch_prof = $prof_repo->getById((int) $prof_id); + /* (1) Get All professors or 1 by its id (if set) */ + $fetched = $prof_repo->getWithVH($prof_id); - /* (2) If nothing found -> return empty set */ - if( is_null($fetch_prof) ) - return ['professors' => []]; + /* (2) Process VH */ + foreach($fetched as &$vh_prof) + self::proccess_vh($vh_prof); - /* (3) Else -> return the professor */ - return ['professors' => [$fetch_prof]]; + /* (3) Return data */ + return ['professors' => $fetched]; } - /* (2) Else -> getAll() + /* (2) If without VH data (only Professor.*) ---------------------------------------------------------*/ - return ['professors' => $prof_repo->getAll()]; + /* (1) Get All professors or 1 by its id (if set) */ + $fetched = $prof_repo->get($prof_id); + + /* (3) Return data */ + return ['professors' => $fetched]; } diff --git a/build/database/repo/professor.php b/build/database/repo/professor.php index 72627b3..e620532 100644 --- a/build/database/repo/professor.php +++ b/build/database/repo/professor.php @@ -163,33 +163,37 @@ class professor extends Repo_i { - /* (5) Gets a professor by its UID + /* (5) Gets a professor by its UID ||| getAll * - * @idProf The professor's UID + * @prof_id [OPT] The professor's UID, if not set, getAll() * - * @return teacher The professor's data (NULL on error / not found) + * @return teachers The professors matching id (NULL on error) * ---------------------------------------------------------*/ - public function getById(int $id) : ?array{ + public function get(?int $prof_id=null) : ?array{ - /* (1) Prepare Statement */ - $st = $this->pdo->prepare("SELECT * FROM `Professeur` WHERE `idProfesseur` = :id"); + /* (1) Manage if no id given */ + $cond = is_null($prof_id) ? '' : ' WHERE `idProfesseur` = :id'; + $parm = is_null($prof_id) ? [] : [':id' => $prof_id]; - /* (2) Bind params and execute statement */ - $success = $st->execute([ ':id' => $id ]); + /* (2) Prepare Statement */ + $st = $this->pdo->prepare("SELECT * FROM `Professeur`$cond GROUP BY abreviation ASC"); - /* (3) Manage error */ + /* (3) Bind params and execute statement */ + $success = $st->execute($parm); + + /* (4) Manage error */ if( !$success ) - return NULL; + return []; - /* (4) Get data */ - $fetched = $st->fetch(); + /* (5) Get data */ + $fetched = $st->fetchAll(); - /* (5) Return NULL on no result */ + /* (6) Return [] on no result */ if( $fetched === false ) - return NULL; + return []; - /* (6) Return data */ + /* (7) Return data */ return $fetched; } @@ -231,22 +235,57 @@ class professor extends Repo_i { - /* (7) Gets all professors + /* (7) Gets a professor by its UID ||| getAll * - * @return teachers The professors' data ([] on error) + * @prof_id [OPT] The professor's UID, if not set, getAll() + * + * @return teachers The professors matching id (NULL on error) * ---------------------------------------------------------*/ - public function getAll() : array{ + public function getWithVH(?int $prof_id=null) : array{ - /* (1) Set query */ - $query = $this->pdo->query("SELECT * FROM `Professeur` ORDER BY `abreviation` ASC"); + /* (1) Manage if no id given */ + $cond = is_null($prof_id) ? '' : 'Prof.idProfesseur = :id AND'; + $parm = is_null($prof_id) ? [] : [':id' => $prof_id]; - /* (2) If error return empty array */ - if( $query === false ) + /* (2) Prepare Statement */ + $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); + $st = $this->pdo->prepare("SELECT + Prof.idProfesseur, Prof.firstName, Prof.lastName, Prof.casLogin, Prof.abreviation, Prof.admin, Prof.hoursToDo, + VHCours, VHTd, VHTp, + Cat.idCategorie idCat, + Cat.labelCategorie categorie + + FROM + Professeur Prof, + Categorie Cat, + (SELECT IFNULL(SUM(Cours.volume),0) VHCours, Prof.idProfesseur idProf FROM Professeur Prof LEFT JOIN Cours ON Prof.idProfesseur = Cours.Professeur_idProfesseur GROUP BY Prof.idProfesseur) VHCours, + (SELECT IFNULL(SUM(TD.volume),0) VHTd , Prof.idProfesseur idProf FROM Professeur Prof LEFT JOIN TD ON TD.Professeur_idProfesseur = Prof.idProfesseur GROUP BY Prof.idProfesseur) VHTd, + (SELECT IFNULL(SUM(TP.volume),0) VHTp, Prof.idProfesseur idProf FROM Professeur Prof LEFT JOIN TP ON TP.Professeur_idProfesseur = Prof.idProfesseur GROUP BY Prof.idProfesseur) VHTp + + WHERE $cond Prof.Categorie_idCategorie = Cat.idCategorie + AND VHCours.idProf = Prof.idProfesseur + AND VHTp.idProf = Prof.idProfesseur + AND VHTd.idProf = Prof.idProfesseur + GROUP BY + Prof.idProfesseur;"); + + /* (3) Bind params and execute statement */ + $success = $st->execute($parm); + + /* (4) Manage error */ + if( !$success ) return []; - /* (3) Return fetched data */ - return $query->fetchAll(); + /* (5) Get data */ + $fetched = $st->fetchAll(); + + /* (6) Return [] on no result */ + if( $fetched === false ) + return []; + + /* (7) Return data */ + return $fetched; } @@ -280,16 +319,16 @@ class professor extends Repo_i { /* (1) Prepare Statement */ $st = $this->pdo->prepare("SELECT VHCours, VHTd, VHTp, Cat.idCategorie idCat, Prof.hoursToDo du - FROM Professeur Prof, Categorie Cat, - (SELECT IFNULL(SUM(Cours.volume),0) VHCours, Prof.idProfesseur idProf FROM Professeur Prof LEFT JOIN Cours ON Prof.idProfesseur = Cours.Professeur_idProfesseur GROUP BY Prof.idProfesseur) VHCours, - (SELECT IFNULL(SUM(TD.volume),0) VHTd , Prof.idProfesseur idProf FROM Professeur Prof LEFT JOIN TD ON TD.Professeur_idProfesseur = Prof.idProfesseur GROUP BY Prof.idProfesseur) VHTd, - (SELECT IFNULL(SUM(TP.volume),0) VHTp, Prof.idProfesseur idProf FROM Professeur Prof LEFT JOIN TP ON TP.Professeur_idProfesseur = Prof.idProfesseur GROUP BY Prof.idProfesseur) VHTp - WHERE - Prof.idProfesseur = :idProf AND - Prof.Categorie_idCategorie = Cat.idCategorie AND - VHCours.idProf = Prof.idProfesseur AND - VHTp.idProf = Prof.idProfesseur AND - VHTd.idProf = Prof.idProfesseur;"); + FROM Professeur Prof, Categorie Cat, + (SELECT IFNULL(SUM(Cours.volume),0) VHCours, Prof.idProfesseur idProf FROM Professeur Prof LEFT JOIN Cours ON Prof.idProfesseur = Cours.Professeur_idProfesseur GROUP BY Prof.idProfesseur) VHCours, + (SELECT IFNULL(SUM(TD.volume),0) VHTd , Prof.idProfesseur idProf FROM Professeur Prof LEFT JOIN TD ON TD.Professeur_idProfesseur = Prof.idProfesseur GROUP BY Prof.idProfesseur) VHTd, + (SELECT IFNULL(SUM(TP.volume),0) VHTp, Prof.idProfesseur idProf FROM Professeur Prof LEFT JOIN TP ON TP.Professeur_idProfesseur = Prof.idProfesseur GROUP BY Prof.idProfesseur) VHTp + WHERE + Prof.idProfesseur = :idProf AND + Prof.Categorie_idCategorie = Cat.idCategorie AND + VHCours.idProf = Prof.idProfesseur AND + VHTp.idProf = Prof.idProfesseur AND + VHTd.idProf = Prof.idProfesseur;"); /* (2) Bind params and execute */ $success = $st->execute([ ':idProf' => $id ]); diff --git a/config/modules.json b/config/modules.json index a7bf457..6e1e102 100644 --- a/config/modules.json +++ b/config/modules.json @@ -82,7 +82,8 @@ "des": "Get one or all professors", "per": [], "par": { - "URL0": { "des": "Optional professor UID.", "typ": "id", "ren": "prof_id", "opt": true } + "URL0": { "des": "Whether to return VH", "typ": "id", "ren": "with_vh" }, + "URL1": { "des": "Optional professor UID.", "typ": "id", "ren": "prof_id", "opt": true } }, "out": { "professors": { "des": "Teacher list", "typ": "array" }