ptut-vhost/build/database/repo/formation.php

174 lines
5.4 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* Created by PhpStorm.
* User: lucas
* Date: 20/02/18
* Time: 21:35
*/
namespace database\repo;
use database\core\Repo_i;
class formation extends Repo_i {
/* (1) Creates a new formation
*
* @label<String> The formation label
* @isInternal<bool> Whether the formation is internal
*
* @return form_id<int> The formation UID (or NULL on error)
*
---------------------------------------------------------*/
public function create(string $label, bool $isInternal) : ?int{
/* (1) Prepare statement */
$st = $this->pdo->prepare("INSERT INTO `Formation` (`labelFormation`, `isInternal`) VALUE (:label, :isInternal);");
/* (2) Bind params and execute statement */
if( is_bool($st) ) return NULL;
$success = $st->execute([
':label' => $label,
':isInternal' => $isInternal ? 1 : 0
]);
/* (3) Manage error */
if( !$success )
return NULL;
/* (4) Return inserted ID */
return $this->pdo->lastInsertId();
}
/* (2) Check if a formation exists (by its label)
*
* @label<String> The formation label
*
* @return form_id<int> The formation UID (or NULL on error)
*
---------------------------------------------------------*/
public function exists(string $label) : ?int{
/* (1) Prepare statement */
$st = $this->pdo->prepare("SELECT `idFormation` FROM `Formation` WHERE `labelFormation` = :label");
/* (2) Bind params and execute statement */
if( is_bool($st) ) return NULL;
$success = $st->execute([ ':label' => $label ]);
/* (3) Manage error */
if( !$success )
return NULL;
/* (4) Return if we have a result */
return $st->fetch()['idFormation'] ?: 0;
}
/* (3) Deletes a formation
*
* @return deleted<bool> Whether the formation have been deleeted successfully
*
---------------------------------------------------------*/
public function delete(int $id) : bool{
/* (1) Prepare statement */
$st = $this->pdo->prepare("DELETE FROM `Formation` WHERE `idFormation` = :id");
/* (2) Bind params and execute request */
if( is_bool($st) ) return false;
/* (3) Dispatch the execution status */
return $st->execute([ ':id' => $id ]);
}
/* (4) Gets a formation by its ID || getAll
*
* @form_id<int> [OPT] The formation id, if not set, getAll()
*
* @return formations<array> 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
$cond;");
/* (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;
}
/* (5) Gets all professors who teaches a formation by ids (array)
*
* @form_id<int> The formation id
*
* @return professors<array> The professors' UID matching the @form_id formation
*
---------------------------------------------------------*/
public function getProfessors(int $form_id) : array{
/* (1) Prepare statement */
$st = $this->pdo->prepare("SELECT p.idProfesseur
FROM Professeur p, Formation f
WHERE (
p.idProfesseur IN ( SELECT p_cr.idProfesseur FROM Professeur p_cr, UE ue, Cours c WHERE c.Professeur_idProfesseur = p_cr.idProfesseur AND c.UE_code = ue.code AND ue.Formation_idFormation = f.idFormation )
OR p.idProfesseur IN ( SELECT p_td.idProfesseur FROM Professeur p_td, UE ue, TD t WHERE t.Professeur_idProfesseur = p_td.idProfesseur AND t.UE_code = ue.code AND ue.Formation_idFormation = f.idFormation )
OR p.idProfesseur IN ( SELECT p_tp.idProfesseur FROM Professeur p_tp, UE ue, TP t WHERE t.Professeur_idProfesseur = p_tp.idProfesseur AND t.UE_code = ue.code AND ue.Formation_idFormation = f.idFormation )
)
AND f.idFormation = :form_id;");
/* (2) Bind params and execute statement */
if( is_bool($st) ) return [];
$success = $st->execute([ ':form_id' => $form_id ]);
/* (3) Manage error */
if( !$success )
return [];
/* (4) Get data */
$fetched = $st->fetchAll();
/* (5) Return [] on no result */
if( $fetched === false )
return [];
/* (6) Return data */
return $fetched;
}
}