src/EventListener/Questionary/SendToCuratorEventListener.php line 26

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventListener\Questionary;
  4. use App\DBAL\Types\RoleEnumType;
  5. use App\Entity\QuestionaryAttempt;
  6. use App\Event\Questionary\SendToCuratorEvent;
  7. use App\Repository\UserRepository;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. class SendToCuratorEventListener
  10. {
  11.     private UserRepository $userRepository;
  12.     private EntityManagerInterface $em;
  13.     public function __construct(
  14.         UserRepository $userRepository,
  15.         EntityManagerInterface $em
  16.     ) {
  17.         $this->userRepository $userRepository;
  18.         $this->em $em;
  19.     }
  20.     public function onCuratorSend(SendToCuratorEvent $event): void
  21.     {
  22.         $users $this->userRepository->findUsersWithRole(RoleEnumType::ROLE_CURATOR);
  23.         foreach ($users as $user) {
  24.             $qAttempt = new QuestionaryAttempt();
  25.             $qAttempt->setStudent($user);
  26.             $qAttempt->setQuestionary($event->getQuestionary());
  27.             $qAttempt->setIsDone(false);
  28.             $qAttempt->setTitle($event->getQuestionary()->getTitle());
  29.             $this->em->persist($qAttempt);
  30.         }
  31.         $this->em->flush();
  32.     }
  33. }