[repo.professor] code clean up + error management (to test)

This commit is contained in:
xdrm-brackets 2018-03-01 17:22:22 +01:00
parent ffdc0bd15e
commit a2f84116ca
1 changed files with 210 additions and 46 deletions

View File

@ -13,80 +13,238 @@ use database\core\Repo_i;
class professor extends 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([ /* (1) Creates a new professor
"casLogin" => $casLogin, *
"lastName" => $lastName, * @lastName<String> The professor's lastName
"firstName" => $firstName, * @firstName<String> The professor's firstName
"abrev" => $initials, * @category<int> The professor's category ID
"admin" => $isAdmin? 1 : 0, * @hoursToDo<int> The professor's number of hours to do
"hoursToDo" => $hoursToDo, * @initials<int> The professor's initials
"cat" => $category * @isAdmin<bool> Whether the professor is an admin
* @casLogin<String> The professor's CAS username
*
* @return prof_id<int> 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(); 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, /* (2) Check if a professor exists (by its names)
"lastName" => $lastName *
* @lastName<String> The professor's lastName
* @firstName<String> The professor's firstName
*
* @return prof_id<int> 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,[ /* (3) Check if a professor exists (by its names)
"id" => $id *
])); * @idProf<int> The professor's UID
* @lastName<String> [OPT] The professor's new lastName
* @firstName<String> [OPT] The professor's new firstName
* @category<int> [OPT] The professor's new category ID
* @hoursToDo<int> [OPT] The professor's new number of hours to do
* @initials<int> [OPT] The professor's new initials
* @isAdmin<bool> [OPT] Whether the professor is an admin
* @casLogin<String> [OPT] The professor's new CAS username
*
* @return updated<bool> 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<int> The professor's UID
*
* @return is_admin<bool> Whether the professor is an admin (FALSE if does not exist)
*
---------------------------------------------------------*/
public function isAdmin(int $id) : bool{ public function isAdmin(int $id) : bool{
$st = $this->pdo->prepare("SELECT admin FROM Professeur WHERE idProfesseur = :id");
$st->execute([ /* (1) Prepare statement */
"id" => $id $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<int> The professor's UID
*
* @return teacher<array> 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<array> 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<bool> Whether the professor have been deleeted successfully
*
---------------------------------------------------------*/
public function delete(int $id) : bool{ public function delete(int $id) : bool{
$st = $this->pdo->prepare("DELETE FROM Professeur WHERE idProfesseur = :id");
return $st->execute([ /* (1) Prepare statement */
"id" => $id $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<array> Professor data
*
---------------------------------------------------------*/
public function getVH(int $id) : array{ public function getVH(int $id) : array{
/* (1) Prepare Statement */
$st = $this->pdo->prepare("SELECT VHCours, VHTd, VHTp, Cat.idCategorie idCat, Prof.hoursToDo du $st = $this->pdo->prepare("SELECT VHCours, VHTd, VHTp, Cat.idCategorie idCat, Prof.hoursToDo du
FROM Professeur Prof, Categorie Cat, 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(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 VHCours.idProf = Prof.idProfesseur AND
VHTp.idProf = Prof.idProfesseur AND VHTp.idProf = Prof.idProfesseur AND
VHTd.idProf = Prof.idProfesseur;"); 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() ?: []; return $st->fetch() ?: [];
} }
} }