diff --git a/project/src/.gitignore b/project/src/.gitignore index da055ac..c588538 100755 --- a/project/src/.gitignore +++ b/project/src/.gitignore @@ -1,4 +1,5 @@ vendor/ composer.lock js/bootstrap*.js* -css/bootstrap*.css* \ No newline at end of file +css/bootstrap*.css* +config.php diff --git a/project/src/controller/AdminController.php b/project/src/controller/AdminController.php index 2ababbc..fd26338 100755 --- a/project/src/controller/AdminController.php +++ b/project/src/controller/AdminController.php @@ -11,101 +11,99 @@ 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 ''; + // } + // } + //} - 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; + echo $twig->render('admin/stats.html'); + } + public function ajouterScientifiques(array $params) { + global $twig; - case 'ajouterScientifiques': - $sexe = new MdlSexe(); - $theme = new MdlThematique(); - $diff = new MdlDifficulte(); - if(!empty($_POST)){ - $sci=new MdlScientifique(); - $sci->addScientifique(new Scientifique(0, - $_POST["name"], - $_POST["prenom"], - $_POST["url"], - \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"])) - )); - } - echo $twig->render('admin/ajouterScientifiques.html',['sexe' => $sexe->getAll(), 'themes' => $theme->getAll(), 'difficultes' => $diff->getAll()]); - break; + $sexe = new MdlSexe(); + $theme = new MdlThematique(); + $diff = new MdlDifficulte(); + $scient=null; + if(!empty($_POST)){ + $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"]), + $_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); + } + } + if(isset($_GET["id"])){ + $scient=new MdlScientifique(); + $scient=$scient->getScientifique($_GET["id"]); + } - case 'listeScientifiques': - $ms = new MdlScientifique(); - if (!isset($params['id'])) { - $page = 1; - } else { - $page = Validation::valPosInt($params['id']); - } - $dVue['listeScientifiques'] = $ms->getScientifiquesParPage($page); - $dVue['pageMax'] = $ms->getMaxPages(); - $dVue['page'] = $page; - if ($page - 1 <= 0) { - $dVue['pagePrec'] = 1; - } else { - $dVue['pagePrec'] = $page - 1; - } - if ($page + 1 >= $dVue['pageMax']) { - $dVue['pageSuiv'] = $dVue['pageMax']; - } else { - $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 ''; - } - exit(0); - } - -} + echo $twig->render('admin/ajouterScientifiques.html',['sexe' => $sexe->getAll(), 'themes' => $theme->getAll(), 'difficultes' => $diff->getAll(), 'scientifique' => $scient]); + } + public function listeScientifique() { + global $twig; + $ms = new MdlScientifique(); + if (!isset($params['id'])) { + $page = 1; + } else { + $page = Validation::valPosInt($params['id']); + } + $dVue['listeScientifiques'] = $ms->getScientifiquesParPage($page); + $dVue['pageMax'] = $ms->getMaxPages(); + $dVue['page'] = $page; + if ($page - 1 <= 0) { + $dVue['pagePrec'] = 1; + } else { + $dVue['pagePrec'] = $page - 1; + } + if ($page + 1 >= $dVue['pageMax']) { + $dVue['pageSuiv'] = $dVue['pageMax']; + } else { + $dVue['pageSuiv'] = $page + 1; + } + echo $twig->render('admin/listeScientifiques.html',['dVue' => $dVue]); + } +} ?> \ No newline at end of file diff --git a/project/src/controller/FrontController.php b/project/src/controller/FrontController.php index 52e8189..a11dde2 100755 --- a/project/src/controller/FrontController.php +++ b/project/src/controller/FrontController.php @@ -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': diff --git a/project/src/db.sql b/project/src/db.sql index 9e9a5d2..a9d1a07 100755 --- a/project/src/db.sql +++ b/project/src/db.sql @@ -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, c’est 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 diff --git a/project/src/model/gateways/ScientifiqueGateway.php b/project/src/model/gateways/ScientifiqueGateway.php index 3fa1519..0e0d6c3 100755 --- a/project/src/model/gateways/ScientifiqueGateway.php +++ b/project/src/model/gateways/ScientifiqueGateway.php @@ -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(); + } } \ No newline at end of file diff --git a/project/src/model/gateways/UserGateway.php b/project/src/model/gateways/UserGateway.php deleted file mode 100755 index 7563880..0000000 --- a/project/src/model/gateways/UserGateway.php +++ /dev/null @@ -1,113 +0,0 @@ -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']); - } -} diff --git a/project/src/model/mdl/MdlScientifique.php b/project/src/model/mdl/MdlScientifique.php index 8c389b8..52246f7 100755 --- a/project/src/model/mdl/MdlScientifique.php +++ b/project/src/model/mdl/MdlScientifique.php @@ -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 + ); + } } \ No newline at end of file diff --git a/project/src/model/mdl/MdlUser.php b/project/src/model/mdl/MdlUser.php index 7693ed5..e4b4bc1 100755 --- a/project/src/model/mdl/MdlUser.php +++ b/project/src/model/mdl/MdlUser.php @@ -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); diff --git a/project/src/templates/admin/ajouterScientifiques.html b/project/src/templates/admin/ajouterScientifiques.html index ef3a6a7..700550d 100644 --- a/project/src/templates/admin/ajouterScientifiques.html +++ b/project/src/templates/admin/ajouterScientifiques.html @@ -21,34 +21,45 @@

ajouterScientifiques




-
- +
- +
- +
- +
- +
Sexe : {% for se in sexe %}
- +
{% endfor %} @@ -57,7 +68,14 @@ Thematique : {% for se in themes %}
- +
{% endfor %} @@ -66,7 +84,14 @@ Difficulté : {% for se in difficultes %}
- +
{% endfor %}