From 02ba404afe6971e97d58b82ec8751604ecb6fd95 Mon Sep 17 00:00:00 2001 From: Audrey Jensen Date: Thu, 29 Jun 2023 14:56:51 +0000 Subject: [PATCH] parse quiz file --- src/Form/SafetyQuizType.php | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/Form/SafetyQuizType.php b/src/Form/SafetyQuizType.php index af66223..f5edc68 100644 --- a/src/Form/SafetyQuizType.php +++ b/src/Form/SafetyQuizType.php @@ -2,34 +2,35 @@ namespace App\Form; +use Psr\Log\LoggerInterface; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\RadioType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; +use Symfony\Component\Yaml\Yaml; class SafetyQuizType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options): void { - $builder - ->add('Question_1', ChoiceType::class, [ - 'choices' => [ - 'Correct' => true, - 'False' => false, - 'False again' => false - ], - 'expanded' => true - ]) - ->add('Question_2', ChoiceType::class, [ - 'choices' => [ - 'Correct' => true, - 'False' => false, - 'False again' => false - ], - 'expanded' => true - ]) - ; + $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) + { + //read answer choices + $choiceArr = array(); + foreach($value['Choices'] as $choice ) + { + $choiceArr[$choice['Label']] = $choice['Value']; + } + + $builder->add($key, ChoiceType::class, [ + 'choices' => $choiceArr, + 'expanded' => true, + 'label' => $value['Text'] + ]); + } } public function configureOptions(OptionsResolver $resolver): void