Refactor Repo + Fix TD Equivalent

This commit is contained in:
Unknown 2018-02-27 20:41:36 +01:00
parent fd9357fdba
commit c0bb4541a7
6 changed files with 73 additions and 228 deletions

View File

@ -11,6 +11,12 @@ namespace api\module;
use database\core\Repo;
use database\core\Repo_i;
use database\repo\category;
use database\repo\cours;
use database\repo\professor;
use database\repo\td;
use database\repo\tp;
use database\repo\ue;
use error\core\Err;
use error\core\Error;
use PhpOffice\PhpSpreadsheet\Exception;
@ -254,31 +260,54 @@ class Excel
*/
//professors
/** @var professor $profRepo */
$profRepo = Repo::getRepo("professor");
/** @var category $catRepo */
$catRepo = Repo::getRepo("category");
foreach ($allProf as $initials => &$prof){
//create or update the professor category
Repo::request("category", "createOrUpdate", $prof["categoryIndex"], utf8_decode($prof["categoryLabel"]));
$catRepo->createOrUpdate($prof["categoryIndex"], utf8_decode($prof["categoryLabel"]));
//create the professor, as some names are missing, we replace them by something else
if(!$prof["lastName"]) $prof["lastName"] = "missingLastName";
if(!$prof["firstName"]) $prof["firstName"] = "missingFirstName";
$prof["dbId"] = Repo::request("professor", "exists", utf8_decode($prof["lastName"]), utf8_decode($prof["firstName"]));
$prof["dbId"] = $profRepo->exists(utf8_decode($prof["lastName"]), utf8_decode($prof["firstName"]));
if(!$prof["dbId"]){
$prof["dbId"] = Repo::request("professor", "create", utf8_decode($prof["lastName"]), utf8_decode($prof["firstName"]), $prof["categoryIndex"], $prof["hoursToDo"], $initials);
$prof["dbId"] = $profRepo->create( utf8_decode($prof["lastName"]), utf8_decode($prof["firstName"]), $prof["categoryIndex"], $prof["hoursToDo"], $initials);
}
}
//formation and retreive the databse IDs
/** @var \database\repo\formation $formRepo */
$formRepo = Repo::getRepo("formation");
foreach ($allFormations as &$form){
$form["dbId"] = Repo::request("formation","exists", utf8_decode($form["name"]));
$form["dbId"] = $formRepo->exists( utf8_decode($form["name"]));
if(!$form["dbId"]){
$form["dbId"] = Repo::request("formation", "create", utf8_decode($form["name"]), $form["internal"]);
$form["dbId"] = $formRepo->create( utf8_decode($form["name"]), $form["internal"]);
}
}
//UEs and corresponding groups
/** @var ue $ueRepo */
$ueRepo = Repo::getRepo("ue");
/** @var cours $coursRepo */
$coursRepo = Repo::getRepo("cours");
/** @var td $tdRepo */
$tdRepo = Repo::getRepo("td");
/** @var tp $tpRepo */
$tpRepo = Repo::getRepo("tp");
foreach ($allUE as $codeUE => $UE){
Repo::request("ue","create", utf8_decode($codeUE),
$ueRepo->create(utf8_decode($codeUE),
utf8_decode($UE["name"]),
$UE["required"] == "OBL",
$UE["CourseVH"],
@ -296,19 +325,19 @@ class Excel
switch ($type){
case "Course":
Repo::request("cours","create", utf8_decode($codeUE),
$coursRepo->create( utf8_decode($codeUE),
$allProf[$group["professor"]]["dbId"],
$group["VH"],
$formations);
break;
case "TD":
Repo::request("td","create", utf8_decode($codeUE),
$tdRepo->create(utf8_decode($codeUE),
$allProf[$group["professor"]]["dbId"],
$group["VH"],
$formations);
break;
case "TP":
Repo::request("tp","create", utf8_decode($codeUE),
$tpRepo->create(utf8_decode($codeUE),
$allProf[$group["professor"]]["dbId"],
$group["VH"],
$formations);

View File

@ -10,6 +10,7 @@ namespace api\module\Professor;
use database\core\Repo;
use database\repo\professor;
class Stats
{
@ -18,7 +19,10 @@ class Stats
extract($args);
//get data from the database
$VH = Repo::request("professor", "getVH", $idProf);
/** @var professor $profRepo */
$profRepo = Repo::getRepo("professor");
$VH = $profRepo->getVH($idProf);
if(in_array($VH["idCat"],[1,3])){
$VH["equiTD"] = $VH["VHTd"] + $VH["VHTp"] + 1.5*$VH["VHCours"];

View File

@ -1,142 +0,0 @@
<?php
namespace api\module;
use \error\core\Error;
use \error\core\Err;
use \database\core\Repo;
class admin{
/* (1) Return an admin data
*
* @id_admin<id> [OPT] UID de l'administrateur
*
* @return data<Array> Administrateurs correspondants
*
---------------------------------------------------------*/
public static function get($args){
$id_admin = 0;
extract($args);
/* (1) If @id_admin is set -> get by id
---------------------------------------------------------*/
if( is_numeric($id_admin) ){
/* (1) Search admin by id */
$fetch_admin = Repo::request('admin', 'getById', $id_admin);
/* (2) If not found -> return empty data */
if( !$fetch_admin )
return [ 'data' => [] ];
/* (3) Return fetched admin */
return [ 'data' => [$fetch_admin] ];
/* (2) Else -> get all
---------------------------------------------------------*/
}else
return [ 'data' => Repo::request('admin', 'getAll') ];
}
/* (2) Creates a new administrator
*
* @username<string> Identifiant de l'administrateur
* @mail<string> Adresse mail de l'administrateur
* @password<string> Mot de passe de l'administrateur
*
* @return admin<array> Données de l'administrateur crée
*
---------------------------------------------------------*/
public static function post($args){
$username = "";
$mail = "";
$password = "";
extract($args);
/* (1) Création admin */
$id_created = Repo::request('admin', 'create', $username, $mail, $password);
/* (2) Gestion erreur */
if( $id_created === false )
return [ 'error' => new Error(Err::RepoError) ];
/* (3) Renvoi @id_admin */
return [ 'id_admin' => Repo::request('admin', 'getById', $id_created) ];
}
/* (3) Updates an existing administrator
*
* @id_admin<id> UID de l'administrateur
* @mail<string> [OPT] Adresse mail de l'administrateur
* @password<string> [OPT] Mot de passe de l'administrateur
*
* @return admin<array> The new administrator data
*
---------------------------------------------------------*/
public static function put($args){
//helps the static analysis
$mail = null;
$password = null;
$id_admin = 0;
extract($args);
/* (1) If @mail given
---------------------------------------------------------*/
if( !is_null($mail) ){
/* (1) Update mail address */
$updated = Repo::request('admin', 'setMail', $id_admin, $mail);
/* (2) Gestion erreur */
if( $updated === false )
return [ 'error' => new Error(Err::RepoError) ];
}
/* (2) If @password given
---------------------------------------------------------*/
if( !is_null($password) ){
/* (1) Update password */
$updated = Repo::request('admin', 'setPassword', $id_admin, $password);
/* (2) Gestion erreur */
if( $updated === false )
return [ 'error' => new Error(Err::RepoError) ];
}
/* (3) Renvoi @id_admin */
return [ 'id_admin' => Repo::request('admin', 'getById', $id_admin) ];
}
/* (4) Deletes an existing administrator
*
* @id_admin<id> UID de l'administrateur
*
* @return removed<bool> Whether the admin has been removed
*
---------------------------------------------------------*/
public static function delete($args){
$id_admin = 0;
extract($args);
/* (1) Dispatch du status */
return [ 'removed' => Repo::request('admin', 'delete', $id_admin) ];
}
}

View File

@ -26,7 +26,7 @@
public static function request(String $repo=null, String $method=null){
public static function getRepo(String $repo=null) : Repo_i{
/* (1) Check arguments
---------------------------------------------------------*/
@ -34,11 +34,7 @@
if( !is_string($repo) || strlen($repo) < 1 )
throw new \Exception("@repo is not a non-empty string");
/* (2) Check @method */
if( !is_string($method) || strlen($method) < 1 )
throw new \Exception("@method is not a non-empty string");
/* (3) Check class path */
/* (1) Check class path */
$class_path = "\\database\\repo\\$repo";
if( !\class_exists($class_path) )
@ -58,19 +54,7 @@
\call_user_func([$instance, 'setPDO'], self::$driver->pdo());
/* (3) Check if the method exists */
if( !\method_exists($instance, $method) )
throw new \Exception("Repo '$repo' has no public method '$method'");
/* (4) Fetch response (send arguments as well) */
$response = call_user_func_array([$instance, $method], array_slice(func_get_args(), 2));
/* (5) Call post-script */
$instance = null;
/* (6) Dispatch response */
return $response;
return $instance;
}

View File

@ -89,9 +89,9 @@ class professor extends Repo_i {
public function getVH(int $id) : array{
$st = $this->pdo->prepare("SELECT VHCours, VHTd, VHTp, Cat.idCategorie idCat, Prof.hoursToDo du
FROM Professeur Prof, Categorie Cat,
(SELECT SUM(C.volume) VHCours , Prof.idProfesseur idProf FROM Cours C, Professeur Prof WHERE C.Professeur_idProfesseur = Prof.idProfesseur GROUP BY Prof.idProfesseur) VHCours,
(SELECT SUM(T.volume) VHTd , Prof.idProfesseur idProf FROM TD T, Professeur Prof WHERE T.Professeur_idProfesseur = Prof.idProfesseur GROUP BY Prof.idProfesseur) VHTd,
(SELECT SUM(T2.volume) VHTp, Prof.idProfesseur idProf FROM TP T2 , Professeur Prof WHERE T2.Professeur_idProfesseur = Prof.idProfesseur GROUP BY Prof.idProfesseur) VHTp
(SELECT IFNULL(SUM(Cours.volume),0) VHCours, Prof.idProfesseur idProf FROM Professeur Prof LEFT JOIN Cours ON Prof.idProfesseur = Cours.Professeur_idProfesseur GROUP BY Prof.idProfesseur) VHCours,
(SELECT IFNULL(SUM(TD.volume),0) VHTd , Prof.idProfesseur idProf FROM Professeur Prof LEFT JOIN TD ON TD.Professeur_idProfesseur = Prof.idProfesseur GROUP BY Prof.idProfesseur) VHTd,
(SELECT IFNULL(SUM(TP.volume),0) VHTp, Prof.idProfesseur idProf FROM Professeur Prof LEFT JOIN TP ON TP.Professeur_idProfesseur = Prof.idProfesseur GROUP BY Prof.idProfesseur) VHTp
WHERE
Prof.idProfesseur = :idProf AND
Prof.Categorie_idCategorie = Cat.idCategorie AND

View File

@ -7,46 +7,6 @@
}
},
"admin": {
"POST": {
"des": "Creates a new administrator",
"per": [["admin"]],
"par": {
"username": { "des": "The new administrator username", "typ": "varchar(3,20,alphanumeric)" },
"mail": { "des": "The new administrator email address", "typ": "mail" },
"password": { "des": "The new administrator passowrd", "typ": "text" }
}
},
"PUT": {
"des": "Updates an existing administrator's data",
"per": [["admin"]],
"par": {
"URL0": { "des": "The UID of the wanted administrator.", "typ": "id", "ren": "id_admin" },
"mail": { "des": "The new administrator email address", "typ": "mail", "opt": true },
"password": { "des": "The new administrator passowrd", "typ": "text", "opt": true }
}
},
"DELETE": {
"des": "Deletes an administrator",
"per": [["admin"]],
"par": {
"URL0": { "des": "The UID of the wanted administrator.", "typ": "id", "opt": true, "ren": "id_admin" }
}
},
"GET": {
"des": "Gets an administrator | Gets all administrators if no id defined",
"per": [["admin"]],
"par": {
"URL0": { "des": "The UID of the wanted administrator.", "typ": "id", "opt": true, "ren": "id_admin" }
}
}
},
"release": {
"GET": {
@ -82,6 +42,16 @@
}
},
"Formation": {
"GET":{
"des": "Get all data about a formation",
"per": [],
"par": {
"URL0":{"des" : "Id of the formation", "typ": "id", "ren": "idForm", "opt" : false}
}
}
},
"a": {
"b": {