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

133 lines
3.0 KiB
PHP
Raw Normal View History

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