From 12b1a29ed0f08d3e6b6932dddfebc58d207f7ba6 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Tue, 20 Mar 2018 18:44:43 +0100 Subject: [PATCH] [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] --- build/api/module/casController.php | 5 +- build/api/module/departmentController.php | 14 +- build/database/repo/meta.php | 257 ++++++++++++++++++++++ build/database/repo/professor.php | 220 +++++++++--------- 4 files changed, 382 insertions(+), 114 deletions(-) create mode 100644 build/database/repo/meta.php diff --git a/build/api/module/casController.php b/build/api/module/casController.php index 58c4b70..5ceb96b 100644 --- a/build/api/module/casController.php +++ b/build/api/module/casController.php @@ -11,6 +11,7 @@ namespace api\module; use database\core\Repo; use database\repo\professor; +use database\repo\meta; use error\core\Error; use error\core\Err; @@ -110,9 +111,11 @@ class casController{ ---------------------------------------------------------*/ /** @var professor $prof_repo */ $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 */ - $departments = $prof_repo->getLinkedDepartments($cas_login); + $departments = $meta_repo->get_prof_departments($cas_login); /* (2) Failure: if no department for @cas_login */ if( count($departments) === 0 ) diff --git a/build/api/module/departmentController.php b/build/api/module/departmentController.php index d4a103e..d27b2d2 100644 --- a/build/api/module/departmentController.php +++ b/build/api/module/departmentController.php @@ -10,7 +10,7 @@ namespace api\module; use database\core\Repo; -use database\repo\professor; +use database\repo\meta; class departmentController { @@ -26,11 +26,11 @@ class departmentController $id_dep = null; extract($args); - /** @var professor $prof_repo */ - $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 */ - $departments = $prof_repo->getLinkedDepartments($_SESSION['CAS']['login']); + $departments = $meta_repo->get_prof_departments($_SESSION['CAS']['login']); /* (2) If no @id_dep -> return all */ if( is_null($id_dep) ) @@ -62,12 +62,12 @@ class departmentController extract($args); - /** @var professor $prof_repo */ - $prof_repo = Repo::getRepo('professor'); + /** @var meta $meta_repo */ + $meta_repo = Repo::getRepo('meta'); - $deps = $prof_repo->getLinkedDepartments($_SESSION['CAS']['login']); + $deps = $meta_repo->get_prof_departments($_SESSION['CAS']['login']); diff --git a/build/database/repo/meta.php b/build/database/repo/meta.php new file mode 100644 index 0000000..01fedc4 --- /dev/null +++ b/build/database/repo/meta.php @@ -0,0 +1,257 @@ + The professor's cas login + * @firstName [OPT] The professor's firstName + * @lastName [OPT] The professor's lastName + * + * @return created 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 The professor's cas login + * + * @return exists 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 The professor's CAS username + * + * @return departments 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 The professor's CAS username + * + * @return deleted 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 The professor's cas login + * @idDep The department id + * + * @return exists 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 The professor's CAS username + * @idDep The department id + * + * @return linked 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 The professor's CAS username + * @idDep The department id + * + * @return unlinked 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; + + } + +} \ No newline at end of file diff --git a/build/database/repo/professor.php b/build/database/repo/professor.php index 2d1c340..2c07090 100644 --- a/build/database/repo/professor.php +++ b/build/database/repo/professor.php @@ -10,6 +10,7 @@ namespace database\repo; use database\core\Repo_i; +use database\core\Repo; 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 */ - $st = $this->pdo->prepare("INSERT IGNORE INTO meta_vhost.casUser(casLogin, firstName, lastName) VALUE (:casLogin, :firstName, :lastName)"); + /** @var meta $meta_repo */ + $meta_repo = Repo::getRepo('meta'); - /* (2) Manage statement error */ - if( !is_bool($st) ){ + /* (1) If user does not exist */ + if( !$meta_repo->prof_exists($casLogin) ){ - /* (3) Try to execute */ - $success = $st->execute([ - ':casLogin' => $casLogin, - ':firstName' => $firstName, - ':lastName' => $lastName - ]); - - /* (4) Manage execution error */ - // if( !$success ) + /* (2) Try to create -> dispatch error */ + if( !$meta_repo->create_prof($casLogin, $firstName, $lastName) ) + return NULL; } + /* (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 - ---------------------------------------------------------*/ - /* (1) Try to insert user */ - $st = $this->pdo->prepare("INSERT INTO meta_vhost.linkedDep(departement_iddepartement, casUser_casLogin) VALUE (:idDep, :casLogin)"); + /* (4) Else: try to link prof to dep -> dispatch error */ + if( !$meta_repo->link($casLogin, $_SESSION['CurrentDepartmentId']) ) + return NULL; - /* (2) Manage statement error */ - 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 */ + /* (5) If reached here -> Success */ 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{ - /* (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 */ $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 */ - $prof = $this->get($id); + if( !is_null($oldCasLogin) && strlen($oldCasLogin) > 0 ){ - /* (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() ); + /* Proccess only if professor exists */ + if( $meta_repo->prof_exists($oldCasLogin) ){ - /* (3) Does not exist: Create professor in meta database - ---------------------------------------------------------*/ - if( !$exists ){ + /* (1) If link exists -> remove it */ + if( $meta_repo->link_exists($oldCasLogin, $_SESSION['CurrentDepartmentId']) ){ - /* (1) Prepare statement */ - $st = $this->pdo->prepare("INSERT INTO meta_vhost.casUser(casLogin, firstName, lastName) - VALUE(:casLogin, :firstName, :lastName)"); + /* (2) Try to unlink-> ignore error */ + $meta_repo->unlink($oldCasLogin, $_SESSION['CurrentDepartmentId']); - /* (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'] - ]); + /* (3) If has no more department -> remove professor */ + if( count( $meta_repo->get_prof_departments($oldCasLogin) ) == 0 ){ - return TRUE; + /* (4) Try to remove professor -> dispatch error */ + if( !$meta_repo->delete_prof($oldCasLogin) ) + return FALSE; + + } + + } } - - /* (4) If exists: Update professor in meta database + /* (4) Synchronize meta database -> create new ---------------------------------------------------------*/ - /* (1) Prepare statement */ - $st = $this->pdo->prepare("UPDATE meta_vhost.casUser - SET casLogin = :casLogin, - firstName = :firstName, - lastName = :lastName "); + if( !is_null($casLogin) && strlen($casLogin) > 0 ){ - /* (2) Manage statement error */ - if( is_bool($st) ) - return TRUE; + /* (1) If user does not exist */ + if( !$meta_repo->prof_exists($casLogin) ){ - /* (3) Try to execute */ - $st->execute([ - ':firstName' => $prof[0]['firstName'], - ':lastName' => $prof[0]['lastName'], - ':casLogin' => $prof[0]['casLogin'] - ]); + /* (2) Try to create -> dispatch error */ + if( !$meta_repo->create_prof($casLogin, $prof['firstName'], $prof['lastName']) ) + return FALSE; + } + + /* (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; } @@ -543,6 +532,10 @@ class professor extends Repo_i { ---------------------------------------------------------*/ 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 */ @@ -552,6 +545,14 @@ class professor extends Repo_i { if( count($prof) === 0 ) return false; + /* (3) Take first matching professor */ + $prof = $prof[0]; + + /* (4) Extract @casLogin */ + $casLogin = $prof['casLogin']; + + + /* (2) Remove professor from current database @@ -570,31 +571,38 @@ class professor extends Repo_i { if( !$success ) 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 */ - $st = $this->pdo->prepare('DELETE FROM meta_vhost.linkedDep WHERE casUser_casLogin = :casLogin AND departement_iddepartement = :idDep'); + /* Proccess only if professor exists */ + if( $meta_repo->prof_exists($casLogin) ){ - /* (2) Manage statement error */ - if( is_bool($st) ) - return true; + /* (1) If link exists -> remove it */ + if( $meta_repo->link_exists($casLogin, $_SESSION['CurrentDepartmentId']) ){ - /* (3) Try to execute */ - $success = $st->execute([ - ':casLogin' => $prof[0]['casLogin'], - ':idDep' => $_SESSION['CurrentDepartmentId'] - ]); + /* (2) Try to unlink-> ignore error */ + $meta_repo->unlink($casLogin, $_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; }