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

108 lines
3.1 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: lucas
* Date: 20/02/18
* Time: 20:31
*/
namespace database\repo;
use database\core\Repo_i;
class professor extends Repo_i {
public function create(string $lastName, string $firstName, int $category, $hoursToDo = 0, $initials = "", $isAdmin = false , $casLogin = "" ) : int{
$st = $this->pdo->prepare("INSERT INTO Professeur (casLogin, lastName, firstName, abreviation, admin,hoursToDo, Categorie_idCategorie)
VALUE (:casLogin, :lastName, :firstName, :abrev, :admin, :hoursToDo, :cat);
");
$st->execute([
"casLogin" => $casLogin,
"lastName" => $lastName,
"firstName" => $firstName,
"abrev" => $initials,
"admin" => $isAdmin? 1 : 0,
"hoursToDo" => $hoursToDo,
"cat" => $category
]);
return $this->pdo->lastInsertId();
}
public function exists(string $lastName, string $firstName) : int{
$st = $this->pdo->prepare("SELECT idProfesseur FROM Professeur WHERE firstName = :firstName AND lastName = :lastName");
$st->execute([
"firstName" => $firstName,
"lastName" => $lastName
]);
return $st->fetch()["idProfesseur"]?: 0;
}
public function update(int $id, array $data) : bool{
$updSt = "";
foreach ($data as $key => $field){
$updSt .= "$key = :$key,";
}
$updSt = rtrim($updSt,",");
$st = $this->pdo->prepare("UPDATE Professeur SET $updSt WHERE idProfesseur = :id");
return $st->execute(array_merge($data,[
"id" => $id
]));
}
public function isAdmin(int $id) : bool{
$st = $this->pdo->prepare("SELECT admin FROM Professeur WHERE idProfesseur = :id");
$st->execute([
"id" => $id
]);
return $st->fetch()["admin"] == 1;
}
public function get(int $id) : array{
$st = $this->pdo->prepare("SELECT * FROM Professeur WHERE idProfesseur = :id");
$st->execute([
"id" => $id
]);
return $st->fetch();
}
public function delete(int $id) : bool{
$st = $this->pdo->prepare("DELETE FROM Professeur WHERE idProfesseur = :id");
return $st->execute([
"id" => $id
]);
}
public function getVH(int $id) : array{
$st = $this->pdo->prepare("SELECT VHCours, VHTd, VHTp, Cat.idCategorie idCat, Prof.hoursToDo du
FROM Professeur Prof, Categorie Cat,
(SELECT SUM(C.volume) VHCours , Prof.idProfesseur idProf FROM Cours C, Professeur Prof WHERE C.Professeur_idProfesseur = Prof.idProfesseur GROUP BY Prof.idProfesseur) VHCours,
(SELECT SUM(T.volume) VHTd , Prof.idProfesseur idProf FROM TD T, Professeur Prof WHERE T.Professeur_idProfesseur = Prof.idProfesseur GROUP BY Prof.idProfesseur) VHTd,
(SELECT SUM(T2.volume) VHTp, Prof.idProfesseur idProf FROM TP T2 , Professeur Prof WHERE T2.Professeur_idProfesseur = Prof.idProfesseur GROUP BY Prof.idProfesseur) VHTp
WHERE
Prof.idProfesseur = :idProf AND
Prof.Categorie_idCategorie = Cat.idCategorie AND
VHCours.idProf = Prof.idProfesseur AND
VHTp.idProf = Prof.idProfesseur AND
VHTd.idProf = Prof.idProfesseur;");
$st->execute([
"idProf" => $id
]);
return $st->fetch();
}
}