Renaud BEURET 1 year ago
commit c275e41df0

@ -2,3 +2,4 @@ vendor/
composer.lock
js/bootstrap*.js*
css/bootstrap*.css*
config.php

@ -11,49 +11,76 @@ use model\Scientifique;
//gerer la connexion des admins
class AdminController {
public function __construct(array $params){
global $twig;
global $dVue;
//on initialise un tableau d'erreur pour être utilisé par la vue erreur
$dVueErreur = [];
//verifier si l'utilisateur est connecté et admin
if(isset($_SESSION["isAdmin"])){
if($_SESSION["isAdmin"]==true){
//donner la page admin a l'admin
try {
switch($params['action']) {
//public function __construct(array $params)
//{
//
// //verifier si l'utilisateur est connecté et admin
// if (isset($_SESSION["isAdmin"])) {
// if ($_SESSION["isAdmin"] == true) {
// } else if (isset($_SESSION["isLogged"])) {
// //verifier si l'utilisateur est connecté mais pas admin
// if ($_SESSION["isLogged"] == true) {
// exit(0);
// }
// } else {
// //renvoyer a la page de connexion pour les non connectés
// echo '<meta http-equiv="refresh" content="0; url=login">';
// }
// }
//}
case '':
echo $twig->render('admin/accueil.html');
break;
public function notLogged(array $params) {
global $twig;
//dire acces interdit aux non admins
$dVueErreur[] = 'Erreur 403 : Accès interdit !';
echo $twig->render('erreur.html', ['dVueErreur' => $dVueErreur]);
}
public function stats(array $params) {
global $twig;
case 'stats':
echo $twig->render('admin/stats.html');
break;
}
public function ajouterScientifiques(array $params) {
global $twig;
case 'ajouterScientifiques':
$sexe = new MdlSexe();
$theme = new MdlThematique();
$diff = new MdlDifficulte();
$scient=null;
if(!empty($_POST)){
$sci=new MdlScientifique();
$sci->addScientifique(new Scientifique(0,
$id=0;
if(isset($_GET["id"])){
$id=intval($_GET["id"]);
}
$sci = new Scientifique(
$id,
$_POST["name"],
$_POST["prenom"],
$_POST["url"],
\DateTime::createFromFormat("Y-m-d",$_POST["date"]),
\DateTime::createFromFormat("Y-m-d", $_POST["date"]),
$_POST["description"],
0,
$theme->getFromId(intval($_POST["theme"])),
$diff->getFromId(intval($_POST["difficulte"])),
$sexe->getFromId(intval($_POST["sexe"]))
));
);
$mdlsci=new MdlScientifique();
if(isset($_GET["id"])){
$mdlsci->editScientifique($sci);
} else {
$mdlsci->addScientifique($sci);
}
}
echo $twig->render('admin/ajouterScientifiques.html',['sexe' => $sexe->getAll(), 'themes' => $theme->getAll(), 'difficultes' => $diff->getAll()]);
break;
if(isset($_GET["id"])){
$scient=new MdlScientifique();
$scient=$scient->getScientifique($_GET["id"]);
}
echo $twig->render('admin/ajouterScientifiques.html',['sexe' => $sexe->getAll(), 'themes' => $theme->getAll(), 'difficultes' => $diff->getAll(), 'scientifique' => $scient]);
}
public function listeScientifique() {
global $twig;
case 'listeScientifiques':
$ms = new MdlScientifique();
if (!isset($params['id'])) {
$page = 1;
@ -74,38 +101,9 @@ class AdminController {
$dVue['pageSuiv'] = $page + 1;
}
echo $twig->render('admin/listeScientifiques.html',['dVue' => $dVue]);
break;
//mauvaise action
default:
$dVueErreur[] = "Erreur d'appel php";
echo $twig->render('erreur.html', ['dVueErreur' => $dVueErreur]);
break;
}
} catch (\Exception $e2) {
$dVueErreur[] = 'Erreur inattendue !';
echo $twig->render('erreur.html', ['dVueErreur' => $dVueErreur]);
}
}
}
else if(isset($_SESSION["isLogged"])){
//verifier si l'utilisateur est connecté mais pas admin
if($_SESSION["isLogged"]==true) {
//dire acces interdit aux non admins
$dVueErreur[] = 'Erreur 403 : Accès interdit !';
echo $twig->render('erreur.html', ['dVueErreur' => $dVueErreur]);
exit(0);
}
} else {
//renvoyer a la page de connexion pour les non connectés
echo '<meta http-equiv="refresh" content="0; url=login">';
}
exit(0);
}
}
}
?>

@ -60,10 +60,10 @@ class FrontController
case 'AdminController':
$action = $match['params']['action'] ?? '';
//if (!MdlAdmin::isAdmin()) {
// $action = 'login';
//}
new AdminController($match['params']);
if (!MdlAdmin::isAdmin()) {
$match['params']['action'] = 'notLogged';
}
$this->callController('AdminController',$match);
break;
case 'PseudoController':

@ -159,20 +159,34 @@ CREATE TABLE Partie(
-- INSERTS
INSERT INTO Jeu(nom) VALUES ('Qui-est-ce ?'),('Kahoot'), ('Pendu');
-- Scientifiques
INSERT INTO Difficulte(libelle) VALUES ('Facile'),('Intermédiaire'),('Difficile');
INSERT INTO Sexe(libelle) VALUES ('Homme'),('Femme');
INSERT INTO Thematique(libelle) VALUES ('Nucléaire'),('Mathématiques');
INSERT INTO Scientifique(nom, prenom, photo, dateNaissance, descriptif, ratioTrouvee, idThematique, idDifficulte, idSexe)
VALUES
('nomToto', 'prenomToto', '', CURRENT_DATE, 'desc', 0, 1, 1, 1),
('nomTiti', 'prenomTiti', '', CURRENT_DATE, 'desc', 0, 2, 2, 2);
('Marie', 'Curie', '', CURRENT_DATE, 'desc', 0, 1, 1, 2),
('Albert', 'Einstein', '', CURRENT_DATE, 'desc', 0, 2, 1, 1),
('Sophie', 'Germain', '', CURRENT_DATE, 'desc', 0, 2, 2, 2);
-- Jeu
INSERT INTO Jeu(nom) VALUES ('Qui-est-ce ?'),('Science Quizz'), ('Pendu');
-- Questions
INSERT INTO Question(question)
VALUES
('Qui a reçu le prix Nobel de chimie en 1911, pour avoir réussi à isoler un gramme de radium ?'),
('Quel mathématicien a dit : « Dieu existe, cest les mathématiques » ?'),
('Quel mathématicienne utilisa comme nom d"emprunt « Antoine Auguste Le Blanc » ?');
-- Réponses
INSERT INTO Reponse(reponse, idQuestion, idScientifique)
VALUES
('Marie Curie', 1, 1),
('Albert Einstein', 2, 2),
('Sophie Germain', 3, 3);
-- Utilisateurs
INSERT INTO Joueur(id,pseudo) VALUES (1337, 'moi, le meilleur joueur du monde');
INSERT INTO Utilisateur(idJoueur,email,password) VALUES (1337, 'joueur','$2y$10$juGnlWC9cS19popEKLZsYeir0Jl39k6hDl0dpaCix00FDcdiEbtmS');
-- mdp = test

@ -57,4 +57,37 @@ class ScientifiqueGateway
$this->con->executeQuery($query);
return $this->con->getResults()[0]['val'];
}
public function editScientifique(Scientifique $sci): bool{
return $this->con->executeQuery(
"UPDATE Scientifique SET nom = :nom, prenom = :prenom, photo = :photo, dateNaissance = :dateNaissance, descriptif = :descriptif, ratioTrouvee = :ratioTrouvee, idThematique = :idThematique, idDifficulte = :idDifficulte, idSexe = :idSexe WHERE id=:id;"
,[
":nom"=>[$sci->getNom(),$this->con::PARAM_STR],
":prenom"=>[$sci->getPrenom(),$this->con::PARAM_STR],
":photo"=>[$sci->getPhoto(),$this->con::PARAM_STR],
":dateNaissance"=>[date("Y-m-d H:i:s", $sci->getDateNaiss()->getTimestamp()),$this->con::PARAM_STR],
":descriptif"=>[$sci->getDescriptif(),$this->con::PARAM_STR],
":ratioTrouvee"=>[$sci->getRatioTrouvee(),$this->con::PARAM_STR],
":idThematique"=>[$sci->getThematique()->getId(),$this->con::PARAM_STR],
":idDifficulte"=>[$sci->getDifficulte()->getId(),$this->con::PARAM_STR],
":idSexe"=>[$sci->getSexe()->getId(),$this->con::PARAM_STR],
":id"=>[$sci->getId(),$this->con::PARAM_INT]
]);
}
public function deleteScientifique(int $id): bool{
return $this->con->executeQuery(
"DELETE FROM Scientifique WHERE id=:id;"
,[
":id"=>[$id,$this->con::PARAM_INT]
]);
}
public function getScientifique(int $id) {
$this->con->executeQuery(
"SELECT id, nom, prenom, photo, dateNaissance, descriptif, ratioTrouvee, idThematique, idDifficulte, idSexe FROM Scientifique WHERE id=:id;"
,[
":id"=>[$id,$this->con::PARAM_INT]
]);
return $this->con->getOneResult();
}
}

