src/CoreBundle/Entity/QuizSession.php line 18

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace CoreBundle\Entity;
  4. use DateTime;
  5. use DateTimeZone;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Exception;
  8. use UserBundle\Entity\User;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. use JsonSerializable;
  11. /**
  12. * @ORM\Table("quiz_session")
  13. * @ORM\Entity(repositoryClass="CoreBundle\Entity\QuizSessionRepository")
  14. */
  15. class QuizSession implements JsonSerializable
  16. {
  17. /**
  18. * @var int
  19. *
  20. * @ORM\Column(name="id", type="integer")
  21. * @ORM\Id
  22. * @ORM\GeneratedValue(strategy="AUTO")
  23. */
  24. private int $id;
  25. /**
  26. * @var Quiz
  27. *
  28. * @ORM\ManyToOne(targetEntity="CoreBundle\Entity\Quiz", inversedBy="quizSessions")
  29. * @ORM\JoinColumn(name="quiz", referencedColumnName="id", onDelete="CASCADE", nullable=true)
  30. */
  31. private ?Quiz $quiz;
  32. /**
  33. * @var User
  34. *
  35. * @ORM\ManyToOne(targetEntity="UserBundle\Entity\User")
  36. * @ORM\JoinColumn(name="created_by", referencedColumnName="id")
  37. */
  38. private ?User $createdBy;
  39. /**
  40. * @var Classroom
  41. *
  42. * @ORM\ManyToOne(targetEntity="Classroom", inversedBy="quizSessions")
  43. * @ORM\JoinColumn(name="classroom", referencedColumnName="id", onDelete="CASCADE")
  44. */
  45. private ?Classroom $classroom;
  46. /**
  47. * @var bool
  48. *
  49. * @ORM\Column(name="dormant", type="boolean", nullable=true)
  50. */
  51. private ?bool $dormant;
  52. /**
  53. * @var int
  54. *
  55. * @ORM\Column(type="smallint", nullable=true)
  56. */
  57. private ?int $duration;
  58. /**
  59. * @var DateTime|null
  60. *
  61. * @ORM\Column(name="startTime", type="datetime", nullable=true)
  62. */
  63. private ?DateTime $startTime;
  64. /**
  65. * @ORM\Column(type="datetime", nullable=true)
  66. */
  67. private ?DateTime $deadline;
  68. /**
  69. * @ORM\Column(type="string", length=255, nullable=true)
  70. */
  71. private ?string $name;
  72. /**
  73. * @var DateTime
  74. *
  75. * @ORM\Column(name="createdAt", type="datetime", nullable=false)
  76. */
  77. private DateTime $createdAt;
  78. /**
  79. * @var integer
  80. * [0 - created, in phase of editing; 1 - ready to start, if it's dormant ; 2 - startTime set]
  81. *
  82. * @ORM\Column(type="integer", nullable=false, options={"default"=0})
  83. */
  84. private int $status = 0;
  85. /**
  86. * @var integer
  87. *
  88. * @ORM\Column(type="integer", nullable=false, options={"default"=1})
  89. */
  90. private int $isEnabled = 1;
  91. /**
  92. * @var QuizUnit[]
  93. *
  94. * @ORM\OneToMany(targetEntity="CoreBundle\Entity\QuizUnit", mappedBy="quizSession", cascade={"persist"})
  95. */
  96. private $quizUnits;
  97. /**
  98. * @var bool
  99. *
  100. * @ORM\Column(type="boolean", nullable=true)
  101. */
  102. private ?bool $durationIgnored;
  103. /**
  104. * @var bool
  105. *
  106. * @ORM\Column(name="hints_available", type="boolean", nullable=true)
  107. */
  108. private ?bool $hintsAvailable;
  109. /**
  110. * @ORM\Column(name="results_available_before_deadline", type="boolean", nullable=true)
  111. */
  112. private ?bool $resultsAvailableBeforeDeadline;
  113. /**
  114. * @var string|null
  115. * @ORM\Column(name="isbn", type="string", length=255, nullable=true)
  116. */
  117. private ?string $isbn;
  118. /**
  119. * @var DateTime
  120. * @ORM\Column(name="updated_at", type="datetime", nullable=true)
  121. */
  122. private DateTime $updatedAt;
  123. /**
  124. * @var User
  125. *
  126. * @ORM\ManyToOne(targetEntity="UserBundle\Entity\User")
  127. * @ORM\JoinColumn(name="updated_by", referencedColumnName="id")
  128. */
  129. private User $updatedBy;
  130. /**
  131. * @var string|null
  132. * @ORM\Column(name="description", type="text", nullable=true)
  133. */
  134. private ?string $description;
  135. /**
  136. * @var DateTime
  137. * @ORM\Column(name="result_view_open_at", type="datetime", nullable=true)
  138. */
  139. private ?DateTime $resultViewOpenAt;
  140. /**
  141. * @var ?bool
  142. * @ORM\Column(name="extra_teachers", type="boolean", nullable=true)
  143. */
  144. private ?bool $extraTeachers = null;
  145. /**
  146. * possible values: 'self_study_exam', 'self_study_topic', 'self_study_typical_problem'
  147. * If it is null then it is a regular quiz
  148. *
  149. * @ORM\Column(name="type", type="string", length=255, nullable=true, options={"default"=""})
  150. */
  151. private ?string $type = '';
  152. /**
  153. * @var SelfStudyTopic
  154. *
  155. * @ORM\ManyToOne(targetEntity="SelfStudyTopic")
  156. * @ORM\JoinColumn(name="self_study_topic", referencedColumnName="id")
  157. */
  158. private $selfStudyTopic = null;
  159. /**
  160. * @var SelfStudyTypicalExamProblem
  161. *
  162. * @ORM\ManyToOne(targetEntity="SelfStudyTypicalExamProblem")
  163. * @ORM\JoinColumn(name="self_study_typical_exam_problem", referencedColumnName="id")
  164. */
  165. private $selfStudyTypicalExamProblem = null;
  166. /**
  167. * @var SelfStudyExamQuiz
  168. *
  169. * @ORM\ManyToOne(targetEntity="SelfStudyExamQuiz")
  170. * @ORM\JoinColumn(name="self_study_exam", referencedColumnName="id")
  171. */
  172. private $selfStudyExam = null;
  173. /**
  174. * @var bool
  175. * @ORM\Column(name="deactivated_questions", type="boolean", nullable=true)
  176. */
  177. private ?bool $deactivatedProblems = null;
  178. /**
  179. * @var bool
  180. * @ORM\Column(name="is_archived", type="boolean", nullable=true)
  181. */
  182. private ?bool $isArchived = null;
  183. /**
  184. * @var int|null
  185. * @ORM\Column(name="copy_of", type="integer", nullable=true)
  186. */
  187. private ?int $copyOfSession = null;
  188. /**
  189. * @var bool|null
  190. * @ORM\Column(name="is_shared_quiz", type="boolean", nullable=true)
  191. */
  192. private ?bool $isSharedQuiz = null;
  193. /**
  194. * @var bool|null
  195. * @ORM\Column(name="is_pinned", type="boolean", nullable=true)
  196. */
  197. private ?bool $isPinned = null;
  198. /**
  199. * @ORM\Column(name="can_pause", type="boolean", options={"default"=false})
  200. */
  201. private bool $canPause = false;
  202. /**
  203. */
  204. public function __construct(?int $duration)
  205. {
  206. $this->createdAt = new DateTime('now', new DateTimeZone('Europe/Copenhagen'));
  207. $this->updatedAt = new DateTime('now', new DateTimeZone('Europe/Copenhagen'));
  208. $this->quizUnits = new ArrayCollection();
  209. $this->duration = $duration;
  210. }
  211. /**
  212. * @return int|null
  213. */
  214. public function getCopyOfSession(): ?int
  215. {
  216. return $this->copyOfSession;
  217. }
  218. /**
  219. * @param int|null $copyOfSession
  220. * @return void
  221. */
  222. public function setCopyOfSession(?int $copyOfSession): void
  223. {
  224. $this->copyOfSession = $copyOfSession;
  225. }
  226. /**
  227. * @return int
  228. */
  229. public function getId(): int
  230. {
  231. return $this->id;
  232. }
  233. /**
  234. * @return Quiz
  235. */
  236. public function getQuiz(): ?Quiz
  237. {
  238. return $this->quiz;
  239. }
  240. /**
  241. * @param Quiz $quiz
  242. * @return $this
  243. */
  244. public function setQuiz(?Quiz $quiz): static
  245. {
  246. $this->quiz = $quiz;
  247. return $this;
  248. }
  249. /**
  250. * @return User
  251. */
  252. public function getCreatedBy(): ?User
  253. {
  254. return $this->createdBy;
  255. }
  256. /**
  257. * @param User $createdBy
  258. * @return $this
  259. */
  260. public function setCreatedBy(?User $createdBy): static
  261. {
  262. $this->createdBy = $createdBy;
  263. return $this;
  264. }
  265. /**
  266. * @return Classroom
  267. */
  268. public function getClassroom(): ?Classroom
  269. {
  270. return $this->classroom;
  271. }
  272. /**
  273. * @param Classroom $classroom
  274. * @return $this
  275. */
  276. public function setClassroom(?Classroom $classroom)
  277. {
  278. $this->classroom = $classroom;
  279. return $this;
  280. }
  281. /**
  282. * @return boolean
  283. */
  284. public function isDormant(): ?bool
  285. {
  286. return $this->dormant;
  287. }
  288. /**
  289. * @param boolean $dormant
  290. * @return $this
  291. */
  292. public function setDormant(?bool $dormant)
  293. {
  294. $this->dormant = $dormant;
  295. return $this;
  296. }
  297. /**
  298. * @return DateTime
  299. */
  300. public function getStartTime(): ?DateTime
  301. {
  302. return $this->startTime;
  303. }
  304. /**
  305. * @param DateTime $startTime
  306. * @return $this
  307. */
  308. public function setStartTime(?DateTime $startTime)
  309. {
  310. $this->startTime = $startTime;
  311. return $this;
  312. }
  313. /**
  314. * @return DateTime
  315. */
  316. public function getCreatedAt(): DateTime
  317. {
  318. return $this->createdAt;
  319. }
  320. /**
  321. * @param DateTime $createdAt
  322. * @return void
  323. */
  324. public function setCreatedAt(DateTime $createdAt): void
  325. {
  326. $this->createdAt = $createdAt;
  327. }
  328. /**
  329. * @return int
  330. */
  331. public function getStatus(): int
  332. {
  333. return $this->status;
  334. }
  335. /**
  336. * @param int $status
  337. * @return $this
  338. */
  339. public function setStatus(int $status): static
  340. {
  341. $this->status = $status;
  342. return $this;
  343. }
  344. /**
  345. * @return QuizUnit[]
  346. */
  347. public function getQuizUnits()
  348. {
  349. return $this->quizUnits;
  350. }
  351. /**
  352. * @param QuizUnit[] $quizUnits
  353. * @return $this
  354. */
  355. public function setQuizUnits($quizUnits)
  356. {
  357. $this->quizUnits = $quizUnits;
  358. return $this;
  359. }
  360. /**
  361. * @param QuizUnit $quizUnit
  362. *
  363. * @return $this
  364. */
  365. public function addQuizUnit(QuizUnit $quizUnit)
  366. {
  367. $quizUnit->setQuizSession($this);
  368. $this->quizUnits->add($quizUnit);
  369. return $this;
  370. }
  371. public function getEndTime()
  372. {
  373. if ($this->getDeadline() !== null) {
  374. return $this->getDeadline();
  375. }
  376. $endTime = clone $this->startTime;
  377. return $endTime->modify(sprintf('+%d minutes', $this->getDuration()));
  378. }
  379. /**
  380. * @param int $isEnabled
  381. */
  382. public function setIsEnabled(int $isEnabled): void
  383. {
  384. $this->isEnabled = $isEnabled;
  385. }
  386. /**
  387. * @return int
  388. */
  389. public function getIsEnabled(): int
  390. {
  391. return $this->isEnabled;
  392. }
  393. /**
  394. * @return DateTime|null
  395. */
  396. public function getDeadline(): ?DateTime
  397. {
  398. return $this->deadline;
  399. }
  400. /**
  401. * @param DateTime|null $deadline
  402. */
  403. public function setDeadline(?DateTime $deadline = null): void
  404. {
  405. $this->deadline = $deadline;
  406. }
  407. /**
  408. * @return bool|null
  409. */
  410. public function isDurationIgnored(): ?bool
  411. {
  412. return $this->durationIgnored;
  413. }
  414. /**
  415. * @param bool $durationIgnored
  416. * @return void
  417. */
  418. public function setDurationIgnored(bool $durationIgnored): void
  419. {
  420. $this->durationIgnored = $durationIgnored;
  421. }
  422. /**
  423. * @return int|null
  424. */
  425. public function getDuration(): ?int
  426. {
  427. return $this->duration;
  428. }
  429. /**
  430. * @param int|null $duration
  431. * @return void
  432. */
  433. public function setDuration(?int $duration): void
  434. {
  435. $this->duration = $duration;
  436. }
  437. /**
  438. * @return bool|null
  439. */
  440. public function isHintsAvailable(): ?bool
  441. {
  442. return $this->hintsAvailable;
  443. }
  444. /**
  445. * @param bool $hintsAvailable
  446. * @return void
  447. */
  448. public function setHintsAvailable(bool $hintsAvailable): void
  449. {
  450. $this->hintsAvailable = $hintsAvailable;
  451. }
  452. /**
  453. * @return bool
  454. */
  455. public function areResultsAvailableBeforeDeadline(): bool
  456. {
  457. return (bool)$this->resultsAvailableBeforeDeadline;
  458. }
  459. /**
  460. * @param bool|null $resultsAvailableBeforeDeadline
  461. * @return void
  462. */
  463. public function setResultsAvailableBeforeDeadline(?bool $resultsAvailableBeforeDeadline): void
  464. {
  465. $this->resultsAvailableBeforeDeadline = $resultsAvailableBeforeDeadline;
  466. }
  467. /**
  468. * @return string|null
  469. */
  470. public function getName(): ?string
  471. {
  472. return $this->name;
  473. }
  474. /**
  475. * @param string|null $name
  476. * @return void
  477. */
  478. public function setName(?string $name): void
  479. {
  480. $this->name = $name;
  481. }
  482. public function isHomework(): bool
  483. {
  484. return (bool)$this->deadline;
  485. }
  486. public function getType(): string
  487. {
  488. return $this->type;
  489. }
  490. public function isSelfStudyExamQuiz(): bool
  491. {
  492. return $this->type === 'self_study_exam';
  493. }
  494. public function isSelfStudyTopicQuiz(): bool
  495. {
  496. return $this->type === 'self_study_topic';
  497. }
  498. public function isSelfStudyTypicalProblemQuiz(): bool
  499. {
  500. return $this->type === 'self_study_typical_exam_problem';
  501. }
  502. public function isSelfStudyQuiz(): bool
  503. {
  504. return $this->isSelfStudyExamQuiz() || $this->isSelfStudyTopicQuiz() || $this->isSelfStudyTypicalProblemQuiz();
  505. }
  506. public function setTypeToSelfStudyExamQuiz(): void
  507. {
  508. $this->type = 'self_study_exam';
  509. }
  510. public function setTypeToSelfStudyTopicQuiz(): void
  511. {
  512. $this->type = 'self_study_topic';
  513. }
  514. public function setTypeToSelfStudyTypicalProblemQuiz(): void
  515. {
  516. $this->type = 'self_study_typical_problem';
  517. }
  518. public function setType(?string $type): void
  519. {
  520. $this->type = $type;
  521. }
  522. public function getSelfStudyTopic(): ?SelfStudyTopic
  523. {
  524. return $this->selfStudyTopic;
  525. }
  526. public function setSelfStudyTopic(?SelfStudyTopic $selfStudyTopic): void
  527. {
  528. $this->selfStudyTopic = $selfStudyTopic;
  529. }
  530. public function getSelfStudyTypicalExamProblem(): ?SelfStudyTypicalExamProblem
  531. {
  532. return $this->selfStudyTypicalExamProblem;
  533. }
  534. public function setSelfStudyTypicalExamProblem(?SelfStudyTypicalExamProblem $selfStudyTypicalExamProblem): void
  535. {
  536. $this->selfStudyTypicalExamProblem = $selfStudyTypicalExamProblem;
  537. }
  538. public function getSelfStudyExam(): ?SelfStudyExamQuiz
  539. {
  540. return $this->selfStudyExam;
  541. }
  542. public function setSelfStudyExam(?SelfStudyExamQuiz $selfStudyExam): void
  543. {
  544. $this->selfStudyExam = $selfStudyExam;
  545. }
  546. /**
  547. * @throws Exception
  548. */
  549. public function jsonSerialize(): mixed
  550. {
  551. return [
  552. "duration" => $this->getDuration(),
  553. "id" => $this->getId(),
  554. "name" => $this->getName(),
  555. "startTime" => $this->getStartTime(),
  556. 'deadline' => $this->getDeadline(),
  557. 'isHomework' => $this->isHomework(),
  558. 'secondsUntilDeadline' => $this->getSecondsUntilDeadline(),
  559. 'durationIgnored' => $this->isDurationIgnored()
  560. ];
  561. }
  562. public function setISBN(?string $isbn = null): void
  563. {
  564. $this->isbn = $isbn;
  565. }
  566. public function getISBN(): ?string
  567. {
  568. return $this->isbn;
  569. }
  570. public function getUpdatedAt(): DateTime
  571. {
  572. return $this->updatedAt;
  573. }
  574. public function setUpdatedAt(DateTime $updatedAt): void
  575. {
  576. $this->updatedAt = $updatedAt;
  577. }
  578. public function getUpdatedBy(): User
  579. {
  580. return $this->updatedBy;
  581. }
  582. public function setUpdatedBy(User $updatedBy): void
  583. {
  584. $this->updatedBy = $updatedBy;
  585. }
  586. public function getDescription(): string
  587. {
  588. return $this->description;
  589. }
  590. public function setDescription(string $description): void
  591. {
  592. $this->description = $description;
  593. }
  594. public function getResultViewOpenAt(): ?DateTime
  595. {
  596. return $this->resultViewOpenAt;
  597. }
  598. public function setResultViewOpenAt(?DateTime $resultViewOpenAt): void
  599. {
  600. $this->resultViewOpenAt = $resultViewOpenAt;
  601. }
  602. public function hasExtraTeachers(): ?bool
  603. {
  604. return $this->extraTeachers;
  605. }
  606. public function setExtraTeachers(?bool $extraTeachers): void
  607. {
  608. $this->extraTeachers = $extraTeachers;
  609. }
  610. public function hasDeactivatedProblems(): ?bool
  611. {
  612. return $this->deactivatedProblems;
  613. }
  614. public function setDeactivatedProblems(?bool $deactivatedProblems): void
  615. {
  616. $this->deactivatedProblems = $deactivatedProblems;
  617. }
  618. public function isArchived(): ?bool
  619. {
  620. return $this->isArchived;
  621. }
  622. public function setIsArchived(?bool $isArchived): void
  623. {
  624. $this->isArchived = $isArchived;
  625. }
  626. public function isSharedQuiz(): ?bool
  627. {
  628. return $this->isSharedQuiz;
  629. }
  630. public function setIsSharedQuiz(bool $isSharedQuiz): void
  631. {
  632. $this->isSharedQuiz = $isSharedQuiz;
  633. }
  634. public function getIsPinned(): ?bool
  635. {
  636. return $this->isPinned;
  637. }
  638. public function setIsPinned(?bool $isPinned): void
  639. {
  640. $this->isPinned = $isPinned;
  641. }
  642. public function getNameOrDefaultToQuizName(): ?string
  643. {
  644. return trim($this->getName() ?? '') === '' ? $this->getQuiz()->getName() : $this->getName();
  645. }
  646. /**
  647. * @return bool
  648. */
  649. public function getCanPause(): bool
  650. {
  651. return $this->canPause;
  652. }
  653. /**
  654. * @return bool
  655. */
  656. public function canPause(): bool
  657. {
  658. return $this->canPause;
  659. }
  660. /**
  661. * @param bool $canPause
  662. */
  663. public function setCanPause(?bool $canPause): void
  664. {
  665. $this->canPause = $canPause;
  666. }
  667. /**
  668. * @return int
  669. * @throws Exception
  670. */
  671. public function getSecondsUntilDeadline():int
  672. {
  673. if($this->getDeadline() === null){
  674. return 0;
  675. }
  676. $now = new DateTime('now', new DateTimeZone('Europe/Copenhagen'));
  677. $interval = $now->diff($this->getDeadline());
  678. $seconds = ($interval->days * 24 * 60 * 60) + ($interval->h * 60 * 60) + ($interval->i * 60) + $interval->s;
  679. if($interval->invert){
  680. return 0;
  681. }
  682. return $seconds;
  683. }
  684. }