diff --git a/build/api/module/ue/tpController.php b/build/api/module/ue/tpController.php index c97bdf4..4fe66e1 100644 --- a/build/api/module/ue/tpController.php +++ b/build/api/module/ue/tpController.php @@ -11,7 +11,37 @@ use error\core\Err; class tpController{ - /* (1) Get groups for a specific UE + /* (1) Create a new TP + * + * @code UE code + * + * @return groups The list of groups for this UE + * + ---------------------------------------------------------*/ + public static function post($args){ + $code = ""; + $idProf = null; + $volume = 0; + $formations = []; + extract($args); + + /* Get the tp repo */ + /** @var tp $tp_repo */ + $tp_repo = Repo::getRepo('tp'); + + /* (1) Try to create tp */ + $created_id = $tp_repo->create($code, $idProf, $volume, $formations); + + /* (2) Manage error */ + if( is_null($created_id) || !is_int($created_id) ) + return ['error' => new Error(Err::RepoError)]; + + return ['created_id' => $created_id]; + + } + + + /* (2) Get groups for a specific UE * * @code UE code * @@ -43,4 +73,98 @@ class tpController{ } + /* (3) Updates an existing TP + * + * @idTP Id of the TP to update + * @idProf [OPT] The new professor ID (-1 to unset) + * @volume [OPT] The new number of hours for the TP + * @add_form List of ids of formations to add + * @rem_form List of ids of formations to remove + * + * @return updated Whether the TP have been updated + * + ---------------------------------------------------------*/ + public static function put($args){ + $idTP = -1; + $idProf = null; + $volume = null; + $add_form = []; + $rem_form = []; + extract($args); + + /* Get the tp repo */ + /** @var tp $tp_repo */ + $tp_repo = Repo::getRepo('tp'); + + /* (1) If nothing to do -> error */ + if( is_null($idProf) && is_null($volume) && count($add_form) + count($rem_form) == 0) + return ['error' => new Error(Err::MissingParam)]; + + /* (2) If have to update @idProf */ + if( !is_null($idProf) ){ + + // call repo + $updated = $tp_repo->updateProf($idTP, $idProf < 0 ? NULL : $idProf ); + + // if error -> dispatch + if( !$updated ) + return ['error' => new Error(Err::RepoError)]; + + } + + /* (3) If have to update @volume */ + if( !is_null($volume) ){ + + // call repo + $updated = $tp_repo->updateVolume($idTP, $volume); + + // if error -> dispatch + if( !$updated ) + return ['error' => new Error(Err::RepoError)]; + + } + + /* (4) If have to add formations -> add them */ + if( count($add_form) > 0 ){ + + foreach($add_form as $id_form) + $tp_repo->linkFormation($id_form, $idTP); + + } + + /* (5) If have to remove formations -> remove them */ + if( count($rem_form) > 0 ){ + + foreach($rem_form as $id_form) + $tp_repo->unlinkFormation($id_form, $idTP); + + } + + + return ['updated' => true]; + + } + + + /* (4) Deletes an existing TP + * + * @idTP Id of the TP + * + * @return deleted Whether it has been deleted + * + ---------------------------------------------------------*/ + public static function delete($args){ + $idTP = 0; + extract($args); + + /* Get the tp repo */ + /** @var tp $tp_repo */ + $tp_repo = Repo::getRepo('tp'); + + /* (1) Dispatch response from repo */ + return ['deleted' => $tp_repo->delete($idTP)]; + + } + + } \ No newline at end of file diff --git a/build/database/repo/tp.php b/build/database/repo/tp.php index e50a5c9..1e9ad18 100644 --- a/build/database/repo/tp.php +++ b/build/database/repo/tp.php @@ -13,75 +13,191 @@ use database\core\Repo_i; class tp extends Repo_i { - public function create(string $codeUE, ?int $idProf, float $volume, array $formations) : int{ - //create the group - $st = $this->pdo->prepare("INSERT INTO TP(UE_code, Professeur_idProfesseur, volume) - VALUE(:UE, :idProf, :vol)"); - $st->execute([ - "UE" => $codeUE, - "idProf" => $idProf, - "vol" => $volume + /* (1) Create a new TP + * + * @code The code of the UE containing the TP + * @idProf [OPT] The ID of the prof who teaches the TP + * @volume The number of hours (float) + * @formations The list of formations for that TP (list of int) + * @return created_code Code of the created UE (NULL on error) + * + * @return created_id UID of the created TP (NULL on error) + * + ---------------------------------------------------------*/ + public function create(string $code, ?int $idProf, float $volume, array $formations) : int{ + + /* (1) Prepare statement */ + $st = $this->pdo->prepare('INSERT INTO TP(UE_code, Professeur_idProfesseur, volume) + VALUE(:UE, :idProf, :vol)'); + + /* (2) Manage statement error */ + if( is_bool($st) ) + return NULL; + + /* (3) Try to execute request */ + $success = $st->execute([ + ':UE' => $code, + ':idProf' => $idProf, + ':vol' => $volume ]); - $idTP = $this->pdo->lastInsertId(); + /* (4) Manage error */ + if( !$success ) + return NULL; - //if there is formations, link them with the group - if(count($formations) > 0){ - $linkSt = $this->pdo->prepare("INSERT INTO GroupeTP(Formation_idFormation, TP_idTP) - VALUE (:form, :TP)"); - foreach ($formations as $form){ - $linkSt->execute([ - "form" => $form, - "TP" => $idTP - ]); - } + /* (5) Store @created_id */ + $created_id = (int) $this->pdo->lastInsertId(); + + + /* (6) We are done if there is no formations */ + if( count($formations) <= 0) + return $created_id; + + /* (7) Else -> create each formation */ + foreach($formations as $form_id){ + + // 1. Ignore if wrong format + if( !is_int($form_id) || $form_id < 0 ) + continue; + + // 2. Link formation to created TP + $this->linkFormation($form_id, $created_id); } - return $idTP; + /* (7) Return @created_id */ + return $created_id; } + + /* (2) Unlink a formation from a TP + * + * @idFormation Id of the formation + * @idTP Id of the TP + * + * @return unlinked Whether it has been unlinked + * + ---------------------------------------------------------*/ public function unlinkFormation(int $idFormation, int $idTP) : bool{ - $st = $this->pdo->prepare("DELETE FROM GroupeTP WHERE TP_idTP = :TP AND Formation_idFormation = :form"); + /* (1) Prepare statement */ + $st = $this->pdo->prepare('DELETE FROM GroupeTP WHERE TP_idTP = :tp AND Formation_idFormation = :form'); + + /* (2) Manage statement error */ + if( is_bool($st) ) + return false; + + /* (3) EXecute and dispatch status */ return $st->execute([ - "TP" => $idTP, - "form" => $idFormation + ':tp' => $idTP, + ':form' => $idFormation ]); + } + + /* (3) Link a formation to a TP + * + * @idFormation Id of the formation + * @idTP Id of the TP + * + * @return linked Whether it has been linked + * + ---------------------------------------------------------*/ public function linkFormation(int $idFormation, int $idTP) : bool{ - $st = $this->pdo->prepare("INSERT INTO GroupeTP(TP_idTP,Formation_idFormation) - VALUE(:TP, :form)"); + /* (1) Try to remove first if it already exists */ + $this->unlinkFormation($idFormation, $idTP); + + /* (2) Prepare statement */ + $st = $this->pdo->prepare('INSERT INTO GroupeTP(TP_idTP,Formation_idFormation) + VALUE(:tp, :form)'); + + /* (3) Manage statement error */ + if( is_bool($st) ) + return false; + + /* (4) EXecute and dispatch status */ return $st->execute([ - "TP" => $idTP, - "form" => $idFormation + ':tp' => $idTP, + ':form' => $idFormation ]); + } - public function updateProf(?int $prof) : bool { - $st = $this->pdo->prepare("UPDATE TP SET Professeur_idProfesseur = :prof"); - return $st->execute([ - "prof" => $prof - ]); + + + + /* (4.1) Updates an existing TP + * + * @idTP Id of the TP + * @idProf [OPT] Id of the prof (NULL to set to NULL) + * + * @return updated Whether it has been updated + * + ---------------------------------------------------------*/ + public function updateProf(int $idTP, ?int $idProf) : bool { + + /* (1) Prepare statement */ + $st = $this->pdo->prepare('UPDATE TP + SET Professeur_idProfesseur = :idProf + WHERE idTP = :idTP'); + + /* (2) Manage statement error */ + if( is_bool($st) ) + return false; + + /* (3) Execute and dispatch status */ + return $st->execute([ ':idTP' => $idTP, ':idProf' => $idProf ]); + } - public function updateVolume(float $volume) : bool { - $st = $this->pdo->prepare("UPDATE TP SET volume = :vol"); + /* (4.2) Updates an existing TP + * + * @idTP Id of the TP + * @volume [OPT] The new number of hours + * + * @return updated Whether it has been updated + * + ---------------------------------------------------------*/ + public function updateVolume(int $idTP, float $volume) : bool { + + /* (1) Prepare statement */ + $st = $this->pdo->prepare('UPDATE TP + SET volume = :volume + WHERE idTP = :idTP'); + + /* (2) Manage statement error */ + if( is_bool($st) ) + return false; + + /* (3) Execute and dispatch status */ + return $st->execute([ ':idTP' => $idTP, ':volume' => $volume ]); - return $st->execute([ - "vol" => $volume - ]); } + + + /* (5) Deletes an existing tp + * + * @idTP Id of the TP + * + * @return deleted Whether it has been deleted + * + ---------------------------------------------------------*/ public function delete(int $id) :bool { + + /* (1) Prepare statement */ $st = $this->pdo->prepare("DELETE FROM TP WHERE idTP = :id"); - return $st->execute([ - "id" => $id - ]); + /* (2) Manage statement error */ + if( is_bool($st) ) + return false; + + /* (3) Execute and dispatch status */ + return $st->execute([ ':id' => $id ]); + } @@ -97,21 +213,21 @@ class tp extends Repo_i { /* (1) Prepare statement */ $st = $this->pdo->prepare("SELECT - tp.UE_code code, - tp.idTP, + c.UE_code code, + c.idTP, p.idProfesseur idProf, p.firstName, p.lastName, IFNULL(lform.flist, '[]') formations, - tp.volume - FROM TP tp - LEFT JOIN ( SELECT gtp.TP_idTP idTP, CONCAT('[', GROUP_CONCAT(DISTINCT gtp.Formation_idFormation),']') flist - FROM GroupeTP gtp - GROUP BY gtp.TP_idTP - ORDER BY gtp.Formation_idFormation DESC - ) lform ON tp.idTP = lform.idTP - LEFT JOIN Professeur p ON p.idProfesseur = tp.Professeur_idProfesseur - WHERE tp.UE_code = :code;"); + c.volume + FROM TP c + LEFT JOIN ( SELECT gc.TP_idTP idTP, CONCAT('[', GROUP_CONCAT(DISTINCT gc.Formation_idFormation),']') flist + FROM GroupeTP gc + GROUP BY gc.TP_idTP + ORDER BY gc.Formation_idFormation DESC + ) lform ON c.idTP = lform.idTP + LEFT JOIN Professeur p ON p.idProfesseur = c.Professeur_idProfesseur + WHERE c.UE_code = :code;"); /* (2) Check statement */ if( is_bool($st) ) diff --git a/config/modules.json b/config/modules.json index df169f3..248ad2e 100644 --- a/config/modules.json +++ b/config/modules.json @@ -372,12 +372,52 @@ "tp": { + "POST": { + "des" : "Creates a new TP for an UE", + "per": [], + "par": { + "code": { "des": "Code of the UE", "typ": "varchar(4,20,alphanumeric)" }, + "idProf": { "des": "Id of the professor", "typ": "id", "opt": true }, + "volume": { "des": "Number of hours for TP", "typ": "id", "opt": true, "def": 0 }, + "formations": { "des": "List of formations (ids)", "typ": "array", "opt": true, "def": [] } + }, + "output": { + "created_id" : { "des": "The id of the created TP", "typ": "id" } + } + }, + "GET": { "des" : "Get all TP data about a given UE", "per": [], "par": { "URL0": { "des": "Code of the UE", "typ": "varchar(4,20,alphanumeric)", "ren": "code" } } + }, + + "PUT": { + "des" : "Updates an existing TP", + "per": [], + "par": { + "URL0": { "des": "Id of the TP", "typ": "id", "ren": "idTP" }, + "idProf": { "des": "Id of the professor (-1 to unset)", "typ": "int", "opt": true }, + "volume": { "des": "Number of hours for TP", "typ": "float", "opt": true }, + "add_form": { "des": "Id of formations to add", "typ": "array", "opt": true, "def": [] }, + "rem_form": { "des": "Id of formations to remove", "typ": "array", "opt": true, "def": [] } + }, + "output": { + "updated" : { "des": "Whether it has been updated", "typ": "bool" } + } + }, + + "DELETE": { + "des" : "Deletes an existing TP", + "per": [], + "par": { + "URL0": { "des": "Id of the TP", "typ": "id", "ren": "idTP" } + }, + "output": { + "deleted" : { "des": "Whether it has been deleted", "typ": "bool" } + } } }