From a696e4eb5a6e02d2dbbc737a764f3b4c7d62eb82 Mon Sep 17 00:00:00 2001 From: anperederi Date: Mon, 20 Nov 2023 16:45:51 +0100 Subject: [PATCH] :sparkles: :poop: :construction: add TrainingRepository and and Training Menu in Console.php --- Sources/src/console/Console.php | 105 +++++++++++++++--- Sources/src/data/model/CoachAthlete.php | 39 ++++++- Sources/src/data/model/Training.php | 4 +- .../src/data/model/manager/CoachManager.php | 45 ++++++-- .../model/repository/ITrainingRepository.php | 8 ++ .../stub/repository/TrainingRepository.php | 73 ++++++++++++ 6 files changed, 246 insertions(+), 28 deletions(-) create mode 100644 Sources/src/data/model/repository/ITrainingRepository.php create mode 100644 Sources/src/data/stub/repository/TrainingRepository.php diff --git a/Sources/src/console/Console.php b/Sources/src/console/Console.php index 682b7b3b..299d99e6 100755 --- a/Sources/src/console/Console.php +++ b/Sources/src/console/Console.php @@ -81,6 +81,7 @@ function displayCoachMenu() echo "3. Analyses par athlète\n"; echo "4. Gérer la liste de mes athlètes\n"; // Gérer les athlètes (comprend : Ajouter un athlète, Supprimer un athlète, Consulter les statistiques d'un athlète) + echo "5. Gérer la liste de mes exercices\n"; echo "0. Retour au menu principal\n"; echo "Choisissez une option: "; } @@ -110,6 +111,34 @@ function displaySettingsMenu() echo "0. Retour au menu principal\n"; echo "Choisissez une option: "; } + +function displayManagementArrayAthlete() +{ + clearScreen(); + echo "\n--- Menu Management Groupe Athlete ---\n"; + echo "1. Ajouter un athlète\n"; + echo "2. Supprimer un athlète\n"; + echo "3. Voir tout les athlètes de la liste\n"; + echo "0. Retour au menu du coach\n"; + echo "Choisissez une option: "; +} + +function displayManagementArrayTraining() +{ + clearScreen(); + echo "\n--- Menu Management des entrainements ---\n"; + echo "1. Ajouter un entrainement\n"; + echo "2. Supprimer un entrainement\n"; + echo "3. Voir tout les entrainements de la liste\n"; + echo "0. Retour au menu du coach\n"; + echo "Choisissez une option: "; +} +/** + * Fonction permettant à un utilisateur de se connecter. + * + * @param DataManager $model La couche d'accès au modèle de données. + * @return bool Retourne true si l'utilisateur s'est connecté avec succès, sinon false. + */ function loginUser(DataManager $model) { try { @@ -133,7 +162,12 @@ function loginUser(DataManager $model) } } - +/** + * Fonction permettant à un utilisateur de s'inscrire. + * + * @param DataManager $model La couche d'accès au modèle de données. + * @return bool Retourne true si l'inscription a réussi, sinon false. + */ function registerUser(DataManager $model) { try { @@ -197,21 +231,10 @@ function registerUser(DataManager $model) } } -function displayManagementArray() -{ - clearScreen(); - echo "\n--- Menu Management Groupe Athlete ---\n"; - echo "1. Ajouter un athlète\n"; - echo "2. Supprimer un athlète\n"; - echo "3. Voir tout les athlètes de la liste\n"; - echo "0. Retour au menu du coach\n"; - echo "Choisissez une option: "; -} - -function ArrayMenu(DataManager $model) +function ArrayAthleteMenu(DataManager $model) { do { - displayManagementArray(); + displayManagementArrayAthlete(); $coachChoice = trim(fgets(STDIN)); switch ($coachChoice) { @@ -256,6 +279,54 @@ function ArrayMenu(DataManager $model) } while($coachChoice); } +function ArrayTrainingMenu(DataManager $model) +{ + do { + displayManagementArrayTraining(); + $coachChoice = trim(fgets(STDIN)); + + 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; + case '2': + echo "Renseignez l'id de l'entrainement : "; + $idTraining = trim(fgets(STDIN)); + if($model->coachMgr->removeTraining($idTraining)){ + echo "Suppression avec succès !"; + } else { + echo "Pb suppression ou aucun utilisateur de ce nom !"; + } + sleep(2); + break; + case '3': + $trainingArray = $model->coachMgr->getTrainingsList(); + if (!empty($trainingArray)) { + foreach ($trainingArray as $value) { + echo $value->__toString() . "\n"; + } + } else { + echo "Aucun entrainement dans la liste\n"; + } + sleep(2); + break; + case '0': + return; + default : + echo "Option invalide. Veuillez réessayer.\n"; + sleep(2); + break; + } + } while($coachChoice); +} + //function displayCoachMenu() //{ // clearScreen(); @@ -265,6 +336,7 @@ function ArrayMenu(DataManager $model) // echo "3. Analyses par athlète\n"; // echo "4. Gérer la liste de mes athlètes\n"; // // Gérer les athlètes (comprend : Ajouter un athlète, Supprimer un athlète, Consulter les statistiques d'un athlète) +// echo "5. Gérer la liste de mes exercices\n"; // echo "0. Retour au menu principal\n"; // echo "Choisissez une option: "; //} @@ -357,7 +429,10 @@ function CoachMenu(DataManager $model) sleep(2); break; case '4': // echo "4. Gérer la liste de mes athlètes\n"; - ArrayMenu($model); + ArrayAthleteMenu($model); + break; + case '5': // echo "5. Gérer la liste de mes exercices\n"; + ArrayTrainingMenu($model); break; case '0': // Quitter return; diff --git a/Sources/src/data/model/CoachAthlete.php b/Sources/src/data/model/CoachAthlete.php index 6dd5536f..50f74643 100644 --- a/Sources/src/data/model/CoachAthlete.php +++ b/Sources/src/data/model/CoachAthlete.php @@ -3,7 +3,7 @@ namespace Model; class CoachAthlete extends Coach { - private Training $arrayTraining; + private array $trainingsList = []; public function getUsersList(): ?array { if (!empty($this->usersList)) { return $this->usersList; @@ -18,12 +18,32 @@ class CoachAthlete extends Coach { } return null; // L'utilisateur n'est pas dans la liste } + public function getTrainingsList(): ?array { + if (!empty($this->trainingsList)) { + return $this->trainingsList; + } + return null; + } + public function getTrainingList(Training $training): ?Training { + foreach ($this->trainingsList as $existingTraining) { + if ($existingTraining->getId() === $training->getId()) { + return $training; // L'utilisateur est présent dans la liste + } + } + return null; // L'utilisateur n'est pas dans la liste + } public function CheckAdd(User $user) : bool { if($user->getRole() instanceof \Model\Athlete){ return true; } return false; } + public function CheckAddTraining(Training $training) : bool { + if($training instanceof \Model\Training){ + return true; + } + return false; + } public function addUser(User $user): bool { if($this->CheckAdd($user)){ array_push($this->usersList, $user); @@ -41,4 +61,21 @@ class CoachAthlete extends Coach { } return false; } + public function addTraining(Training $training): bool { + if($this->CheckAddTraining($training)){ + array_push($this->trainingsList, $training); + return true; + } + return false; + } + public function removeTraining(Training $training): bool { + if($this->CheckAddTraining($training)){ + $key = array_search($training, $this->trainingsList); + if ($key !== false) { + array_splice($this->trainingsList, $key, 1); + } + return true; + } + return false; + } } \ No newline at end of file diff --git a/Sources/src/data/model/Training.php b/Sources/src/data/model/Training.php index 95c93935..77e86b70 100644 --- a/Sources/src/data/model/Training.php +++ b/Sources/src/data/model/Training.php @@ -19,14 +19,14 @@ class Training public function getDate():String { return $this->date; } - public function getLocation():String { + public function getLocation(): String { return $this->longitude . $this->latitude; } public function getDescription(): Text { return $this->description; } - public function getFeedback():Text { + public function getFeedback(): Text { return $this->feedback; } } \ No newline at end of file diff --git a/Sources/src/data/model/manager/CoachManager.php b/Sources/src/data/model/manager/CoachManager.php index 71eb43f7..e18c788c 100644 --- a/Sources/src/data/model/manager/CoachManager.php +++ b/Sources/src/data/model/manager/CoachManager.php @@ -6,29 +6,26 @@ use Model\Coach; use Model\Role; use Model\Statistic; use Model\User; +use \Model\Training; use Network\IAuthService; use Stub\AuthService; +use Stub\TrainingRepository; +use Stub\UserRepository; class CoachManager { // public ?User $currentUser = null; // Initialisé à null private AuthService $authService; + private TrainingRepository $trainingRepository; + function __construct(AuthService $authService) { $this->authService = $authService; } - public function getRole(): void - { - // Assurez-vous que currentUser n'est pas null avant d'accéder à getRole() -// if ($this->authService->currentUser && $this->authService->currentUser->getRole()) { -// return $this->authService->currentUser->getRole(); -// } - var_dump($this->authService->currentUser->getRole()); - -// return null; + public function getTrainingRepository():TrainingRepository { + return $this->trainingRepository; } - public function getUsersList(): ?array { if ($this->authService->currentUser && $this->authService->currentUser->getRole()->getUsersList()) { @@ -36,6 +33,12 @@ class CoachManager } return null; } + public function getTrainingsList(): ?array { + 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()) { @@ -58,6 +61,28 @@ class CoachManager } return false; } + public function addTraining(String $training): bool + { + if ($this->authService->currentUser && $this->authService->currentUser->getRole()) { + if(($training = $this->authService->getTrainingRepository()->GetItemByName($training,0,1))) { // count 1 seul et debuis 0 (debut) + if ($this->authService->currentUser->getRole()->addTraining($training)) { + return true; + } + } + } + return false; + } + public function removeTraining(String $training): bool + { + if ($this->authService->currentUser && $this->authService->currentUser->getRole()) { + if(($user = $this->authService->getTrainingRepository()->GetItemByName($training,0,1))) { // count 1 seul et debuis 0 (debut) + if ($this->authService->currentUser->getRole()->removeUser($user)) { + return true; + } + } + } + return false; + } // public function getStatistics(User $user) : ?Statistic { // if ($this->authService->currentUser && $this->authService->currentUser->getRole()) { diff --git a/Sources/src/data/model/repository/ITrainingRepository.php b/Sources/src/data/model/repository/ITrainingRepository.php new file mode 100644 index 00000000..ef489fe4 --- /dev/null +++ b/Sources/src/data/model/repository/ITrainingRepository.php @@ -0,0 +1,8 @@ + \ No newline at end of file diff --git a/Sources/src/data/stub/repository/TrainingRepository.php b/Sources/src/data/stub/repository/TrainingRepository.php new file mode 100644 index 00000000..17ecf1e6 --- /dev/null +++ b/Sources/src/data/stub/repository/TrainingRepository.php @@ -0,0 +1,73 @@ +training[] = new Training(1, new \DateTime("1985-05-10"), 48.5, 55.8, "john.doe@example.com", "hello"); + $this->training[] = new Training(2, new \DateTime("1986-06-12"), 48.5, 55.8, "john.doe@exavdffgmple.com", "hedfdfllo"); + $this->training[] = new Training(3, new \DateTime("1989-07-14"), 48.5, 55.8, "john.doe@exdfdfample.com", "hedfdfllo"); + $this->training[] = new Training(4, new \DateTime("1990-08-16"), 48.5, 55.8, "john.doe@exdfdfample.com", "hedffdllo"); + $this->training[] = new Training(5, new \DateTime("2000-09-18"), 48.5, 55.8, "john.doe@exdffdample.com", "hedfdfllo"); + } + + public function getItemById(int $id): ?Training { + foreach ($this->training as $training) { + if ($training->getId() === $id) { + return $training; + } + } + return null; + } + + public function GetNbItems(): int { + return count($this->training); + } + + public function GetItems(int $index, int $count, ?string $orderingPropertyName = null, bool $descending = false): array { + // Cette méthode est un exemple simple, on ne gère pas l'ordonnancement ici + return array_slice($this->training, $index, $count); + } + + public function GetItemsByName(string $substring, int $index, int $count, ?string $orderingPropertyName = null, bool $descending = false): array { + $filteredTrainings = array_filter($this->training, function ($training) use ($substring) { + return strpos(strtolower($training->getDescription()), strtolower($substring)) !== false || strpos(strtolower($training->getFeedback()), strtolower($substring)) !== false; + }); + return array_slice($filteredTrainings, $index, $count); + } + public function GetItemByName(string $substring, int $index, int $count, ?string $orderingPropertyName = null, bool $descending = false): ?Training { + $filteredTrainings = $this->GetItemsByName($substring, $index, $count, $orderingPropertyName, $descending); + return isset($filteredTrainings[0]) ? $filteredTrainings[0] : null; + } + + // should have type here + public function UpdateItem($oldTraining, $newTraining): void { + $index = array_search($oldTraining, $this->training); + if ($index !== false) { + $this->training[$index] = $newTraining; + } + } + + // should have type here + public function AddItem($training): void { + $this->training[] = $training; + } + + // should have type here + public function DeleteItem($training): bool { + $index = array_search($training, $this->training); + if ($index !== false) { + unset($this->training[$index]); + return true; + } + return false; + } +} \ No newline at end of file