From 208b134bc78e2138da8647600b5b9c1c6ce8e342 Mon Sep 17 00:00:00 2001 From: anperederi Date: Thu, 23 Nov 2023 14:20:02 +0100 Subject: [PATCH] add Athlete Manager ans her dependences --- Sources/src/console/Console.php | 143 +++++++++++++++--- Sources/src/data/model/Athlete.php | 14 +- .../src/data/model/manager/AthleteManager.php | 109 +++++++++++++ .../src/data/model/manager/DataManager.php | 1 + Sources/src/data/stub/StubData.php | 8 +- .../data/stub/repository/UserRepository.php | 9 ++ 6 files changed, 259 insertions(+), 25 deletions(-) create mode 100644 Sources/src/data/model/manager/AthleteManager.php diff --git a/Sources/src/console/Console.php b/Sources/src/console/Console.php index ae68f32d..d557b38f 100755 --- a/Sources/src/console/Console.php +++ b/Sources/src/console/Console.php @@ -92,11 +92,12 @@ function displaySocialManagementMenu() { clearScreen(); echo "\n--- Gestion sociale ---\n"; - echo "1. Rechercher des utilisateurs\n"; - echo "2. Gérer la liste d'amis\n"; + echo "1. Rechercher des coach\n"; + echo "2. Rechercher des athletes\n"; + echo "3. Gérer la liste d'amis\n"; // Ajouter des amis // Supprimer des amis ... - echo "3. Options de partage\n"; + echo "4. Options de partage\n"; echo "0. Retour au menu principal\n"; echo "Choisissez une option: "; } @@ -289,19 +290,6 @@ function ArrayTrainingMenu(DataManager $model) switch ($coachChoice) { case '1': -// echo "Renseignez l'id de l'entrainement : "; -// $idTraining = trim(fgets(STDIN)); -// if($model->coachMgr->addTraining($idTraining)){ -// echo "Ajout avec succès !"; -// } else { -// echo "Pb ajout !"; -// } -// sleep(2); -// break; - // ... - - // ... - $existingTrainings = $model->coachMgr->getTrainingsList(); $lastTraining = end($existingTrainings); $lastTrainingId = $lastTraining ? $lastTraining->getId() : 0; @@ -474,6 +462,126 @@ function CoachMenu(DataManager $model) } while($coachChoice); } +//function displaySocialManagementMenu() +//{ +// clearScreen(); +// echo "\n--- Gestion sociale ---\n"; +// echo "1. Rechercher des coach\n"; +// echo "2. Rechercher des athletes\n"; +// echo "3. Gérer la liste d'amis\n"; +// // Ajouter des amis +// // Supprimer des amis ... +// echo "4. Options de partage\n"; +// echo "0. Retour au menu principal\n"; +// echo "Choisissez une option: "; +//} + +function socialManagementMenu(DataManager $model) { + do { + displaySocialManagementMenu(); + $managementChoice = trim(fgets(STDIN)); + + switch ($managementChoice) { + case '1': // echo "1. Rechercher des coach\n"; + echo "Renseignez le surnom du coach que vous recherchez : "; + $coachUsername = trim(fgets(STDIN)); + if (($coach = $model->athleteMgr->getCoachByName($coachUsername)) != null) { + $coach->__toString(); + } else { + echo "Aucun coach de ce nom : $coachUsername\n"; + } + sleep(2); + break; + case '2': // echo "1. Rechercher des athletes\n"; + echo "Renseignez le surnom de l'athlete que vous recherchez : "; + $athleteUsername = trim(fgets(STDIN)); + if (($athlete = $model->athleteMgr->getAthleteByName($athleteUsername)) != null) { + $athlete->__toString(); + } else { + echo "Aucun athlete de ce nom : $athleteUsername\n"; + } + sleep(2); + break; + case '3': // echo "3. Gérer la liste d'amis\n"; + $arrayUsers = $model->coachMgr->getUsersList(); + + if (!empty($arrayUsers)) { + do { + clearScreen(); + $cpt = 0; + foreach ($arrayUsers as $value) { + echo $cpt . " - " . $value->__toString() . "\n"; + $cpt = $cpt + 1; + } + + echo "Renseignez le numéro de l'utilisateur choisi : "; + $usernameNumber = trim(fgets(STDIN)); + + // Vérifier si l'index saisi est valide + if (isset($arrayUsers[$usernameNumber])) { + $selectedUser = $arrayUsers[$usernameNumber]; + if (($arrayStats = $model->coachMgr->getStatistics($selectedUser))) { + foreach ($arrayStats as $value) { + echo $value->__toString() . "\n"; + } + } else { + echo "Pas de statistiques valides présentent !\n"; + } + } else { + echo "Numéro d'utilisateur non valide.\n"; + $cpt = 0; + } + } while($cpt == 0); + } else { + echo "Aucun utilisateur dans la liste.\n"; + } + sleep(2); + break; + case '4': // echo "4. Options de partage\n"; + $arrayUsers = $model->coachMgr->getUsersList(); + + if (!empty($arrayUsers)) { + do { + clearScreen(); + $cpt = 0; + foreach ($arrayUsers as $value) { + echo $cpt . " - " . $value->__toString() . "\n"; + $cpt = $cpt + 1; + } + + echo "Renseignez le numéro de l'utilisateur choisi : "; + $usernameNumber = trim(fgets(STDIN)); + + // Vérifier si l'index saisi est valide + if (isset($arrayUsers[$usernameNumber])) { + $selectedUser = $arrayUsers[$usernameNumber]; + if (($arrayStats = $model->coachMgr->getAnalyse($selectedUser))) { + foreach ($arrayStats as $value) { + echo $value->__toString() . "\n"; + } + } else { + echo "Pas d'Analyses valides présentent !\n"; + } + } else { + echo "Numéro d'utilisateur non valide.\n"; + $cpt = 0; + } + } while($cpt == 0); + } else { + echo "Aucun utilisateur dans la liste.\n"; + } + sleep(2); + break; + case '0': // echo "0. Retour au menu principal\n"; + return; + default: + echo "Option invalide. Veuillez réessayer.\n"; + sleep(2); + break; + } + } while($managementChoice); +} + while (true) { $loggedIn = false; @@ -531,8 +639,7 @@ while (true) { break; case '4': // Gestion sociale - displaySocialManagementMenu(); - $socialChoice = trim(fgets(STDIN)); + socialManagementMenu($model); // TODO: Ajouter la logique pour les options de gestion sociale ici. break; diff --git a/Sources/src/data/model/Athlete.php b/Sources/src/data/model/Athlete.php index 59c10ea9..ff6a5d43 100644 --- a/Sources/src/data/model/Athlete.php +++ b/Sources/src/data/model/Athlete.php @@ -38,12 +38,15 @@ class Athlete extends Role { public function getTraining(): ?TrainingRepository{ return null; } - public function getTrainingsList(): ?array { - return $this->trainingRepository->getTrainingsList(); - } +// public function getTrainingsList(): ?array { +// if(foreach()) +// } public function getTrainingList(Training $training): ?Training { return $training; } + public function setTrainingsList(array $newTrainingsList): void { + $this->trainingRepository->setTrainingsList($newTrainingsList); + } public function CheckAdd(User $user): bool { if($user instanceof \Model\Athlete){ @@ -87,6 +90,11 @@ class Athlete extends Role { return true; } + public function getTrainingsList(): ?array + { + // TODO: Implement getTrainingsList() method. + return null; + } } ?> \ No newline at end of file diff --git a/Sources/src/data/model/manager/AthleteManager.php b/Sources/src/data/model/manager/AthleteManager.php new file mode 100644 index 00000000..1f5fdea7 --- /dev/null +++ b/Sources/src/data/model/manager/AthleteManager.php @@ -0,0 +1,109 @@ +authService = $authService; + } + public function getCoachByName(String $coachUsername): ?User { + if(($coach = $this->authService->getUserRepository()->getItemByName($coachUsername, 0, 1)) instanceof CoachAthlete) { + return $coach; + } + return null; + } + public function getAthleteByName(String $athleteUsername): ?User { + if(($athlete =$this->authService->getUserRepository()->getItemByName($athleteUsername, 0, 1)) instanceof Athlete) { + return $athlete; + } + return null; + } + public function getUsersList(): ?array + { + if ($this->authService->currentUser && $this->authService->currentUser->getRole()->getUsersList()) { + return $this->authService->currentUser->getRole()->getUsersList(); + } + return null; + } + public function getTrainingsList(): ?array { + foreach ($this->authService->getUserRepository()->getItemByRole() as $coach) { + foreach ($coach->getUsersList()->getUsername() as $username) { + if($this->authService->currentUser->getUsername() == $username) { + $this->authService->currentUser->getRole()->setTrainingsList($coach->getTrainingsList()); + } + } + } + if ($this->authService->currentUser && $this->authService->currentUser->getRole()->getTrainingsList()) { + return $this->authService->currentUser->getRole()->getTrainingsList(); + } + return null; + } + public function addUser(String $username): bool + { + if ($this->authService->currentUser && $this->authService->currentUser->getRole()) { + if(($user = $this->authService->getUserRepository()->GetItemByName($username,0,1))) { // count 1 seul et debuis 0 (debut) + if ($this->authService->currentUser->getRole()->addUser($user)) { + return true; + } + } + } + return false; + } + public function removeUser(String $username): bool + { + if ($this->authService->currentUser && $this->authService->currentUser->getRole()) { + if(($user = $this->authService->getUserRepository()->GetItemByName($username,0,1))) { // count 1 seul et debuis 0 (debut) + if ($this->authService->currentUser->getRole()->removeUser($user)) { + return true; + } + } + } + return false; + } + public function addTraining(Training $training): bool + { + if ($this->authService->currentUser && $this->authService->currentUser->getRole()) { + if ($this->authService->currentUser->getRole()->addTraining($training)) { + return true; + } + } + return false; + } + public function removeTraining(String $trainingId): bool + { + if ($this->authService->currentUser && $this->authService->currentUser->getRole()) { + if(($training = $this->authService->currentUser->getRole()->getTraining()->getItemById($trainingId))) { // count 1 seul et debuis 0 (debut) + if ($this->authService->currentUser->getRole()->removeTraining($training)) { + return true; + } + } + } + return false; + } + +// public function getStatistics(User $user) : ?Statistic { +// if ($this->authService->currentUser && $this->authService->currentUser->getRole()) { +// if (($arrayStat = $this->authService->currentUser->getRole()->getUserList($user)->getStatistic())) { +// return $arrayStat; +// } +// } +// return null; +// } +// +// public function getAnalyse(User $user): bool { +// return false; +// } + +} + +?> \ No newline at end of file diff --git a/Sources/src/data/model/manager/DataManager.php b/Sources/src/data/model/manager/DataManager.php index 3523ccba..78020537 100644 --- a/Sources/src/data/model/manager/DataManager.php +++ b/Sources/src/data/model/manager/DataManager.php @@ -4,6 +4,7 @@ namespace Manager; abstract class DataManager { public $userMgr; public $coachMgr; + public $athleteMgr; } diff --git a/Sources/src/data/stub/StubData.php b/Sources/src/data/stub/StubData.php index 25bfd218..b62c821d 100644 --- a/Sources/src/data/stub/StubData.php +++ b/Sources/src/data/stub/StubData.php @@ -1,8 +1,9 @@ userMgr = new UserManager($authService); -// if($this->userMgr->currentUser != null){ - $this->coachMgr = new CoachManager($authService); -// } + $this->coachMgr = new CoachManager($authService); + $this->athleteMgr = new AthleteManager($authService); } } diff --git a/Sources/src/data/stub/repository/UserRepository.php b/Sources/src/data/stub/repository/UserRepository.php index 90b18f89..7659f4ea 100644 --- a/Sources/src/data/stub/repository/UserRepository.php +++ b/Sources/src/data/stub/repository/UserRepository.php @@ -31,6 +31,15 @@ class UserRepository implements IUserRepository { return null; } + public function getItemByRole():?User{ + foreach ($this->users as $user) { + if ($user->getRole() instanceof \Model\Coach) { + return $user; + } + } + return null; + } + public function getItemByEmail(string $email): ?User { foreach ($this->users as $user) { if ($user->getEmail() === $email) {