[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:
parent
e562bec77f
commit
12b1a29ed0
|
@ -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 )
|
||||
|
|
|
@ -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']);
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue