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

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