src/Entity/Core/Quiz/QuizUnit.php line 17

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity\Core\Quiz;
  4. use App\Entity\User\User;
  5. use App\Session\Core\SessionUnitInterface;
  6. use DateTime;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\ORM\Mapping as ORM;
  10. #[ORM\Table('quiz_unit')]
  11. #[ORM\Entity(repositoryClass: QuizUnitRepository::class)]
  12. class QuizUnit implements QuizUnitInterface, SessionUnitInterface
  13. {
  14. public function getParent(): QuizSession
  15. {
  16. return $this->getQuizSession();
  17. }
  18. /**
  19. * @var int
  20. */
  21. #[ORM\Column(name: 'id', type: 'integer')]
  22. #[ORM\Id]
  23. #[ORM\GeneratedValue(strategy: 'AUTO')]
  24. private int $id;
  25. /**
  26. * @var QuizSession
  27. */
  28. #[ORM\JoinColumn(name: 'quiz_session', referencedColumnName: 'id', onDelete: 'CASCADE')]
  29. #[ORM\ManyToOne(targetEntity: QuizSession::class, inversedBy: 'quizUnits')]
  30. private QuizSession $quizSession;
  31. /**
  32. * @var User
  33. */
  34. #[ORM\JoinColumn(name: 'student', referencedColumnName: 'id')]
  35. #[ORM\ManyToOne(targetEntity: User::class)]
  36. private User $student;
  37. /**
  38. * @var DateTime|null
  39. */
  40. #[ORM\Column(name: 'deadline', type: 'datetime', nullable: true)]
  41. private ?DateTime $deadline = null;
  42. /**
  43. * @var bool
  44. */
  45. #[ORM\Column(name: 'finished', type: 'boolean', options: ['default' => false])]
  46. private bool $finished;
  47. /**
  48. * @var DateTime|null
  49. */
  50. #[ORM\Column(name: 'finished_at', type: 'datetime', nullable: true)]
  51. private ?DateTime $finishedAt;
  52. /**
  53. * @var DateTime|null
  54. */
  55. #[ORM\Column(name: 'started_at', type: 'datetime', nullable: true)]
  56. private ?DateTime $startedAt;
  57. /**
  58. * @var QuizResponse[]
  59. */
  60. #[ORM\OneToMany(targetEntity: QuizResponse::class, mappedBy: 'quizUnit', cascade: ['persist'])]
  61. private Collection|array $quizResponses;
  62. /**
  63. * @var DateTime|null
  64. */
  65. #[ORM\Column(name: 'extended_deadline', type: 'datetime', nullable: true)]
  66. private ?DateTime $extendedDeadline = null;
  67. /**
  68. * @var string|null
  69. */
  70. #[ORM\Column(name: 'mathproblems_data', type: 'string', length: 5400, nullable: true)]
  71. private ?string $mathproblemsData = null;
  72. /**
  73. * @var int|null
  74. */
  75. #[ORM\Column(name: 'seconds_left', type: 'integer', nullable: true, options: ['default' => 0])]
  76. private ?int $secondsLeft = null;
  77. /**
  78. * @var int
  79. */
  80. #[ORM\Column(name: 'seconds_paused', type: 'integer', options: ['default' => 0])]
  81. private int $secondsPaused;
  82. /**
  83. * @var DateTime|null
  84. */
  85. #[ORM\Column(name: 'time_resumed', type: 'datetime', nullable: true, options: ['default' => '1970-01-01 00:00:00'])]
  86. private ?DateTime $timeResumed;
  87. /**
  88. * @var DateTime|null
  89. */
  90. #[ORM\Column(name: 'time_paused', type: 'datetime', nullable: true, options: ['default' => '1970-01-01 00:00:00'])]
  91. private ?DateTime $timePaused;
  92. /**
  93. * @var DateTime|null
  94. */
  95. #[ORM\Column(name: 'start_time', type: 'datetime', nullable: true)]
  96. private ?DateTime $startTime;
  97. /**
  98. * @var ?User
  99. */
  100. #[ORM\JoinColumn(name: 'shared_by', referencedColumnName: 'id', nullable: true)]
  101. #[ORM\ManyToOne(targetEntity: User::class)]
  102. private ?User $sharedBy = null;
  103. public function __construct()
  104. {
  105. $this->quizResponses = new ArrayCollection();
  106. $this->secondsPaused = 0;
  107. $this->startTime = new DateTime('1970-01-01 00:00:00');
  108. }
  109. public function getStartTime(): ?DateTime
  110. {
  111. return $this->startTime;
  112. }
  113. public function setStartTime(?DateTime $startTime): void
  114. {
  115. $this->startTime = $startTime;
  116. }
  117. public function getTimePaused(): ?DateTime
  118. {
  119. return $this->timePaused;
  120. }
  121. public function setTimePaused(?DateTime $timePaused): void
  122. {
  123. $this->timePaused = $timePaused;
  124. }
  125. public function getTimeResumed(): ?DateTime
  126. {
  127. return $this->timeResumed;
  128. }
  129. public function setTimeResumed(?DateTime $timeResumed): void
  130. {
  131. $this->timeResumed = $timeResumed;
  132. }
  133. public function getSecondsPaused(): int
  134. {
  135. return $this->secondsPaused;
  136. }
  137. public function setSecondsPaused(int $secondsPaused): void
  138. {
  139. $this->secondsPaused = $secondsPaused;
  140. }
  141. public function addSecondsPaused(int $seconds): void
  142. {
  143. $this->secondsPaused += $seconds;
  144. }
  145. public function getSecondsLeft(): ?int
  146. {
  147. return $this->secondsLeft;
  148. }
  149. public function setSecondsLeft(?int $seconds): void
  150. {
  151. $this->secondsLeft = $seconds;
  152. }
  153. public function getId(): int
  154. {
  155. return $this->id;
  156. }
  157. public function getQuizSession(): QuizSession
  158. {
  159. return $this->quizSession;
  160. }
  161. /**
  162. * @return $this
  163. */
  164. public function setQuizSession(QuizSession $quizSession): static
  165. {
  166. $this->quizSession = $quizSession;
  167. return $this;
  168. }
  169. public function getStudent(): User
  170. {
  171. return $this->student;
  172. }
  173. /**
  174. * @return $this
  175. */
  176. public function setStudent(User $student): static
  177. {
  178. $this->student = $student;
  179. return $this;
  180. }
  181. public function getQuizResponses(): Collection
  182. {
  183. return $this->quizResponses;
  184. }
  185. public function getValidQuizResponses(): array
  186. {
  187. $quizResponses = [];
  188. foreach ($this->quizResponses as $response) {
  189. if ($response->isValid()) {
  190. $quizResponses[] = $response;
  191. }
  192. }
  193. // sort by position
  194. usort($quizResponses, function (QuizResponse $first, QuizResponse $second) {
  195. return $first->getPosition() <=> $second->getPosition();
  196. });
  197. return $quizResponses;
  198. }
  199. /**
  200. * @param QuizResponse[] $quizResponses
  201. *
  202. * @return $this
  203. */
  204. public function setQuizResponses(array $quizResponses): static
  205. {
  206. $this->quizResponses = $quizResponses;
  207. return $this;
  208. }
  209. /**
  210. * @return $this
  211. */
  212. public function addQuizResponse(QuizResponseInterface $quizResponse): static
  213. {
  214. $quizResponse->setQuizUnit($this);
  215. $this->quizResponses->add($quizResponse);
  216. return $this;
  217. }
  218. public function isFinished(): bool
  219. {
  220. return $this->finished;
  221. }
  222. public function setFinished(bool $finished): void
  223. {
  224. $this->finished = $finished;
  225. }
  226. public function isPaused(): bool
  227. {
  228. return $this->timePaused > $this->timeResumed;
  229. }
  230. public function getDeadline(): ?DateTime
  231. {
  232. return $this->deadline;
  233. }
  234. public function setDeadline(?DateTime $deadline): void
  235. {
  236. $this->deadline = $deadline;
  237. }
  238. public function getExtendedDeadline(): ?DateTime
  239. {
  240. return $this->extendedDeadline;
  241. }
  242. public function setExtendedDeadline(?DateTime $extendedDeadline): void
  243. {
  244. $this->extendedDeadline = $extendedDeadline;
  245. }
  246. public function getMathproblemsData(): ?string
  247. {
  248. return $this->mathproblemsData;
  249. }
  250. /**
  251. * @param $mathproblemsData
  252. */
  253. public function setMathproblemsData($mathproblemsData): void
  254. {
  255. $this->mathproblemsData = $mathproblemsData;
  256. }
  257. public function getFinishedAt(): ?DateTime
  258. {
  259. return $this->finishedAt;
  260. }
  261. public function setFinishedAt(?DateTime $finishedAt): void
  262. {
  263. $this->finishedAt = $finishedAt;
  264. }
  265. public function getStartedAt(): ?DateTime
  266. {
  267. return $this->startedAt;
  268. }
  269. public function setStartedAt(?DateTime $startedAt): void
  270. {
  271. $this->startedAt = $startedAt;
  272. }
  273. public function getTimeSpent(): ?array
  274. {
  275. if ($this->startedAt === null || $this->finishedAt === null)
  276. return null;
  277. $interval = $this->startedAt->diff($this->finishedAt);
  278. return [
  279. 'days' => (int)$interval->format('%a'), // Total days
  280. 'hours' => (int)$interval->format('%H'),
  281. 'minutes' => (int)$interval->format('%I'),
  282. 'seconds' => (int)$interval->format('%s')
  283. ];
  284. }
  285. public function getSharedBy(): ?User
  286. {
  287. return $this->sharedBy;
  288. }
  289. public function setSharedBy(?User $sharedBy): void
  290. {
  291. $this->sharedBy = $sharedBy;
  292. }
  293. }