séparation user visitor

php
Anthony RICHARD 1 year ago
parent 45953fca03
commit 790f8b6fd0

@ -6,7 +6,7 @@ use config\Validation;
use model\MdlAdmin;
use Exception;
class AdminController
class AdminController extends UserController
{
public function showAllUsers(): void {
global $twig;

@ -23,8 +23,7 @@ class FrontController
$router->map('GET|POST', '/admin/[i:id]/[a:action]?', 'Admin');
$router->map('GET|POST', '/teacher/[i:id]/[a:action]?', 'Teacher');
$router->map('GET|POST', '/student/[i:id]/[a:action]?', 'Student');
$router->map('GET|POST', '/user/[a:action]?', 'User');
$router->map('GET|POST', '/user/[a:action]/[i:id]', 'User');
$router->map('GET|POST', '/visitor/[a:action]/[i:id]?', 'Visitor');
$twig->addGlobal('base', $altorouterPath);
@ -38,8 +37,8 @@ class FrontController
$action = Validation::val_action($match['params']['action'] ?? null);
$id = $match['params']['id'] ?? null;
if ($target == 'User') {
$userCtrl = new UserController();
if ($target == 'Visitor') {
$userCtrl = new VisitorController();
if (is_callable(array($userCtrl, $action)))
call_user_func_array(array($userCtrl, $action), array($match['params']));
}

@ -6,7 +6,7 @@ use model\MdlStudent;
use gateway\TranslationGateway;
use Exception;
class StudentController
class StudentController extends UserController
{
public function affAllVocab(): void
{

@ -6,7 +6,7 @@ use model\MdlTeacher;
use gateway\VocabularyListGateway;
use Exception;
class TeacherController
class TeacherController extends UserController
{
public function affAllStudent(): void
{

@ -10,20 +10,13 @@ use model\MdlStudent;
use model\VocabularyList;
use model\Translation;
class UserController
class UserController extends VisitorController
{
public function showAccountInfos(): void {
try {
global $twig;
$userID = Validation::filter_int($_GET['user'] ?? null);
$mdl = new MdlStudent();
$user = $mdl->getUser($userID);
echo $twig->render('myAccountView.html', ['user' => $user]);
}
catch (Exception $e){
throw new Exception("invalid user ID");
}
global $twig;
global $user;
echo $twig->render('myAccountView.html', ['user' => $user, 'userID' => $user->getId(), 'userRole' => $user->getRoles()]);
}
public function modifyPassword(): void {
@ -60,109 +53,4 @@ class UserController
throw new Exception("invalid entries");
}
}
public static function memory($match): void{
global $twig;
try{
$idVoc = Validation::filter_int($match['id'] ?? null);
$wordList = (new \gateway\TranslationGateway)->findByIdVoc($idVoc);
$wordShuffle = array();
shuffle($wordList);
$pairs = [];
$maxWords = 28;
for ($i = 0; $i < min(count($wordList), $maxWords / 2); $i++) {
$wordShuffle[] = $word1 = $wordList[$i]->getWord1();
$wordShuffle[] = $word2 = $wordList[$i]->getWord2();
$pairs[] = [$word1, $word2];
}
shuffle($wordShuffle);
echo $twig->render('memory.html', [
'wordShuffle' => $wordShuffle,
'pairs' => json_encode($pairs),
]);
}
catch (Exception $e){
throw new Exception("Erreur");
}
}
public function quiz($match): void
{
global $twig;
$vocabId = Validation::filter_int($match['id'] ?? null);
$vocabList = (new VocabularyListGateway())->findById($vocabId) ?? null;
if ($vocabList == null) throw new Exception("liste inconnue");
$mdl = new TranslationGateway();
$allTranslation = $mdl->findByIdVoc($vocabId);
$shuffle = $allTranslation;
shuffle($shuffle);
$questions = array();
$goodAnswers = array();
$allEnglishWords = array();
foreach ($allTranslation as $translation) {
$questions[] = $translation->getWord1();
$allEnglishWords[] = $translation->getWord2();
$goodAnswers[] = $translation->getWord2();
}
$answers = array();
for($i=0 ; $i< count($questions) ; $i++) {
$correctAnswer = $allTranslation[$i]->getWord2();
array_splice($allEnglishWords, array_search($correctAnswer, $allEnglishWords), 1);
$tab = array_rand(array_flip($allEnglishWords), 3);
array_push($allEnglishWords, $correctAnswer);
$tab[] = $correctAnswer;
shuffle($tab);
$answers[] = $tab;
}
echo $twig->render('quizView.html', ['questions' => $questions, 'answers' => $answers, 'goodAnswers' => $goodAnswers, 'listName' => $vocabList->getName()]);
}
public function login(): void {
global $twig;
echo $twig->render('login.html');
}
public function confirmLogin(): void {
$model = new MdlStudent();
if($_POST['logemail']!=null && $_POST['logpass']!=null) {
$login = strip_tags($_POST['logemail']);
$password = strip_tags($_POST['logpass']);
}
else throw new Exception("logmail ou logpass null");
if (!$this->checkLoginExist($login)) throw new Exception(("login invalide"));
$user = $model->connection($login, $password);
if ($user == null) throw new Exception("mot de passe invalide");
FrontController::home();
}
public function checkLoginExist(string $login): bool {
$mdl = new MdlStudent();
return $mdl->checkLoginExist($login);
}
public function disconnect(): void {
$mdl = new MdlStudent();
$mdl->deconnection();
FrontController::home();
}
public function resultatsJeux(): void{
global $twig;
echo $twig->render('resultatsJeux.html');
}
}

@ -0,0 +1,116 @@
<?php
namespace controller;
use config\Validation;
use gateway\TranslationGateway;
use gateway\VocabularyListGateway;
use model\MdlUser;
class VisitorController
{
public function memory($match): void{
global $twig;
try{
$idVoc = Validation::filter_int($match['id'] ?? null);
$wordList = (new \gateway\TranslationGateway)->findByIdVoc($idVoc);
$wordShuffle = array();
shuffle($wordList);
$pairs = [];
$maxWords = 28;
for ($i = 0; $i < min(count($wordList), $maxWords / 2); $i++) {
$wordShuffle[] = $word1 = $wordList[$i]->getWord1();
$wordShuffle[] = $word2 = $wordList[$i]->getWord2();
$pairs[] = [$word1, $word2];
}
shuffle($wordShuffle);
echo $twig->render('memory.html', [
'wordShuffle' => $wordShuffle,
'pairs' => json_encode($pairs),
]);
}
catch (Exception $e){
throw new Exception("Erreur");
}
}
public function quiz($match): void
{
global $twig;
$vocabId = Validation::filter_int($match['id'] ?? null);
$vocabList = (new VocabularyListGateway())->findById($vocabId) ?? null;
if ($vocabList == null) throw new Exception("liste inconnue");
$mdl = new TranslationGateway();
$allTranslation = $mdl->findByIdVoc($vocabId);
$shuffle = $allTranslation;
shuffle($shuffle);
$questions = array();
$goodAnswers = array();
$allEnglishWords = array();
foreach ($allTranslation as $translation) {
$questions[] = $translation->getWord1();
$allEnglishWords[] = $translation->getWord2();
$goodAnswers[] = $translation->getWord2();
}
$answers = array();
for($i=0 ; $i< count($questions) ; $i++) {
$correctAnswer = $allTranslation[$i]->getWord2();
array_splice($allEnglishWords, array_search($correctAnswer, $allEnglishWords), 1);
$tab = array_rand(array_flip($allEnglishWords), 3);
array_push($allEnglishWords, $correctAnswer);
$tab[] = $correctAnswer;
shuffle($tab);
$answers[] = $tab;
}
echo $twig->render('quizView.html', ['questions' => $questions, 'answers' => $answers, 'goodAnswers' => $goodAnswers, 'listName' => $vocabList->getName()]);
}
public function login(): void {
global $twig;
echo $twig->render('login.html');
}
public function confirmLogin(): void {
$model = new MdlUser();
if($_POST['logemail']!=null && $_POST['logpass']!=null) {
$login = strip_tags($_POST['logemail']);
$password = strip_tags($_POST['logpass']);
}
else throw new Exception("logmail ou logpass null");
if (!$this->checkLoginExist($login)) throw new Exception(("login invalide"));
$user = $model->connection($login, $password);
if ($user == null) throw new Exception("mot de passe invalide");
FrontController::home();
}
public function checkLoginExist(string $login): bool {
$mdl = new MdlUser();
return $mdl->checkLoginExist($login);
}
public function disconnect(): void {
$mdl = new MdlUser();
$mdl->deconnection();
FrontController::home();
}
public function resultatsJeux(): void{
global $twig;
echo $twig->render('resultatsJeux.html');
}
}

@ -31,21 +31,6 @@ class MdlStudent extends AbsModel
return $res;
}
public function getUser(int $id): User{
$gtw = new UserGateway();
return $gtw->findById($id);
}
public function modifyNickname(int $id, string $newNickname): void{
$gtw = new UserGateway();
$gtw->modifyNickname($id, $newNickname);
}
public function ModifyPassword(int $id, string $newPassword): void {
$gtw = new UserGateway();
$gtw->modifyPassword($id, $newPassword);
}
public function is(string $login, array $roles)
{
$gtw = new UserGateway();

@ -0,0 +1,31 @@
<?php
namespace model;
use gateway\UserGateway;
public class MdlUser extends AbsModel {
public function getUser(int $id): User{
$gtw = new UserGateway();
return $gtw->findById($id);
}
public function modifyNickname(int $id, string $newNickname): void{
$gtw = new UserGateway();
$gtw->modifyNickname($id, $newNickname);
}
public function ModifyPassword(int $id, string $newPassword): void {
$gtw = new UserGateway();
$gtw->modifyPassword($id, $newPassword);
}
public function is(string $login, array $roles)
{
$gtw = new UserGateway();
$user = $gtw->findUserByEmail($login);
if (!empty($user->getRoles())) return $user;
else return false;
}
}
Loading…
Cancel
Save