src/EventSubscriber/ElExamAnswerSubscriber.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\ElCourseJoin;
  4. use App\Entity\ElExamAnswer;
  5. use App\Entity\ElExamAttempt;
  6. use App\Entity\ElExamQuestion;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use App\Event\ElExamAnswerPreUpdateEvent;
  9. use App\Event\ElExamAnswerPostPersistEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class ElExamAnswerSubscriber implements EventSubscriberInterface
  12. {
  13.     public function __construct(
  14.         private EntityManagerInterface $_em
  15.     ) {
  16.     }
  17.     public static function getSubscribedEvents(): array
  18.     {
  19.         return [
  20.             ElExamAnswerPostPersistEvent::NAME => 'onPostPersist',
  21.             ElExamAnswerPreUpdateEvent::NAME => 'onPreUpdate'
  22.         ];
  23.     }
  24.     public function onPostPersist(ElExamAnswerPostPersistEvent $event)
  25.     {
  26.         $object $event->getObject();
  27.         $elExamAttempt $object->getElExamAttempt();
  28.         if ($elExamAttempt) {
  29.             $elExamAttempt->setCurrentQuestionId($object->getElExamQuestion()->getId());
  30.             $this->_em->persist($elExamAttempt);
  31.             $this->_em->flush();
  32.         }
  33.     }
  34.     public function onPreUpdate(ElExamAnswerPreUpdateEvent $event)
  35.     {
  36.         $data $event->getObject();
  37.         $elExamAttempt $data->getElExamAttempt();
  38.         if ($elExamAttempt->getStatus() === ElExamAttempt::STATUSEXAMATTEMPT_STARTED) {
  39.             $elExamQuestion $data->getElExamQuestion();
  40.             $elExam $elExamQuestion->getElExam();
  41.             if ($elExamQuestion->getQuestionType() === ElExamQuestion::QUESTIONTYPE_SINGLE) {
  42.                 $options $elExamQuestion->getElExamOptions();
  43.                 $data->setIsCorrect(false);
  44.                 foreach ($options as $option) {
  45.                     if ($option->getIsCorrect() && $option->getId() == $data->getAnswer()) {
  46.                         $data->setIsCorrect(true);
  47.                         break;
  48.                     }
  49.                 }
  50.                 if ($elExam->getIsOnlyChoiceQuestion()) {
  51.                     $data->setGivenPoint($data->getIsCorrect() ? $elExamQuestion->getCorrectPoint() : $elExamQuestion->getWrongPoint());
  52.                 }
  53.             } elseif ($elExamQuestion->getQuestionType() === ElExamQuestion::QUESTIONTYPE_MULTI) {
  54.                 $options $elExamQuestion->getElExamOptions();
  55.                 $answers $data->getAnswer();
  56.                 $correctOptions = [];
  57.                 if ($answers) {
  58.                     $answers explode(","$answers);
  59.                     sort($answers);
  60.                     foreach ($options as $option) {
  61.                         if ($option->getIsCorrect()) {
  62.                             $correctOptions[] = $option->getId();
  63.                         }
  64.                     }
  65.                     sort($correctOptions);
  66.                 }
  67.                 $data->setIsCorrect($answers == $correctOptions);
  68.                 if ($elExam->getIsOnlyChoiceQuestion()) {
  69.                     $data->setGivenPoint($data->getIsCorrect() ? $elExamQuestion->getCorrectPoint() : $elExamQuestion->getWrongPoint());
  70.                 }
  71.             }
  72.         }
  73.     }
  74. }