2018-02-27 13:46:38 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Created by PhpStorm.
|
|
|
|
* User: lucas
|
|
|
|
* Date: 20/02/18
|
|
|
|
* Time: 20:31
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace database\repo;
|
|
|
|
|
|
|
|
|
|
|
|
use database\core\Repo_i;
|
|
|
|
|
|
|
|
class professor extends Repo_i {
|
|
|
|
|
2018-03-01 16:22:22 +00:00
|
|
|
|
|
|
|
/* (1) Creates a new professor
|
|
|
|
*
|
|
|
|
* @lastName<String> The professor's lastName
|
|
|
|
* @firstName<String> The professor's firstName
|
|
|
|
* @category<int> The professor's category ID
|
|
|
|
* @hoursToDo<int> The professor's number of hours to do
|
|
|
|
* @initials<int> The professor's initials
|
|
|
|
* @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
|
2018-02-27 13:46:38 +00:00
|
|
|
]);
|
|
|
|
|
2018-03-01 16:22:22 +00:00
|
|
|
/* (3) Manage error */
|
|
|
|
if( !$success )
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* (4) Return inserted ID */
|
2018-02-27 13:46:38 +00:00
|
|
|
return $this->pdo->lastInsertId();
|
2018-03-01 16:22:22 +00:00
|
|
|
|
2018-02-27 13:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-01 16:22:22 +00:00
|
|
|
|
|
|
|
/* (2) Check if a professor exists (by its names)
|
|
|
|
*
|
|
|
|
* @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
|
2018-02-27 13:46:38 +00:00
|
|
|
]);
|
|
|
|
|
2018-03-01 16:22:22 +00:00
|
|
|
/* (3) Return NULL on error */
|
|
|
|
if( !$success )
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* (4) Return @prof_id or NULL if nothing found */
|
|
|
|
return $st->fetch()['idProfesseur'] ?: NULL;
|
|
|
|
|
2018-02-27 13:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-03-01 16:22:22 +00:00
|
|
|
/* (3) Check if a professor exists (by its names)
|
|
|
|
*
|
|
|
|
* @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 ];
|
|
|
|
|
2018-03-01 17:25:21 +00:00
|
|
|
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[] = '`Categorie_idCategorie` = :category'; $bind_param[':category'] = $category; }
|
|
|
|
if( !is_null($hoursToDo) ){ $build_rq[] = '`hoursToDo` = :hoursToDo'; $bind_param[':hoursToDo'] = $hoursToDo; }
|
|
|
|
if( !is_null($initials) ){ $build_rq[] = '`abreviation` = :initials'; $bind_param[':initials'] = $initials; }
|
|
|
|
if( !is_null($isAdmin) ){ $build_rq[] = '`admin` = :isAdmin'; $bind_param[':isAdmin'] = $isAdmin; }
|
|
|
|
if( !is_null($casLogin) ){ $build_rq[] = '`casLogin` = :casLogin'; $bind_param[':casLogin'] = $casLogin; }
|
2018-03-01 16:22:22 +00:00
|
|
|
|
|
|
|
/* (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);
|
|
|
|
|
2018-02-27 13:46:38 +00:00
|
|
|
}
|
|
|
|
|
2018-03-01 16:22:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (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)
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
2018-02-27 13:46:38 +00:00
|
|
|
public function isAdmin(int $id) : bool{
|
|
|
|
|
2018-03-01 16:22:22 +00:00
|
|
|
/* (1) Prepare statement */
|
|
|
|
$st = $this->pdo->prepare("SELECT `admin` FROM `Professeur` WHERE `idProfesseur` = :id AND `admin` = 1");
|
2018-02-27 13:46:38 +00:00
|
|
|
|
2018-03-01 16:22:22 +00:00
|
|
|
/* (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;
|
2018-02-27 13:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-03-01 16:22:22 +00:00
|
|
|
|
|
|
|
/* (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)
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
2018-03-01 16:45:40 +00:00
|
|
|
public function getById(int $id) : ?array{
|
2018-03-01 16:22:22 +00:00
|
|
|
|
|
|
|
/* (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();
|
|
|
|
|
2018-02-27 13:46:38 +00:00
|
|
|
}
|
|
|
|
|
2018-03-01 16:22:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (7) Deletes a professor
|
|
|
|
*
|
|
|
|
* @return deleted<bool> Whether the professor have been deleeted successfully
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
2018-02-27 13:46:38 +00:00
|
|
|
public function delete(int $id) : bool{
|
|
|
|
|
2018-03-01 16:22:22 +00:00
|
|
|
/* (1) Prepare statement */
|
|
|
|
$st = $this->pdo->prepare("DELETE FROM `Professeur` WHERE `idProfesseur` = :id");
|
|
|
|
|
|
|
|
/* (2) Return the execution status */
|
|
|
|
return $st->execute([ ':id' => $id ]);
|
|
|
|
|
2018-02-27 13:46:38 +00:00
|
|
|
}
|
|
|
|
|
2018-03-01 16:22:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (8) Returns a professor's statistic data
|
|
|
|
*
|
|
|
|
* @return data<array> Professor data
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
2018-02-27 16:22:08 +00:00
|
|
|
public function getVH(int $id) : array{
|
2018-03-01 16:22:22 +00:00
|
|
|
|
|
|
|
/* (1) Prepare Statement */
|
2018-02-27 16:22:08 +00:00
|
|
|
$st = $this->pdo->prepare("SELECT VHCours, VHTd, VHTp, Cat.idCategorie idCat, Prof.hoursToDo du
|
|
|
|
FROM Professeur Prof, Categorie Cat,
|
2018-02-27 19:41:36 +00:00
|
|
|
(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
|
2018-02-27 16:22:08 +00:00
|
|
|
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;");
|
|
|
|
|
2018-03-01 16:22:22 +00:00
|
|
|
/* (2) Bind params and execute */
|
|
|
|
$success = $st->execute([ ':idProf' => $id ]);
|
|
|
|
|
|
|
|
/* (3) Manage error */
|
|
|
|
if( !$success )
|
|
|
|
return [];
|
|
|
|
|
|
|
|
/* (4) Return data */
|
2018-03-01 14:57:25 +00:00
|
|
|
return $st->fetch() ?: [];
|
2018-03-01 16:22:22 +00:00
|
|
|
|
2018-02-27 16:22:08 +00:00
|
|
|
}
|
|
|
|
|
2018-02-27 13:46:38 +00:00
|
|
|
}
|