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

89 lines
2.0 KiB
PHP
Raw Normal View History

<?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
]);
}
}