From 94a5e9ead44bfeb60a962b5d98ddb78cff60b6ae Mon Sep 17 00:00:00 2001 From: dave Date: Sun, 26 Nov 2023 18:54:24 +0100 Subject: [PATCH] ok --- Sources/src/console/Console.php | 2 +- Sources/src/data/model/Role.php | 2 +- .../src/data/model/manager/CoachManager.php | 15 ++-- .../repository/NotificationRepository.php | 56 ++++++++++--- .../stub/repository/TrainingRepository.php | 78 ++++++++++++------- 5 files changed, 108 insertions(+), 45 deletions(-) diff --git a/Sources/src/console/Console.php b/Sources/src/console/Console.php index 54cd517e..100758d6 100755 --- a/Sources/src/console/Console.php +++ b/Sources/src/console/Console.php @@ -75,7 +75,6 @@ function displayCoachMenu() clearScreen(); echo "\n--- Menu Coach ---\n"; echo "1. Liste des athlètes\n"; - echo "2. Statistiques globales\n"; 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) @@ -848,6 +847,7 @@ while (true) { break; case '7': // Notifications + echo "Affichage des notifications...\n"; break; diff --git a/Sources/src/data/model/Role.php b/Sources/src/data/model/Role.php index 3c032587..48c73db5 100644 --- a/Sources/src/data/model/Role.php +++ b/Sources/src/data/model/Role.php @@ -61,7 +61,7 @@ abstract class Role { public function addTraining(Training $training) { $this->trainingList [] = $training; - + return true; } public function getTrainingsList(): array { diff --git a/Sources/src/data/model/manager/CoachManager.php b/Sources/src/data/model/manager/CoachManager.php index dd67ded0..eebd2827 100644 --- a/Sources/src/data/model/manager/CoachManager.php +++ b/Sources/src/data/model/manager/CoachManager.php @@ -2,6 +2,7 @@ namespace Manager; +use Model\Coach; use Model\Training; use Network\IAuthService; @@ -26,10 +27,7 @@ class CoachManager return null; } public function getTrainingsList(): ?array { - if ($this->authService->getCurrentUser() && $this->authService->getCurrentUser()->getRole()->getTrainingsList()) { - return $this->authService->getCurrentUser()->getRole()->getTrainingsList(); - } - return null; + return $this->dataManager->trainingRepository->getItems(0,10); } public function addUser(string $username): bool { @@ -56,7 +54,8 @@ class CoachManager } public function addTraining(Training $training): bool { - if ($this->authService->getCurrentUser() && $this->authService->getCurrentUser()->getRole()) { + if ($this->authService->getCurrentUser() && $this->authService->getCurrentUser()->getRole() instanceof Coach) { + if ($this->authService->getCurrentUser()->getRole()->addTraining($training)) { $this->dataManager->trainingRepository->addItem($training); return true; @@ -66,7 +65,11 @@ class CoachManager } public function removeTraining(String $trainingId): bool { - + $index = array_search($trainingId, $this->authService->getCurrentUser()->getRole()->getTrainingsList()); + if ($index !== false) { + unset($this->authService->getCurrentUser()->getRole()->getTrainingsList()[$index]); + return true; + } return false; } diff --git a/Sources/src/data/stub/repository/NotificationRepository.php b/Sources/src/data/stub/repository/NotificationRepository.php index c360b9cf..e712a676 100644 --- a/Sources/src/data/stub/repository/NotificationRepository.php +++ b/Sources/src/data/stub/repository/NotificationRepository.php @@ -3,47 +3,83 @@ namespace Stub; use Repository\INotificationRepository; +use Model\Notification; class NotificationRepository implements INotificationRepository { - + private $notifications = []; // Array to store notifications + public function __construct() + { + // Initialize with some sample notifications for user IDs 1, 2, and 3 + $this->notifications[] = new Notification(1, 'info', 'Welcome to our service!'); + $this->notifications[] = new Notification(2, 'alert', 'Your subscription is about to expire.'); + $this->notifications[] = new Notification(3, 'info', 'New features available.'); + $this->notifications[] = new Notification(1, 'reminder', 'Don’t forget your upcoming appointment.'); + $this->notifications[] = new Notification(2, 'update', 'Service update completed.'); + // Add more notifications as needed + } public function getItemById(int $id) { - // TODO: Implement getItemById() method. + // Assuming each notification has a unique ID + foreach ($this->notifications as $notification) { + if ($notification->getId() == $id) { + return $notification; + } + } + return null; } public function getNbItems(): int { - // TODO: Implement getNbItems() method. + return count($this->notifications); } public function getItems(int $index, int $count, ?string $orderingPropertyName = null, bool $descending = false): array { - // TODO: Implement getItems() method. + // For simplicity, returning a slice of the array + return array_slice($this->notifications, $index, $count); } public function getItemsByName(string $substring, int $index, int $count, ?string $orderingPropertyName = null, bool $descending = false): array { - // TODO: Implement getItemsByName() method. + $filtered = array_filter($this->notifications, function($notification) use ($substring) { + return strpos($notification->getMessage(), $substring) !== false; + }); + + // For simplicity, just returning a slice without ordering + return array_slice($filtered, $index, $count); } public function getItemByName(string $substring, int $index, int $count, ?string $orderingPropertyName = null, bool $descending = false) { - // TODO: Implement getItemByName() method. + $items = $this->getItemsByName($substring, $index, $count, $orderingPropertyName, $descending); + return $items ? $items[0] : null; } public function updateItem($oldItem, $newItem): void { - // TODO: Implement updateItem() method. + // Find and update the item. This is a simplistic implementation. + foreach ($this->notifications as $key => $notification) { + if ($notification === $oldItem) { + $this->notifications[$key] = $newItem; + break; + } + } } public function addItem($item): void { - // TODO: Implement addItem() method. + $this->notifications[] = $item; } public function deleteItem($item): bool { - // TODO: Implement deleteItem() method. + foreach ($this->notifications as $key => $notification) { + if ($notification === $item) { + unset($this->notifications[$key]); + return true; + } + } + return false; } -} \ No newline at end of file +} diff --git a/Sources/src/data/stub/repository/TrainingRepository.php b/Sources/src/data/stub/repository/TrainingRepository.php index 30285635..f43c5cb4 100644 --- a/Sources/src/data/stub/repository/TrainingRepository.php +++ b/Sources/src/data/stub/repository/TrainingRepository.php @@ -2,48 +2,72 @@ namespace Stub; +use Model\Training; use Repository\ITrainingRepository; -class TrainingRepository implements ITrainingRepository -{ +class TrainingRepository implements ITrainingRepository { + private array $trainings = []; - public function getItemById(int $id) - { - // TODO: Implement getItemById() method. + public function __construct() { + } - public function getNbItems(): int - { - // TODO: Implement getNbItems() method. + public function getItemById(int $id): ?Training { + foreach ($this->trainings as $training) { + if ($training->getId() === $id) { + return $training; + } + } + return null; } - public function getItems(int $index, int $count, ?string $orderingPropertyName = null, bool $descending = false): array - { - // TODO: Implement getItems() method. + public function getNbItems(): int { + return count($this->trainings); } - public function getItemsByName(string $substring, int $index, int $count, ?string $orderingPropertyName = null, bool $descending = false): array - { - // TODO: Implement getItemsByName() method. + public function getItems(int $index, int $count, ?string $orderingPropertyName = null, bool $descending = false): array { + // Implement the logic for ordering and slicing the array + return array_slice($this->trainings, $index, $count); } - public function getItemByName(string $substring, int $index, int $count, ?string $orderingPropertyName = null, bool $descending = false) - { - // TODO: Implement getItemByName() method. + public function getItemsByDate(\DateTime $date, int $index, int $count, ?string $orderingPropertyName = null, bool $descending = false): array { + $filteredTrainings = array_filter($this->trainings, function ($training) use ($date) { + return $training->getDate() == $date; + }); + // Implement sorting if necessary + return array_slice($filteredTrainings, $index, $count); } - public function updateItem($oldItem, $newItem): void - { - // TODO: Implement updateItem() method. + public function updateItem( $oldTraining, $newTraining): void { + $index = array_search($oldTraining, $this->trainings); + if ($index !== false) { + $this->trainings[$index] = $newTraining; + } } - public function addItem($item): void - { - // TODO: Implement addItem() method. + public function addItem( $training): void { + $this->trainings[] = $training; } - public function deleteItem($item): bool - { - // TODO: Implement deleteItem() method. + public function deleteItem( $training): bool { + $index = array_search($training, $this->trainings); + if ($index !== false) { + unset($this->trainings[$index]); + return true; + } + return false; } -} \ No newline at end of file + public function getItemsByName(string $substring, int $index, int $count, ?string $orderingPropertyName = null, bool $descending = false): array { + $filteredTrainings = array_filter($this->trainings, function ($training) use ($substring) { + // Assuming the 'description' field is the one to be searched + return str_contains(strtolower($training->getDescription()), strtolower($substring)); + }); + // Implement sorting if necessary + 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 $filteredTrainings[0] ?? null; + } +}