You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ScienceQuest/project/src/model/mdl/MdlScientifique.php

141 lines
4.2 KiB

<?php
namespace model;
use DateTime;
use Exception;
use RuntimeException;
class MdlScientifique extends MdlBase{
private ScientifiqueGateway $gw;
private MdlSexe $mdlSexe;
private MdlDifficulte $mdlDifficulte;
private MdlThematique $mdlThematique;
public function __construct(){
parent::__construct();
$this->gw = new ScientifiqueGateway($this->con);
$this->mdlSexe = new MdlSexe();
$this->mdlDifficulte = new MdlDifficulte();
$this->mdlThematique = new MdlThematique();
}
/**
* @throws Exception
*/
public function getRandom(): Scientifique{
$row = $this->gw->getRandom();
if(!$row) throw new RuntimeException("Erreur aucun scientifique trouvé");
$sexe = $this->mdlSexe->getFromId($row['idsexe']);
$difficulte = $this->mdlDifficulte->getFromId($row['iddifficulte']);
$thematique = $this->mdlThematique->getFromId($row['idthematique']);
return new Scientifique($row['id'],
$row['nom'],
$row['prenom'],
$row['photo'],
new DateTime($row['datenaissance']),
$row['descriptif'],
$row['ratiotrouvee'],
$thematique,
$difficulte,
$sexe);
}
/**
* @throws Exception
*/
public function addScientifique(Scientifique $s): bool
{
return $this->gw->addScientifique($s);
}
public function getScientifiquesParPage(int $page) : array {
$nbElemParPage = 20;
$pageMax = ceil($this->gw->getNbScientifique()/$nbElemParPage);
if ($page <= 0) {
$page = 1;
} elseif ($page > $pageMax) {
$page = $pageMax;
}
$result = $this->gw->getScientifiquesParPages($page,$nbElemParPage);
$scientifiques = array();
foreach ($result as $scientifique) {
$sexe = $this->mdlSexe->getFromId($scientifique['idsexe']);
$difficulte = $this->mdlDifficulte->getFromId($scientifique['iddifficulte']);
$thematique = $this->mdlThematique->getFromId($scientifique['idthematique']);
$scientifiques[] = new Scientifique($scientifique['id'],
$scientifique['nom'],
$scientifique['prenom'],
$scientifique['photo'],
new DateTime($scientifique['datenaissance']),
$scientifique['descriptif'],
$scientifique['ratiotrouvee'],
$thematique,
$difficulte,
$sexe);
}
return $scientifiques;
}
public function getMaxPages() : int {
$nbElemParPage = 20;
return ceil($this->gw->getNbScientifique()/$nbElemParPage);
}
public function editScientifique(Scientifique $s){
return $this->gw->editScientifique($s);
}
/**
* @throws Exception
*/
public function getScientifique(int $id): Scientifique
{
$t=$this->gw->getScientifique($id);
if(gettype($t)!="array"){
throw new Exception("Scientifique non trouvé");
}
$sexe=new MdlSexe();
$sexe=$sexe->getFromId($t["idsexe"]);
$diff=new MdlDifficulte();
$diff=$diff->getFromId($t["iddifficulte"]);
$theme=new MdlThematique();
$theme=$theme->getFromId($t["idthematique"]);
return new Scientifique(
$id,
$t["nom"],
$t["prenom"],
$t["photo"],
DateTime::createFromFormat("Y-m-d", $t["datenaissance"]),
$t["descriptif"],
$t["ratiotrouvee"],
$theme,
$diff,
$sexe
);
}
/**
* @throws Exception
*/
public function getQuestions(int $id): array
{
$t = $this->gw->getQuestions($id);
if (gettype($t) != "array") {
throw new Exception("Scientifique non trouvé");
}
$questions = [];
foreach ($t as $question) {
$questions[] = $question["libelle"];
}
return $questions;
}
}