[repo.meta] created meta database repo (create_prof, delete_prof, prof_exists, link_exists, link, unlink, get_prof_departments) [repo.professor] used [repo.meta] to synchronize meta database (fully tested, even if meta not already synchronized) [module.casController] now uses [repo.meta] [module.department] now uses [repo.meta]

This commit is contained in:
xdrm-brackets 2018-03-20 18:44:43 +01:00
parent e562bec77f
commit 12b1a29ed0
4 changed files with 382 additions and 114 deletions

View File

@ -11,6 +11,7 @@ namespace api\module;
use database\core\Repo; use database\core\Repo;
use database\repo\professor; use database\repo\professor;
use database\repo\meta;
use error\core\Error; use error\core\Error;
use error\core\Err; use error\core\Err;
@ -110,9 +111,11 @@ class casController{
---------------------------------------------------------*/ ---------------------------------------------------------*/
/** @var professor $prof_repo */ /** @var professor $prof_repo */
$prof_repo = Repo::getRepo('professor'); $prof_repo = Repo::getRepo('professor');
/** @var meta $meta_repo */
$meta_repo = Repo::getRepo('meta');
/* (1) Get the list of linked departments for this @cas_login */ /* (1) Get the list of linked departments for this @cas_login */
$departments = $prof_repo->getLinkedDepartments($cas_login); $departments = $meta_repo->get_prof_departments($cas_login);
/* (2) Failure: if no department for @cas_login */ /* (2) Failure: if no department for @cas_login */
if( count($departments) === 0 ) if( count($departments) === 0 )

View File

@ -10,7 +10,7 @@ namespace api\module;
use database\core\Repo; use database\core\Repo;
use database\repo\professor; use database\repo\meta;
class departmentController class departmentController
{ {
@ -26,11 +26,11 @@ class departmentController
$id_dep = null; $id_dep = null;
extract($args); extract($args);
/** @var professor $prof_repo */ /** @var meta $meta_repo */
$prof_repo = Repo::getRepo('professor'); $meta_repo = Repo::getRepo('meta');
/* (1) Get the list of linked departments for this @cas_login */ /* (1) Get the list of linked departments for this @cas_login */
$departments = $prof_repo->getLinkedDepartments($_SESSION['CAS']['login']); $departments = $meta_repo->get_prof_departments($_SESSION['CAS']['login']);
/* (2) If no @id_dep -> return all */ /* (2) If no @id_dep -> return all */
if( is_null($id_dep) ) if( is_null($id_dep) )
@ -62,12 +62,12 @@ class departmentController
extract($args); extract($args);
/** @var professor $prof_repo */ /** @var meta $meta_repo */
$prof_repo = Repo::getRepo('professor'); $meta_repo = Repo::getRepo('meta');
$deps = $prof_repo->getLinkedDepartments($_SESSION['CAS']['login']); $deps = $meta_repo->get_prof_departments($_SESSION['CAS']['login']);

View File

@ -0,0 +1,257 @@
<?php
namespace database\repo;
use database\core\Repo_i;
class meta extends Repo_i {
/* (1) Creates a new professor
*
* @casLogin<String> The professor's cas login
* @firstName<String> [OPT] The professor's firstName
* @lastName<String> [OPT] The professor's lastName
*
* @return created<bool> Whether the professor has been created
*
---------------------------------------------------------*/
public function create_prof(String $casLogin, ?String $firstName, ?String $lastName) : ?int{
/* (1) Create user in meta database
---------------------------------------------------------*/
/* (1) Try to insert user */
$st = $this->pdo->prepare("INSERT INTO
meta_vhost.casUser(casLogin, firstName, lastName)
VALUES(:cas_login, :first_name, :last_name)");
/* (2) Manage statement error */
if( is_bool($st) )
return FALSE;
/* (3) Try to execute */
$success = $st->execute([
':cas_login' => $casLogin,
':first_name' => $firstName,
':last_name' => $lastName
]);
/* (4) Dispatch execution error */
return $success;
}
/* (2) Check if a professor exists (by its casLogin)
*
* @casLogin<String> The professor's cas login
*
* @return exists<bool> Whether the professor exists
*
---------------------------------------------------------*/
public function prof_exists(String $casLogin) : bool{
/* (1) Prepare Statement */
$st = $this->pdo->prepare("SELECT casLogin
FROM meta_vhost.casUser
WHERE casLogin = :cas_login;");
/* (2) Statement eror */
if( is_bool($st) )
return FALSE;
/* (3) Bind params and execute */
$success = $st->execute([ ':cas_login' => $casLogin ]);
/* (4) Manage execution error */
if( !$success )
return FALSE;
/* (7) Return That it exists */
return is_array( $st->fetch() );
}
/* (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 get_prof_departments(String $casLogin) : array{
/* (1) Prepare Statement */
$st = $this->pdo->prepare("SELECT d2.iddepartement idDep, d2.label labelDep, d2.databaseName dbName
FROM meta_vhost.casUser
JOIN meta_vhost.linkedDep D ON casUser.casLogin = D.casUser_casLogin
JOIN meta_vhost.departement d2 ON D.departement_iddepartement = d2.iddepartement
WHERE casLogin = :cas_login");
/* (2) Check if statement error */
if( is_bool($st) )
return [];
/* (3) Bind params and execute sstatement */
$success = $st->execute([ ':cas_login' => $casLogin ]);
/* (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;
}
/* (4) Deletes a professor
*
* @casLogin<String> The professor's CAS username
*
* @return deleted<bool> Whether the professor have been deleeted successfully
*
---------------------------------------------------------*/
public function delete_prof(String $casLogin) : bool{
/* (1) Prepare statement */
$st = $this->pdo->prepare('DELETE FROM meta_vhost.casUser
WHERE casLogin = :cas_login');
/* (2) Manage statement error */
if( is_bool($st) )
return FALSE;
/* (3) Try to execute */
$success = $st->execute([ ':cas_login' => $casLogin ]);
/* (4) Dispatch execution error */
return $success;
}
/* (5) Check if a link exists
*
* @casLogin<String> The professor's cas login
* @idDep<int> The department id
*
* @return exists<bool> Whether the professor exists
*
---------------------------------------------------------*/
public function link_exists(String $casLogin, int $idDep) : bool{
/* (1) Prepare Statement */
$st = $this->pdo->prepare("SELECT *
FROM meta_vhost.linkedDep
WHERE departement_iddepartement = :id_dep
AND casUser_casLogin = :cas_login");
/* (2) Statement eror */
if( is_bool($st) )
return FALSE;
/* (3) Bind params and execute */
$success = $st->execute([
':id_dep' => $idDep,
':cas_login' => $casLogin
]);
/* (4) Manage execution error */
if( !$success )
return FALSE;
/* (7) Return That it exists */
return is_array( $st->fetch() );
}
/* (6) Associates a professor to a department
*
* @casLogin<String> The professor's CAS username
* @idDep<int> The department id
*
* @return linked<bool> Whether the link have been created
*
---------------------------------------------------------*/
public function link(String $casLogin, int $idDep) : bool{
/* (1) Prepare statement */
$st = $this->pdo->prepare("INSERT INTO meta_vhost.linkedDep(departement_iddepartement, casUser_casLogin)
VALUES(:id_dep, :cas_login);");
/* (2) Manage statement error */
if( is_bool($st) )
return FALSE;
/* (3) Try to execute request */
$success = $st->execute([
':id_dep' => $idDep,
':cas_login' => $casLogin
]);
/* (4) Dispatch execution error */
return $success;
}
/* (7) Remove a department from a professor
*
* @casLogin<String> The professor's CAS username
* @idDep<int> The department id
*
* @return unlinked<bool> Whether the link have been removed
*
---------------------------------------------------------*/
public function unlink(String $casLogin, int $idDep) : bool{
/* (1) Prepare statement */
$st = $this->pdo->prepare("DELETE FROM meta_vhost.linkedDep
WHERE departement_iddepartement = :id_dep
AND casUser_casLogin = :cas_login;");
/* (2) Manage statement error */
if( is_bool($st) )
return FALSE;
/* (3) Try to execute request */
$success = $st->execute([
':id_dep' => $idDep,
':cas_login' => $casLogin
]);
/* (4) Dispatch execution error */
return $success;
}
}

View File

@ -10,6 +10,7 @@ namespace database\repo;
use database\core\Repo_i; use database\core\Repo_i;
use database\core\Repo;
class professor extends Repo_i { class professor extends Repo_i {
@ -66,49 +67,29 @@ class professor extends Repo_i {
/* (2) Create user in meta database /* (2) Synchronize meta database
---------------------------------------------------------*/ ---------------------------------------------------------*/
/* (1) Try to insert user */ /** @var meta $meta_repo */
$st = $this->pdo->prepare("INSERT IGNORE INTO meta_vhost.casUser(casLogin, firstName, lastName) VALUE (:casLogin, :firstName, :lastName)"); $meta_repo = Repo::getRepo('meta');
/* (2) Manage statement error */ /* (1) If user does not exist */
if( !is_bool($st) ){ if( !$meta_repo->prof_exists($casLogin) ){
/* (3) Try to execute */ /* (2) Try to create -> dispatch error */
$success = $st->execute([ if( !$meta_repo->create_prof($casLogin, $firstName, $lastName) )
':casLogin' => $casLogin, return NULL;
':firstName' => $firstName,
':lastName' => $lastName
]);
/* (4) Manage execution error */
// if( !$success )
} }
/* (3) If link already exists -> done */
if( $meta_repo->link_exists($casLogin, $_SESSION['CurrentDepartmentId']) )
return $id_prof;
/* (3) Add user to department in meta database /* (4) Else: try to link prof to dep -> dispatch error */
---------------------------------------------------------*/ if( !$meta_repo->link($casLogin, $_SESSION['CurrentDepartmentId']) )
/* (1) Try to insert user */ return NULL;
$st = $this->pdo->prepare("INSERT INTO meta_vhost.linkedDep(departement_iddepartement, casUser_casLogin) VALUE (:idDep, :casLogin)");
/* (2) Manage statement error */ /* (5) If reached here -> Success */
if( !is_bool($st) ){
/* (3) Try to execute */
$success = $st->execute([
':idDep' => $_SESSION['CurrentDepartmentId'],
':casLogin' => $casLogin
]);
/* (4) Manage error */
// if( !$success )
}
/* (5) Return inserted ID */
return $id_prof; return $id_prof;
} }
@ -217,7 +198,27 @@ 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{ 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) Check professor data
---------------------------------------------------------*/
/* (1) Try to fetch professor's data */
$prof = $this->get($id);
/* (2) Error: no professor found */
if( count($prof) === 0 )
return FALSE;
/* (3) Take first matching professor */
$prof = $prof[0];
/* (4) Extract old CAS login */
$oldCasLogin = $prof['casLogin'];
/* (2) Update current database
---------------------------------------------------------*/ ---------------------------------------------------------*/
/* (1) Build request */ /* (1) Build request */
$build_rq = []; $build_rq = [];
@ -254,77 +255,65 @@ class professor extends Repo_i {
/* (2) Check existence in meta database /** @var meta $meta_repo */
$meta_repo = Repo::getRepo('meta');
/* (3) Synchronize meta database -> remove old value
---------------------------------------------------------*/ ---------------------------------------------------------*/
/* (1) Try to fetch professor's data */ if( !is_null($oldCasLogin) && strlen($oldCasLogin) > 0 ){
$prof = $this->get($id);
/* (2) Error: no professor found */ /* Proccess only if professor exists */
if( count($prof) === 0 ) if( $meta_repo->prof_exists($oldCasLogin) ){
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 /* (1) If link exists -> remove it */
---------------------------------------------------------*/ if( $meta_repo->link_exists($oldCasLogin, $_SESSION['CurrentDepartmentId']) ){
if( !$exists ){
/* (1) Prepare statement */ /* (2) Try to unlink-> ignore error */
$st = $this->pdo->prepare("INSERT INTO meta_vhost.casUser(casLogin, firstName, lastName) $meta_repo->unlink($oldCasLogin, $_SESSION['CurrentDepartmentId']);
VALUE(:casLogin, :firstName, :lastName)");
/* (2) Manage statement error */ }
if( is_bool($st) )
return TRUE;
/* (3) Try to execute */ /* (3) If has no more department -> remove professor */
$st->execute([ if( count( $meta_repo->get_prof_departments($oldCasLogin) ) == 0 ){
':firstName' => $prof[0]['firstName'],
':lastName' => $prof[0]['lastName'],
':casLogin' => $prof[0]['casLogin']
]);
return TRUE; /* (4) Try to remove professor -> dispatch error */
if( !$meta_repo->delete_prof($oldCasLogin) )
return FALSE;
}
}
} }
/* (4) Synchronize meta database -> create new
/* (4) If exists: Update professor in meta database
---------------------------------------------------------*/ ---------------------------------------------------------*/
/* (1) Prepare statement */ if( !is_null($casLogin) && strlen($casLogin) > 0 ){
$st = $this->pdo->prepare("UPDATE meta_vhost.casUser
SET casLogin = :casLogin,
firstName = :firstName,
lastName = :lastName ");
/* (2) Manage statement error */ /* (1) If user does not exist */
if( is_bool($st) ) if( !$meta_repo->prof_exists($casLogin) ){
return TRUE;
/* (3) Try to execute */ /* (2) Try to create -> dispatch error */
$st->execute([ if( !$meta_repo->create_prof($casLogin, $prof['firstName'], $prof['lastName']) )
':firstName' => $prof[0]['firstName'], return FALSE;
':lastName' => $prof[0]['lastName'],
':casLogin' => $prof[0]['casLogin']
]);
}
/* (3) If link already exists -> done */
if( $meta_repo->link_exists($casLogin, $_SESSION['CurrentDepartmentId']) )
return TRUE;
/* (4) Else: try to link prof to dep -> dispatch error */
if( !$meta_repo->link($casLogin, $_SESSION['CurrentDepartmentId']) )
return FALSE;
}
/* (5) If reached here -> Success */
return TRUE; return TRUE;
} }
@ -543,6 +532,10 @@ class professor extends Repo_i {
---------------------------------------------------------*/ ---------------------------------------------------------*/
public function delete(int $id) : bool{ public function delete(int $id) : bool{
/** @var meta $meta_repo */
$meta_repo = Repo::getRepo('meta');
/* (1) Try to fetch professor data /* (1) Try to fetch professor data
---------------------------------------------------------*/ ---------------------------------------------------------*/
/* (1) Try to fetch */ /* (1) Try to fetch */
@ -552,6 +545,14 @@ class professor extends Repo_i {
if( count($prof) === 0 ) if( count($prof) === 0 )
return false; return false;
/* (3) Take first matching professor */
$prof = $prof[0];
/* (4) Extract @casLogin */
$casLogin = $prof['casLogin'];
/* (2) Remove professor from current database /* (2) Remove professor from current database
@ -570,31 +571,38 @@ class professor extends Repo_i {
if( !$success ) if( !$success )
return false; return false;
/* (5) Success if no @casLogin to update in meta */
if( !$meta_repo->prof_exists($casLogin) )
return TRUE;
/* (3) Remove professor from meta database
/* (3) Synchronize meta database
---------------------------------------------------------*/ ---------------------------------------------------------*/
/* (1) Prepare statement */ /* Proccess only if professor exists */
$st = $this->pdo->prepare('DELETE FROM meta_vhost.linkedDep WHERE casUser_casLogin = :casLogin AND departement_iddepartement = :idDep'); if( $meta_repo->prof_exists($casLogin) ){
/* (2) Manage statement error */ /* (1) If link exists -> remove it */
if( is_bool($st) ) if( $meta_repo->link_exists($casLogin, $_SESSION['CurrentDepartmentId']) ){
return true;
/* (3) Try to execute */ /* (2) Try to unlink-> ignore error */
$success = $st->execute([ $meta_repo->unlink($casLogin, $_SESSION['CurrentDepartmentId']);
':casLogin' => $prof[0]['casLogin'],
':idDep' => $_SESSION['CurrentDepartmentId']
]);
/* (4) Manage execution error */ }
if( !$success )
return true;
/* (3) If has no more department -> remove professor */
if( count( $meta_repo->get_prof_departments($casLogin) ) == 0 ){
/* (4) Try to remove professor -> dispatch error */
if( !$meta_repo->delete_prof($casLogin) )
return FALSE;
}
return true; }
return TRUE;
} }