src/Entity/Core/CrammingActivity/CrammingActivity.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Core\CrammingActivity;
  3. use App\Entity\Core\Classroom\Classroom;
  4. use App\Entity\Core\TemplateSet;
  5. use DateTime;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Exception;
  9. use JsonSerializable;
  10. #[ORM\Table('cramming_activity')]
  11. #[ORM\Entity(repositoryClass: CrammingActivityRepository::class)]
  12. class CrammingActivity implements JsonSerializable
  13. {
  14. /**
  15. * @var int
  16. */
  17. #[ORM\Column(name: 'id', type: 'integer')]
  18. #[ORM\Id]
  19. #[ORM\GeneratedValue(strategy: 'AUTO')]
  20. protected $id;
  21. #[ORM\Column(type: 'string', nullable: true)]
  22. protected $name;
  23. /**
  24. * @var Classroom
  25. */
  26. #[ORM\JoinColumn(name: 'classroom', referencedColumnName: 'id', onDelete: 'CASCADE')]
  27. #[ORM\ManyToOne(targetEntity: Classroom::class)]
  28. private $classroom;
  29. /**
  30. * JSON map of template set ID -> selected classroom type ID (or null).
  31. */
  32. #[ORM\Column(name: 'template_set_classroom_types', type: 'text', nullable: true)]
  33. private ?string $templateSetClassroomTypes = null;
  34. /**
  35. * @var DateTime
  36. */
  37. #[ORM\Column(name: 'start_time', type: 'datetime', nullable: true)]
  38. protected $startTime;
  39. /**
  40. * @var DateTime
  41. */
  42. #[ORM\Column(name: 'deadline', type: 'datetime', nullable: true)]
  43. protected $deadline;
  44. #[ORM\Column(type: 'integer', nullable: true)]
  45. protected $duration;
  46. #[ORM\Column(name: 'is_enabled', type: 'integer', nullable: true, options: ['default' => 1])]
  47. private $isEnabled = 1;
  48. #[ORM\Column(name: 'template_sets_count', type: 'integer')]
  49. private $templateSetsCount;
  50. /**
  51. * @var ArrayCollection|TemplateSet[]
  52. */
  53. #[ORM\JoinTable(name: 'cramming_activity_template_sets')]
  54. #[ORM\JoinColumn(name: 'cramming_activity', referencedColumnName: 'id')]
  55. #[ORM\InverseJoinColumn(name: 'template_set', referencedColumnName: 'id')]
  56. #[ORM\ManyToMany(targetEntity: TemplateSet::class)]
  57. private $templateSets;
  58. /**
  59. * @var CrammingActivityUnit[]
  60. */
  61. #[ORM\OneToMany(targetEntity: CrammingActivityUnit::class, mappedBy: 'crammingActivity', cascade: ['persist', 'remove'])]
  62. private $crammingActivityUnits;
  63. #[ORM\Column(name: 'show_hints', type: 'boolean', nullable: true, options: ['default' => false])]
  64. private $showHints = false;
  65. #[ORM\Column(name: 'is_homework', type: 'boolean', nullable: true, options: ['default' => false])]
  66. private $isHomework = false;
  67. #[ORM\Column(name: 'can_pause', type: 'boolean', nullable: true, options: ['default' => false])]
  68. private $canPause = false;
  69. public function __construct()
  70. {
  71. $this->startTime = new DateTime();
  72. $this->crammingActivityUnits = new ArrayCollection();
  73. $this->templateSets = new ArrayCollection();
  74. }
  75. /**
  76. * @throws Exception
  77. */
  78. public function resetActivity(): void
  79. {
  80. $this->id = null;
  81. $this->startTime = new DateTime();
  82. $this->crammingActivityUnits = new ArrayCollection();
  83. $this->templateSets = new ArrayCollection();
  84. }
  85. public function getId(): int
  86. {
  87. return $this->id;
  88. }
  89. /**
  90. * @param int $id
  91. */
  92. public function setId(?int $id): void
  93. {
  94. $this->id = $id;
  95. }
  96. /**
  97. * @return mixed
  98. */
  99. public function getName()
  100. {
  101. return $this->name;
  102. }
  103. /**
  104. * @param mixed $name
  105. */
  106. public function setName($name): void
  107. {
  108. $this->name = $name;
  109. }
  110. /**
  111. * @return Classroom
  112. */
  113. public function getClassroom(): ?Classroom
  114. {
  115. return $this->classroom;
  116. }
  117. /**
  118. * @param Classroom $classroom
  119. */
  120. public function setClassroom(?Classroom $classroom): void
  121. {
  122. $this->classroom = $classroom;
  123. }
  124. public function getTemplateSetClassroomTypeIds(): array
  125. {
  126. if ($this->templateSetClassroomTypes === null || $this->templateSetClassroomTypes === '') {
  127. return [];
  128. }
  129. $decoded = json_decode($this->templateSetClassroomTypes, true);
  130. return is_array($decoded) ? $decoded : [];
  131. }
  132. public function setTemplateSetClassroomTypeIds(?array $templateSetClassroomTypeIds): void
  133. {
  134. if (empty($templateSetClassroomTypeIds)) {
  135. $this->templateSetClassroomTypes = null;
  136. return;
  137. }
  138. $this->templateSetClassroomTypes = json_encode($templateSetClassroomTypeIds);
  139. }
  140. /**
  141. * @return DateTime
  142. */
  143. public function getStartTime(): ?DateTime
  144. {
  145. return $this->startTime;
  146. }
  147. /**
  148. * @param DateTime $startTime
  149. */
  150. public function setStartTime(?DateTime $startTime): void
  151. {
  152. $this->startTime = $startTime;
  153. }
  154. /**
  155. * @return DateTime
  156. */
  157. public function getDeadline(): ?DateTime
  158. {
  159. return $this->deadline;
  160. }
  161. /**
  162. * @param DateTime $deadline
  163. */
  164. public function setDeadline(?DateTime $deadline): void
  165. {
  166. $this->deadline = $deadline;
  167. }
  168. /**
  169. * @return mixed
  170. */
  171. public function getDuration()
  172. {
  173. return $this->duration;
  174. }
  175. /**
  176. * @param mixed $duration
  177. */
  178. public function setDuration($duration): void
  179. {
  180. $this->duration = $duration;
  181. }
  182. public function getIsEnabled(): int
  183. {
  184. return $this->isEnabled;
  185. }
  186. public function setIsEnabled(int $isEnabled): void
  187. {
  188. $this->isEnabled = $isEnabled;
  189. }
  190. /**
  191. * @return mixed
  192. */
  193. public function getTemplateSetsCount()
  194. {
  195. return $this->templateSetsCount;
  196. }
  197. /**
  198. * @param mixed $templateSetsCount
  199. */
  200. public function setTemplateSetsCount($templateSetsCount): void
  201. {
  202. $this->templateSetsCount = $templateSetsCount;
  203. }
  204. /**
  205. * @return TemplateSet[]|ArrayCollection
  206. */
  207. public function getTemplateSets()
  208. {
  209. return $this->templateSets;
  210. }
  211. /**
  212. * @param TemplateSet[]|ArrayCollection $templateSets
  213. */
  214. public function setTemplateSets($templateSets): void
  215. {
  216. $this->templateSets = $templateSets;
  217. }
  218. /**
  219. * @return $this
  220. */
  221. public function addTemplateSet(TemplateSet $templateSet)
  222. {
  223. $this->templateSets->add($templateSet);
  224. return $this;
  225. }
  226. public function removeTemplateSet(TemplateSet $templateSet)
  227. {
  228. $this->templateSets->removeElement($templateSet);
  229. }
  230. /**
  231. * @return bool
  232. */
  233. public function removeStudent(TemplateSet $templateSet)
  234. {
  235. return $this->templateSets->removeElement($templateSet);
  236. }
  237. /**
  238. * @return CrammingActivityUnit[]|ArrayCollection
  239. */
  240. public function getCrammingActivityUnits()
  241. {
  242. return $this->crammingActivityUnits;
  243. }
  244. /**
  245. * @param CrammingActivityUnit[] $crammingActivityUnits
  246. */
  247. public function setCrammingActivityUnits(?array $crammingActivityUnits): void
  248. {
  249. $this->crammingActivityUnits = $crammingActivityUnits;
  250. }
  251. public function isShowHints(): bool
  252. {
  253. return $this->showHints;
  254. }
  255. public function setShowHints(bool $showHints): void
  256. {
  257. $this->showHints = $showHints;
  258. }
  259. public function isHomework(): bool
  260. {
  261. return $this->isHomework;
  262. }
  263. public function setIsHomework(bool $isHomework): void
  264. {
  265. $this->isHomework = $isHomework;
  266. }
  267. public function isCanPause(): bool
  268. {
  269. return $this->canPause;
  270. }
  271. public function setCanPause(bool $canPause): void
  272. {
  273. $this->canPause = $canPause;
  274. }
  275. public function isSelfStudy():bool
  276. {
  277. return $this->classroom === null;
  278. }
  279. public function jsonSerialize(): array
  280. {
  281. $topics = [];
  282. foreach ($this->getTemplateSets() as $templateSet) {
  283. $topic = $templateSet->getTopic();
  284. if ($topic) {
  285. $topics[$topic->getId()] = $topic->jsonSerialize();
  286. }
  287. }
  288. return [
  289. 'id' => $this->getId(),
  290. 'name' => $this->getName(),
  291. 'deadline' => $this->getDeadline(),
  292. 'startTime' => $this->getStartTime(),
  293. 'duration' => $this->getDuration(),
  294. 'isHomework' => $this->isHomework(),
  295. 'topics' => array_values($topics),
  296. ];
  297. }
  298. }