Add an endpoint to generate 20 questions test.

This commit is contained in:
Krzysztof Płaczek
2024-10-25 16:38:40 +02:00
parent d07e20a1d5
commit c9768f3d15
3 changed files with 22 additions and 14 deletions

View File

@@ -32,6 +32,22 @@ class ChallengeController extends AbstractController
return $this->render('challenge/start.html.twig', ['questions' => $questions, 'challenge' => $challenge]); 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'])] #[Route('/challenge/{challenge<.*>}', name: 'app_challenge2', methods: ['POST'])]
public function challengeSubmit(Request $request, QuestionsService $questionsService, string $challenge): Response public function challengeSubmit(Request $request, QuestionsService $questionsService, string $challenge): Response
@@ -39,13 +55,6 @@ class ChallengeController extends AbstractController
$answers = $request->getPayload()->all('answer'); $answers = $request->getPayload()->all('answer');
$questions = $questionsService->questionsById($request->getPayload()->all('question_id')); $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]); return $this->render('challenge/submit.html.twig', ['questions' => $questions, 'challenge' => $challenge, 'answers' => $answers]);
} }

View File

@@ -17,7 +17,9 @@ class QuestionsService
$questions = []; $questions = [];
foreach ($this->getQuestionFiles(file: $file) as $file) { foreach ($this->getQuestionFiles(file: $file) as $file) {
$yamlFile = Yaml::parseFile($file->getRealPath()); $yamlFile = Yaml::parseFile($file->getRealPath());
$questions = array_merge($questions, $yamlFile['questions']); if (isset($yamlFile['questions'])) {
$questions = array_merge($questions, $yamlFile['questions']);
}
} }
if ($shuffle) { if ($shuffle) {
shuffle($questions); shuffle($questions);
@@ -41,14 +43,11 @@ class QuestionsService
foreach ($this->getQuestionFiles() as $file) { foreach ($this->getQuestionFiles() as $file) {
$yamlFile = Yaml::parseFile($file->getRealPath()); $yamlFile = Yaml::parseFile($file->getRealPath());
if (isset($yamlFile['questions'])) { 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_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 private function getQuestionFiles(?string $file = null, ?string $group = null): array

View File

@@ -53,8 +53,8 @@
<div class="hidden sm:ml-6 sm:block"> <div class="hidden sm:ml-6 sm:block">
<div class="flex space-x-4"> <div class="flex space-x-4">
<!-- Current: "bg-gray-900 text-white", Default: "text-gray-300 hover:bg-gray-700 hover:text-white" --> <!-- Current: "bg-gray-900 text-white", Default: "text-gray-300 hover:bg-gray-700 hover:text-white" -->
<a href="#" class="rounded-md bg-gray-900 px-3 py-2 text-sm font-medium text-white" aria-current="page">Home</a> <a href="{{ path('app_home') }}" class="rounded-md bg-gray-900 px-3 py-2 text-sm font-medium text-white" aria-current="page">Home</a>
{# <a href="#" class="rounded-md px-3 py-2 text-sm font-medium text-gray-300 hover:bg-gray-700 hover:text-white">Team</a>#} <a href="{{ path('app_quick_20') }}" class="rounded-md px-3 py-2 text-sm font-medium text-gray-300 hover:bg-gray-700 hover:text-white">Quick 20</a>
{# <a href="#" class="rounded-md px-3 py-2 text-sm font-medium text-gray-300 hover:bg-gray-700 hover:text-white">Projects</a>#} {# <a href="#" class="rounded-md px-3 py-2 text-sm font-medium text-gray-300 hover:bg-gray-700 hover:text-white">Projects</a>#}
{# <a href="#" class="rounded-md px-3 py-2 text-sm font-medium text-gray-300 hover:bg-gray-700 hover:text-white">Calendar</a>#} {# <a href="#" class="rounded-md px-3 py-2 text-sm font-medium text-gray-300 hover:bg-gray-700 hover:text-white">Calendar</a>#}
</div> </div>