From a2f84116cad8327bb9f36e32dd8ff41f2c6fae06 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Thu, 1 Mar 2018 17:22:22 +0100 Subject: [PATCH] [repo.professor] code clean up + error management (to test) --- build/database/repo/professor.php | 256 ++++++++++++++++++++++++------ 1 file changed, 210 insertions(+), 46 deletions(-) diff --git a/build/database/repo/professor.php b/build/database/repo/professor.php index c2f8afa..c4c4d9d 100644 --- a/build/database/repo/professor.php +++ b/build/database/repo/professor.php @@ -13,80 +13,238 @@ use database\core\Repo_i; class professor extends Repo_i { - public function create(string $lastName, string $firstName, int $category, $hoursToDo = 0, $initials = "", $isAdmin = false , $casLogin = "" ) : int{ - $st = $this->pdo->prepare("INSERT INTO Professeur (casLogin, lastName, firstName, abreviation, admin,hoursToDo, Categorie_idCategorie) - VALUE (:casLogin, :lastName, :firstName, :abrev, :admin, :hoursToDo, :cat); - "); - $st->execute([ - "casLogin" => $casLogin, - "lastName" => $lastName, - "firstName" => $firstName, - "abrev" => $initials, - "admin" => $isAdmin? 1 : 0, - "hoursToDo" => $hoursToDo, - "cat" => $category + /* (1) Creates a new professor + * + * @lastName The professor's lastName + * @firstName The professor's firstName + * @category The professor's category ID + * @hoursToDo The professor's number of hours to do + * @initials The professor's initials + * @isAdmin Whether the professor is an admin + * @casLogin The professor's CAS username + * + * @return prof_id The professor's UID (or NULL on error) + * + ---------------------------------------------------------*/ + public function create(string $lastName, string $firstName, int $category, $hoursToDo = 0, $initials = "", $isAdmin = false , $casLogin = "" ) : ?int{ + + /* (1) Prepare Statement */ + $st = $this->pdo->prepare("INSERT INTO + Professeur(`casLogin`, `lastName`, `firstName`, `abreviation`, `admin`, `hoursToDo`, `Categorie_idCategorie`) + VALUE (:casLogin, :lastName, :firstName, :abrev, :is_admin, :hoursToDo, :cat);"); + + /* (2) Bind params and execute */ + $success = $st->execute([ + ':casLogin' => $casLogin, + ':lastName' => $lastName, + ':firstName' => $firstName, + ':abrev' => $initials, + ':is_admin' => $isAdmin ? 1 : 0, + ':hoursToDo' => $hoursToDo, + ':cat' => $category ]); + /* (3) Manage error */ + if( !$success ) + return NULL; + + /* (4) Return inserted ID */ return $this->pdo->lastInsertId(); + } - public function exists(string $lastName, string $firstName) : int{ - $st = $this->pdo->prepare("SELECT idProfesseur FROM Professeur WHERE firstName = :firstName AND lastName = :lastName"); - $st->execute([ - "firstName" => $firstName, - "lastName" => $lastName + + /* (2) Check if a professor exists (by its names) + * + * @lastName The professor's lastName + * @firstName The professor's firstName + * + * @return prof_id The professor's UID (or NULL on error) + * + ---------------------------------------------------------*/ + public function exists(string $lastName, string $firstName) : ?int{ + + /* (1) Prepare Statement */ + $st = $this->pdo->prepare("SELECT idProfesseur + FROM Professeur + WHERE firstName = :firstName + AND lastName = :lastName"); + + /* (2) Bind params and execute */ + $success = $st->execute([ + ':firstName' => $firstName, + ':lastName' => $lastName ]); - return $st->fetch()["idProfesseur"]?: 0; + /* (3) Return NULL on error */ + if( !$success ) + return NULL; + + /* (4) Return @prof_id or NULL if nothing found */ + return $st->fetch()['idProfesseur'] ?: NULL; + } - public function update(int $id, array $data) : bool{ - $updSt = ""; - foreach ($data as $key => $field){ - $updSt .= "$key = :$key,"; - } - $updSt = rtrim($updSt,","); - $st = $this->pdo->prepare("UPDATE Professeur SET $updSt WHERE idProfesseur = :id"); - return $st->execute(array_merge($data,[ - "id" => $id - ])); + /* (3) Check if a professor exists (by its names) + * + * @idProf The professor's UID + * @lastName [OPT] The professor's new lastName + * @firstName [OPT] The professor's new firstName + * @category [OPT] The professor's new category ID + * @hoursToDo [OPT] The professor's new number of hours to do + * @initials [OPT] The professor's new initials + * @isAdmin [OPT] Whether the professor is an admin + * @casLogin [OPT] The professor's new CAS username + * + * @return updated Whether the updated have been successful + * + ---------------------------------------------------------*/ + public function update(int $id, ?String $lastName, ?String $firstName, ?int $category, ?int $hoursToDo, ?String $initials, ?bool $isAdmin, ?String $casLogin) : bool{ + + /* (1) Build request */ + $build_rq = []; + $bind_param = [ ':idProfesseur' => $id ]; + + if( !is_null($lastName) ){ $build_rq[] = '`lastName` = :lastName'; $bind_param[':lastName'] = $lastName; } + if( !is_null($firstName) ){ $build_rq[] = '`firstName` = :firstName'; $bind_param[':firstName'] = $firstName; } + if( !is_null($category) ){ $build_rq[] = '`category` = :category'; $bind_param[':category'] = $category; } + if( !is_null($hoursToDo) ){ $build_rq[] = '`hoursToDo` = :hoursToDo'; $bind_param[':hoursToDo'] = $hoursToDo; } + if( !is_null($initials) ){ $build_rq[] = '`initials` = :initials'; $bind_param[':initials'] = $initials; } + if( !is_null($isAdmin) ){ $build_rq[] = '`isAdmin` = :isAdmin'; $bind_param[':isAdmin'] = $isAdmin; } + if( !is_null($casLogin) ){ $build_rq[] = '`casLogin` = :casLogin'; $bind_param[':casLogin'] = $casLogin; } + + /* (2) ERROR if no updated field */ + if( count($build_rq) <= 0 || count($bind_param) <= 1 ) + return FALSE; + + /* (3) Build request */ + $sql_rq = "UPDATE `Professeur` SET ".implode(', ', $build_rq)." WHERE `idProfesseur` = :idProfesseur"; + + /* (4) Prepare statement */ + $st = $this->pdo->prepare($sql_rq); + + /* (5) Return execution success */ + return $st->execute($bind_param); + } + + + + /* (4) Return whether a professor is an admin + * + * @idProf The professor's UID + * + * @return is_admin Whether the professor is an admin (FALSE if does not exist) + * + ---------------------------------------------------------*/ public function isAdmin(int $id) : bool{ - $st = $this->pdo->prepare("SELECT admin FROM Professeur WHERE idProfesseur = :id"); - $st->execute([ - "id" => $id - ]); + /* (1) Prepare statement */ + $st = $this->pdo->prepare("SELECT `admin` FROM `Professeur` WHERE `idProfesseur` = :id AND `admin` = 1"); - return $st->fetch()["admin"] == 1; + /* (2) Bind params and execute statement */ + $success = $st->execute([ ':id' => $id ]); + + /* (3) FALSE on error */ + if( !$success ) + return FALSE; + + /* (4) Return whether we have a result or not */ + return $st->fetch() == 1; } - public function get(int $id) : array{ - $st = $this->pdo->prepare("SELECT * FROM Professeur WHERE idProfesseur = :id"); - $st->execute([ - "id" => $id - ]); - return $st->fetch(); + + /* (5) Gets a professor by its UID + * + * @idProf The professor's UID + * + * @return teacher The professor's data (NULL on error / not found) + * + ---------------------------------------------------------*/ + public function get(int $id) : ?array{ + + /* (1) Prepare Statement */ + $st = $this->pdo->prepare("SELECT * FROM `Professeur` WHERE `idProfesseur` = :id"); + + /* (2) Bind params and execute statement */ + $success = $st->execute([ ':id' => $id ]); + + /* (3) Manage error */ + if( !$success ) + return NULL; + + /* (4) Get data */ + $fetched = $st->fetch(); + + /* (5) Return NULL on no result */ + if( $fetched === false ) + return NULL; + + /* (6) Return data */ + return $fetched; + } + + + + /* (6) Gets all professors + * + * @return teachers The professors' data ([] on error) + * + ---------------------------------------------------------*/ + public function getAll() : array{ + + /* (1) Set query */ + $query = $this->pdo->query("SELECT * FROM `Professeur` ORDER BY `abreviation` ASC"); + + /* (2) If error return empty array */ + if( $query === false ) + return []; + + /* (3) Return fetched data */ + return $query->fetchAll(); + + } + + + + + /* (7) Deletes a professor + * + * @return deleted Whether the professor have been deleeted successfully + * + ---------------------------------------------------------*/ public function delete(int $id) : bool{ - $st = $this->pdo->prepare("DELETE FROM Professeur WHERE idProfesseur = :id"); - return $st->execute([ - "id" => $id - ]); + /* (1) Prepare statement */ + $st = $this->pdo->prepare("DELETE FROM `Professeur` WHERE `idProfesseur` = :id"); + + /* (2) Return the execution status */ + return $st->execute([ ':id' => $id ]); + } + + + + /* (8) Returns a professor's statistic data + * + * @return data Professor data + * + ---------------------------------------------------------*/ public function getVH(int $id) : array{ + + /* (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, @@ -98,11 +256,17 @@ class professor extends Repo_i { VHCours.idProf = Prof.idProfesseur AND VHTp.idProf = Prof.idProfesseur AND VHTd.idProf = Prof.idProfesseur;"); - $st->execute([ - "idProf" => $id - ]); + /* (2) Bind params and execute */ + $success = $st->execute([ ':idProf' => $id ]); + + /* (3) Manage error */ + if( !$success ) + return []; + + /* (4) Return data */ return $st->fetch() ?: []; + } } \ No newline at end of file