mirror of
https://github.com/MarijnDoeve/TijdVoorDeTest.git
synced 2026-03-06 04:44:19 +01:00
Add quiz management features and improve UI
This commit is contained in:
@@ -10,11 +10,13 @@ use App\Entity\User;
|
||||
use App\Repository\CandidateRepository;
|
||||
use App\Repository\SeasonRepository;
|
||||
use App\Security\Voter\SeasonVoter;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Attribute\AsController;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
|
||||
#[AsController]
|
||||
#[IsGranted('ROLE_USER')]
|
||||
@@ -59,4 +61,20 @@ final class BackofficeController extends AbstractController
|
||||
'result' => $this->candidateRepository->getScores($quiz),
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/backoffice/{seasonCode}/{quiz}/enable', name: 'app_backoffice_enable')]
|
||||
#[IsGranted(SeasonVoter::EDIT, subject: 'season')]
|
||||
public function enableQuiz(Season $season, Quiz $quiz, EntityManagerInterface $em): Response
|
||||
{
|
||||
$season->setActiveQuiz($quiz);
|
||||
$em->flush();
|
||||
|
||||
return $this->redirectToRoute('app_backoffice_season', ['seasonCode' => $season->getSeasonCode()]);
|
||||
}
|
||||
|
||||
#[Route('/backoffice/{seasonCode}/{quiz}/yaml')]
|
||||
public function testRoute(Season $season, Quiz $quiz, SerializerInterface $serializer): Response
|
||||
{
|
||||
return new Response($serializer->serialize(\App\Resource\Quiz::fromEntity($quiz)->questions, 'yaml', ['yaml_inline' => 100, 'yaml_flags' => 0]), headers: ['Content-Type' => 'text/yaml']);
|
||||
}
|
||||
}
|
||||
|
||||
42
src/Resource/Quiz.php
Normal file
42
src/Resource/Quiz.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Resource;
|
||||
|
||||
use App\Entity\Answer;
|
||||
use App\Entity\Quiz as QuizEntity;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
|
||||
final class Quiz
|
||||
{
|
||||
/** @param array<string, array<string, bool>> $questions*/
|
||||
public function __construct(
|
||||
public array $questions,
|
||||
) {}
|
||||
|
||||
public static function fromEntity(QuizEntity $quiz): self
|
||||
{
|
||||
$questions = [];
|
||||
|
||||
foreach ($quiz->getQuestions() as $question) {
|
||||
$questions[$question->getQuestion()] = self::answerArray($question->getAnswers());
|
||||
}
|
||||
|
||||
return new self($questions);
|
||||
}
|
||||
|
||||
/** @param Collection<int, Answer> $answers
|
||||
* @return array<string, bool>
|
||||
**/
|
||||
private static function answerArray(Collection $answers): array
|
||||
{
|
||||
$result = [];
|
||||
|
||||
foreach ($answers as $answer) {
|
||||
$result[$answer->getText()] = $answer->isRightAnswer();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user