src/CoreBundle/Entity/QuizUnit.php line 17

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