src/Entity/Core/ReadingTest/ReadingTestSession.php line 20

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity\Core\ReadingTest;
  4. use App\Entity\Core\Classroom\Classroom;
  5. use App\Entity\Core\FolderAwareInterface;
  6. use App\Entity\Core\FolderAwareTrait;
  7. use App\Entity\User\User;
  8. use DateTime;
  9. use DateTimeZone;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use Exception;
  13. use JsonSerializable;
  14. #[ORM\Table('reading_test_session')]
  15. #[ORM\Entity(repositoryClass: ReadingTestSessionRepository::class)]
  16. class ReadingTestSession implements JsonSerializable, FolderAwareInterface
  17. {
  18. use FolderAwareTrait;
  19. private const FOLDER_ACTIVITY_TYPE = 'reading_test';
  20. /**
  21. * @var int
  22. */
  23. #[ORM\Column(name: 'id', type: 'integer')]
  24. #[ORM\Id]
  25. #[ORM\GeneratedValue(strategy: 'AUTO')]
  26. private $id;
  27. /**
  28. * @var ReadingTest
  29. */
  30. #[ORM\JoinColumn(name: 'readingTest', referencedColumnName: 'id')]
  31. #[ORM\ManyToOne(targetEntity: ReadingTest::class)]
  32. private $readingTest;
  33. /**
  34. * @var User
  35. */
  36. #[ORM\JoinColumn(name: 'created_by', referencedColumnName: 'id')]
  37. #[ORM\ManyToOne(targetEntity: User::class)]
  38. private $createdBy;
  39. /**
  40. * @var Classroom
  41. */
  42. #[ORM\ManyToOne(targetEntity: Classroom::class)]
  43. private $classroom;
  44. /**
  45. * @var string
  46. */
  47. #[ORM\Column(name: 'name', type: 'string', length: 255, nullable: true)]
  48. private $name;
  49. /**
  50. * @var DateTime
  51. */
  52. #[ORM\Column(name: 'startTime', type: 'datetime', nullable: true)]
  53. private $startTime;
  54. /**
  55. * @var DateTime
  56. */
  57. #[ORM\Column(name: 'deadline', type: 'datetime', nullable: true)]
  58. private $deadline;
  59. /**
  60. * @var DateTime
  61. */
  62. #[ORM\Column(name: 'createdAt', type: 'datetime', nullable: false)]
  63. private $createdAt;
  64. /**
  65. * @var ReadingTestUnit[]
  66. */
  67. #[ORM\OneToMany(targetEntity: ReadingTestUnit::class, mappedBy: 'readingTestSession', cascade: ['persist'])]
  68. private $readingTestUnits;
  69. #[ORM\Column(type: 'boolean', nullable: true)]
  70. private ?bool $deleted;
  71. /**
  72. * @var int
  73. */
  74. #[ORM\Column(type: 'smallint', nullable: true)]
  75. private $duration;
  76. /**
  77. * ReadingTestSession constructor.
  78. * @throws Exception
  79. */
  80. public function __construct()
  81. {
  82. $this->createdAt = new DateTime('now', new DateTimeZone('Europe/Copenhagen'));
  83. $this->readingTestUnits = new ArrayCollection();
  84. $this->startTime = new DateTime("now");
  85. $this->deadline = new DateTime("+1DAY");
  86. }
  87. public function getId(): int
  88. {
  89. return $this->id;
  90. }
  91. public function setId(int $id): void
  92. {
  93. $this->id = $id;
  94. }
  95. /**
  96. * @return ReadingTest
  97. */
  98. public function getReadingTest()
  99. {
  100. return $this->readingTest;
  101. }
  102. public function setReadingTest(ReadingTest $readingTest): void
  103. {
  104. $this->readingTest = $readingTest;
  105. }
  106. /**
  107. * @return User
  108. */
  109. public function getCreatedBy()
  110. {
  111. return $this->createdBy;
  112. }
  113. public function setCreatedBy(User $createdBy): void
  114. {
  115. $this->createdBy = $createdBy;
  116. }
  117. /**
  118. * @return Classroom
  119. */
  120. public function getClassroom()
  121. {
  122. return $this->classroom;
  123. }
  124. public function setClassroom(Classroom $classroom): void
  125. {
  126. $this->classroom = $classroom;
  127. }
  128. /**
  129. * @return string
  130. */
  131. public function getName()
  132. {
  133. return $this->name;
  134. }
  135. public function setName(?string $name): void
  136. {
  137. $this->name = $name;
  138. }
  139. /**
  140. * @return DateTime
  141. */
  142. public function getStartTime()
  143. {
  144. return $this->startTime;
  145. }
  146. public function setStartTime(DateTime $startTime): void
  147. {
  148. $this->startTime = $startTime;
  149. }
  150. /**
  151. * @return DateTime
  152. */
  153. public function getDeadline()
  154. {
  155. return $this->deadline;
  156. }
  157. public function setDeadline(\DateTime $deadline): void
  158. {
  159. $this->deadline = $deadline;
  160. }
  161. /**
  162. * @return DateTime
  163. */
  164. public function getCreatedAt()
  165. {
  166. return $this->createdAt;
  167. }
  168. public function setCreatedAt(\DateTime $createdAt): void
  169. {
  170. $this->createdAt = $createdAt;
  171. }
  172. /**
  173. * @return ReadingTestUnit[]
  174. */
  175. public function getReadingTestUnits()
  176. {
  177. return $this->readingTestUnits;
  178. }
  179. /**
  180. * @param ReadingTestUnit[] $readingTestUnits
  181. */
  182. public function setReadingTestUnits(array $readingTestUnits): void
  183. {
  184. $this->readingTestUnits = $readingTestUnits;
  185. }
  186. public function isDeleted(): ?bool
  187. {
  188. return $this->deleted;
  189. }
  190. public function setDeleted(bool $deleted): void
  191. {
  192. $this->deleted = $deleted;
  193. }
  194. public function getDuration(): ?int
  195. {
  196. return $this->duration;
  197. }
  198. public function setDuration(?int $duration)
  199. {
  200. $this->duration = $duration;
  201. }
  202. public function jsonSerialize(): mixed
  203. {
  204. $topics = [];
  205. $readingTest = $this->getReadingTest();
  206. if ($readingTest) {
  207. foreach ($readingTest->getSortedMathproblems() as $mathproblem) {
  208. $template = $mathproblem->getTemplate();
  209. if (!$template) {
  210. continue;
  211. }
  212. if($topic = $template->getTopic()){
  213. $topics[$topic->getId()] = $topic->jsonSerialize();
  214. }
  215. }
  216. $topics = array_values($topics);
  217. }
  218. return [
  219. 'duration' => $this->getDuration(),
  220. 'id' => $this->getId(),
  221. 'name' => $this->prefixNameWithFolderSortOrder($this->getName(), self::FOLDER_ACTIVITY_TYPE, $this->getReadingTest()?->getId()),
  222. 'startTime' => $this->getStartTime(),
  223. 'deadline' => $this->getDeadline(),
  224. 'topics' => $topics,
  225. 'session_type' => basename(str_replace('\\', '/', __CLASS__)),
  226. 'activity_type' => 'reading_test',
  227. 'start' => $this->getStartTime()->getTimestamp()
  228. ];
  229. }
  230. public function getSortOrderInFolder(): ?int
  231. {
  232. return $this->getFolderSortOrder(self::FOLDER_ACTIVITY_TYPE, $this->getReadingTest()?->getId());
  233. }
  234. }