diff --git a/src/Controller/SafetyController.php b/src/Controller/SafetyController.php index 9818f40..524d299 100644 --- a/src/Controller/SafetyController.php +++ b/src/Controller/SafetyController.php @@ -7,7 +7,8 @@ use Symfony\Component\Routing\Annotation\Route; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Yaml\Yaml; use Symfony\Component\HttpFoundation\Request; -use jv\intranet\Types\NavbarItem; +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; +use Psr\Log\LoggerInterface; class SafetyController extends AbstractController { @@ -19,15 +20,29 @@ class SafetyController extends AbstractController return $this->render('Training/Safety/safetyHome.html.twig',['header' => $headerFile, 'content' => $contentFile]); } #[Route('/training/safety/quiz')] - public function Quiz(Request $request) + public function Quiz(Request $request, LoggerInterface $log) { $headerFile = Yaml::parseFile('../config/portalLinks.yaml', 2, 2, Yaml::PARSE_OBJECT_FOR_MAP); $form = $this->createForm(SafetyQuizType::class); $form->handleRequest($request); - if($form->isSubmitted()) + if($form->isSubmitted() && $form->isValid()) { //TODO: Process quiz submissions. Calculate score & return a printable results page + $data = $form->getData(); + $testerName = $data['Name']; + unset($data['Name']); + $correctAnswers = 0; + foreach($data as $question) + { + if($question) + { + $correctAnswers++; + } + } + $log->Debug($testerName . ' quiz answers correct: ' . $correctAnswers); + return $this->render('Training/Safety/quizResults.html.twig', ['header'=>$headerFile, 'testerName'=>$testerName, + 'correctAnswers'=>$correctAnswers, 'totalAnswers'=>count($data)]); } return $this->render('Training/Safety/SafetyQuiz.html.twig',['header' => $headerFile, 'quiz'=>$form]); } diff --git a/src/Form/SafetyQuizType.php b/src/Form/SafetyQuizType.php index d839adc..fb5afa8 100644 --- a/src/Form/SafetyQuizType.php +++ b/src/Form/SafetyQuizType.php @@ -15,6 +15,8 @@ class SafetyQuizType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options): void { + //Get the user's name + $builder->add('Name'); $quizFile = Yaml::parseFile("../config/SafetyQuiz.yaml", 2, 2, Yaml::PARSE_OBJECT_FOR_MAP); //Parse the quiz file & register each question as a multiple-choice form input foreach($quizFile['Questions'] as $key => $value) diff --git a/templates/Training/Safety/SafetyQuiz.html.twig b/templates/Training/Safety/SafetyQuiz.html.twig index 9d55434..78ac920 100644 --- a/templates/Training/Safety/SafetyQuiz.html.twig +++ b/templates/Training/Safety/SafetyQuiz.html.twig @@ -15,9 +15,8 @@
When you're finished, click 'Submit' at the bottom and print your results.
-
You may retake this quiz as many times as you need.
-
To pass this quiz you must answer NaN% of questions correctly.
Print this page and show it to your supervisor or instructor.
{#TODO: figure out what the minimum # of correct answers is #} + +