@ -1,113 +0,0 @@
<?php
namespace model;
use PDO;
use PDOStatement;
class UserGateway
{
private Connection $con;
private PDOStatement $stmt;
public function __construct(Connection $con)
{
$this->con=$con;
}
public function login(string $email, string $password): bool
{
$sql = "SELECT * FROM Utilisateur WHERE email=:email";
$this->con->executeQuery($sql, array(
':email' => array($email, PDO::PARAM_STR)
));
$result = $this->con->getOneResult();
if (!empty($result)) {
return password_verify($password,$result['password']);
}
return false;
}
public function addUser(string $email, string $password): void
{
$sql = "INSERT INTO Utilisateur (email, password) VALUES (:email, :password)";
$stmt = $this->con->prepare($sql);
$stmt->bindValue(':email', $email);
$stmt->bindValue(':password', password_hash($password, PASSWORD_DEFAULT));
$stmt->execute();
}
public function deleteUser(int $id): void
{
$sql = "DELETE FROM Utilisateur WHERE idJoueur=:id";
$stmt = $this->con->prepare($sql);
$stmt->bindValue(':id', $id);
$stmt->execute();
}
public function updateUser(int $id, string $email, string $password): void
{
$sql = "UPDATE Utilisateur SET email=:email, password=:password WHERE idJoueur=:id";
$stmt = $this->con->prepare($sql);
$stmt->bindValue(':id', $id);
$stmt->bindValue(':email', $email);
$stmt->bindValue(':password', password_hash($password, PASSWORD_DEFAULT));
$stmt->execute();
}
public function getUser(int $id): User
{
$sql = "SELECT * FROM Utilisateur WHERE idJoueur=:id";
$stmt = $this->con->prepare($sql);
$stmt->bindValue(':id', $id);
$stmt->execute();
$result = $stmt->fetch();
return new User($result['id'], $result['email'], $result['password']);
}
public function getUsers(): array
{
$sql = "SELECT * FROM utilisateur";
$stmt = $this->con->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll();
$users = [];
foreach ($result as $user) {
$users[] = new User($user['id'], $user['email'], $user['password']);
}
return $users;
}
public function getHashedPasswordById(int $id): string
{
$sql = "SELECT password FROM Utilisateur WHERE idJoueur=:id";
$stmt = $this->con->prepare($sql);
$stmt->bindValue(':id', $id);
$stmt->execute();
$result = $stmt->fetch();
return $result['password'];
}
public function getHashedPassword(int $email): string
{
$sql = "SELECT password FROM Utilisateur WHERE email=:email";
$stmt = $this->con->prepare($sql);
$stmt->bindValue(':email', $email);
$stmt->execute();
$result = $stmt->fetch();
return $result['password'];
}
public function getUserId(string $email): int
{
$sql = "SELECT idJoueur FROM Utilisateur WHERE email=:email";
$stmt = $this->con->prepare($sql);
$stmt->bindValue(':email', $email);
$stmt->execute();
$result = $stmt->fetch();
return $result['id'];
}
public function getUserByEmailAndPassword(string $email, string $password): User
{
$sql = "SELECT * FROM utilisateur WHERE email=:email AND password=:password";
$stmt = $this->con->prepare($sql);
$stmt->bindValue(':email', $email);
$stmt->bindValue(':password', password_hash($password, PASSWORD_DEFAULT));
$stmt->execute();
$result = $stmt->fetch();
return new User($result['id'], $result['email'], $result['password']);
}
}

