src/Entity/Core/Quiz/QuizResponse.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Core\Quiz;
  3. use App\Entity\Core\Answer;
  4. use App\Entity\Core\Mathproblem;
  5. use App\Entity\Core\TemplateMathproblem;
  6. use DateTime;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use JsonException;
  9. #[ORM\Table('quiz_response')]
  10. #[ORM\Entity(repositoryClass: QuizResponseRepository::class)]
  11. class QuizResponse implements QuizResponseInterface
  12. {
  13. /**
  14. * @var int
  15. */
  16. #[ORM\Column(name: 'id', type: 'integer')]
  17. #[ORM\Id]
  18. #[ORM\GeneratedValue(strategy: 'AUTO')]
  19. private int $id;
  20. /**
  21. * @var QuizUnit
  22. */
  23. #[ORM\JoinColumn(name: 'quiz_unit', referencedColumnName: 'id', onDelete: 'CASCADE')]
  24. #[ORM\ManyToOne(targetEntity: QuizUnit::class, inversedBy: 'quizResponses')]
  25. private QuizUnit $quizUnit;
  26. /**
  27. * @var Mathproblem
  28. */
  29. #[ORM\JoinColumn(name: 'mathproblem', referencedColumnName: 'id', onDelete: 'CASCADE')]
  30. #[ORM\ManyToOne(targetEntity: Mathproblem::class)]
  31. private Mathproblem $mathproblem;
  32. #[ORM\Column(name: 'createdAt', type: 'datetime', nullable: true)]
  33. private ?DateTime $createdAt;
  34. /**
  35. * @var Answer|null
  36. */
  37. #[ORM\JoinColumn(name: 'answer', referencedColumnName: 'id', onDelete: 'RESTRICT')]
  38. #[ORM\ManyToOne(targetEntity: Answer::class)]
  39. private ?Answer $answerChosen;
  40. #[ORM\Column(name: 'answer_text', type: 'text', nullable: true)]
  41. private ?string $answerText;
  42. #[ORM\Column(type: 'array', nullable: true)]
  43. private ?array $shuffledAnswerOrder;
  44. /**
  45. * @var int
  46. */
  47. #[ORM\Column(name: 'position', type: 'integer')]
  48. private int $position;
  49. /**
  50. * @var bool
  51. */
  52. #[ORM\Column(name: 'is_valid', type: 'boolean', nullable: true, options: ['default' => true])]
  53. private bool $isValid = true;
  54. /**
  55. * @var DateTime
  56. */
  57. #[ORM\Column(name: 'archived_at', type: 'datetime', nullable: true)]
  58. private ?DateTime $archivedAt;
  59. /**
  60. * @var int
  61. */
  62. #[ORM\Column(name: 'bin', type: 'integer', nullable: true)]
  63. private int $bin;
  64. /**
  65. * @var bool
  66. */
  67. #[ORM\Column(name: 'check_answer_clicked', type: 'boolean', nullable: true, options: ['default' => false])]
  68. private bool $checkAnswerClicked;
  69. /**
  70. * @var DateTime
  71. */
  72. #[ORM\Column(name: 'start_time', type: 'datetime', options: ['default' => '1970-01-01 00:00:00'])]
  73. private DateTime $startTime;
  74. /**
  75. * @var int
  76. */
  77. #[ORM\Column(name: 'seconds_delayed', type: 'integer', nullable: false, options: ['default' => 0])]
  78. private int $secondsDelayed = 0;
  79. public function __construct()
  80. {
  81. $this->startTime = new DateTime('1970-01-01 00:00:00');
  82. }
  83. public function getSecondsDelayed(): int
  84. {
  85. return $this->secondsDelayed;
  86. }
  87. /**
  88. * @return $this
  89. */
  90. public function setSecondsDelayed(int $secondsDelayed): static
  91. {
  92. $this->secondsDelayed = $secondsDelayed;
  93. return $this;
  94. }
  95. public function setStartTime(DateTime $startTime): void
  96. {
  97. $this->startTime = $startTime;
  98. }
  99. public function getStartTime(): DateTime
  100. {
  101. return $this->startTime;
  102. }
  103. public function isCheckAnswerClicked(): bool
  104. {
  105. return $this->checkAnswerClicked;
  106. }
  107. public function setCheckAnswerClicked(bool $checkAnswerClicked): void
  108. {
  109. $this->checkAnswerClicked = $checkAnswerClicked;
  110. }
  111. public function getBin(): int
  112. {
  113. return $this->bin;
  114. }
  115. public function setBin(int $bin): void
  116. {
  117. $this->bin = $bin;
  118. }
  119. public function getId(): int
  120. {
  121. return $this->id;
  122. }
  123. public function getQuizUnit(): QuizUnit
  124. {
  125. return $this->quizUnit;
  126. }
  127. /**
  128. * @return $this
  129. */
  130. public function setQuizUnit(QuizUnit $quizUnit): static
  131. {
  132. $this->quizUnit = $quizUnit;
  133. return $this;
  134. }
  135. public function getMathproblem(): Mathproblem
  136. {
  137. return $this->mathproblem;
  138. }
  139. /**
  140. * @return $this
  141. */
  142. public function setMathproblem(Mathproblem $mathproblem): static
  143. {
  144. $this->mathproblem = $mathproblem;
  145. return $this;
  146. }
  147. public function getCreatedAt(): ?DateTime
  148. {
  149. return $this->createdAt;
  150. }
  151. public function setCreatedAt(?DateTime $createdAt): self
  152. {
  153. $this->createdAt = $createdAt;
  154. return $this;
  155. }
  156. public function getAnswerChosen(): ?Answer
  157. {
  158. return $this->answerChosen;
  159. }
  160. public function getDropdownAnswersChosen(): array
  161. {
  162. $answers = json_decode($this->answerText, true);
  163. if (!is_array($answers)) {
  164. // dropdown answers were stored as a string in "answerText" with '|' as delimiter
  165. $answers = explode('|', $this->answerText);
  166. }
  167. return $answers;
  168. }
  169. /**
  170. * @return $this
  171. */
  172. public function setAnswerChosen(?Answer $answerChosen): static
  173. {
  174. $this->answerChosen = $answerChosen;
  175. $this->createdAt = new DateTime("now");
  176. return $this;
  177. }
  178. public function getAnswerText(): ?string
  179. {
  180. return $this->answerText;
  181. }
  182. public function setAnswerText(?string $answerText): self
  183. {
  184. $this->answerText = $answerText;
  185. $this->createdAt = new DateTime("now");
  186. return $this;
  187. }
  188. public function getShuffledAnswerOrder(): ?array
  189. {
  190. return $this->shuffledAnswerOrder;
  191. }
  192. public function setShuffledAnswerOrder(?array $shuffledAnswerOrder): void
  193. {
  194. $this->shuffledAnswerOrder = $shuffledAnswerOrder;
  195. }
  196. public function getPosition(): int
  197. {
  198. return $this->position;
  199. }
  200. /**
  201. * @return $this
  202. */
  203. public function setPosition(int $position): static
  204. {
  205. $this->position = $position;
  206. return $this;
  207. }
  208. public function isValid(): bool
  209. {
  210. return $this->isValid;
  211. }
  212. public function setIsValid(bool $isValid): void
  213. {
  214. $this->isValid = $isValid;
  215. }
  216. public function getArchivedAt(): ?DateTime
  217. {
  218. return $this->archivedAt;
  219. }
  220. public function setArchivedAt(DateTime $archivedAt): void
  221. {
  222. $this->archivedAt = $archivedAt;
  223. }
  224. /**
  225. * @throws JsonException
  226. */
  227. public function getAnswerIsCorrect(): bool
  228. {
  229. $type = $this->getMathproblem()->getTemplate()->getType();
  230. if ($type === TemplateMathproblem::TYPE_DROPDOWN) {
  231. $answersChosen = $this->getDropdownAnswersChosen();
  232. $correctAnswers = $this->getMathproblem()->getCorrectAnswers();
  233. $subAnswer = null;
  234. $isCorrect = true;
  235. $numberOfCorrect = count($correctAnswers);
  236. for ($i = 0; $i < $numberOfCorrect; $i++) {
  237. $corAnswer = $correctAnswers[$i];
  238. $subAnswer = $answersChosen[$i] ?? $subAnswer;
  239. $isCorrect = strcmp($corAnswer, $subAnswer) === 0 ? $isCorrect : false;
  240. }
  241. return $isCorrect;
  242. }
  243. return $this->getAnswerChosen() && $this->getAnswerChosen()->getIsCorrect();
  244. }
  245. }