From f7003a1309db7f9da5d13ce93abbd694d5dbf280 Mon Sep 17 00:00:00 2001 From: "anthony.richard" Date: Tue, 5 Dec 2023 23:48:30 +0100 Subject: [PATCH] code smells --- Project/php/controller/AdminController.php | 89 ++++-- Project/php/controller/FrontController.php | 59 ++-- Project/php/controller/StudentController.php | 18 +- Project/php/controller/TeacherController.php | 45 ++- Project/php/controller/UserController.php | 38 ++- Project/php/controller/VisitorController.php | 87 ++++-- Project/php/gateway/AbsGateway.php | 13 +- Project/php/gateway/GroupGateway.php | 159 +++++----- Project/php/gateway/TranslationGateway.php | 36 +-- Project/php/gateway/UserGateway.php | 291 +++++++++++++----- Project/php/gateway/VocabularyListGateway.php | 128 ++++---- Project/php/model/AbsModel.php | 19 +- Project/php/model/Group.php | 2 +- Project/php/model/MdlAdmin.php | 42 ++- Project/php/model/MdlStudent.php | 20 +- Project/php/model/MdlTeacher.php | 35 ++- Project/php/model/MdlUser.php | 13 +- Project/php/model/Translation.php | 2 +- Project/php/model/User.php | 16 +- Project/php/model/VocabularyList.php | 2 +- 20 files changed, 703 insertions(+), 411 deletions(-) diff --git a/Project/php/controller/AdminController.php b/Project/php/controller/AdminController.php index a6b99a0..f926705 100755 --- a/Project/php/controller/AdminController.php +++ b/Project/php/controller/AdminController.php @@ -8,60 +8,80 @@ use Exception; class AdminController extends UserController { - public function showAllUsers(): void { + private string $invgpID = "invalid group ID"; + + public function showAllUsers(): void + { global $twig; global $user; $model = new MdlAdmin(); $users = $model->getAllUsers(); - echo $twig->render('usersView.html', ['users' => $users, 'userID' => $user->getId(), 'userRole' => $user->getRoles()]); + echo $twig->render('usersView.html', ['users' => $users, + 'userID' => $user->getId(), + 'userRole' => $user->getRoles()]); } - public function showAllAdmins(): void { + public function showAllAdmins(): void + { global $twig; global $user; $model = new MdlAdmin(); $users = $model->getAllAdmins(); - echo $twig->render('usersView.html', ['users' => $users, 'userID' => $user->getId(), 'userRole' => $user->getRoles()]); + echo $twig->render('usersView.html', ['users' => $users, + 'userID' => $user->getId(), + 'userRole' => $user->getRoles()]); } - public function showAllTeachers(): void { + public function showAllTeachers(): void + { global $twig; global $user; $model = new MdlAdmin(); $users = $model->getAllTeachers(); - echo $twig->render('usersView.html', ['users' => $users, 'userID' => $user->getId(), 'userRole' => $user->getRoles()]); + echo $twig->render('usersView.html', ['users' => $users, + 'userID' => $user->getId(), + 'userRole' => $user->getRoles()]); } - public function showAllStudents(): void { + public function showAllStudents(): void + { global $twig; global $user; $model = new MdlAdmin(); $users = $model->getAllStudents(); - echo $twig->render('usersView.html', ['users' => $users, 'userID' => $user->getId(), 'userRole' => $user->getRoles()]); + echo $twig->render('usersView.html', ['users' => $users, + 'userID' => $user->getId(), + 'userRole' => $user->getRoles()]); } - public function removeUser(): void { + public function removeUser(): void + { try { $userToRemove = Validation::filter_int($_GET['userToRemove'] ?? null); $model = new MdlAdmin(); $model->removeUser($userToRemove); $this->showAllUsers(); - } - catch (Exception $e) { + } catch (Exception $e) { throw new Exception("invalid user ID"); } } - public function showAllGroups(): void { + public function showAllGroups(): void + { global $twig; global $user; $model = new MdlAdmin(); $groups = $model->getAllGroups(); $unassignedUsers = $model->getUnassignedUsers(); - echo $twig->render('manageGroupView.html', ['groups' => $groups, 'unassignedUsers' => $unassignedUsers, 'userID' => $user->getId(), 'userRole' => $user->getRoles()]); + + echo $twig->render('manageGroupView.html', ['groups' => $groups, + 'unassignedUsers' => $unassignedUsers, + 'userID' => $user->getId(), + 'userRole' => $user->getRoles()]); } - public function showGroupDetails(): void { + public function showGroupDetails(): void + { try { global $twig; global $user; @@ -71,14 +91,20 @@ class AdminController extends UserController $users = $model->getUsersOfGroup($selectedGroup); $unassignedUsers = $model->getUnassignedUsers(); - echo $twig->render('manageGroupView.html', ['groups' => $groups, 'selectedGroup' => $selectedGroup, 'users' => $users, 'unassignedUsers' => $unassignedUsers, 'userID' => $user->getId(), 'userRole' => $user->getRoles()]); - } - catch (Exception $e) { - throw new Exception("invalid group ID"); + echo $twig->render('manageGroupView.html', ['groups' => $groups, + 'selectedGroup' => $selectedGroup, + 'users' => $users, + 'unassignedUsers' => $unassignedUsers, + 'userID' => $user->getId(), + 'userRole' => $user->getRoles()]); + + } catch (Exception $e) { + throw new Exception($this->invgpID); } } - public function removeUserFromGroup(): void { + public function removeUserFromGroup(): void + { try { $userToRemove = Validation::filter_int($_GET['userToRemove'] ?? null); $groupID = Validation::filter_int($_GET['selectedGroup'] ?? null); @@ -86,25 +112,25 @@ class AdminController extends UserController $model->removeUserFromGroup($userToRemove); $_GET['selectedGroup'] = $groupID; $this->showGroupDetails(); - } - catch (Exception $e) { - throw new Exception("invalid group ID"); + } catch (Exception $e) { + throw new Exception($this->invgpID); } } - public function removeGroup(): void { + public function removeGroup(): void + { try { $selectedGroup = Validation::filter_int($_GET['selectedGroup'] ?? null); $model = new MdlAdmin(); $model->removeGroup($selectedGroup); $this->showAllGroups(); - } - catch (Exception $e) { - throw new Exception("invalid group ID"); + } catch (Exception $e) { + throw new Exception($this->invgpID); } } - public function addGroup(): void { + public function addGroup(): void + { try { $num = Validation::filter_int($_POST['num'] ?? null); $year = Validation::filter_int($_POST['year'] ?? null); @@ -114,13 +140,13 @@ class AdminController extends UserController $groupID = $model->addGroup($num, $year, $sector); $_GET['selectedGroup'] = $groupID; $this->showGroupDetails(); - } - catch (Exception $e) { + } catch (Exception $e) { throw new Exception("invalid form"); } } - public function addUserToGroup(): void { + public function addUserToGroup(): void + { try { $userToAdd = Validation::filter_int($_GET['userToAdd'] ?? null); $group = Validation::filter_int($_GET['groupID'] ?? null); @@ -128,8 +154,7 @@ class AdminController extends UserController $model->addUserToGroup($userToAdd, $group); $_GET['selectedGroup'] = $group; $this->showGroupDetails(); - } - catch (Exception $e) { + } catch (Exception $e) { throw new Exception("invalid IDs"); } } diff --git a/Project/php/controller/FrontController.php b/Project/php/controller/FrontController.php index bb203c7..09991f8 100755 --- a/Project/php/controller/FrontController.php +++ b/Project/php/controller/FrontController.php @@ -19,11 +19,13 @@ class FrontController $router = new \AltoRouter(); $router->setBasePath($altorouterPath); + $getpost = 'GET|POST'; + $router->map('GET', '/', 'User'); - $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', '/visitor/[a:action]', 'Visitor'); + $router->map($getpost, '/admin/[i:id]/[a:action]?', 'Admin'); + $router->map($getpost, '/teacher/[i:id]/[a:action]?', 'Teacher'); + $router->map($getpost, '/student/[i:id]/[a:action]?', 'Student'); + $router->map($getpost, '/visitor/[a:action]', 'Visitor'); $twig->addGlobal('base', $altorouterPath); @@ -31,51 +33,64 @@ class FrontController if (!$match) { throw new Exception("Erreur 404 page not found"); - } - if ($match) { + } else { $target = $match['target'] ?? null; $action = Validation::val_action($match['params']['action'] ?? null); $id = $match['params']['id'] ?? null; if ($target == 'Visitor') { $userCtrl = new VisitorController(); - if (is_callable(array($userCtrl, $action))) + + if (is_callable(array($userCtrl, $action))) { call_user_func_array(array($userCtrl, $action), array($match['params'])); - } - else { - if ($target == null) throw new Exception("pas de target"); + } + } else { + if ($target == null) { + throw new Exception("pas de target"); + } if (isset($_SESSION['login']) && isset($_SESSION['roles'])) { $_SESSION['login'] = strip_tags($_SESSION['login']); - for ($i=0 ; $igetId() != $id) throw new Exception("erreur 403 permission denied"); + $user = call_user_func_array( + array($mdl, 'is'), + array($_SESSION['login'], + $_SESSION['roles']) + ); + + if ($target == 'User' && $action == null) { + UserController::home(); + } elseif (!$user || $user->getId() != $id) { + throw new Exception("erreur 403 permission denied"); + } $controller = '\\controller\\' . $target . 'Controller'; $controller = new $controller; - if (is_callable(array($controller, $action))) + if (is_callable(array($controller, $action))) { call_user_func_array(array($controller, $action), array($match['params'])); + } } + } elseif ($target == 'User' && $action == null) { + UserController::home(); + } else { + (new UserController())->login(); } - else if ($target == 'User' && $action == null) UserController::home(); - else (new UserController())->login(); } } - } - catch - (Exception $e) { + } catch (Exception $e) { $dVueEreur[] = $e->getMessage(); echo $twig->render('erreur.html', ['dVueEreur' => $dVueEreur]); - } + } } } \ No newline at end of file diff --git a/Project/php/controller/StudentController.php b/Project/php/controller/StudentController.php index e79c5b6..1db5439 100755 --- a/Project/php/controller/StudentController.php +++ b/Project/php/controller/StudentController.php @@ -1,6 +1,7 @@ render('manageVocabView.html', ['vocabularies' => $vocab]); } - public function ListVocChoice(): void { + public function listVocChoice(): void + { global $twig; global $user; $jeu = $_POST['jeu']; $model = new MdlStudent(); $voc = $model->getVocByGroup($user->getGroup()); - echo $twig->render('vocabList.html', ['jeu' => $jeu, 'vocabularies' => $voc, 'userID' => $user->getId(), 'userRole' => $user->getRoles()]); + echo $twig->render('vocabList.html', ['jeu' => $jeu, + 'vocabularies' => $voc, + 'userID' => $user->getId(), + 'userRole' => $user->getRoles()]); } - public function gameChoice(): void { + public function gameChoice(): void + { global $twig; global $user; - $model = new MdlStudent(); - $voc = $model->getAll(); - echo $twig->render('gamesList.html',[ 'userID' => $user->getId(), 'userRole' => $user->getRoles()]); + echo $twig->render('gamesList.html', [ 'userID' => $user->getId(), 'userRole' => $user->getRoles()]); } -} \ No newline at end of file +} diff --git a/Project/php/controller/TeacherController.php b/Project/php/controller/TeacherController.php index 68b520e..81a96b9 100755 --- a/Project/php/controller/TeacherController.php +++ b/Project/php/controller/TeacherController.php @@ -1,6 +1,7 @@ findByUser($user->getId()); $groups = $mdl->getAllGroups(); - echo $twig->render('manageVocabListView.html', ['vocabularies' => $vocabularies, 'groups' => $groups, 'userID' => $user->getId(), 'userRole' => $user->getRoles()]); + echo $twig->render('manageVocabListView.html', ['vocabularies' => $vocabularies, + 'groups' => $groups, + 'userID' => $user->getId(), + 'userRole' => $user->getRoles()]); } - public function DelById():void{ + public function delById():void + { global $user; $mdl = new MdlTeacher(); $id = Validation::filter_int($_GET['vocabID'] ?? null); $mdl->removeVocById($id); $this->affAllVocab(); } - public function getContent():void{ + + public function getContent():void + { global $twig; global $user; $mdl = new MdlTeacher(); @@ -35,10 +42,17 @@ class TeacherController extends UserController $groupsVocab = $mdl->findGroupVocab($name); $groupsNoVocab = $mdl->findGroupNoVocab($name); $content= $mdl->findByIdVoc($name); - echo $twig->render('manageVocabListView.html', ['vocabularies' => $vocabularies, 'groupsVocab' => $groupsVocab, 'groupsNoVocab' => $groupsNoVocab, 'userID' => $user->getId(), 'userRole' => $user->getRoles(), 'content' => $content, 'vocabID' => $name]); + echo $twig->render('manageVocabListView.html', ['vocabularies' => $vocabularies, + 'groupsVocab' => $groupsVocab, + 'groupsNoVocab' => $groupsNoVocab, + 'userID' => $user->getId(), + 'userRole' => $user->getRoles(), + 'content' => $content, + 'vocabID' => $name]); } - public function addVocabToGroup():void { + public function addVocabToGroup():void + { global $twig; global $user; $vocabID = Validation::filter_int($_GET['vocabID'] ?? null); @@ -49,7 +63,8 @@ class TeacherController extends UserController $this->getContent(); } - public function removeVocabFromGroup():void { + public function removeVocabFromGroup():void + { global $twig; global $user; $vocabID = Validation::filter_int($_GET['vocabID'] ?? null); @@ -61,31 +76,35 @@ class TeacherController extends UserController } - public function showVocabListForm(): void { + public function showVocabListForm(): void + { global $twig; global $user; echo $twig->render('addVocabList.html', ['userID' => $user->getId(), 'userRole' => $user->getRoles() ]); } - public function addVocabList():void { + public function addVocabList():void + { global $user; $mdl = new MdlTeacher(); $name = Validation::filter_str_simple($_POST['listName'] ?? null); $words = array(); for ($i = 0; $i <= 20; $i++) { - if (empty($_POST['frenchWord' . $i]) || empty($_POST['englishWord' . $i])) break; + if (empty($_POST['frenchWord' . $i]) || empty($_POST['englishWord' . $i])) { + break; + } $frenchWord = Validation::filter_str_simple($_POST['frenchWord' . $i] ?? null); $englishWord = Validation::filter_str_simple($_POST['englishWord' . $i] ?? null); $words[] = array($frenchWord, $englishWord); } - if (count($words) % 2 == 1) throw new Exception("il manque un mot"); - else { + if (count($words) % 2 == 1) { + throw new Exception("il manque un mot"); + } else { $mdl->addVocabList($user->getId(), $name, "", $words); $this->affAllVocab(); } } - -} \ No newline at end of file +} diff --git a/Project/php/controller/UserController.php b/Project/php/controller/UserController.php index 1d4e89f..56b9907 100755 --- a/Project/php/controller/UserController.php +++ b/Project/php/controller/UserController.php @@ -13,34 +13,38 @@ use model\Translation; class UserController extends VisitorController { - - public function showAccountInfos(): void { + public function showAccountInfos(): void + { global $twig; global $user; - echo $twig->render('myAccountView.html', ['user' => $user, 'userID' => $user->getId(), 'userRole' => $user->getRoles()]); + echo $twig->render('myAccountView.html', ['user' => $user, + 'userID' => $user->getId(), + 'userRole' => $user->getRoles()]); } - public function modifyPassword(): void { + public function modifyPassword(): void + { try { global $user; $currentPassword = Validation::val_password($_POST['currentPassword'] ?? null); $newPassword = Validation::val_password($_POST['newPassword'] ?? null); $confirmNewPassword = Validation::val_password($_POST['confirmNewPassword'] ?? null); - if (!password_verify($currentPassword, $user->getPassword()) || $newPassword != $confirmNewPassword) + if (!password_verify($currentPassword, $user->getPassword()) || $newPassword != $confirmNewPassword) { throw new Exception(""); + } $mdl = new MdlUser(); - $mdl->ModifyPassword($user->getId(), password_hash($newPassword, null)); + $mdl->modifyPassword($user->getId(), password_hash($newPassword, null)); $user = $mdl->getUserById($user->getId()); $this->showAccountInfos(); - } - catch (Exception $e){ + } catch (Exception $e) { throw new Exception("invalid entries".$e->getLine()); } } - public function modifyNickname(): void { + public function modifyNickname(): void + { try { global $user; $newNickname = Validation::filter_str_nospecialchar($_POST['newNickname'] ?? null); @@ -48,20 +52,20 @@ class UserController extends VisitorController $mdl->modifyNickname($user->getId(), $newNickname); $user = $mdl->getUserById($user->getId()); $this->showAccountInfos(); - } - catch (Exception $e){ + } catch (Exception $e) { throw new Exception("invalid entries". $e->getMessage()); } } - public static function home(): void { + public static function home(): void + { global $twig; global $user; - if(isset($user)){ + + if (isset($user)) { echo $twig->render('home.html', ['userID' => $user->getId(), 'userRole' => $user->getRoles()]); - } - else{ - echo $twig->render('home.html', ); + } else { + echo $twig->render('home.html'); } } -} \ No newline at end of file +} diff --git a/Project/php/controller/VisitorController.php b/Project/php/controller/VisitorController.php index a4d0d61..f196478 100755 --- a/Project/php/controller/VisitorController.php +++ b/Project/php/controller/VisitorController.php @@ -12,11 +12,12 @@ use Exception; class VisitorController { - public function memory($match): void{ + public function memory(): void + { global $twig; global $user; - try{ + try { $idVoc = Validation::filter_int($_POST['idVoc'] ?? 4); $wordList = (new MdlTeacher())->findByIdVoc($idVoc); $name = ((new MdlStudent())->getVocabById($idVoc))->getName(); @@ -36,7 +37,7 @@ class VisitorController shuffle($wordShuffle); - if(isset($user)) { + if (isset($user)) { echo $twig->render('memory.html', [ 'wordShuffle' => $wordShuffle, 'pairs' => json_encode($pairs), @@ -44,8 +45,7 @@ class VisitorController 'userID' => $user->getID(), 'userRole' => $user->getRoles() ]); - } - else{ + } else { echo $twig->render('memory.html', [ 'wordShuffle' => $wordShuffle, 'pairs' => json_encode($pairs), @@ -53,19 +53,23 @@ class VisitorController ]); } - } - catch (Exception $e){ + } catch (Exception $e) { throw new Exception($e->getMessage()); } } - public function quiz($match): void + public function quiz(): void { global $twig; global $user; + try { $vocabId = Validation::filter_int($_POST['idVoc'] ?? 4); $vocabList = (new VocabularyListGateway())->findById($vocabId) ?? null; - if ($vocabList == null) throw new Exception("liste inconnue"); + + if ($vocabList == null) { + throw new Exception("liste inconnue"); + } + $mdl = new TranslationGateway(); $allTranslation = $mdl->findByIdVoc($vocabId); $shuffle = $allTranslation; @@ -86,9 +90,11 @@ class VisitorController for ($i = 0; $i < count($questions); $i++) { $correctAnswer = $allTranslation[$i]->getWord2(); array_splice($allEnglishWords, array_search($correctAnswer, $allEnglishWords), 1); - if(count($allEnglishWords) < 3) { + + if (count($allEnglishWords) < 3) { throw new Exception("pas assez de vocabulaire"); } + $tab = array_rand(array_flip($allEnglishWords), 3); array_push($allEnglishWords, $correctAnswer); @@ -113,57 +119,84 @@ class VisitorController 'goodAnswers' => $goodAnswers, 'listName' => $vocabList->getName()]); } - } - catch (Exception $e){ + } catch (Exception $e) { throw new Exception($e->getMessage()); } } - public function login(): void { + public function login(): void + { global $twig; echo $twig->render('login.html'); } - public function confirmLogin(): void { + public function confirmLogin(): void + { global $user; $model = new MdlUser(); - if($_POST['logemail']!=null && $_POST['logpass']!=null) { + + 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")); } - 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"); + + if ($user == null) { + throw new Exception("mot de passe invalide"); + } + UserController::home(); } - public function checkLoginExist(string $login): bool { + public function checkLoginExist(string $login): bool + { $mdl = new MdlUser(); return $mdl->checkLoginExist($login); } - public function disconnect(): void { + public function disconnect(): void + { $mdl = new MdlUser(); $mdl->deconnection(); UserController::home(); } - public function resultatsQuiz(): void{ + public function resultatsQuiz(): void + { global $twig; global $user; $score = Validation::filter_int(intval($_POST['score'] ?? null)); $len = Validation::filter_int(intval($_POST['total']??null)); $res = $score . '/'. $len; - if(isset($user)) echo $twig->render('resultatsJeux.html', ['userID' => $user->getId(),'res' => $res, 'userRole' => $user->getRoles()]); - else echo $twig->render('resultatsJeux.html', ['res' => $res]); + + if (isset($user)) { + echo $twig->render('resultatsJeux.html', ['userID' => $user->getId(), + 'res' => $res, + 'userRole' => $user->getRoles()]); + } else { + echo $twig->render('resultatsJeux.html', ['res' => $res]); + } } - public function resultatsMemory(): void{ + public function resultatsMemory(): void + { global $twig; global $user; $score = Validation::filter_int(intval($_POST['score'] ?? null)); - if(isset($user)) echo $twig->render('resultatsJeux.html', ['userID' => $user->getId(),'res' => $score, 'userRole' => $user->getRoles()]); - else echo $twig->render('resultatsJeux.html', ['res' => $score]); + + if (isset($user)) { + echo $twig->render('resultatsJeux.html', ['userID' => $user->getId(), + 'res' => $score, + 'userRole' => $user->getRoles()]); + } else { + echo $twig->render('resultatsJeux.html', ['res' => $score]); + } } -} \ No newline at end of file +} diff --git a/Project/php/gateway/AbsGateway.php b/Project/php/gateway/AbsGateway.php index 0b3ebfc..2101546 100755 --- a/Project/php/gateway/AbsGateway.php +++ b/Project/php/gateway/AbsGateway.php @@ -10,15 +10,16 @@ abstract class AbsGateway { protected Connection $con; - public function __construct() { + public function __construct() + { global $dsn; global $login; global $password; $this->con = new Connection($dsn, $login, $password); } - public abstract function add(array $parameters): int; - public abstract function remove(int $id): void; - public abstract function findAll(): array; - public abstract function findById(int $id); -} \ No newline at end of file + abstract public function add(array $parameters): int; + abstract public function remove(int $id): void; + abstract public function findAll(): array; + abstract public function findById(int $id); +} diff --git a/Project/php/gateway/GroupGateway.php b/Project/php/gateway/GroupGateway.php index 16a03ba..de898b3 100755 --- a/Project/php/gateway/GroupGateway.php +++ b/Project/php/gateway/GroupGateway.php @@ -10,28 +10,23 @@ use model\Group; class GroupGateway extends AbsGateway { - public function __construct(){ - parent::__construct(); - } - - public function add(array $parameters): int //require 4 elements + public function add(array $parameters): int { - try{ + try { $query = "INSERT INTO Group_ values(null, :num,:year,:sec)"; - $args = array(':num'=>array($parameters[0],PDO::PARAM_INT), - ':year'=>array($parameters[1],PDO::PARAM_INT), - ':sec'=>array($parameters[2],PDO::PARAM_STR)); - $this->con->ExecuteQuery($query,$args); + $args = array(':num'=>array($parameters[0] ,PDO::PARAM_INT), + ':year'=>array($parameters[1] ,PDO::PARAM_INT), + ':sec'=>array($parameters[2] ,PDO::PARAM_STR)); + $this->con->ExecuteQuery($query, $args); return $this->con->lastInsertId(); - } - catch (PDOException $e){ + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } public function remove(int $id): void { - try{ + try { $query="UPDATE User_ SET groupID=NULL WHERE groupID=:id"; $args = array(':id' => array($id, PDO::PARAM_INT)); $this->con->executeQuery($query, $args); @@ -41,123 +36,137 @@ class GroupGateway extends AbsGateway $query = "DELETE FROM Group_ WHERE id=:id "; $args = array(':id' => array($id, PDO::PARAM_INT)); $this->con->executeQuery($query, $args); - } - catch (PDOException $e){ + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } public function findAll(): array { - try{ + try { $query = "SELECT * FROM Group_"; $this->con->ExecuteQuery($query); $res = $this->con->getResults(); - $tab_group=[]; - foreach($res as $r){ - $tab_group[]=new Group($r['id'],$r['num'],$r['year'],$r['sector']); + $tabGroup=[]; + + foreach ($res as $r) { + $tabGroup[]=new Group($r['id'], $r['num'], $r['year'], $r['sector']); } - Return $tab_group; - } - catch(PDOException $e ){ + + return $tabGroup; + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } public function findById(int $id):array { - try{ + try { $query = "SELECT * FROM Group_ WHERE id = :id"; $args = array(':id'=>array($id, PDO::PARAM_INT)); $this->con->executeQuery($query, $args); return $this->con->getResults(); - } - catch (PDOException $e){ + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } - public function findByNum(String $num): array{ - try{ + public function findByNum(String $num): array + { + try { $query = "SELECT * FROM Group_ g WHERE g.num = :num"; - $args = array(':num'=>array($num,PDO::PARAM_INT)); - $this->con->ExecuteQuery($query,$args); + $args = array(':num'=>array($num ,PDO::PARAM_INT)); + $this->con->ExecuteQuery($query, $args); $res = $this->con->getResults(); - $tab_group=[]; - foreach($res as $r){ - $tab_group[]=new Group($r['id'],$r['num'],$r['year'],$r['sector']); + $tabGroup=[]; + + foreach ($res as $r) { + $tabGroup[]=new Group($r['id'], $r['num'], $r['year'], $r['sector']); } - Return $tab_group; - } - catch(PDOException $e ){ + return $tabGroup; + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } - public function modifGroupById(int $id, int $num, int $year ,String $sector):void{ - try{ + public function modifGroupById(int $id, int $num, int $year ,String $sector):void + { + try { $query = "UPDATE Group_ SET num=:num, year=:year, sector=:sector WHERE id=:id"; - $args = array(':id'=>array($id,PDO::PARAM_INT), - ':num'=>array($num,PDO::PARAM_INT), - ':year'=>array($year,PDO::PARAM_INT), - ':sector'=>array($sector,PDO::PARAM_STR)); - $this->con->ExecuteQuery($query,$args); - } - catch (PDOException $e){ + $args = array(':id'=>array($id ,PDO::PARAM_INT), + ':num'=>array($num ,PDO::PARAM_INT), + ':year'=>array($year ,PDO::PARAM_INT), + ':sector'=>array($sector ,PDO::PARAM_STR)); + $this->con->ExecuteQuery($query, $args); + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } - public function addVocabToGroup(int $vocab, int $group): void { + public function addVocabToGroup(int $vocab, int $group): void + { try { $query = "INSERT INTO Practice VALUES (:vocabID, :groupID)"; - $args = array(':vocabID'=>array($vocab,PDO::PARAM_INT), - ':groupID'=>array($group,PDO::PARAM_INT)); - $this->con->ExecuteQuery($query,$args); - } - catch (PDOException $e){ + $args = array(':vocabID'=>array($vocab ,PDO::PARAM_INT), + ':groupID'=>array($group ,PDO::PARAM_INT)); + $this->con->ExecuteQuery($query, $args); + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } - public function removeVocabFromGroup(int $vocab, int $group): void { + public function removeVocabFromGroup(int $vocab, int $group): void + { try { $query = "DELETE FROM Practice WHERE vocabID=:vocabID and groupID=:groupID"; - $args = array(':vocabID'=>array($vocab,PDO::PARAM_INT), - ':groupID'=>array($group,PDO::PARAM_INT)); - $this->con->ExecuteQuery($query,$args); - } - catch (PDOException $e){ + $args = array(':vocabID'=>array($vocab ,PDO::PARAM_INT), + ':groupID'=>array($group ,PDO::PARAM_INT)); + $this->con->ExecuteQuery($query, $args); + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } - public function findGroupVocab(int $vocab) : array{ - $query = "SELECT g.* FROM Practice p, Group_ g WHERE g.id=p.groupID AND p.vocabID=:vocabID;"; - $args = array(':vocabID'=>array($vocab,PDO::PARAM_INT)); - $this->con->ExecuteQuery($query,$args); - $results = $this->con->getResults(); - $tab = array(); - - foreach ($results as $row) $tab[] = new Group($row['id'],$row['num'],$row['year'],$row['sector']); + public function findGroupVocab(int $vocab) : array + { + try { + $query = "SELECT g.* FROM Practice p, Group_ g WHERE g.id=p.groupID AND p.vocabID=:vocabID;"; + $args = array(':vocabID'=>array($vocab ,PDO::PARAM_INT)); + $this->con->ExecuteQuery($query, $args); + $results = $this->con->getResults(); + $tab = array(); + + foreach ($results as $row) { + $tab[] = new Group($row['id'], $row['num'], $row['year'], $row['sector']); + } - return $tab; + return $tab; + } catch (PDOException $e) { + throw new Exception($e->getMessage()); + } } - public function findGroupNoVocab(int $vocab) : array { - $query = "SELECT * FROM Group_ WHERE id NOT IN (SELECT groupID FROM Practice Where vocabID=:vocabID);"; - $args = array(':vocabID'=>array($vocab,PDO::PARAM_INT)); - $this->con->ExecuteQuery($query,$args); - $results = $this->con->getResults(); - $tab = array(); - - foreach ($results as $row) $tab[] = new Group($row['id'],$row['num'],$row['year'],$row['sector']); + public function findGroupNoVocab(int $vocab) : array + { + try { + $query = "SELECT * FROM Group_ WHERE id NOT IN (SELECT groupID FROM Practice Where vocabID=:vocabID);"; + $args = array(':vocabID'=>array($vocab ,PDO::PARAM_INT)); + $this->con->ExecuteQuery($query, $args); + $results = $this->con->getResults(); + $tab = array(); + + foreach ($results as $row) { + $tab[] = new Group($row['id'], $row['num'], $row['year'], $row['sector']); + } - return $tab; + return $tab; + } catch (PDOException $e) { + throw new Exception($e->getMessage()); + } } -} \ No newline at end of file +} diff --git a/Project/php/gateway/TranslationGateway.php b/Project/php/gateway/TranslationGateway.php index 5ccf88b..a8489a6 100755 --- a/Project/php/gateway/TranslationGateway.php +++ b/Project/php/gateway/TranslationGateway.php @@ -9,18 +9,18 @@ use PDOException; class TranslationGateway extends AbsGateway { - private function addWord(string $word): void { + private function addWord(string $word): void + { try { $query = "INSERT INTO Vocabulary VALUES (:word) ON DUPLICATE KEY UPDATE word=:word"; $args = array(':word' => array($word, PDO::PARAM_STR)); $this->con->executeQuery($query, $args); - } - catch (PDOException $e) { + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } - public function add(array $parameters): int // require 4 elements + public function add(array $parameters): int { try { $this->addWord($parameters[0]); @@ -31,19 +31,18 @@ class TranslationGateway extends AbsGateway ':idVoc' => array($parameters[2], PDO::PARAM_INT)); $this->con->executeQuery($query, $args); return $this->con->lastInsertId(); - } - catch (PDOException $e) { + } catch (PDOException $e) { throw new Exception($e->getMessage().+$e->getLine()); } } - public function remove(int $id): void { + public function remove(int $id): void + { try { $query = "DELETE FROM Translate WHERE id=:id"; $args = array(':id' => array($id, PDO::PARAM_INT)); $this->con->executeQuery($query, $args); - } - catch (PDOException $e) { + } catch (PDOException $e) { throw new Exception($e->getMessage().+$e->getLine()); } } @@ -55,11 +54,12 @@ class TranslationGateway extends AbsGateway $this->con->executeQuery($query); $results = $this->con->getResults(); $tab = array(); - foreach ($results as $row) $tab[] = new Translation($row['id'], $row['firstWord'], $row['secondWord'], $row['listVoc']); + foreach ($results as $row) { + $tab[] = new Translation($row['id'], $row['firstWord'], $row['secondWord'], $row['listVoc']); + } return $tab; - } - catch (PDOException $e) { + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } @@ -71,8 +71,7 @@ class TranslationGateway extends AbsGateway $args = array(':id' => array($id, PDO::PARAM_INT)); $this->con->executeQuery($query, $args); return $this->con->getResults(); - } - catch (PDOException $e){ + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } @@ -85,12 +84,13 @@ class TranslationGateway extends AbsGateway $this->con->executeQuery($query, $args); $results = $this->con->getResults(); $tab = array(); - foreach ($results as $row) $tab[] = new Translation($row['id'], $row['firstWord'], $row['secondWord'], $row['listVoc']); + foreach ($results as $row) { + $tab[] = new Translation($row['id'], $row['firstWord'], $row['secondWord'], $row['listVoc']); + } return $tab; - } - catch (PDOException $e) { + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } -} \ No newline at end of file +} diff --git a/Project/php/gateway/UserGateway.php b/Project/php/gateway/UserGateway.php index 2f93662..aaf5cbc 100755 --- a/Project/php/gateway/UserGateway.php +++ b/Project/php/gateway/UserGateway.php @@ -9,15 +9,12 @@ use Exception; class UserGateway extends AbsGateway { - public function __construct() - { - parent::__construct(); - } - public function add(array $parameters): int //require 9 elements { try { - $query = "INSERT INTO User_ VALUES (NULL, :password, :email, :name, :surname, :nickname, :image, :extraTime, :group)"; + $query = "INSERT INTO User_ + VALUES (NULL, :password, :email, :name, :surname, :nickname, :image, :extraTime, :group)"; + $args = array(':password' => array($parameters[0], PDO::PARAM_STR), ':email' => array($parameters[1], PDO::PARAM_STR), ':name' => array($parameters[2], PDO::PARAM_STR), @@ -26,6 +23,7 @@ class UserGateway extends AbsGateway ':image' => array($parameters[5], PDO::PARAM_STR), ':extraTime' => array($parameters[6], PDO::PARAM_BOOL), ':group' => array($parameters[7], PDO::PARAM_INT)); + $this->con->executeQuery($query, $args); $userID = $this->con->lastInsertId(); @@ -37,8 +35,7 @@ class UserGateway extends AbsGateway } return $userID; - } - catch (PDOException $e){ + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } @@ -55,8 +52,7 @@ class UserGateway extends AbsGateway $query="DELETE FROM User_ WHERE id=:id"; $this->con->executeQuery($query, $args); - } - catch(PDOException $e ){ + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } @@ -67,39 +63,77 @@ class UserGateway extends AbsGateway $this->con->executeQuery("SELECT * FROM User_"); $results = $this->con->getResults(); $tab = array(); - foreach ($results as $row) - $tab[] = new User($row['id'], $row['password'], $row['email'], $row['name'], $row['surname'], $row['nickname'], $row['image'], $row['extraTime'], $row['groupID'], $this->getRoles($row['id'])); + + foreach ($results as $row) { + $tab[] = new User( + $row['id'], + $row['password'], + $row['email'], + $row['name'], + $row['surname'], + $row['nickname'], + $row['image'], + $row['extraTime'], + $row['groupID'], + $this->getRoles($row['id'])); + } + return $tab; - } - catch(PDOException $e ){ + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } - public function findAllAdmins(): array { + public function findAllAdmins(): array + { try { $this->con->executeQuery("SELECT u.* FROM User_ u, Be b WHERE u.id=b.userID AND b.roleID=1 "); $results = $this->con->getResults(); $tab = array(); - foreach ($results as $row) - $tab[] = new User($row['id'], $row['password'], $row['email'], $row['name'], $row['surname'], $row['nickname'], $row['image'], $row['extraTime'], $row['groupID'], $this->getRoles($row['id'])); + + foreach ($results as $row) { + $tab[] = new User( + $row['id'], + $row['password'], + $row['email'], + $row['name'], + $row['surname'], + $row['nickname'], + $row['image'], + $row['extraTime'], + $row['groupID'], + $this->getRoles($row['id'])); + } + return $tab; - } - catch(PDOException $e ){ + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } - public function findAllTeachers(): array { + public function findAllTeachers(): array + { try { $this->con->executeQuery("SELECT u.* FROM User_ u, Be b WHERE u.id=b.userID AND b.roleID=2"); $results = $this->con->getResults(); $tab = array(); - foreach ($results as $row) - $tab[] = new User($row['id'], $row['password'], $row['email'], $row['name'], $row['surname'], $row['nickname'], $row['image'], $row['extraTime'], $row['groupID'], $this->getRoles($row['id'])); + + foreach ($results as $row) { + $tab[] = new User( + $row['id'], + $row['password'], + $row['email'], + $row['name'], + $row['surname'], + $row['nickname'], + $row['image'], + $row['extraTime'], + $row['groupID'], + $this->getRoles($row['id'])); + } + return $tab; - } - catch(PDOException $e ){ + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } @@ -109,11 +143,23 @@ class UserGateway extends AbsGateway $this->con->executeQuery("SELECT u.* FROM User_ u, Be b WHERE u.id=b.userID AND b.roleID=3"); $results = $this->con->getResults(); $tab = array(); - foreach ($results as $row) - $tab[] = new User($row['id'], $row['password'], $row['email'], $row['name'], $row['surname'], $row['nickname'], $row['image'], $row['extraTime'], $row['groupID'], $this->getRoles($row['id'])); + + foreach ($results as $row) { + $tab[] = new User( + $row['id'], + $row['password'], + $row['email'], + $row['name'], + $row['surname'], + $row['nickname'], + $row['image'], + $row['extraTime'], + $row['groupID'], + $this->getRoles($row['id'])); + } + return $tab; - } - catch(PDOException $e ){ + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } @@ -125,99 +171,171 @@ class UserGateway extends AbsGateway $args = array(':id' => array($id, PDO::PARAM_INT)); $this->con->executeQuery($query, $args); $results = $this->con->getResults(); - if (empty($results)) return null; - return new User($results[0]['id'], $results[0]['password'], $results[0]['email'], $results[0]['name'], $results[0]['surname'], $results[0]['nickname'], $results[0]['image'], $results[0]['extraTime'], $results[0]['groupID'], $this->getRoles($results[0]['id'])); - } - catch(PDOException $e ){ + + if (empty($results)) { + return null; + } + + return new User( + $results[0]['id'], + $results[0]['password'], + $results[0]['email'], + $results[0]['name'], + $results[0]['surname'], + $results[0]['nickname'], + $results[0]['image'], + $results[0]['extraTime'], + $results[0]['groupID'], + $this->getRoles($results[0]['id'])); + + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } - private function getRoles(int $id): array { + private function getRoles(int $id): array + { try { $query = "SELECT r.name FROM Be b, Role_ r WHERE b.userID=:id AND b.roleID=r.id"; $args = array(':id' => array($id, PDO::PARAM_INT)); $this->con->executeQuery($query, $args); $results = $this->con->getResults(); $tab = array(); - foreach ($results as $row) $tab[] = $row['name']; + + foreach ($results as $row) { + $tab[] = $row['name']; + } + return $tab; - } - catch (PDOException $e) { + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } - public function login(string $login) : string{ + public function login(string $login) : string + { try { $query = "SELECT password FROM User_ WHERE email=:email"; $args = array(':email' => array($login, PDO::PARAM_STR)); $this->con->executeQuery($query, $args); return $this->con->getResults()[0]['password']; - } - catch(PDOException $e ){ + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } - public function findUserByEmail(string $email): ?User{ + public function findUserByEmail(string $email): ?User + { try { $query = "SELECT * FROM User_ WHERE email=:email"; $args = array(':email' => array($email, PDO::PARAM_STR)); $this->con->executeQuery($query, $args); $results = $this->con->getResults(); - if (empty($results)) return null; - return new User($results[0]['id'], $results[0]['password'], $results[0]['email'], $results[0]['name'], $results[0]['surname'], $results[0]['nickname'], $results[0]['image'], $results[0]['extraTime'], $results[0]['groupID'], $this->getRoles($results[0]['id'])); - } - catch(PDOException $e ){ + + if (empty($results)) { + return null; + } + + return new User( + $results[0]['id'], + $results[0]['password'], + $results[0]['email'], + $results[0]['name'], + $results[0]['surname'], + $results[0]['nickname'], + $results[0]['image'], + $results[0]['extraTime'], + $results[0]['groupID'], + $this->getRoles($results[0]['id'])); + + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } - public function findUserByName(string $name) : array{ + public function findUserByName(string $name) : array + { try { $query = "SELECT * FROM User_ WHERE name=:name"; $args = array(':name' => array($name, PDO::PARAM_STR)); $this->con->executeQuery($query, $args); $results = $this->con->getResults(); $tab = array(); - foreach ($results as $row) - $tab[] = new User($row['id'], $row['password'], $row['email'], $row['name'], $row['surname'], $row['nickname'], $row['image'], $row['extraTime'], $row['groupID'], $this->getRoles($row['id'])); + + foreach ($results as $row) { + $tab[] = new User( + $row['id'], + $row['password'], + $row['email'], + $row['name'], + $row['surname'], + $row['nickname'], + $row['image'], + $row['extraTime'], + $row['groupID'], + $this->getRoles($row['id'])); + } + return $tab; - } - catch(PDOException $e ){ + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } - public function findUserBySurname(string $surname) : array{ + public function findUserBySurname(string $surname) : array + { try { $query = "SELECT * FROM User_ WHERE surname=:surname"; $args = array(':surname' => array($surname, PDO::PARAM_STR)); $this->con->executeQuery($query, $args); $results = $this->con->getResults(); $tab = array(); - foreach ($results as $row) - $tab[] = new User($row['id'], $row['password'], $row['email'], $row['name'], $row['surname'], $row['nickname'], $row['image'], $row['extraTime'], $row['groupID'], $this->getRoles($row['id'])); + + foreach ($results as $row) { + $tab[] = new User( + $row['id'], + $row['password'], + $row['email'], + $row['name'], + $row['surname'], + $row['nickname'], + $row['image'], + $row['extraTime'], + $row['groupID'], + $this->getRoles($row['id'])); + } + return $tab; - } - catch(PDOException $e ){ + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } - public function findUserByNickname(string $nickname) : array{ + public function findUserByNickname(string $nickname) : array + { try { $query = "SELECT * FROM User_ WHERE nickname=:nickname"; $args = array(':nickname' => array($nickname, PDO::PARAM_STR)); $this->con->executeQuery($query, $args); $results = $this->con->getResults(); $tab = array(); - foreach ($results as $row) - $tab[] = new User($row['id'], $row['password'], $row['email'], $row['name'], $row['surname'], $row['nickname'], $row['image'], $row['extraTime'], $row['groupID'], $this->getRoles($row['id'])); + + foreach ($results as $row) { + $tab[] = new User( + $row['id'], + $row['password'], + $row['email'], + $row['name'], + $row['surname'], + $row['nickname'], + $row['image'], + $row['extraTime'], + $row['groupID'], + $this->getRoles($row['id'])); + } + return $tab; - } - catch(PDOException $e ){ + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } @@ -228,8 +346,7 @@ class UserGateway extends AbsGateway $query="UPDATE User_ SET password=:password WHERE id=:id"; $args = array(':id' => array($id, PDO::PARAM_INT), ':password' => array($newPassword, PDO::PARAM_STR)); $this->con->executeQuery($query, $args); - } - catch(PDOException $e ){ + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } @@ -240,8 +357,7 @@ class UserGateway extends AbsGateway $query="UPDATE User_ SET nickname=:nickname WHERE id=:id"; $args = array(':id' => array($id, PDO::PARAM_INT), ':nickname' => array($newNickname, PDO::PARAM_STR)); $this->con->executeQuery($query, $args); - } - catch(PDOException $e ){ + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } @@ -252,8 +368,7 @@ class UserGateway extends AbsGateway $query="UPDATE User_ SET image=:image WHERE id=:id"; $args = array(':id' => array($id, PDO::PARAM_INT), ':image' => array($newImage, PDO::PARAM_STR)); $this->con->executeQuery($query, $args); - } - catch(PDOException $e ){ + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } @@ -264,24 +379,36 @@ class UserGateway extends AbsGateway $query="UPDATE User_ SET groupID=:group WHERE id=:id"; $args = array(':id' => array($id, PDO::PARAM_INT), ':group' => array($newGroup, PDO::PARAM_STR)); $this->con->executeQuery($query, $args); - } - catch(PDOException $e ){ + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } - public function findUsersByGroup(int $id): array { + public function findUsersByGroup(int $id): array + { try { $query = "SELECT * FROM User_ WHERE groupID=:group"; $args = array(':group' => array($id, PDO::PARAM_INT)); $this->con->executeQuery($query, $args); $results = $this->con->getResults(); $tab = array(); - foreach ($results as $row) - $tab[] = new User($row['id'], $row['password'], $row['email'], $row['name'], $row['surname'], $row['nickname'], $row['image'], $row['extraTime'], $row['groupID'], $this->getRoles($row['id'])); + + foreach ($results as $row) { + $tab[] = new User( + $row['id'], + $row['password'], + $row['email'], + $row['name'], + $row['surname'], + $row['nickname'], + $row['image'], + $row['extraTime'], + $row['groupID'], + $this->getRoles($row['id'])); + } + return $tab; - } - catch(PDOException $e ){ + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } @@ -293,11 +420,23 @@ class UserGateway extends AbsGateway $this->con->executeQuery($query); $results = $this->con->getResults(); $tab = array(); - foreach ($results as $row) - $tab[] = new User($row['id'], $row['password'], $row['email'], $row['name'], $row['surname'], $row['nickname'], $row['image'], $row['extraTime'], $row['groupID'], $this->getRoles($row['id'])); + + foreach ($results as $row) { + $tab[] = new User( + $row['id'], + $row['password'], + $row['email'], + $row['name'], + $row['surname'], + $row['nickname'], + $row['image'], + $row['extraTime'], + $row['groupID'], + $this->getRoles($row['id'])); + } + return $tab; - } - catch(PDOException $e ){ + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } diff --git a/Project/php/gateway/VocabularyListGateway.php b/Project/php/gateway/VocabularyListGateway.php index 91cb090..37e5255 100755 --- a/Project/php/gateway/VocabularyListGateway.php +++ b/Project/php/gateway/VocabularyListGateway.php @@ -8,61 +8,55 @@ use model\VocabularyList; class VocabularyListGateway extends AbsGateway { - public function __construct(){ - parent::__construct(); - } - - public function add(array $parameters): int // require 3 elements + public function add(array $parameters): int { - try{ + try { $query = "INSERT INTO VocabularyList VALUES(NULL, :name,:img,:aut)"; - $args = array(':name'=>array($parameters[0],PDO::PARAM_STR), - ':img'=>array($parameters[1],PDO::PARAM_STR), - ':aut'=>array($parameters[2],PDO::PARAM_INT)); - $this->con->ExecuteQuery($query,$args); + $args = array(':name'=>array($parameters[0], PDO::PARAM_STR), + ':img'=>array($parameters[1], PDO::PARAM_STR), + ':aut'=>array($parameters[2], PDO::PARAM_INT)); + $this->con->ExecuteQuery($query, $args); return $this->con->lastInsertId(); - } - catch (PDOException $e){ + } catch (PDOException $e) { throw new Exception('problème pour ajouter une liste de vocabulaire'); } } public function remove(int $id): void { - try{ + try { $query = "DELETE FROM Translate WHERE listVoc=:id"; - $args = array(':id'=>array($id,PDO::PARAM_INT)); - $this->con->ExecuteQuery($query,$args); + $args = array(':id'=>array($id, PDO::PARAM_INT)); + $this->con->ExecuteQuery($query, $args); $query = "DELETE FROM Practice WHERE vocabID=:id"; - $args = array(':id'=>array($id,PDO::PARAM_INT)); - $this->con->ExecuteQuery($query,$args); + $args = array(':id'=>array($id, PDO::PARAM_INT)); + $this->con->ExecuteQuery($query, $args); $query = "DELETE FROM VocabularyList WHERE id=:id "; - $args = array(':id'=>array($id,PDO::PARAM_INT)); - $this->con->ExecuteQuery($query,$args); - } - catch (PDOException $e){ + $args = array(':id'=>array($id, PDO::PARAM_INT)); + $this->con->ExecuteQuery($query, $args); + } catch (PDOException $e) { throw new Exception('problème pour supprimer les vocabulaires avec leur Id'. $e->getMessage()); } } public function findAll(): array { - try{ - + try { $query = "SELECT * FROM VocabularyList"; $this->con->ExecuteQuery($query); $res = $this->con->getResults(); - $tab_vocab=[]; - foreach($res as $r){ - $tab_vocab[]=new VocabularyList($r['id'],$r['name'],$r['image'],$r['userID']); + $tabVocab=[]; + + foreach ($res as $r) { + $tabVocab[]=new VocabularyList($r['id'], $r['name'], $r['image'], $r['userID']); } - Return $tab_vocab; - } - catch(PDOException $e ){ + + return $tabVocab; + } catch (PDOException $e) { throw new Exception('problème pour affichage de tous les vocabulaires'); } } @@ -74,75 +68,91 @@ class VocabularyListGateway extends AbsGateway $args = array(':id' => array($id, PDO::PARAM_INT)); $this->con->executeQuery($query, $args); $results = $this->con->getResults(); - if (empty($results)) return null; - return new VocabularyList($results[0]['id'],$results[0]['name'],$results[0]['image'],$results[0]['userID']); + + if (empty($results)) { + return null; + } + + return new VocabularyList($results[0]['id'], + $results[0]['name'], + $results[0]['image'], + $results[0]['userID']); } - catch (PDOException $e){ + catch (PDOException $e) { throw new Exception($e->getMessage()); } } - public function findByName(String $name): array { - try{ + public function findByName(String $name): array + { + try { $query = "SELECT * FROM VocabularyList v WHERE v.name = :name"; - $args = array(':name'=>array($name,PDO::PARAM_STR)); - $this->con->ExecuteQuery($query,$args); + $args = array(':name'=>array($name, PDO::PARAM_STR)); + $this->con->ExecuteQuery($query, $args); $res = $this->con->getResults(); - $tab_vocab=[]; - foreach($res as $r){ - $tab_vocab[]=new VocabularyList($r['id'],$r['name'],$r['image'],$r['userID']); + $tabVocab=[]; + + foreach ($res as $r) { + $tabVocab[]=new VocabularyList($r['id'], $r['name'], $r['image'], $r['userID']); } - Return $tab_vocab; - } - catch(PDOException $e ){ + return $tabVocab; + } catch (PDOException $e) { throw new Exception('problème pour affichage d\'vocabulaire en fonction de son nom'); } } - public function ModifVocabListById(int $id, String $name,String $img,String $aut):void{ - try{ + public function modifVocabListById(int $id, String $name,String $img,String $aut):void + { + try { $query = "UPDATE VocabularyList SET name=:name, image=:img, userID=:aut WHERE id=:id"; - $args = array(':id'=>array($id,PDO::PARAM_INT), - ':name'=>array($name,PDO::PARAM_STR), - ':img'=>array($img,PDO::PARAM_STR), - ':aut'=>array($aut,PDO::PARAM_INT)); - $this->con->ExecuteQuery($query,$args); - } - catch (PDOException $e){ + $args = array(':id'=>array($id, PDO::PARAM_INT), + ':name'=>array($name, PDO::PARAM_STR), + ':img'=>array($img, PDO::PARAM_STR), + ':aut'=>array($aut, PDO::PARAM_INT)); + $this->con->ExecuteQuery($query, $args); + } catch (PDOException $e) { throw new Exception('problème pour modifier les vocabulaires'); } } - public function findByGroup(int $id): array { + public function findByGroup(int $id): array + { try { $query = "SELECT v.* FROM VocabularyList v, Practice p WHERE v.id=p.vocabID AND p.groupID=:id"; $args = array(':id' => array($id, PDO::PARAM_INT)); $this->con->executeQuery($query, $args); $results = $this->con->getResults(); $tab = array(); - foreach ($results as $row) $tab[] = new VocabularyList($row['id'], $row['name'], $row['image'], $row['userID']); + + foreach ($results as $row) { + $tab[] = new VocabularyList($row['id'], $row['name'], $row['image'], $row['userID']); + } + return $tab; - } - catch (PDOException $e) { + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } - public function findByUser(int $id): array { + public function findByUser(int $id): array + { try { $query = "SELECT * FROM VocabularyList WHERE userID=:id"; $args = array(':id' => array($id, PDO::PARAM_INT)); $this->con->executeQuery($query, $args); $results = $this->con->getResults(); $tab = array(); - foreach ($results as $row) $tab[] = new VocabularyList($row['id'], $row['name'], $row['image'], $row['userID']); + + foreach ($results as $row) { + $tab[] = new VocabularyList($row['id'], $row['name'], $row['image'], $row['userID']); + } + return $tab; - } - catch (PDOException $e) { + } catch (PDOException $e) { throw new Exception($e->getMessage()); } } diff --git a/Project/php/model/AbsModel.php b/Project/php/model/AbsModel.php index 1694975..c31e9a4 100755 --- a/Project/php/model/AbsModel.php +++ b/Project/php/model/AbsModel.php @@ -6,7 +6,8 @@ use gateway\UserGateway; abstract class AbsModel { - public function connection(string $login, string $password):?User{ + public function connection(string $login, string $password):?User + { $gtw = new UserGateway(); $hash = $gtw->login($login) ?? null; @@ -15,7 +16,11 @@ abstract class AbsModel $_SESSION['login'] = $login; $roles = array(); - foreach ($user->getRoles() as $role) $roles[] = $role; + + foreach ($user->getRoles() as $role) { + $roles[] = $role; + } + $_SESSION['roles'] = $roles; return $user; @@ -24,16 +29,18 @@ abstract class AbsModel return null; } - public function deconnection():void{ + public function deconnection():void + { session_unset(); session_destroy(); $_SESSION = array(); } - public function checkLoginExist(string $login): bool { + public function checkLoginExist(string $login): bool + { $gtw = new UserGateway(); return $gtw->findUserByEmail($login) != null; } - public abstract function is(string $login, array $roles): ?User; -} \ No newline at end of file + abstract public function is(string $login, array $roles): ?User; +} diff --git a/Project/php/model/Group.php b/Project/php/model/Group.php index 86b70e0..c6dfc34 100755 --- a/Project/php/model/Group.php +++ b/Project/php/model/Group.php @@ -53,4 +53,4 @@ class Group { return $this->sector; } -} \ No newline at end of file +} diff --git a/Project/php/model/MdlAdmin.php b/Project/php/model/MdlAdmin.php index bf1754a..c110fa7 100755 --- a/Project/php/model/MdlAdmin.php +++ b/Project/php/model/MdlAdmin.php @@ -7,62 +7,74 @@ use gateway\UserGateway; class MdlAdmin extends MdlUser { - public function getAllUsers(): array { + public function getAllUsers(): array + { $gtw = new UserGateway(); return $gtw->findAll(); } - public function getAllAdmins(): array { + public function getAllAdmins(): array + { $gtw = new UserGateway(); return $gtw->findAllAdmins(); } - public function getAllTeachers(): array { + public function getAllTeachers(): array + { $gtw = new UserGateway(); return $gtw->findAllTeachers(); } - public function getAllStudents(): array { + public function getAllStudents(): array + { $gtw = new UserGateway(); return $gtw->findAllStudents(); } - public function removeUser(int $id): void { + public function removeUser(int $id): void + { $gtw = new UserGateway(); $gtw->remove($id); } - public function getAllGroups(): array { + public function getAllGroups(): array + { $gtw = new GroupGateway(); return $gtw->findAll(); } - public function getUsersOfGroup(int $id): array { + public function getUsersOfGroup(int $id): array + { $gtw = new UserGateway(); return $gtw->findUsersByGroup($id); } - public function removeUserFromGroup(int $id): void { + public function removeUserFromGroup(int $id): void + { $gtw = new UserGateway(); $gtw->modifyGroup($id); } - public function removeGroup(int $id): void { + public function removeGroup(int $id): void + { $gtw = new GroupGateway(); $gtw->remove($id); } - public function addGroup(int $num, int $year, string $sector): int { + public function addGroup(int $num, int $year, string $sector): int + { $gtw = new GroupGateway(); return $gtw->add(array($num, $year, $sector)); } - public function addUserToGroup(int $user, int $group): void { + public function addUserToGroup(int $user, int $group): void + { $gtw = new UserGateway(); $gtw->modifyGroup($user, $group); } - public function getUnassignedUsers(): array { + public function getUnassignedUsers(): array + { $gtw = new UserGateway(); return $gtw->findUnassignedUsers(); } @@ -71,8 +83,6 @@ class MdlAdmin extends MdlUser { $gtw = new UserGateway(); $user = $gtw->findUserByEmail($login); - - if ($user->getRoles() == $roles && in_array('admin', $user->getRoles())) return $user; - else return null; + return $user->getRoles() == $roles && in_array('admin', $user->getRoles()) ? $user : null; } -} \ No newline at end of file +} diff --git a/Project/php/model/MdlStudent.php b/Project/php/model/MdlStudent.php index 659e12e..0296285 100755 --- a/Project/php/model/MdlStudent.php +++ b/Project/php/model/MdlStudent.php @@ -8,23 +8,26 @@ use gateway\VocabularyListGateway; class MdlStudent extends MdlUser { - public function getAll():array{ + public function getAll():array + { $gtw = new VocabularyListGateway(); return $gtw->findAll(); } - public function getVocabByName(string $name):array{ + public function getVocabByName(string $name):array + { $gtw = new VocabularyListGateway(); - $res = $gtw->findByName($name); - return $res; + return $gtw->findByName($name); } - public function getVocByGroup(int $group): array{ + public function getVocByGroup(int $group): array + { $gtw = new VocabularyListGateway(); return $gtw->findByGroup($group); } - public function getVocabById(int $id): VocabularyList { + public function getVocabById(int $id): VocabularyList + { $gtw = new VocabularyListGateway(); return $gtw->findById($id); } @@ -33,9 +36,6 @@ class MdlStudent extends MdlUser { $gtw = new UserGateway(); $user = $gtw->findUserByEmail($login); - - if ($user->getRoles() == $roles && in_array('student', $user->getRoles())) return $user; - else return null; + return $user->getRoles() == $roles && in_array('student', $user->getRoles()) ? $user : null; } } - diff --git a/Project/php/model/MdlTeacher.php b/Project/php/model/MdlTeacher.php index 661afda..abd667a 100755 --- a/Project/php/model/MdlTeacher.php +++ b/Project/php/model/MdlTeacher.php @@ -10,12 +10,14 @@ use gateway\VocabularyListGateway; class MdlTeacher extends MdlUser { - public function getAll():array{ + public function getAll():array + { $gtw = new VocabularyListGateway(); return $gtw->findAll(); } - public function getAllGroups(): array { + public function getAllGroups(): array + { $gtw = new GroupGateway(); return $gtw->findAll(); } @@ -26,37 +28,44 @@ class MdlTeacher extends MdlUser return $gtw->findByUser($id); } - public function findByIdVoc($id):array { + public function findByIdVoc($id):array + { $gtw = new TranslationGateway(); return $gtw->findByIdVoc($id); } - public function removeVocabFromGroup(int $vocabID, int $groupID): void{ + public function removeVocabFromGroup(int $vocabID, int $groupID): void + { $mdl = new GroupGateway(); $mdl->removeVocabFromGroup($vocabID, $groupID); } - public function addVocabToGroup(int $vocabID, int $groupID): void{ + public function addVocabToGroup(int $vocabID, int $groupID): void + { $mdl = new GroupGateway(); $mdl->addVocabToGroup($vocabID, $groupID); } - public function findGroupVocab(int $vocab): array { + public function findGroupVocab(int $vocab): array + { $mdl = new GroupGateway(); return $mdl->findGroupVocab($vocab); } - public function findGroupNoVocab(int $vocab): array { + public function findGroupNoVocab(int $vocab): array + { $mdl = new GroupGateway(); return $mdl->findGroupNoVocab($vocab); } - public function RemoveVocById(int $id):void{ + public function removeVocById(int $id):void + { $gtw = new VocabularyListGateway(); - $res = $gtw->remove($id); + $gtw->remove($id); } - public function addVocabList(int $userID, string $name, string $image, array $words): void { + public function addVocabList(int $userID, string $name, string $image, array $words): void + { $vocabGtw = new VocabularyListGateway(); $vocabID = $vocabGtw->add(array($name, $image, $userID)); $transGtw = new TranslationGateway(); @@ -69,8 +78,6 @@ class MdlTeacher extends MdlUser { $gtw = new UserGateway(); $user = $gtw->findUserByEmail($login); - - if ($user->getRoles() == $roles && in_array('teacher', $user->getRoles())) return $user; - else return null; + return $user->getRoles() == $roles && in_array('teacher', $user->getRoles()) ? $user : null; } -} \ No newline at end of file +} diff --git a/Project/php/model/MdlUser.php b/Project/php/model/MdlUser.php index bc1e7a6..2063a8d 100755 --- a/Project/php/model/MdlUser.php +++ b/Project/php/model/MdlUser.php @@ -4,14 +4,17 @@ namespace model; use gateway\UserGateway; -class MdlUser extends AbsModel { +class MdlUser extends AbsModel +{ - public function modifyNickname(int $id, string $newNickname): void{ + public function modifyNickname(int $id, string $newNickname): void + { $gtw = new UserGateway(); $gtw->modifyNickname($id, $newNickname); } - public function ModifyPassword(int $id, string $newPassword): void { + public function modifyPassword(int $id, string $newPassword): void + { $gtw = new UserGateway(); $gtw->modifyPassword($id, $newPassword); } @@ -20,9 +23,7 @@ class MdlUser extends AbsModel { { $gtw = new UserGateway(); $user = $gtw->findUserByEmail($login); - - if (!empty($user->getRoles())) return $user; - else return null; + return !empty($user->getRoles()) ? $user : null; } public function getUserById($id): User diff --git a/Project/php/model/Translation.php b/Project/php/model/Translation.php index a4e19f3..25a0bf7 100755 --- a/Project/php/model/Translation.php +++ b/Project/php/model/Translation.php @@ -42,4 +42,4 @@ class Translation { return $this->listVocab; } -} \ No newline at end of file +} diff --git a/Project/php/model/User.php b/Project/php/model/User.php index 162bfe8..c17d329 100755 --- a/Project/php/model/User.php +++ b/Project/php/model/User.php @@ -27,8 +27,18 @@ class User * @param int|null $group * @param array $roles */ - public function __construct(int $id, string $password, string $email, string $name, string $surname, string $nickname, string $image, ?bool $extraTime, ?int $group, array $roles) - { + public function __construct( + int $id, + string $password, + string $email, + string $name, + string $surname, + string $nickname, + string $image, + ?bool $extraTime, + ?int $group, + array $roles + ) { $this->id = $id; $this->password = $password; $this->email = $email; @@ -90,7 +100,5 @@ class User { return $this->roles; } - - } diff --git a/Project/php/model/VocabularyList.php b/Project/php/model/VocabularyList.php index 02751fd..baa4d29 100755 --- a/Project/php/model/VocabularyList.php +++ b/Project/php/model/VocabularyList.php @@ -42,4 +42,4 @@ class VocabularyList { return $this->aut; } -} \ No newline at end of file +}