[repo.professor] renamed 'getLinkedDepartment' -> 'getLinkedDepartments' (plural) + Error management (edge cases) + update -> updates meta database + delete -> updates meta database

This commit is contained in:
xdrm-brackets 2018-03-19 19:04:57 +01:00
parent 959df5cfde
commit b47341f792
1 changed files with 147 additions and 43 deletions

View File

@ -115,7 +115,16 @@ class professor extends Repo_i {
}
public function getLinkedDepartment(string $casLogin) : ?array{
/* (3) Get available departments for a CAS login
*
* @casLogin<String> The professor's CAS username
*
* @return departments<array> The list of available departments (empty on error)
*
---------------------------------------------------------*/
public function getLinkedDepartments(string $casLogin) : array{
/* (1) Prepare Statement */
$st = $this->pdo->prepare("SELECT d2.iddepartement idDep, d2.label labelDep, d2.databaseName dbName
@ -126,21 +135,21 @@ class professor extends Repo_i {
/* (2) Check if statement error */
if( is_bool($st) )
return NULL;
return [];
/* (3) Bind params and execute statement */
$success = $st->execute([ ':caslogin' => $casLogin ]);
/* (4) Manage error */
if( !$success )
return NULL;
return [];
/* (5) Get data */
$fetched = $st->fetchAll();
/* (6) Return NULL on no result */
/* (6) Return [] on no result */
if( $fetched === false )
return NULL;
return [];
/* (7) Return data */
return $fetched;
@ -150,7 +159,7 @@ class professor extends Repo_i {
/* (3) Check if a professor exists (by its names)
/* (4) Updates a professor's data
*
* @idProf<int> The professor's UID
* @lastName<String> [OPT] The professor's new lastName
@ -166,6 +175,9 @@ class professor extends Repo_i {
---------------------------------------------------------*/
public function update(int $id, ?String $lastName, ?String $firstName, ?int $category, ?int $hoursToDo, ?String $initials, ?bool $isAdmin, ?String $casLogin) : bool{
/* (1) Update current database
---------------------------------------------------------*/
/* (1) Build request */
$build_rq = [];
$bind_param = [ ':idProfesseur' => $id ];
@ -191,36 +203,95 @@ class professor extends Repo_i {
/* (5) Return execution success */
$success = $st->execute($bind_param);
$prof = $this->get($id);
if($success && !is_null($prof[0]["casLogin"])){
//try to get the user
$st = $this->pdo->prepare("SELECT * FROM meta_vhost.casUser WHERE casLogin = :casLogin");
$st->execute([
"casLogin" => $prof[0]["casLogin"]
]);
/* (6) Manage execution error */
if( !$success )
return FALSE;
//is the user does not already exists, we create it
if(!is_array($st->fetch())){
/* (7) If do not have to update 'meta' database -> success */
if( is_null($casLogin) )
return TRUE;
/* (2) Check existence in meta database
---------------------------------------------------------*/
/* (1) Try to fetch professor's data */
$prof = $this->get($id);
/* (2) Error: no professor found */
if( count($prof) === 0 )
return TRUE;
/* (3) Check if user exists in 'meta' database */
$st = $this->pdo->prepare("SELECT * FROM meta_vhost.casUser WHERE casLogin = :casLogin");
/* (4) Manage statement error */
if( is_bool($st) )
return TRUE;
/* (5) Try to execute request */
$success = $st->execute([ ':casLogin' => $prof[0]['casLogin'] ]);
/* (6) Manage execution error */
if( !$success )
return TRUE;
/* (7) Check if exists */
$exists = is_array( $st->fetch() );
/* (3) Does not exist: Create professor in meta database
---------------------------------------------------------*/
if( !$exists ){
/* (1) Prepare statement */
$st = $this->pdo->prepare("INSERT INTO meta_vhost.casUser(casLogin, firstName, lastName)
VALUE(:casLogin, :firstName, :lastName)");
}else{
$st = $this->pdo->prepare("UPDATE meta_vhost.casUser SET casLogin = :casLogin,firstName = :firstName, lastName = :lastName ");
}
/* (2) Manage statement error */
if( is_bool($st) )
return TRUE;
/* (3) Try to execute */
$st->execute([
"firstName" => $prof[0]["firstName"],
"lastName" => $prof[0]["lastName"],
"casLogin" => $prof[0]["casLogin"]
':firstName' => $prof[0]['firstName'],
':lastName' => $prof[0]['lastName'],
':casLogin' => $prof[0]['casLogin']
]);
return TRUE;
}
return $success;
/* (4) If exists: Update professor in meta database
---------------------------------------------------------*/
/* (1) Prepare statement */
$st = $this->pdo->prepare("UPDATE meta_vhost.casUser
SET casLogin = :casLogin,
firstName = :firstName,
lastName = :lastName ");
/* (2) Manage statement error */
if( is_bool($st) )
return TRUE;
/* (3) Try to execute */
$st->execute([
':firstName' => $prof[0]['firstName'],
':lastName' => $prof[0]['lastName'],
':casLogin' => $prof[0]['casLogin']
]);
return TRUE;
}
/* (4) Return whether a professor is an admin
/* (5) Return whether a professor is an admin
*
* @idProf<int> The professor's UID
*
@ -246,7 +317,7 @@ class professor extends Repo_i {
/* (5) Gets a professor by its UID ||| getAll
/* (6) Gets a professor by its UID ||| getAll
*
* @prof_id<int> [OPT] The professor's UID, if not set, getAll()
*
@ -285,7 +356,7 @@ class professor extends Repo_i {
/* (6) Gets a professor by its CAS login
/* (7) Gets a professor by its CAS login
*
* @cas_login<String> The professor's CAS login
*
@ -323,7 +394,7 @@ class professor extends Repo_i {
/* (7) Gets a professor by its UID ||| getAll
/* (8) Gets a professor by its UID ||| getAll
*
* @prof_id<int> [OPT] The professor's UID, if not set, getAll()
*
@ -424,32 +495,65 @@ class professor extends Repo_i {
/* (8) Deletes a professor
/* (9) Deletes a professor
*
* @return deleted<bool> Whether the professor have been deleeted successfully
*
---------------------------------------------------------*/
public function delete(int $id) : bool{
//we have to store the professor to synchronize the meta database later
/* (1) Try to fetch professor data
---------------------------------------------------------*/
/* (1) Try to fetch */
$prof = $this->get($id);
/* (2) Error: if not found */
if( count($prof) === 0 )
return false;
/* (2) Remove professor from current database
---------------------------------------------------------*/
/* (1) Prepare statement */
$st = $this->pdo->prepare("DELETE FROM `Professeur` WHERE `idProfesseur` = :id");
/* (2) Return the execution status */
/* (2) Manage statement error */
if( is_bool($st) )
return false;
/* (3) Return the execution status */
$success = $st->execute([ ':id' => $id ]);
if($success){
//delete the association
$st = $this->pdo->prepare("DELETE FROM meta_vhost.linkedDep WHERE casUser_casLogin = :casLogin AND departement_iddepartement = :idDep");
$st->execute([
"casLogin" => $prof[0]["casLogin"],
"idDep" => $_SESSION['CurrentDepartementId']
/* (4) Error: on execution error */
if( !$success )
return false;
/* (3) Remove professor from meta database
---------------------------------------------------------*/
/* (1) Prepare statement */
$st = $this->pdo->prepare('DELETE FROM meta_vhost.linkedDep WHERE casUser_casLogin = :casLogin AND departement_iddepartement = :idDep');
/* (2) Manage statement error */
if( is_bool($st) )
return true;
/* (3) Try to execute */
$success = $st->execute([
':casLogin' => $prof[0]['casLogin'],
':idDep' => $_SESSION['CurrentDepartementId']
]);
}
return $success;
/* (4) Manage execution error */
if( !$success )
return true;
return true;
}