From cec2c491bb0b6f2916222dead6d96f1df80c9437 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Wed, 14 Mar 2018 15:28:41 +0100 Subject: [PATCH] [module.ue.cours|td|tp] GET returns the groups [repo.cours|td|tp.getGroups] used by api --- build/api/module/ue/coursController.php | 46 +++++++++++++++++++++++++ build/api/module/ue/tdController.php | 46 +++++++++++++++++++++++++ build/api/module/ue/tpController.php | 46 +++++++++++++++++++++++++ build/database/repo/cours.php | 46 +++++++++++++++++++++++++ build/database/repo/td.php | 46 +++++++++++++++++++++++++ build/database/repo/tp.php | 46 +++++++++++++++++++++++++ config/modules.json | 37 +++++++++++++++++++- 7 files changed, 312 insertions(+), 1 deletion(-) create mode 100644 build/api/module/ue/coursController.php create mode 100644 build/api/module/ue/tdController.php create mode 100644 build/api/module/ue/tpController.php diff --git a/build/api/module/ue/coursController.php b/build/api/module/ue/coursController.php new file mode 100644 index 0000000..9ebdef5 --- /dev/null +++ b/build/api/module/ue/coursController.php @@ -0,0 +1,46 @@ + UE code + * + * @return groups The list of groups for this UE + * + ---------------------------------------------------------*/ + public static function get($args){ + $code = ""; + extract($args); + + /* Get the cours repo */ + /** @var cours $cours_repo */ + $cours_repo = Repo::getRepo('cours'); + + /* (1) Try to fetch data */ + $fetched = $cours_repo->getGroups($code); + + /* (2) Manage error */ + if( is_null($fetched) || !is_array($fetched) ) + return ['error' => new Error(Err::RepoError)]; + + /* (3) Parse JSON list */ + foreach($fetched as $f=>$v) + $fetched[$f]['formations'] = json_decode($v['formations']); + + + return ['groups' => $fetched]; + + } + + +} \ No newline at end of file diff --git a/build/api/module/ue/tdController.php b/build/api/module/ue/tdController.php new file mode 100644 index 0000000..1eea566 --- /dev/null +++ b/build/api/module/ue/tdController.php @@ -0,0 +1,46 @@ + UE code + * + * @return groups The list of groups for this UE + * + ---------------------------------------------------------*/ + public static function get($args){ + $code = ""; + extract($args); + + /* Get the td repo */ + /** @var td $td_repo */ + $td_repo = Repo::getRepo('td'); + + /* (1) Try to fetch data */ + $fetched = $td_repo->getGroups($code); + + /* (2) Manage error */ + if( is_null($fetched) || !is_array($fetched) ) + return ['error' => new Error(Err::RepoError)]; + + /* (3) Parse JSON list */ + foreach($fetched as $f=>$v) + $fetched[$f]['formations'] = json_decode($v['formations']); + + + return ['groups' => $fetched]; + + } + + +} \ No newline at end of file diff --git a/build/api/module/ue/tpController.php b/build/api/module/ue/tpController.php new file mode 100644 index 0000000..c97bdf4 --- /dev/null +++ b/build/api/module/ue/tpController.php @@ -0,0 +1,46 @@ + UE code + * + * @return groups The list of groups for this UE + * + ---------------------------------------------------------*/ + public static function get($args){ + $code = ""; + extract($args); + + /* Get the tp repo */ + /** @var tp $tp_repo */ + $tp_repo = Repo::getRepo('tp'); + + /* (1) Try to fetch data */ + $fetched = $tp_repo->getGroups($code); + + /* (2) Manage error */ + if( is_null($fetched) || !is_array($fetched) ) + return ['error' => new Error(Err::RepoError)]; + + /* (3) Parse JSON list */ + foreach($fetched as $f=>$v) + $fetched[$f]['formations'] = json_decode($v['formations']); + + + return ['groups' => $fetched]; + + } + + +} \ No newline at end of file diff --git a/build/database/repo/cours.php b/build/database/repo/cours.php index c9d6290..4b9c7e8 100644 --- a/build/database/repo/cours.php +++ b/build/database/repo/cours.php @@ -84,4 +84,50 @@ class cours extends Repo_i { ]); } + + /* (x) Get groups for a specific UE + * + * @code UE code + * + * @return groups The list of groups for this UE + * NULL on error + * + ---------------------------------------------------------*/ + public function getGroups(String $code) : ?array{ + + /* (1) Prepare statement */ + $st = $this->pdo->prepare("SELECT + c.UE_code code, + c.idCours, + p.idProfesseur idProf, + p.firstName, + p.lastName, + IFNULL(lform.flist, '[]') formations, + c.volume + FROM Cours c + LEFT JOIN ( SELECT gc.Cours_idCours idCours, CONCAT('[', GROUP_CONCAT(DISTINCT gc.Formation_idFormation),']') flist + FROM GroupeCours gc + GROUP BY gc.Cours_idCours + ORDER BY gc.Formation_idFormation DESC + ) lform ON c.idCours = lform.idCours + LEFT JOIN Professeur p ON p.idProfesseur = c.Professeur_idProfesseur + WHERE c.UE_code = :code;"); + + /* (2) Check statement */ + if( is_bool($st) ) + return NULL; + + /* (3) Execute statement */ + $success = $st->execute([':code' => $code]); + + /* (4) Check error */ + if( !$success ) + return NULL; + + /* (5) Dispatch fetch result */ + return $st->fetchAll(); + + + } + } \ No newline at end of file diff --git a/build/database/repo/td.php b/build/database/repo/td.php index 03f70e8..d1736dd 100644 --- a/build/database/repo/td.php +++ b/build/database/repo/td.php @@ -85,4 +85,50 @@ class td extends Repo_i { ]); } + + /* (x) Get groups for a specific UE + * + * @code UE code + * + * @return groups The list of groups for this UE + * NULL on error + * + ---------------------------------------------------------*/ + public function getGroups(String $code) : ?array{ + + /* (1) Prepare statement */ + $st = $this->pdo->prepare("SELECT + td.UE_code code, + td.idTD, + p.idProfesseur idProf, + p.firstName, + p.lastName, + IFNULL(lform.flist, '[]') formations, + td.volume + FROM TD td + LEFT JOIN ( SELECT gtd.TD_idTD idTD, CONCAT('[', GROUP_CONCAT(DISTINCT gtd.Formation_idFormation),']') flist + FROM GroupeTD gtd + GROUP BY gtd.TD_idTD + ORDER BY gtd.Formation_idFormation DESC + ) lform ON td.idTD = lform.idTD + LEFT JOIN Professeur p ON p.idProfesseur = td.Professeur_idProfesseur + WHERE td.UE_code = :code;"); + + /* (2) Check statement */ + if( is_bool($st) ) + return NULL; + + /* (3) Execute statement */ + $success = $st->execute([':code' => $code]); + + /* (4) Check error */ + if( !$success ) + return NULL; + + /* (5) Dispatch fetch result */ + return $st->fetchAll(); + + + } + } \ No newline at end of file diff --git a/build/database/repo/tp.php b/build/database/repo/tp.php index 0cb1d2c..e50a5c9 100644 --- a/build/database/repo/tp.php +++ b/build/database/repo/tp.php @@ -84,4 +84,50 @@ class tp extends Repo_i { ]); } + + /* (x) Get groups for a specific UE + * + * @code UE code + * + * @return groups The list of groups for this UE + * NULL on error + * + ---------------------------------------------------------*/ + public function getGroups(String $code) : ?array{ + + /* (1) Prepare statement */ + $st = $this->pdo->prepare("SELECT + tp.UE_code code, + tp.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;"); + + /* (2) Check statement */ + if( is_bool($st) ) + return NULL; + + /* (3) Execute statement */ + $success = $st->execute([':code' => $code]); + + /* (4) Check error */ + if( !$success ) + return NULL; + + /* (5) Dispatch fetch result */ + return $st->fetchAll(); + + + } + } \ No newline at end of file diff --git a/config/modules.json b/config/modules.json index 6ee1c30..103e37c 100644 --- a/config/modules.json +++ b/config/modules.json @@ -239,8 +239,43 @@ "out": { "updated": { "des": "Whether the UE has been updated", "typ": "boolean" } } - } + }, + "cours": { + + "GET": { + "des" : "Get all cours data about a given UE", + "per": [], + "par": { + "URL0": { "des": "Code of the UE", "typ": "varchar(4,20,alphanumeric)", "ren": "code" } + } + } + + }, + + "td": { + + "GET": { + "des" : "Get all TD data about a given UE", + "per": [], + "par": { + "URL0": { "des": "Code of the UE", "typ": "varchar(4,20,alphanumeric)", "ren": "code" } + } + } + + }, + + "tp": { + + "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" } + } + } + + } },