diff --git a/project/src/config/config.php b/project/src/config/config.php index a4d2110..9f7240e 100755 --- a/project/src/config/config.php +++ b/project/src/config/config.php @@ -4,9 +4,9 @@ namespace config; $config = [ "rep" => __DIR__.'/../', - "db" => ["dsn" => 'pgsql:host=localhost;dbname=postgres', + "db" => ["dsn" => 'pgsql:host=localhost;dbname=sae2a', "login" => 'postgres', - "mdp" => 'root'], + "mdp" => 'SKf43V4hmD7a'], "templates" => ["index" => 'vues/index.php', "pseudo" => 'pseudo.html', "jouer" => "jouer.html", diff --git a/project/src/controller/FrontController.php b/project/src/controller/FrontController.php index 3cae8f2..ab37ea5 100755 --- a/project/src/controller/FrontController.php +++ b/project/src/controller/FrontController.php @@ -38,7 +38,7 @@ class FrontController $router->map('GET|POST','/pseudo/[a:action]?','PseudoController'); $router->map('GET|POST','/admin/[a:action]?/[i:id]?','AdminController'); - $router->map('GET|POST','/[a:action]?','UserController'); + $router->map('GET|POST','/[a:action]?/[i:id]?','UserController'); try { session_start(); @@ -46,8 +46,17 @@ class FrontController die('Session start failed: ' . $e->getMessage()); } - if(isset($_SESSION['pseudo'])) + $dVue['idAdmin'] = false; + $dVue['idUser'] = false; + + if(isset($_SESSION['pseudo'])) { $dVue['pseudo'] = $_SESSION['pseudo']; + if (isset($_SESSION['isUser']) && $_SESSION['isUser']) { + $dVue['isUser'] = true; + } elseif (isset($_SESSION['idAdmin']) && $_SESSION['idAdmin']) { + $dVue['isAdmin'] = true; + } + } try { $match = $router->match(); @@ -84,6 +93,7 @@ class FrontController echo $twig->render('login.html'); } catch (Exception $e2) { $dVueErreur[] = 'Erreur inattendue !'; + $dVueErreur[] = $e2->getMessage(); echo $twig->render('erreur.html', ['dVueErreur' => $dVueErreur]); } diff --git a/project/src/controller/UserController.php b/project/src/controller/UserController.php index f1884af..f266216 100755 --- a/project/src/controller/UserController.php +++ b/project/src/controller/UserController.php @@ -9,6 +9,7 @@ use model\Connection; use model\GameGateway; use model\MdlDifficulte; use model\MdlJeu; +use model\MdlScientifique; use model\PseudoDejaPrisException; use model\ValidationException; use model\MdlUser; @@ -23,6 +24,39 @@ class UserController { echo $twig->render('accueil.html', ["dVue"=>$dVue]); } + public function historique(array $params) { + global $twig; + global $dVueErreur; + global $dVue; + + $ms = new MdlScientifique(); + + if (!isset($params['id'])) { + $page = 1; + } else { + $page = Validation::valPosInt($params['id']); + } + + $pseudo = Validation::valPseudo($_SESSION['pseudo'],$dVueErreur); + + $dVue['listeScientifiques'] = $ms->getHistoriqueParPage($pseudo,$page); + $dVue['pageMax'] = $ms->getMaxPagesHistorique($pseudo); + $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('historique.html',['dVue' => $dVue]); + } + public function joinParty(array $params) { global $twig; @@ -76,6 +110,7 @@ class UserController { $ug = new MdlUser(); if ($ug->login($_REQUEST['login'], $_REQUEST['password'])) { $_SESSION['pseudo'] = $_REQUEST['login']; + $_SESSION['idUser'] = true; $_SESSION['isLogged'] = true; header("Location: ."); } else { diff --git a/project/src/db.sql b/project/src/db.sql index a9d1a07..24aa91a 100755 --- a/project/src/db.sql +++ b/project/src/db.sql @@ -191,5 +191,7 @@ 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 +INSERT INTO decouvrir(idUtilisateur,idScientifique) VALUES (1337,1); + INSERT INTO Admin(id,email,password) VALUES (1, 'admin','$2y$10$juGnlWC9cS19popEKLZsYeir0Jl39k6hDl0dpaCix00FDcdiEbtmS'); -- mdp = test diff --git a/project/src/model/gateways/ScientifiqueGateway.php b/project/src/model/gateways/ScientifiqueGateway.php index d47dbb3..a230223 100755 --- a/project/src/model/gateways/ScientifiqueGateway.php +++ b/project/src/model/gateways/ScientifiqueGateway.php @@ -58,12 +58,40 @@ class ScientifiqueGateway return $this->con->getResults(); } + public function getHistorique(string $pseudoJoueur, int $currentPage, int $nbElemByPage) : array { + $query = 'SELECT s.* + FROM Scientifique s, Decouvrir d, Utilisateur u + WHERE s.id = d.idscientifique + AND d.idutilisateur = u.idJoueur + AND u.email = :pseudo + LIMIT :nbElem OFFSET :ind '; + $index = ($currentPage-1)*$nbElemByPage; + $this->con->executeQuery($query,array( + ':ind' => array($index,\PDO::PARAM_INT), + ':nbElem' => array($nbElemByPage,\PDO::PARAM_INT), + ':pseudo' => array($pseudoJoueur,\PDO::PARAM_STR) + )); + return $this->con->getResults(); + } + public function getNbScientifique() : int { $query = 'SELECT DISTINCT count(*) as val FROM Scientifique'; $this->con->executeQuery($query); return $this->con->getResults()[0]['val']; } + public function getNbScientifiqueHistorique(string $pseudoJoueur) : int { + $query = 'SELECT count(s.*) as val + FROM Scientifique s, Decouvrir d, Utilisateur u + WHERE s.id = d.idscientifique + AND d.idutilisateur = u.idJoueur + AND u.email = :pseudo'; + $params = array( + ':pseudo' => array($pseudoJoueur,\PDO::PARAM_STR) + ); + $this->con->executeQuery($query,$params); + return $this->con->getResults()[0]['val']; + } /** * @throws Exception @@ -117,7 +145,6 @@ class ScientifiqueGateway JOIN Reponse r ON q.id = r.idQuestion WHERE r.idScientifique = :idScientifique ORDER BY RANDOM() LIMIT 5"; - $params = [":idScientifique" => [$idScientifique, $this->con::PARAM_INT]]; $this->con->executeQuery($query, $params); diff --git a/project/src/model/mdl/MdlScientifique.php b/project/src/model/mdl/MdlScientifique.php index d23b818..f93560c 100755 --- a/project/src/model/mdl/MdlScientifique.php +++ b/project/src/model/mdl/MdlScientifique.php @@ -77,11 +77,42 @@ class MdlScientifique extends MdlBase{ } return $scientifiques; } - + public function getHistoriqueParPage(string $pseudoJoueur,int $page) { + $nbElemParPage = 20; + $pageMax = ceil($this->gw->getNbScientifiqueHistorique($pseudoJoueur)/$nbElemParPage); + print($pageMax); + if ($page <= 0) { + $page = 1; + } elseif ($page > $pageMax) { + $page = $pageMax; + } + $result = $this->gw->getHistorique($pseudoJoueur,$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 getMaxPagesHistorique(string $pseudo) : int { + $nbElemParPage = 20; + return ceil($this->gw->getNbScientifiqueHistorique($pseudo)/$nbElemParPage); + } public function editScientifique(Scientifique $s){ return $this->gw->editScientifique($s); diff --git a/project/src/templates/accueil.html b/project/src/templates/accueil.html index 9d7ca30..9f4acc3 100755 --- a/project/src/templates/accueil.html +++ b/project/src/templates/accueil.html @@ -23,11 +23,14 @@ {% endif %} +
Sexe : {{ scientifique.getSexe() }}
+Né(e) le {{ scientifique.getDateNess() }}
+Thematique : {{ scientifique.getThematique() }}
+Difficulté à trouver : {{ scientifique.getDifficulte() }}
+