From 5673c1e0abe41930c1e2e780cc1e42ecf85f7708 Mon Sep 17 00:00:00 2001 From: Guillaume Aubert Date: Thu, 26 Sep 2024 11:33:41 +0200 Subject: [PATCH 1/8] fix: getExerciseTitle was not returning strings --- .../databases_connectors/postgresql/postgresql_access.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/models/databases_connectors/postgresql/postgresql_access.php b/src/models/databases_connectors/postgresql/postgresql_access.php index 17d4eaa..523c02e 100644 --- a/src/models/databases_connectors/postgresql/postgresql_access.php +++ b/src/models/databases_connectors/postgresql/postgresql_access.php @@ -25,9 +25,12 @@ public function createExercise(string $title): int public function getExerciseTitle(int $id): string { - return $this->postgresql->select('SELECT title FROM exercises WHERE id = :id', [':id' => $id]); + $result = $this->postgresql->select('SELECT title FROM exercises WHERE id = :id', [':id' => $id]); + if (!empty($result) && isset($result[0]['title'])) { + return $result[0]['title']; + } + throw new Exception('Exercise not found'); } - private function create_db_if_not_exist() { if (count($this->postgresql->select("SELECT 1 FROM information_schema.tables WHERE table_name = 'exercises'")) < 1) { From 865af0ceddefee9ae78679a6caba9d7c5d208ac6 Mon Sep 17 00:00:00 2001 From: Guillaume Aubert Date: Thu, 26 Sep 2024 11:34:04 +0200 Subject: [PATCH 2/8] feat: adding getExercises to the controller --- .../postgresql/postgresql_access.php | 5 +++++ src/models/exercise.php | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/models/databases_connectors/postgresql/postgresql_access.php b/src/models/databases_connectors/postgresql/postgresql_access.php index 523c02e..fb8c373 100644 --- a/src/models/databases_connectors/postgresql/postgresql_access.php +++ b/src/models/databases_connectors/postgresql/postgresql_access.php @@ -31,6 +31,11 @@ public function getExerciseTitle(int $id): string } throw new Exception('Exercise not found'); } + + public function getExercises() + { + return $this->postgresql->select('SELECT * FROM exercises;'); + } private function create_db_if_not_exist() { if (count($this->postgresql->select("SELECT 1 FROM information_schema.tables WHERE table_name = 'exercises'")) < 1) { diff --git a/src/models/exercise.php b/src/models/exercise.php index 8fccaa9..c0eedbb 100644 --- a/src/models/exercise.php +++ b/src/models/exercise.php @@ -32,4 +32,18 @@ public function getTitle() { return $this->database_access->getexerciseTitle($this->id); } + + public static function getExercises() + { + $database_access = (new DatabasesChoose())->getDatabase(); + $exercises_data = $database_access->getExercises(); + + $exercises = []; + foreach ($exercises_data as $exercise_data) { + $exercise = new self($exercise_data['id']); + $exercises[] = $exercise; + } + + return $exercises; + } } From ec0bff2a86512565fe9dfca901500a80191cb1f3 Mon Sep 17 00:00:00 2001 From: Guillaume Aubert Date: Thu, 26 Sep 2024 11:34:28 +0200 Subject: [PATCH 3/8] feat: using the actual db data from the controller to the take an exercise page --- src/views/exercises_root.php | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/views/exercises_root.php b/src/views/exercises_root.php index 97a9673..6191d62 100644 --- a/src/views/exercises_root.php +++ b/src/views/exercises_root.php @@ -1,5 +1,8 @@ @@ -13,27 +16,16 @@
- 1, - 'name' => 'Anglais' - ], - [ - 'id' => 2, - 'name' => 'Francais' - ] - ] -?> +
  • From f4ab1503a0c4c98e4f2daeb498bb5d36178d3531 Mon Sep 17 00:00:00 2001 From: Guillaume1868 Date: Thu, 26 Sep 2024 09:35:17 +0000 Subject: [PATCH 4/8] [Action][php-cs-fixer] Fix styling --- .../databases_connectors/postgresql/postgresql_access.php | 1 + src/models/exercise.php | 2 +- src/views/exercises_root.php | 1 - 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/models/databases_connectors/postgresql/postgresql_access.php b/src/models/databases_connectors/postgresql/postgresql_access.php index fb8c373..7e6ee6e 100644 --- a/src/models/databases_connectors/postgresql/postgresql_access.php +++ b/src/models/databases_connectors/postgresql/postgresql_access.php @@ -36,6 +36,7 @@ public function getExercises() { return $this->postgresql->select('SELECT * FROM exercises;'); } + private function create_db_if_not_exist() { if (count($this->postgresql->select("SELECT 1 FROM information_schema.tables WHERE table_name = 'exercises'")) < 1) { diff --git a/src/models/exercise.php b/src/models/exercise.php index c0eedbb..c22e3d9 100644 --- a/src/models/exercise.php +++ b/src/models/exercise.php @@ -45,5 +45,5 @@ public static function getExercises() } return $exercises; - } + } } diff --git a/src/views/exercises_root.php b/src/views/exercises_root.php index 6191d62..8c2e3c0 100644 --- a/src/views/exercises_root.php +++ b/src/views/exercises_root.php @@ -3,7 +3,6 @@ include_once MODEL_DIR . '/exercise.php'; $exercises = exercises::getExercises(); - ob_start(); ?> From fc47e7b1d0ca651787f5296fe368010e5e9b0012 Mon Sep 17 00:00:00 2001 From: Guillaume Aubert Date: Thu, 26 Sep 2024 11:48:34 +0200 Subject: [PATCH 5/8] fix: removing unecessary verification and refining SELECT * to only necessary cols --- .../databases_connectors/postgresql/postgresql_access.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/models/databases_connectors/postgresql/postgresql_access.php b/src/models/databases_connectors/postgresql/postgresql_access.php index fb8c373..79cb6ea 100644 --- a/src/models/databases_connectors/postgresql/postgresql_access.php +++ b/src/models/databases_connectors/postgresql/postgresql_access.php @@ -26,15 +26,12 @@ public function createExercise(string $title): int public function getExerciseTitle(int $id): string { $result = $this->postgresql->select('SELECT title FROM exercises WHERE id = :id', [':id' => $id]); - if (!empty($result) && isset($result[0]['title'])) { - return $result[0]['title']; - } - throw new Exception('Exercise not found'); + return $result[0]['title']; } public function getExercises() { - return $this->postgresql->select('SELECT * FROM exercises;'); + return $this->postgresql->select('SELECT id FROM exercises;'); } private function create_db_if_not_exist() { From 50f0970c534738c968ad85aec1ee1231523a1bec Mon Sep 17 00:00:00 2001 From: Guillaume Aubert Date: Thu, 26 Sep 2024 11:54:50 +0200 Subject: [PATCH 6/8] feat: adding getExercises to interface --- src/models/databases_connectors/databases_access.php | 2 ++ .../databases_connectors/postgresql/postgresql_access.php | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/models/databases_connectors/databases_access.php b/src/models/databases_connectors/databases_access.php index 31a1074..46bf833 100644 --- a/src/models/databases_connectors/databases_access.php +++ b/src/models/databases_connectors/databases_access.php @@ -7,4 +7,6 @@ public function doesExerciseExist(int $id): bool; public function createExercise(string $title): int; public function getExerciseTitle(int $id): string; + + public function getExercises(): array; } diff --git a/src/models/databases_connectors/postgresql/postgresql_access.php b/src/models/databases_connectors/postgresql/postgresql_access.php index 605406c..d6d7bec 100644 --- a/src/models/databases_connectors/postgresql/postgresql_access.php +++ b/src/models/databases_connectors/postgresql/postgresql_access.php @@ -29,7 +29,7 @@ public function getExerciseTitle(int $id): string return $result[0]['title']; } - public function getExercises() + public function getExercises(): array { return $this->postgresql->select('SELECT id FROM exercises;'); } From c6403f377313c2bd97fe6a4db8008615154b162a Mon Sep 17 00:00:00 2001 From: Guillaume Aubert Date: Thu, 26 Sep 2024 11:59:01 +0200 Subject: [PATCH 7/8] feat: Adding methods to get exercices that have answering status and using that in the page instead --- .../databases_connectors/databases_access.php | 1 + .../postgresql/postgresql_access.php | 4 ++++ src/models/exercise.php | 14 ++++++++++++++ src/views/exercises_root.php | 2 +- 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/models/databases_connectors/databases_access.php b/src/models/databases_connectors/databases_access.php index 46bf833..a0cf1a1 100644 --- a/src/models/databases_connectors/databases_access.php +++ b/src/models/databases_connectors/databases_access.php @@ -9,4 +9,5 @@ public function createExercise(string $title): int; public function getExerciseTitle(int $id): string; public function getExercises(): array; + public function getExercisesAnswering(): array; } diff --git a/src/models/databases_connectors/postgresql/postgresql_access.php b/src/models/databases_connectors/postgresql/postgresql_access.php index d6d7bec..9647e1a 100644 --- a/src/models/databases_connectors/postgresql/postgresql_access.php +++ b/src/models/databases_connectors/postgresql/postgresql_access.php @@ -33,6 +33,10 @@ public function getExercises(): array { return $this->postgresql->select('SELECT id FROM exercises;'); } + public function getExercisesAnswering(): array + { + return $this->postgresql->select('SELECT id FROM exercises WHERE status = 1;'); + } private function create_db_if_not_exist() { diff --git a/src/models/exercise.php b/src/models/exercise.php index c22e3d9..47142ca 100644 --- a/src/models/exercise.php +++ b/src/models/exercise.php @@ -46,4 +46,18 @@ public static function getExercises() return $exercises; } + + public static function getExercisesAnswering() + { + $database_access = (new DatabasesChoose())->getDatabase(); + $exercises_data = $database_access->getExercisesAnswering(); + + $exercises = []; + foreach ($exercises_data as $exercise_data) { + $exercise = new self($exercise_data['id']); + $exercises[] = $exercise; + } + + return $exercises; + } } diff --git a/src/views/exercises_root.php b/src/views/exercises_root.php index 8c2e3c0..4575544 100644 --- a/src/views/exercises_root.php +++ b/src/views/exercises_root.php @@ -1,7 +1,7 @@ From d299edcf064d92c2b680f374e9c7bd07ba91b893 Mon Sep 17 00:00:00 2001 From: Guillaume1868 Date: Thu, 26 Sep 2024 09:59:21 +0000 Subject: [PATCH 8/8] [Action][php-cs-fixer] Fix styling --- src/models/databases_connectors/databases_access.php | 1 + .../databases_connectors/postgresql/postgresql_access.php | 1 + src/models/exercise.php | 4 ++-- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/models/databases_connectors/databases_access.php b/src/models/databases_connectors/databases_access.php index a0cf1a1..e5c0895 100644 --- a/src/models/databases_connectors/databases_access.php +++ b/src/models/databases_connectors/databases_access.php @@ -9,5 +9,6 @@ public function createExercise(string $title): int; public function getExerciseTitle(int $id): string; public function getExercises(): array; + public function getExercisesAnswering(): array; } diff --git a/src/models/databases_connectors/postgresql/postgresql_access.php b/src/models/databases_connectors/postgresql/postgresql_access.php index 9647e1a..eb31e42 100644 --- a/src/models/databases_connectors/postgresql/postgresql_access.php +++ b/src/models/databases_connectors/postgresql/postgresql_access.php @@ -33,6 +33,7 @@ public function getExercises(): array { return $this->postgresql->select('SELECT id FROM exercises;'); } + public function getExercisesAnswering(): array { return $this->postgresql->select('SELECT id FROM exercises WHERE status = 1;'); diff --git a/src/models/exercise.php b/src/models/exercise.php index 47142ca..03b6166 100644 --- a/src/models/exercise.php +++ b/src/models/exercise.php @@ -51,13 +51,13 @@ public static function getExercisesAnswering() { $database_access = (new DatabasesChoose())->getDatabase(); $exercises_data = $database_access->getExercisesAnswering(); - + $exercises = []; foreach ($exercises_data as $exercise_data) { $exercise = new self($exercise_data['id']); $exercises[] = $exercise; } - + return $exercises; } }