ptut-vhost/build/database/repo/professor.php

366 lines
11 KiB
PHP
Raw Normal View History

<?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 {
/* (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
*
2018-03-05 17:48:30 +00:00
* @return prof_id<int> The professor's UID (or -1 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();
}
/* (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, ?string $casLogin = null) : ?int{
/* (1) Prepare Statement */
$st = $this->pdo->prepare("SELECT idProfesseur
FROM Professeur
WHERE firstName = :firstName
AND lastName = :lastName
".(is_string($casLogin) ? "AND casLogin = :casLogin" : ""));
/* (2) Bind params and execute */
$params = [
':firstName' => $firstName,
':lastName' => $lastName
];
if(is_string($casLogin)){
$params[":casLogin"] = $casLogin;
}
$success = $st->execute();
/* (3) Return NULL on error */
if( !$success )
return NULL;
/* (4) Return @prof_id or NULL if nothing found */
return $st->fetch()['idProfesseur'] ?: NULL;
}
/* (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 ];
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?1:0; }
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{
/* (1) Prepare statement */
$st = $this->pdo->prepare("SELECT `admin` FROM `Professeur` WHERE `idProfesseur` = :id AND `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;
}
/* (5) Gets a professor by its UID ||| getAll
*
* @prof_id<int> [OPT] The professor's UID, if not set, getAll()
*
* @return teachers<array> The professors matching id (NULL on error)
*
---------------------------------------------------------*/
public function get(?int $prof_id=null) : array{
/* (1) Manage if no id given */
$cond = is_null($prof_id) ? '' : ' WHERE `idProfesseur` = :id';
$parm = is_null($prof_id) ? [] : [':id' => $prof_id];
/* (2) Prepare Statement */
$st = $this->pdo->prepare("SELECT * FROM `Professeur`$cond ORDER BY abreviation ASC");
/* (3) Bind params and execute statement */
if( is_bool($st) ) return [];
$success = $st->execute($parm);
/* (4) Manage error */
if( !$success )
return [];
/* (5) Get data */
$fetched = $st->fetchAll();
/* (6) Return [] on no result */
if( $fetched === false )
return [];
/* (7) Return data */
return $fetched;
}
/* (6) Gets a professor by its CAS login
*
* @cas_login<String> The professor's CAS login
*
* @return teacher<array> The professor's data (NULL on error / not found)
*
---------------------------------------------------------*/
public function getByLogin(String $cas_login) : ?array{
/* (1) Prepare Statement */
$st = $this->pdo->prepare("SELECT * FROM `Professeur` WHERE `casLogin` = :cas_login");
/* (2) Check if statement error */
if( is_bool($st) )
return NULL;
/* (3) Bind params and execute statement */
$success = $st->execute([ ':cas_login' => $cas_login ]);
/* (4) Manage error */
if( !$success )
return NULL;
/* (5) Get data */
$fetched = $st->fetch();
/* (6) Return NULL on no result */
if( $fetched === false )
return NULL;
/* (7) Return data */
return $fetched;
}
/* (7) Gets a professor by its UID ||| getAll
*
* @prof_id<int> [OPT] The professor's UID, if not set, getAll()
*
* @return teachers<array> The professors matching id (NULL on error)
*
---------------------------------------------------------*/
public function getWithVH(?int $prof_id=null) : array{
/* (1) Manage if no id given */
$cond = is_null($prof_id) ? '' : 'Prof.idProfesseur = :id AND';
$parm = is_null($prof_id) ? [] : [':id' => $prof_id];
/* (2) Prepare Statement */
$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
2018-03-06 15:07:15 +00:00
FROM Professeur Prof
LEFT JOIN Cours ON Prof.idProfesseur = Cours.Professeur_idProfesseur
LEFT JOIN UE U ON Cours.UE_code = U.code
GROUP BY Prof.idProfesseur, U.disabled
HAVING (U.disabled = 0 OR U.disabled IS NULL)) VHCours,
2018-03-06 15:07:15 +00:00
(SELECT IFNULL(SUM(TD.volume),0) VHTd , Prof.idProfesseur idProf
FROM Professeur Prof
LEFT JOIN TD ON TD.Professeur_idProfesseur = Prof.idProfesseur
LEFT JOIN UE U2 ON TD.UE_code = U2.code
GROUP BY Prof.idProfesseur, U2.disabled
HAVING (U2.disabled = 0 OR U2.disabled IS NULL)) VHTd,
2018-03-06 15:07:15 +00:00
(SELECT IFNULL(SUM(TP.volume),0) VHTp, Prof.idProfesseur idProf
FROM Professeur Prof
LEFT JOIN TP ON TP.Professeur_idProfesseur = Prof.idProfesseur
LEFT JOIN UE U3 ON TP.UE_code = U3.code
GROUP BY Prof.idProfesseur, U3.disabled
HAVING (U3.disabled = 0 OR U3.disabled IS NULL)) 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 */
if( is_bool($st) ) return [];
$success = $st->execute($parm);
/* (4) Manage error */
if( !$success )
return [];
/* (5) Get data */
$fetched = $st->fetchAll();
/* (6) Return [] on no result */
if( $fetched === false )
return [];
/* (7) Compute additional data */
foreach ($fetched as &$prof){
/* (1) If not in category 1 nor 3 -> equivalentTD = TD + 2/3.TP + 1.5.COURS */
if(in_array($prof["idCat"],[1,3])){
$prof["equiTD"] = $prof["VHTd"] + $prof["VHTp"] + 1.5*$prof["VHCours"];
if($prof["equiTD"] > $prof["hoursToDo"]){
$partTP = $prof["VHTp"] / $prof["equiTD"];
$valReelleTP = $partTP * $prof["hoursToDo"];
$prof["equiTD"] = round(1.5*$prof["VHCours"] + $prof["VHTd"] + $valReelleTP + ($prof["VHTp"] - $valReelleTP)*(2/3),2);
}
$prof['VHComp'] = round($prof['equiTD'] - $prof['hoursToDo'], 2);
$prof['VHComp'] = ( $prof['VHComp'] < 0 ) ? 0 : $prof['VHComp'];
}else{
2018-03-08 17:41:52 +00:00
$prof["equiTD"] = $prof["VHTd"] + (2/3)*$prof["VHTp"] + 1.5*$prof["VHCours"];
2018-03-06 18:40:57 +00:00
if(is_numeric($prof["hoursToDo"]) and $prof["hoursToDo"] > 0){
$prof['VHComp'] = round($prof['equiTD'] - $prof['hoursToDo'], 2);
$prof['VHComp'] = ( $prof['VHComp'] < 0 ) ? 0 : $prof['VHComp'];
}else{
$prof['VHComp'] = 0;
}
}
}
/* (8) Return data */
return $fetched;
}
/* (8) Deletes a professor
*
* @return deleted<bool> Whether the professor have been deleeted successfully
*
---------------------------------------------------------*/
public function delete(int $id) : bool{
/* (1) Prepare statement */
$st = $this->pdo->prepare("DELETE FROM `Professeur` WHERE `idProfesseur` = :id");
/* (2) Return the execution status */
return $st->execute([ ':id' => $id ]);
}
}