257 lines
5.8 KiB
PHP
257 lines
5.8 KiB
PHP
|
<?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;
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|