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

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