[repo.professor] fix CREATE sync with meta database

This commit is contained in:
xdrm-brackets 2018-03-20 10:49:12 +01:00
parent 67e0f9eba2
commit 35b0cb36fa
1 changed files with 63 additions and 22 deletions

View File

@ -29,12 +29,19 @@ class professor extends Repo_i {
---------------------------------------------------------*/
public function create(string $lastName, string $firstName, int $category, int $hoursToDo = 0, ?string $initials = null , bool $isAdmin = false , ?string $casLogin = null ) : ?int{
/* (1) Create professor into local database
---------------------------------------------------------*/
/* (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 */
/* (2) Manage statement error */
if( is_bool($st) )
return NULL;
/* (3) Bind params and execute */
$success = $st->execute([
':casLogin' => $casLogin,
':lastName' => $lastName,
@ -45,30 +52,64 @@ class professor extends Repo_i {
':cat' => $category
]);
$profId = $this->pdo->lastInsertId();
/* (3) synchroize the meta database */
if(!is_null($casLogin)){
$st = $this->pdo->prepare("INSERT IGNORE INTO meta_vhost.casUser(casLogin, firstName, lastName) VALUE (:casLogin,:firstName,:lastName)");
$st->execute([
"casLogin" => $casLogin,
"firstName" => $firstName,
"lastName" => $lastName
]);
$st = $this->pdo->prepare("INSERT INTO meta_vhost.linkedDep(departement_iddepartement, casUser_casLogin) VALUE (:idDep,:casLogin)");
$st->execute([
"idDep" => $_SESSION['CurrentDepartementId'],
"casLogin" => $casLogin
]);
}
/* (3) Manage error */
/* (4) If execution error -> dispatch */
if( !$success )
return NULL;
/* (4) Return inserted ID */
return $profId;
/* (5) Store id */
$id_prof = $this->pdo->lastInsertId();
/* (6) Exit now if no meta database to udpate */
if( is_null($casLogin) )
return $id_prof;
/* (2) Create user in meta database
---------------------------------------------------------*/
/* (1) Try to insert user */
$st = $this->pdo->prepare("INSERT IGNORE INTO meta_vhost.casUser(casLogin, firstName, lastName) VALUE (:casLogin, :firstName, :lastName)");
/* (2) Manage statement error */
if( !is_bool($st) ){
/* (3) Try to execute */
$success = $st->execute([
':casLogin' => $casLogin,
':firstName' => $firstName,
':lastName' => $lastName
]);
/* (4) Manage execution error */
// if( !$success )
}
/* (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)");
/* (2) Manage statement error */
if( !is_bool($st) ){
/* (3) Try to execute */
$success = $st->execute([
':idDep' => $_SESSION['CurrentDepartementId'],
':casLogin' => $casLogin
]);
/* (4) Manage error */
// if( !$success )
}
/* (5) Return inserted ID */
return $id_prof;
}