src/CoreBundle/Entity/QuizResponse.php line 12

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