src/Entity/QuizQuestion.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\QuizQuestionRepository;
  4. use App\Traits\DateTrait;
  5. use DateTime;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\Common\Collections\Collection;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Symfony\Component\HttpFoundation\File\File;
  10. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  11. /**
  12. * @ORM\Entity(repositoryClass=QuizQuestionRepository::class)
  13. *
  14. * @Vich\Uploadable
  15. */
  16. class QuizQuestion
  17. {
  18. const TYPE_RADIO = 'radio';
  19. const TYPE_CHECKBOX = 'checkbox';
  20. const TYPE = [
  21. 'Choix unique' => self::TYPE_RADIO,
  22. 'Choix multiple' => self::TYPE_CHECKBOX,
  23. ];
  24. /**
  25. * @ORM\Id
  26. * @ORM\GeneratedValue
  27. * @ORM\Column(type="integer")
  28. */
  29. private $id;
  30. /**
  31. * @ORM\Column(type="string", length=255)
  32. */
  33. private ?string $text;
  34. /**
  35. * @ORM\Column(type="string", length=255, nullable=true)
  36. */
  37. private ?string $image = NULL;
  38. /**
  39. * @Vich\UploadableField(mapping="custom_product_images", fileNameProperty="image")
  40. */
  41. private ?File $imageFile = NULL;
  42. /**
  43. * @ORM\Column(type="string", length=255, options={"default":self::TYPE_RADIO})
  44. */
  45. private string $type = self::TYPE_RADIO;
  46. /**
  47. * @ORM\Column(type="integer", nullable=true)
  48. */
  49. private ?int $timeLimit;
  50. /**
  51. * @ORM\Column(type="integer", nullable=true)
  52. */
  53. private ?int $orderIndex;
  54. /**
  55. * @ORM\Column(type="text", nullable=true)
  56. */
  57. protected ?string $explanation;
  58. /**
  59. * @ORM\OneToMany(targetEntity=QuizAnswer::class, mappedBy="quizQuestion", cascade={"remove", "persist"})
  60. */
  61. private $quizAnswers;
  62. /**
  63. * @ORM\ManyToOne(targetEntity=Quiz::class, inversedBy="quizQuestions")
  64. */
  65. private ?Quiz $quiz;
  66. use DateTrait;
  67. public function __construct()
  68. {
  69. $this->quizAnswers = new ArrayCollection();
  70. }
  71. public function getId(): ?int
  72. {
  73. return $this->id;
  74. }
  75. public function getText(): ?string
  76. {
  77. return $this->text;
  78. }
  79. public function setText( string $text ): QuizQuestion
  80. {
  81. $this->text = $text;
  82. return $this;
  83. }
  84. public function getImage(): ?string
  85. {
  86. return $this->image;
  87. }
  88. public function setImage( ?string $image ): QuizQuestion
  89. {
  90. $this->image = $image;
  91. return $this;
  92. }
  93. public function getImageFile(): ?File
  94. {
  95. return $this->imageFile;
  96. }
  97. public function setImageFile(File $imageFile): QuizQuestion
  98. {
  99. $this->imageFile = $imageFile;
  100. $this->updatedAt = new DateTime();
  101. return $this;
  102. }
  103. public function getType(): ?string
  104. {
  105. return $this->type;
  106. }
  107. public function setType( string $type ): QuizQuestion
  108. {
  109. $this->type = $type;
  110. return $this;
  111. }
  112. public function getTimeLimit(): ?int
  113. {
  114. return $this->timeLimit;
  115. }
  116. public function setTimeLimit( ?int $timeLimit ): QuizQuestion
  117. {
  118. $this->timeLimit = $timeLimit;
  119. return $this;
  120. }
  121. public function getOrderIndex(): ?int
  122. {
  123. return $this->orderIndex;
  124. }
  125. public function setOrderIndex( ?int $orderIndex ): QuizQuestion
  126. {
  127. $this->orderIndex = $orderIndex;
  128. return $this;
  129. }
  130. public function getExplanation(): ?string
  131. {
  132. return $this->explanation;
  133. }
  134. public function setExplanation( ?string $explanation ): QuizQuestion
  135. {
  136. $this->explanation = $explanation;
  137. return $this;
  138. }
  139. public function getQuizAnswers(): Collection
  140. {
  141. return $this->quizAnswers;
  142. }
  143. public function addQuizAnswer( QuizAnswer $quizAnswer ): QuizQuestion
  144. {
  145. if ( !$this->quizAnswers->contains( $quizAnswer ) ) {
  146. $this->quizAnswers[] = $quizAnswer;
  147. $quizAnswer->setQuizQuestion( $this );
  148. }
  149. return $this;
  150. }
  151. public function removeQuizAnswer( QuizAnswer $quizAnswer ): QuizQuestion
  152. {
  153. if ( $this->quizAnswers->removeElement( $quizAnswer ) ) {
  154. // set the owning side to null (unless already changed)
  155. if ( $quizAnswer->getQuizQuestion() === $this ) {
  156. $quizAnswer->setQuizQuestion( NULL );
  157. }
  158. }
  159. return $this;
  160. }
  161. public function getQuiz(): ?Quiz
  162. {
  163. return $this->quiz;
  164. }
  165. public function setQuiz( ?Quiz $quiz ): QuizQuestion
  166. {
  167. $this->quiz = $quiz;
  168. return $this;
  169. }
  170. public function getCorrectAnswer()
  171. {
  172. return $this->quizAnswers->filter(static function($answer){
  173. return $answer->getIsRight();
  174. })->toArray();
  175. }
  176. public function getWrongAnswer()
  177. {
  178. return $this->quizAnswers->filter(static function($answer){
  179. return !$answer->getIsRight();
  180. })->toArray();
  181. }
  182. public function isTimeLimited(): bool
  183. {
  184. return $this->timeLimit !== NULL && $this->timeLimit > 0;
  185. }
  186. }