From 79da633600ed1850d446d5b5b669b11ee8594c22 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Mon, 5 Mar 2018 11:19:32 +0100 Subject: [PATCH] [repo.formation] formatted get(ID|ALL) | [module.formation] implemented GET --- build/api/module/formationController.php | 4 +-- build/database/repo/formation.php | 42 +++++++++++++++++++----- config/modules.json | 2 +- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/build/api/module/formationController.php b/build/api/module/formationController.php index f2944f4..bf08f1f 100644 --- a/build/api/module/formationController.php +++ b/build/api/module/formationController.php @@ -15,13 +15,13 @@ class formationController { public static function get($args){ - $idForm = 0; + $form_id = null; extract($args); /** @var \database\repo\formation $repo */ $repo = Repo::getRepo("formation"); - return ["data" => $repo->getStats($idForm)]; + return ['formations' => $repo->get($form_id)]; } } \ No newline at end of file diff --git a/build/database/repo/formation.php b/build/database/repo/formation.php index 24cd822..6989427 100644 --- a/build/database/repo/formation.php +++ b/build/database/repo/formation.php @@ -42,22 +42,46 @@ class formation extends Repo_i { ]); } - public function getStats(?int $id) :array{ + + /* (x) Gets a formation by its ID || getAll + * + * @form_id [OPT] The formation id, if not set, getAll() + * + * @return formations The formations matching id + * + ---------------------------------------------------------*/ + public function get(?int $form_id) : array{ + + /* (1) Manage if no id given */ + $cond = is_null($form_id) ? '' : 'WHERE Form.idFormation = :form_id'; + $parm = is_null($form_id) ? [] : [ ':form_id' => $form_id ]; + + /* (2) Prepare statement */ $st = $this->pdo->prepare("SELECT IFNULL(VHCours,0) VHCours, IFNULL(VHTd,0) VHTd, IFNULL(VHTp,0) VHTp, (IFNULL(VHCours,0) + IFNULL(VHTd,0) + IFNULL(VHTp,0)) VHTotal, Form.isInternal isInternal, Form.idFormation idForm, Form.labelFormation labelForm FROM Formation Form LEFT JOIN (SELECT IFNULL(SUM(C.volume),0) VHCours, GC.Formation_idFormation idForm FROM GroupeCours GC LEFT JOIN Cours C ON GC.Cours_idCours = C.idCours GROUP BY GC.Formation_idFormation) VHCours ON VHCours.idForm = Form.idFormation LEFT JOIN (SELECT IFNULL(SUM(T.volume),0) VHTd, GTD.Formation_idFormation idForm FROM GroupeTD GTD LEFT JOIN TD T ON GTD.TD_idTD = T.idTD GROUP BY GTD.Formation_idFormation) VHTd ON VHTd.idForm = Form.idFormation LEFT JOIN (SELECT IFNULL(SUM(T2.volume),0) VHTp, GTP.Formation_idFormation idForm FROM GroupeTP GTP LEFT JOIN TP T2 ON GTP.TP_idTP = T2.idTP GROUP BY GTP.Formation_idFormation) VHTp ON VHTp.idForm = Form.idFormation - ".($id ? " WHERE Form.idFormation = :idForm" : "")." - "); + $cond;"); - $st->execute($id ? ["idForm" => $id] : []); + /* (3) Bind params and execute statement */ + if( is_bool($st) ) return []; + $success = $st->execute($parm); + + /* (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; - if($id){ - return $st->fetch() ?: []; - }else{ - return $st->fetchAll(); - } } } \ No newline at end of file diff --git a/config/modules.json b/config/modules.json index 3ab0843..ebeede3 100644 --- a/config/modules.json +++ b/config/modules.json @@ -172,7 +172,7 @@ "des": "Get all data about a formation", "per": [], "par": { - "URL0":{"des" : "Id of the formation", "typ": "id", "ren": "idForm", "opt" : true} + "URL0":{"des" : "Id of the formation", "typ": "id", "ren": "form_id", "opt" : true } } } },