Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add color and submit function for random quizz #14

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets/controllers.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"controllers": {
"@symfony/ux-turbo": {
"turbo-core": {
"enabled": true,
"enabled": false,
"fetch": "eager"
},
"mercure-turbo-stream": {
Expand Down
12 changes: 11 additions & 1 deletion assets/styles/components/pages/_quizz.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
.questions {

.questions__random__form__tab{
border: solid 0.5rem;
border-radius: 0.5rem;
padding: 0.5rem;
&.sucess {
border-color: $sucess;
}
&.error {
border-color: $error;
}
}
}
15 changes: 11 additions & 4 deletions src/Controller/Front/QuizzController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Form\Front\RandomQuizzType;
use App\Repository\QuestionRepository;
use App\Service\Front\QuizzCorrectionInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -13,14 +14,20 @@ class QuizzController extends AbstractController
{

#[Route('/quizz', name: 'app_front_quizz')]
public function __invoke(QuestionRepository $questionRepository, Request $request): Response
public function __invoke(QuestionRepository $questionRepository, Request $request, QuizzCorrectionInterface $quizzValidator): Response
{
$questions = $questionRepository->findRandom(20);
$form = $this->createForm(RandomQuizzType::class,null,['questions' => $questions]);
$form = $this->createForm(RandomQuizzType::class,null,[
'questions' => $questions]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
return $this->redirectToRoute('task_success');
$data = $form->getData();
$correction = $quizzValidator->correctForm($data);
return $this->render('components/pages/quizz.html.twig',[
'form' => $form,
'correction' => $correction,
'score' => $quizzValidator->calculateScore($correction)
]);
}
return $this->render('components/pages/quizz.html.twig',[
'form' => $form
Expand Down
31 changes: 31 additions & 0 deletions src/Entity/Score.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Entity;

class Score
{

private int $score;

private int $nbrQuestion;

/**
* @param int $score
* @param int $nbrQuestion
*/
public function __construct(int $score, int $nbrQuestion)
{
$this->score = $score;
$this->nbrQuestion = $nbrQuestion;
}

public function getScore(): int
{
return $this->score;
}

public function getNbrQuestion(): int
{
return $this->nbrQuestion;
}
}
3 changes: 2 additions & 1 deletion src/Form/Front/RandomQuizzType.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => null,
'questions' => []
'questions' => [],
'correction' => []
]);
}
}
21 changes: 21 additions & 0 deletions src/Service/Front/QuizzCorrectionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Service\Front;

use App\Entity\Answer;
use App\Entity\Score;

interface QuizzCorrectionInterface
{
/**
* @param array<int,Answer[]> $data
* @return array<int,array<int,bool>>
*/
public function correctForm(array $data):array;

/**
* @param array<int,array<int,bool>> $correction $correction
* @return Score
*/
public function calculateScore(array $correction):Score;
}
62 changes: 62 additions & 0 deletions src/Service/Front/RandomQuizzCorrection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace App\Service\Front;

use App\Entity\Score;
use App\Repository\QuestionRepository;

class RandomQuizzCorrection implements QuizzCorrectionInterface
{
private QuestionRepository $questionRepository;

/**
* @param QuestionRepository $questionRepository
*/
public function __construct(QuestionRepository $questionRepository)
{
$this->questionRepository = $questionRepository;
}


public function calculateScore(array $correction): Score
{
$score = 0;
foreach ($correction as $answers){
$wrongAnswer = false;
foreach ($answers as $answer)
if ($answer === false)
$wrongAnswer = true;
if ($wrongAnswer === false)
$score+=1;
}
return new Score($score, count($correction));
}


/**
* Get all the correct answer with QuestionId and compare to the User response. Give the correct answer in response
*/
public function correctForm(array $data): array
{
$correction = [];
foreach ($data as $questionId => $answersIds){
$question = $this->questionRepository->findOneBy(['id' => $questionId]);
$answers = $question->getAnswers();
foreach ($answers as $answer) {
//Choose a good answer
if ($answer->isValid() && in_array($answer, $answersIds))
$correction[$questionId][$answer->getId()] = true;
//Choose a wrong answer
elseif (!$answer->isValid() && in_array($answer, $answersIds))
$correction[$questionId][$answer->getId()] = false;
//Miss one good answer
elseif ($answer->isValid() && !in_array($answer, $answersIds))
$correction[$questionId][$answer->getId()] = false;
//Don't choose wrong answer
else
$correction[$questionId][$answer->getId()] = true;
}
}
return $correction;
}
}
22 changes: 22 additions & 0 deletions src/Twig/toInt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class toInt extends AbstractExtension
{
public function getFilters(): array
{
return [
new TwigFilter('toInt', [$this, 'toInt']),
];
}

public function toInt(string $string): int
{
return (int)$string;
}

}
10 changes: 9 additions & 1 deletion templates/components/pages/quizz.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@
{% block body %}
<div class="wrapper questions">
<h2 class="section_title">Random questions</h2>
{% if score is defined %}
<section class="questions__results">
{{ score.score }} / {{ score.nbrQuestion }}
</section>
{% endif %}

<section class="symfony_form">
{{ form_start(form) }}
{% for child in form.children %}
<div class="questions__random__form__tab d-none">
{% set questionId = (child.vars.name | toInt) %}
{% set is_valid = (correction[questionId] is defined and not (false in correction[questionId])) %}
<div class="questions__random__form__tab d-none {{ correction is defined and is_valid ? 'sucess' : 'error' }}">
{% if child.vars.row_attr.name is defined %}
<h3>{{ child.vars.row_attr.name }}</h3>
{% endif %}
Expand Down
Loading