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

134 lines
3.0 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: lucas
* Date: 22/02/18
* Time: 13:27
*/
namespace database\repo;
use database\core\Repo_i;
class td extends Repo_i {
public function create(string $codeUE, ?int $idProf, float $volume, array $formations) : int{
//create the group
$st = $this->pdo->prepare("INSERT INTO TD(UE_code, Professeur_idProfesseur, volume)
VALUE(:UE, :idProf, :vol)");
$st->execute([
"UE" => $codeUE,
"idProf" => $idProf,
"vol" => $volume
]);
$idTD = $this->pdo->lastInsertId();
//if there is formations, link them with the group
if(count($formations) > 0){
$linkSt = $this->pdo->prepare("INSERT INTO GroupeTD(Formation_idFormation, TD_idTD)
VALUE (:form, :TD)");
foreach ($formations as $form){
$linkSt->execute([
"form" => $form,
"TD" => $idTD
]);
}
}
return $idTD;
}
public function unlinkFormation(int $idFormation, int $idTD) : bool{
$st = $this->pdo->prepare("DELETE FROM GroupeTD WHERE TD_idTD = :TD AND Formation_idFormation = :form");
return $st->execute([
"TD" => $idTD,
"form" => $idFormation
]);
}
public function linkFormation(int $idFormation, int $idTD) : bool{
$st = $this->pdo->prepare("INSERT INTO GroupeTD(TD_idTD,Formation_idFormation)
VALUE(:TD, :form)");
return $st->execute([
"TD" => $idTD,
"form" => $idFormation
]);
}
public function updateProf(?int $prof) : bool {
$st = $this->pdo->prepare("UPDATE TD SET Professeur_idProfesseur = :prof");
return $st->execute([
"prof" => $prof
]);
}
public function updateVolume(float $volume) : bool {
$st = $this->pdo->prepare("UPDATE TD SET volume = :vol");
return $st->execute([
"vol" => $volume
]);
}
public function delete(int $id) :bool {
$st = $this->pdo->prepare("DELETE FROM TD WHERE idTD = :id");
return $st->execute([
"id" => $id
]);
}
/* (x) Get groups for a specific UE
*
* @code<String> UE code
*
* @return groups<array> 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();
}
}