diff --git a/Website/controllers/ControllerAdminAdministrators.php b/Website/controllers/ControllerAdminAdministrators.php index cc7bcf0..5f61fe3 100644 --- a/Website/controllers/ControllerAdminAdministrators.php +++ b/Website/controllers/ControllerAdminAdministrators.php @@ -19,19 +19,20 @@ class ControllerAdminAdministrators session_start(); try { - if($_SESSION["idAdminConnected"] != null){ - $this->twig =$twig; + if ($_SESSION["idAdminConnected"] != null) { + $this->twig = $twig; $this->vues = $vues; - + $this->mdAdministrator = new ModelAdministrator(); - + $administrators = $this->mdAdministrator->getAdministrators(); - + echo $twig->render($vues["adminAdministrators"], [ 'administrators' => $administrators, + 'error' => $_SESSION["error"], ]); - } - else { + $_SESSION["error"] = null; + } else { header("Location:/loginAdmin"); } } catch (PDOException $e) { @@ -41,27 +42,42 @@ class ControllerAdminAdministrators } } - function delete($param) { + function delete($param) + { $this->mdAdministrator->deleteAdministratorByID($param["id"]); header("Location:/admin/administrators"); } - function add($param) { - - $username = $_POST['username']; - $password = $_POST['password']; - - $Admin = [ - 'username' => $username, - 'password' => $password, - ]; - - $this->mdAdministrator->addAdministrator($Admin); - - header("Location:/admin/administrators"); + function add($param) + { + var_dump($_SERVER['REQUEST_METHOD'] !== 'POST'); + if ($_SERVER['REQUEST_METHOD'] !== 'POST') { + $_SESSION["error"]="Méthode non autorisée."; + } else { + $username = $_POST['username']; + $password = $_POST['password']; + $username = trim($_POST['username']); + $password = trim($_POST['password']); + var_dump(!isset($_POST['username']) || !isset($_POST['password']) || empty($username) || empty($password)); + if (!isset($username) || !isset($password) || empty($username) || empty($password)) { + $_SESSION["error"]="Veuillez remplir tous les champs."; + } else { + $Admin = [ + 'username' => $username, + 'password' => $password, + ]; + if ($this->mdAdministrator->verifyAdministratorByName($Admin) != null) { + $_SESSION["error"]="Cet admin existe déjà."; + } else { + $this->mdAdministrator->addAdministrator($Admin); + header("Location:/admin/administrators"); + } + } + } } - function updatemodal($param) { + function updatemodal($param) + { $administrator = $this->mdAdministrator->getAdministratorByID($param["id"]); @@ -70,7 +86,8 @@ class ControllerAdminAdministrators ]); } - function update($param) { + function update($param) + { $id = $_POST['id']; $username = $_POST['username']; @@ -81,8 +98,8 @@ class ControllerAdminAdministrators 'password' => $password, ]; - $this->mdAdministrator->updateAdministrator($id,$Admin); + $this->mdAdministrator->updateAdministrator($id, $Admin); header("Location:/admin/administrators"); } -} \ No newline at end of file +} diff --git a/Website/controllers/ControllerUser.php b/Website/controllers/ControllerUser.php index 00c7bcf..3614335 100644 --- a/Website/controllers/ControllerUser.php +++ b/Website/controllers/ControllerUser.php @@ -149,6 +149,7 @@ class ControllerUser $_SESSION["Score"] = 0; $difficulty = $_POST['difficulty']; $chapter = $_POST['chapter']; + $_SESSION['id_chapter'] = $_POST['chapter']; $difficultyIsOk = TRUE; $chapterIsOk = TRUE; @@ -238,9 +239,13 @@ class ControllerUser $Final[$c]["PlayerAnswer"] = $answer; $c = $c + 1; } + // $idPlayer = $_SESSION["idPlayerConnected"]; + // $idChapter = $_SESSION["chapter"]; + // $numScore = $this->mdPlayer->getJouerByPlayerAndChapter( $idPlayer, $idChapter); echo $this->twig->render($this->vues["viewScore"], [ 'score' => $_SESSION["Score"], 'Final' => $Final, + // 'numScore' => $numScore, ]); } } @@ -249,12 +254,11 @@ class ControllerUser function userStatus(){ if($_SESSION["idPlayerConnected"] != null){ - //$this->mdAdministrator = new ModelAdministrator(); + //$this->mdPlayer = new ModelPlayer(); - //$administrators = $this->mdAdministrator->getAdministrators(); + //$player = $this->mdPlayer->getPlayerByID($_SESSION["idAdminConnected"]); echo $this->twig->render($this->vues["userStatus"]); - } else { header("Location:/loginPlayer"); diff --git a/Website/gateways/GatewayAdministrator.php b/Website/gateways/GatewayAdministrator.php index aa40056..ebc749f 100755 --- a/Website/gateways/GatewayAdministrator.php +++ b/Website/gateways/GatewayAdministrator.php @@ -87,6 +87,19 @@ class GatewayAdministrator ); $results = $this->con->getResults(); + return $results[0]; + } + public function verifyAdministratorByName($administrator) + { + $query = "SELECT administrators.id FROM administrators WHERE username = :username"; + $this->con->executeQuery( + $query, + array( + ':username' => array($administrator['username'], PDO::PARAM_STR), + ) + ); + $results = $this->con->getResults(); + return $results[0]; } } diff --git a/Website/gateways/GatewayJouer.php b/Website/gateways/GatewayJouer.php new file mode 100644 index 0000000..151be1d --- /dev/null +++ b/Website/gateways/GatewayJouer.php @@ -0,0 +1,71 @@ +con = new Connection($dns, $user, $pass); + } + + public function addJouer($jouer) + { + $query = "INSERT into jouer(idChapter,idPlayer,numScore) values (:idChapter,:idPlayer,:numScore);"; + $this->con->executeQuery( + $query, + array( + 'idChapter' => array($jouer['idChapter'], PDO::PARAM_STR), + 'idPlayer' => array($jouer['idPlayer'], PDO::PARAM_STR), + ':numScore' => array($jouer['numScore'], PDO::PARAM_INT) + ) + ); + } + + public function getJouerByPlayerAndChapter(int $idPlayer, int $idChapter) + { + $query = "SELECT jouer.numScore FROM jouer,player WHERE jouer.idPlayer = :idPlayer AND jouer.idPlayer = player.id AND jouer.idChapter = :idChapter AND jouer.idChapter = chapter.id ;"; + $this->con->executeQuery( + $query, + array( + ':idChapter' => array($idChapter, PDO::PARAM_INT), + ':idPlayer' => array($idPlayer, PDO::PARAM_INT) + ) + ); + $results = $this->con->getResults(); + return $results[0]; + } + + public function updateJouer($idPlayer, $idChapter, $jouer) + { + $query = "UPDATE jouer SET numScore = :numScore WHERE idPlayer = :idPlayer AND idChapter = :idChapter;"; + $this->con->executeQuery( + $query, + array( + ':idChapter' => array($idChapter, PDO::PARAM_INT), + ':idPlayer' => array($idPlayer, PDO::PARAM_INT), + ':numScore' => array($jouer['numScore'], PDO::PARAM_INT) + ) + ); + } + + public function verifyJouer($idChapter, $idPlayer) + { + $query = "SELECT jouer.idChapter, jouer.idPlayer FROM jouer WHERE idPlayer = :idPlayer AND idChapter = :idChapter"; + $this->con->executeQuery( + $query, + array( + ':idChapter' => array($idChapter, PDO::PARAM_STR), + ':idPlayer' => array($idPlayer, PDO::PARAM_STR) + ) + ); + $results = $this->con->getResults(); + return $results[0]; + } +} \ No newline at end of file diff --git a/Website/models/ModelAdministrator.php b/Website/models/ModelAdministrator.php index 9d0b823..b0e2051 100644 --- a/Website/models/ModelAdministrator.php +++ b/Website/models/ModelAdministrator.php @@ -52,4 +52,9 @@ class ModelAdministrator $administratorsId = $this->gwAdministrator->verifyAdministrator($Administrator); return $administratorsId; } + public function verifyAdministratorByName($Administrator) + { + $administratorsId = $this->gwAdministrator->verifyAdministratorByName($Administrator); + return $administratorsId; + } } diff --git a/Website/models/ModelPlayer.php b/Website/models/ModelPlayer.php index 358b6c4..d579b04 100644 --- a/Website/models/ModelPlayer.php +++ b/Website/models/ModelPlayer.php @@ -3,15 +3,18 @@ namespace models; use gateways\GatewayPlayer; +use gateways\GatewayJouer; use classes\Player; class ModelPlayer { private $gwPlayer; + private $gwJouer; public function __construct() { $this->gwPlayer = new GatewayPlayer(); + $this->gwJouer = new GatewayJouer(); } public function addPlayer($player) @@ -40,4 +43,20 @@ class ModelPlayer { $this->gwPlayer->deletePlayerByID($id); } + + public function addJouer($jouer) + { + $this->gwJouer->addJouer($jouer); + } + + public function getJouerByPlayerAndChapter( $idPlayer, $idChapter) + { + $jouerDataArray = $this->gwJouer->getJouerByPlayerAndChapter($idPlayer,$idChapter); + return $jouerDataArray; + } + + public function updateJouer($idPlayer, $idChapter, $jouer) + { + $this->gwJouer->updateJouer($idPlayer, $idChapter, $jouer); + } } diff --git a/Website/templates/userStatus.twig b/Website/templates/userStatus.twig index cbcfccc..d0560d8 100644 --- a/Website/templates/userStatus.twig +++ b/Website/templates/userStatus.twig @@ -16,6 +16,7 @@
test
+