From c9768f3d158c3d600f6b7fbdaf6d0217af63b36b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20P=C5=82aczek?= Date: Fri, 25 Oct 2024 16:38:40 +0200 Subject: [PATCH] Add an endpoint to generate 20 questions test. --- src/Controller/ChallengeController.php | 23 ++++++++++++++++------- src/Service/QuestionsService.php | 9 ++++----- templates/base.html.twig | 4 ++-- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/src/Controller/ChallengeController.php b/src/Controller/ChallengeController.php index 0455613..f224499 100644 --- a/src/Controller/ChallengeController.php +++ b/src/Controller/ChallengeController.php @@ -32,6 +32,22 @@ class ChallengeController extends AbstractController return $this->render('challenge/start.html.twig', ['questions' => $questions, 'challenge' => $challenge]); } + #[Route('/random', name: 'app_quick_20', methods: ['GET'])] + public function quick(QuestionsService $questionsService): Response + { + $questions = array_slice($questionsService->getQuestions(), 0, 20); + + return $this->render('challenge/start.html.twig', ['questions' => $questions, 'challenge' => 'Quick 20 random questions']); + } + + #[Route('/random', name: 'app_quick_20_submit', methods: ['POST'])] + public function quickSubmit(Request $request, QuestionsService $questionsService): Response + { + $answers = $request->getPayload()->all('answer'); + $questions = $questionsService->questionsById($request->getPayload()->all('question_id')); + + return $this->render('challenge/submit.html.twig', ['questions' => $questions, 'challenge' => 'Quick 20 random questions', 'answers' => $answers]); + } #[Route('/challenge/{challenge<.*>}', name: 'app_challenge2', methods: ['POST'])] public function challengeSubmit(Request $request, QuestionsService $questionsService, string $challenge): Response @@ -39,13 +55,6 @@ class ChallengeController extends AbstractController $answers = $request->getPayload()->all('answer'); $questions = $questionsService->questionsById($request->getPayload()->all('question_id')); - //dd($answers); - //dd($questionsService->questionsById($request->getPayload()->all('question_id'))); - //$x = $request->getPayload()->get('question_id'); - //dd($questionsService->questionsById($request->getPayload()->all('question_id'))); - //dd($request->getPayload()->all()); - - //$questions = $questionsService->getQuestions($challenge); return $this->render('challenge/submit.html.twig', ['questions' => $questions, 'challenge' => $challenge, 'answers' => $answers]); } diff --git a/src/Service/QuestionsService.php b/src/Service/QuestionsService.php index 6ddb924..109fa27 100644 --- a/src/Service/QuestionsService.php +++ b/src/Service/QuestionsService.php @@ -17,7 +17,9 @@ class QuestionsService $questions = []; foreach ($this->getQuestionFiles(file: $file) as $file) { $yamlFile = Yaml::parseFile($file->getRealPath()); - $questions = array_merge($questions, $yamlFile['questions']); + if (isset($yamlFile['questions'])) { + $questions = array_merge($questions, $yamlFile['questions']); + } } if ($shuffle) { shuffle($questions); @@ -41,14 +43,11 @@ class QuestionsService foreach ($this->getQuestionFiles() as $file) { $yamlFile = Yaml::parseFile($file->getRealPath()); if (isset($yamlFile['questions'])) { - //dd(array_column($yamlFile['questions'], 'uuid')); - //dd($yamlFile['questions']); $questions += array_combine(array_column($yamlFile['questions'], 'uuid'), $yamlFile['questions']); - //$questions = array_merge($questions, $yamlFile['questions']); } } - return array_filter($questions, fn(array $value): bool => in_array($value['uuid'], $questionIds, true)); + return array_merge(array_flip($questionIds), array_filter($questions, fn(array $value): bool => in_array($value['uuid'], $questionIds, true))); } private function getQuestionFiles(?string $file = null, ?string $group = null): array diff --git a/templates/base.html.twig b/templates/base.html.twig index 64e18d7..45b296f 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -53,8 +53,8 @@