@ -77,4 +77,37 @@ class MdlScientifique extends MdlBase{
$nbElemParPage = 20;
return ceil($this->gw->getNbScientifique()/$nbElemParPage);
}
public function editScientifique(Scientifique $s){
return $this->gw->editScientifique($s);
}
public function getScientifique(int $id){
$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
);
}
}

@ -3,11 +3,11 @@
namespace model;
class MdlUser extends MdlBase{
private UserGateway $gw;
private JoueurGateway $gw;
public function __construct(){
parent::__construct();
$this->gw = new UserGateway($this->con);
$this->gw = new JoueurGateway($this->con);
}
public function login(string $username, string $password): bool{
return $this->gw->login($username, $password);

@ -21,34 +21,45 @@
<h1>ajouterScientifiques</h1>
<br><br><br>
<center>
<form action="" method="post">
<div>
<label for="name">Nom :</label>
<input type="text" id="name" name="name" required minlength="1" maxlength="128"/>
<input type="text" id="name" name="name" required minlength="1" maxlength="128"
value="{% if scientifique is not null %}{{ scientifique.nom }}{% endif %}"/>
</div>
<div>
<label for="name">Prénom :</label>
<input type="text" id="prenom" name="prenom" required minlength="1" maxlength="128"/>
<input type="text" id="prenom" name="prenom" required minlength="1" maxlength="128"
value="{% if scientifique is not null %}{{ scientifique.prenom }}{% endif %}"/>
</div>
<div>
<label for="name">URL de la photo :</label>
<input type="text" id="url" name="url" required minlength="1" maxlength="512"/>
<input type="text" id="url" name="url" required minlength="1" maxlength="512"
value="{% if scientifique is not null %}{{ scientifique.photo }}{% endif %}"/>
</div>
<div>
<label for="name">Date de naissance :</label>
<input type="date" id="date" name="date" required/>
<input type="date" id="date" name="date" required
value="{% if scientifique is not null %}{{ scientifique.date|date('Y-m-d') }}{% endif %}"/>
</div>
<div>
<label for="name">Description (histoire, accomplissements...) :</label>
<textarea name="description" cols="40" rows="5"></textarea>
<textarea name="description" cols="40" rows="5">{% if scientifique is not null %}{{ scientifique.descriptif }}{% endif %}</textarea>
</div>
<fieldset>
<legend>Sexe :</legend>
{% for se in sexe %}
<div>
<input type="radio" id="sexe{{se.id}}" name="sexe" value="{{se.id}}" />
<input type="radio" id="sexe{{se.id}}" name="sexe" value="{{se.id}}"
{% if scientifique is not null %}
{% if scientifique.sexe.getId == se.id %}
checked
{% endif %}
{% endif %}
/>
<label for="{{se.id}}">{{se.libelle}}</label>
</div>
{% endfor %}
@ -57,7 +68,14 @@
<legend>Thematique :</legend>
{% for se in themes %}
<div>
<input type="radio" id="theme{{se.id}}" name="theme" value="{{se.id}}" />
<input type="radio" id="theme{{se.id}}" name="theme" value="{{se.id}}"
{% if scientifique is not null %}
{% if scientifique.thematique.getId == se.id %}
checked
{% endif %}
{% endif %}
/>
<label for="{{se.id}}">{{se.libelle}}</label>
</div>
{% endfor %}
@ -66,7 +84,14 @@
<legend>Difficulté :</legend>
{% for se in difficultes %}
<div>
<input type="radio" id="diff{{se.id}}" name="difficulte" value="{{se.id}}" />
<input type="radio" id="diff{{se.id}}" name="difficulte" value="{{se.id}}"
{% if scientifique is not null %}
{% if scientifique.difficulte.getId == se.id %}
checked
{% endif %}
{% endif %}
/>
<label for="{{se.id}}">{{se.libelle}}</label>
</div>
{% endfor %}

Loading…
Cancel
Save