src/Entity/Core/Dictate/DictateSession.php line 21

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity\Core\Dictate;
  4. use App\Entity\Core\Classroom\Classroom;
  5. use App\Entity\User\User;
  6. use App\Entity\Core\FolderAwareInterface;
  7. use App\Entity\Core\FolderAwareTrait;
  8. use DateTime;
  9. use DateTimeZone;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\Common\Collections\Collection;
  12. use Doctrine\ORM\Mapping as ORM;
  13. use Exception;
  14. use JsonSerializable;
  15. #[ORM\Table('dictate_session')]
  16. #[ORM\Entity(repositoryClass: DictateSessionRepository::class)]
  17. class DictateSession implements JsonSerializable, FolderAwareInterface
  18. {
  19. use FolderAwareTrait;
  20. private const FOLDER_ACTIVITY_TYPE = 'dictate';
  21. /**
  22. * @var int
  23. */
  24. #[ORM\Column(name: 'id', type: 'integer')]
  25. #[ORM\Id]
  26. #[ORM\GeneratedValue(strategy: 'AUTO')]
  27. private int $id;
  28. /**
  29. * @var Dictate
  30. */
  31. #[ORM\JoinColumn(name: 'dictate', referencedColumnName: 'id')]
  32. #[ORM\ManyToOne(targetEntity: Dictate::class)]
  33. private Dictate $dictate;
  34. /**
  35. * @var User
  36. */
  37. #[ORM\JoinColumn(name: 'created_by', referencedColumnName: 'id')]
  38. #[ORM\ManyToOne(targetEntity: User::class)]
  39. private User $createdBy;
  40. /**
  41. * @var
  42. */
  43. #[ORM\ManyToOne(targetEntity: Classroom::class)]
  44. private Classroom $classroom;
  45. /**
  46. * @var
  47. */
  48. #[ORM\Column(name: 'name', type: 'string', length: 255, nullable: true)]
  49. private ?string $name;
  50. #[ORM\Column(name: 'startTime', type: 'datetime', nullable: true)]
  51. private ?DateTime $startTime;
  52. #[ORM\Column(name: 'deadline', type: 'datetime', nullable: true)]
  53. private ?DateTime $deadline;
  54. #[ORM\Column(name: 'createdAt', type: 'datetime', nullable: false)]
  55. private DateTime $createdAt;
  56. /**
  57. * @var DictateUnit[]
  58. */
  59. #[ORM\OneToMany(targetEntity: DictateUnit::class, mappedBy: 'dictateSession', cascade: ['persist'])]
  60. private Collection $dictateUnits;
  61. #[ORM\Column(type: 'boolean', nullable: true)]
  62. private ?bool $deleted;
  63. #[ORM\Column(type: 'smallint', nullable: true)]
  64. private ?int $duration;
  65. /**
  66. * DictateSession constructor.
  67. * @throws Exception
  68. */
  69. public function __construct()
  70. {
  71. $this->createdAt = new DateTime('now', new DateTimeZone('Europe/Copenhagen'));
  72. $this->dictateUnits = new ArrayCollection();
  73. $this->startTime = new DateTime("now");
  74. $this->deadline = new DateTime("+1DAY");
  75. }
  76. public function getId(): int
  77. {
  78. return $this->id;
  79. }
  80. public function setId(int $id): void
  81. {
  82. $this->id = $id;
  83. }
  84. public function getDictate(): Dictate
  85. {
  86. return $this->dictate;
  87. }
  88. public function setDictate(Dictate $dictate): void
  89. {
  90. $this->dictate = $dictate;
  91. }
  92. public function getCreatedBy(): User
  93. {
  94. return $this->createdBy;
  95. }
  96. public function setCreatedBy(User $createdBy): void
  97. {
  98. $this->createdBy = $createdBy;
  99. }
  100. public function getClassroom(): Classroom
  101. {
  102. return $this->classroom;
  103. }
  104. public function setClassroom(Classroom $classroom): void
  105. {
  106. $this->classroom = $classroom;
  107. }
  108. public function getName(): ?string
  109. {
  110. return $this->name;
  111. }
  112. public function setName(?string $name): void
  113. {
  114. $this->name = $name;
  115. }
  116. public function getStartTime(): ?DateTime
  117. {
  118. return $this->startTime;
  119. }
  120. public function setStartTime(DateTime $startTime): void
  121. {
  122. $this->startTime = $startTime;
  123. }
  124. public function getDeadline(): ?DateTime
  125. {
  126. return $this->deadline;
  127. }
  128. public function setDeadline(?DateTime $deadline): void
  129. {
  130. $this->deadline = $deadline;
  131. }
  132. public function getCreatedAt(): DateTime
  133. {
  134. return $this->createdAt;
  135. }
  136. public function setCreatedAt(DateTime $createdAt): void
  137. {
  138. $this->createdAt = $createdAt;
  139. }
  140. public function getDictateUnits(): Collection
  141. {
  142. return $this->dictateUnits;
  143. }
  144. /**
  145. * @param Collection|DictateUnit[] $dictateUnits
  146. */
  147. public function setDictateUnits(Collection $dictateUnits): void
  148. {
  149. $this->dictateUnits = $dictateUnits;
  150. }
  151. public function isDeleted(): ?bool
  152. {
  153. return $this->deleted;
  154. }
  155. public function setDeleted(bool $deleted): void
  156. {
  157. $this->deleted = $deleted;
  158. }
  159. public function getDuration(): ?int
  160. {
  161. return $this->duration;
  162. }
  163. public function setDuration(?int $duration): void
  164. {
  165. $this->duration = $duration;
  166. }
  167. /**
  168. * Specify data which should be serialized to JSON
  169. * @link https://php.net/manual/en/jsonserializable.jsonserialize.php
  170. * @return mixed data which can be serialized by <b>json_encode</b>,
  171. * which is a value of any type other than a resource.
  172. * @since 5.4
  173. */
  174. public function jsonSerialize(): array
  175. {
  176. $topics = [];
  177. $dictate = $this->getDictate();
  178. if ($dictate && !empty($dictate->getIntroduction())) {
  179. $topics[] = ['name' => $dictate->getIntroduction()];
  180. }
  181. return [
  182. 'id' => $this->getId(),
  183. 'name' => $this->prefixNameWithFolderSortOrder($this->getName(), self::FOLDER_ACTIVITY_TYPE, $this->getDictate()?->getId()),
  184. 'deadline' => $this->getDeadline(),
  185. 'startTime' => $this->getStartTime(),
  186. 'duration' => $this->getDuration(),
  187. 'topics' => $topics,
  188. 'session_type' => basename(str_replace('\\', '/', __CLASS__)),
  189. 'activity_type' => 'dictate',
  190. 'start' => $this->getStartTime()->getTimestamp()
  191. ];
  192. }
  193. public function getSortOrderInFolder(): ?int
  194. {
  195. return $this->getFolderSortOrder(self::FOLDER_ACTIVITY_TYPE, $this->getDictate()?->getId());
  196. }
  197. }