From ad56ce17517466740b89cc87894f74b4f4657fdb Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 1 Mar 2018 16:57:44 +0100 Subject: [PATCH] fix DatabaseDriver encoding + implement prof category stats (and add possiblity to get all formations stat in one query) --- build/api/module/Category.php | 27 +++++++++++++++++++++++++ build/api/module/Formation.php | 2 +- build/database/core/DatabaseDriver.php | 2 +- build/database/repo/category.php | 19 +++++++++++++++++ build/database/repo/formation.php | 28 ++++++++++++-------------- config/modules.json | 12 ++++++++++- 6 files changed, 72 insertions(+), 18 deletions(-) create mode 100644 build/api/module/Category.php diff --git a/build/api/module/Category.php b/build/api/module/Category.php new file mode 100644 index 0000000..77e7546 --- /dev/null +++ b/build/api/module/Category.php @@ -0,0 +1,27 @@ + $repo->getStats($idCat)]; + } + +} \ No newline at end of file diff --git a/build/api/module/Formation.php b/build/api/module/Formation.php index 51e87cd..cd73ecb 100644 --- a/build/api/module/Formation.php +++ b/build/api/module/Formation.php @@ -21,7 +21,7 @@ class Formation /** @var \database\repo\formation $repo */ $repo = Repo::getRepo("formation"); - return ["data" => $repo->getStat($idForm)]; + return ["data" => $repo->getStats($idForm)]; } } \ No newline at end of file diff --git a/build/database/core/DatabaseDriver.php b/build/database/core/DatabaseDriver.php index 9639b7b..cc611be 100755 --- a/build/database/core/DatabaseDriver.php +++ b/build/database/core/DatabaseDriver.php @@ -69,7 +69,7 @@ try{ - $this->pdo = new \PDO('mysql:host='.$this->host.';dbname='.$this->dbname, $this->username, $this->password, [ + $this->pdo = new \PDO('mysql:host='.$this->host.';dbname='.$this->dbname.';charset=utf8', $this->username, $this->password, [ \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC, \PDO::ATTR_TIMEOUT => 5 ]); diff --git a/build/database/repo/category.php b/build/database/repo/category.php index 6dc2210..9cf8a8d 100644 --- a/build/database/repo/category.php +++ b/build/database/repo/category.php @@ -31,4 +31,23 @@ class category extends Repo_i{ ]); } + public function getStats(?int $id = null){ + $st = $this->pdo->prepare("SELECT VHCours, VHTd, VHTp, Cat.idCategorie idCat, count(P.idProfesseur) nbrProf, Cat.labelCategorie labelCat + FROM Categorie Cat + LEFT JOIN (SELECT IFNULL(SUM(Cours.volume),0) VHCours, Prof.Categorie_idCategorie idCat FROM Professeur Prof LEFT JOIN Cours ON Prof.idProfesseur = Cours.Professeur_idProfesseur GROUP BY Prof.Categorie_idCategorie) VHCours ON VHCours.idCat = Cat.idCategorie + LEFT JOIN (SELECT IFNULL(SUM(TD.volume),0) VHTd , Prof.Categorie_idCategorie idCat FROM Professeur Prof LEFT JOIN TD ON TD.Professeur_idProfesseur = Prof.idProfesseur GROUP BY Prof.Categorie_idCategorie) VHTd ON VHTd.idCat = Cat.idCategorie + LEFT JOIN (SELECT IFNULL(SUM(TP.volume),0) VHTp, Prof.Categorie_idCategorie idCat FROM Professeur Prof LEFT JOIN TP ON TP.Professeur_idProfesseur = Prof.idProfesseur GROUP BY Prof.Categorie_idCategorie) VHTp ON VHTp.idCat = Cat.idCategorie + LEFT JOIN Professeur P ON Cat.idCategorie = P.Categorie_idCategorie + ".($id ? " WHERE Cat.idCategorie = :idCat" : "")." + GROUP BY Cat.idCategorie;"); + + $st->execute($id ? ["idCat" => $id] : []); + + if($id){ + return $st->fetch() ?: []; + }else{ + return $st->fetchAll(); + } + } + } \ No newline at end of file diff --git a/build/database/repo/formation.php b/build/database/repo/formation.php index da08037..24cd822 100644 --- a/build/database/repo/formation.php +++ b/build/database/repo/formation.php @@ -42,24 +42,22 @@ class formation extends Repo_i { ]); } - public function getStat(int $id) :array{ - $st = $this->pdo->prepare("SELECT VHCours, VHTd, VHTp, (VHCours + VHTd + VHTp) VHTotal, Form.isInternal isInternal - FROM Formation Form, - (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, - (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, - (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 - WHERE - Form.idFormation = :idForm AND - VHCours.idForm = Form.idFormation AND - VHTp.idForm = Form.idFormation AND - VHTd.idForm = Form.idFormation; + public function getStats(?int $id) :array{ + $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" : "")." "); - $st->execute([ - "idForm" => $id - ]); + $st->execute($id ? ["idForm" => $id] : []); - return $st->fetch() ?: []; + 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 45a9abb..cdd4fc0 100644 --- a/config/modules.json +++ b/config/modules.json @@ -47,7 +47,17 @@ "des": "Get all data about a formation", "per": [], "par": { - "URL0":{"des" : "Id of the formation", "typ": "id", "ren": "idForm", "opt" : false} + "URL0":{"des" : "Id of the formation", "typ": "id", "ren": "idForm", "opt" : true} + } + } + }, + + "Category": { + "GET": { + "des" : "Get all data about a professor category", + "per": [], + "par": { + "URL0":{"des" : "Id of the category", "typ": "id", "ren": "idCat", "opt" : true} } } },