src/Entity/Core/Worksheet/WorksheetSession.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Core\Worksheet;
  3. use App\Entity\Core\Classroom\Classroom;
  4. use App\Entity\User\User;
  5. use DateTime;
  6. use DateTimeZone;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use JsonSerializable;
  10. #[ORM\Table('worksheet_session')]
  11. #[ORM\Entity(repositoryClass: WorksheetSessionRepository::class)]
  12. class WorksheetSession implements JsonSerializable
  13. {
  14. /**
  15. * @var int
  16. */
  17. #[ORM\Column(name: 'id', type: 'integer')]
  18. #[ORM\Id]
  19. #[ORM\GeneratedValue(strategy: 'AUTO')]
  20. private $id;
  21. /**
  22. * @var Worksheet
  23. */
  24. #[ORM\JoinColumn(name: 'worksheet', referencedColumnName: 'id', onDelete: 'CASCADE', nullable: true)]
  25. #[ORM\ManyToOne(targetEntity: Worksheet::class, inversedBy: 'worksheetSessions')]
  26. private $worksheet;
  27. /**
  28. * @var User
  29. */
  30. #[ORM\JoinColumn(name: 'created_by', referencedColumnName: 'id')]
  31. #[ORM\ManyToOne(targetEntity: User::class)]
  32. private $createdBy;
  33. /**
  34. * @var Classroom
  35. */
  36. #[ORM\ManyToOne(targetEntity: Classroom::class)]
  37. private $classroom;
  38. /**
  39. * @var bool
  40. */
  41. #[ORM\Column(name: 'dormant', type: 'boolean', nullable: true)]
  42. private $dormant;
  43. /**
  44. * @var int
  45. */
  46. #[ORM\Column(type: 'smallint', nullable: true)]
  47. private $duration;
  48. /**
  49. * @var string
  50. */
  51. #[ORM\Column(name: 'name', type: 'string', length: 255, nullable: true)]
  52. private $name;
  53. /**
  54. * @var DateTime
  55. */
  56. #[ORM\Column(name: 'startTime', type: 'datetime', nullable: true)]
  57. private $startTime;
  58. #[ORM\Column(name: 'deadline', type: 'datetime', nullable: true)]
  59. private ?DateTime $deadline;
  60. /**
  61. * @var DateTime
  62. */
  63. #[ORM\Column(name: 'createdAt', type: 'datetime', nullable: false)]
  64. private $createdAt;
  65. /**
  66. * @var integer
  67. * [0 - created, in phase of editing; 1 - ready to start, if it's dormant ; 2 - startTime set]
  68. */
  69. #[ORM\Column(type: 'integer', nullable: false, options: ['default' => 0])]
  70. private $status = 0;
  71. /**
  72. * @var integer
  73. */
  74. #[ORM\Column(type: 'integer', nullable: false, options: ['default' => 1])]
  75. private $isEnabled = 1;
  76. /**
  77. * @var WorksheetUnit[]
  78. */
  79. #[ORM\OneToMany(targetEntity: WorksheetUnit::class, mappedBy: 'worksheetSession', cascade: ['persist'])]
  80. private $worksheetUnits;
  81. /**
  82. * @var bool
  83. */
  84. #[ORM\Column(type: 'boolean', nullable: true)]
  85. private $durationIgnored;
  86. /**
  87. * @var bool
  88. */
  89. #[ORM\Column(name: 'solutions_available', type: 'boolean', nullable: true)]
  90. private $solutionsAvailable;
  91. public function __construct(?int $duration)
  92. {
  93. $this->createdAt = new DateTime('now', new DateTimeZone('Europe/Copenhagen'));
  94. $this->worksheetUnits = new ArrayCollection();
  95. $this->duration = $duration;
  96. }
  97. /**
  98. * @return int
  99. */
  100. public function getId()
  101. {
  102. return $this->id;
  103. }
  104. /**
  105. * @return Worksheet
  106. */
  107. public function getWorksheet()
  108. {
  109. return $this->worksheet;
  110. }
  111. /**
  112. * @param Worksheet $worksheet
  113. * @return $this
  114. */
  115. public function setWorksheet($worksheet)
  116. {
  117. $this->worksheet = $worksheet;
  118. return $this;
  119. }
  120. /**
  121. * @return User
  122. */
  123. public function getCreatedBy()
  124. {
  125. return $this->createdBy;
  126. }
  127. /**
  128. * @param User $createdBy
  129. * @return $this
  130. */
  131. public function setCreatedBy($createdBy)
  132. {
  133. $this->createdBy = $createdBy;
  134. return $this;
  135. }
  136. /**
  137. * @return Classroom
  138. */
  139. public function getClassroom()
  140. {
  141. return $this->classroom;
  142. }
  143. /**
  144. * @param Classroom $classroom
  145. * @return $this
  146. */
  147. public function setClassroom($classroom)
  148. {
  149. $this->classroom = $classroom;
  150. return $this;
  151. }
  152. /**
  153. * @return boolean
  154. */
  155. public function isDormant()
  156. {
  157. return $this->dormant;
  158. }
  159. /**
  160. * @param boolean $dormant
  161. * @return $this
  162. */
  163. public function setDormant($dormant)
  164. {
  165. $this->dormant = $dormant;
  166. return $this;
  167. }
  168. /**
  169. * @return DateTime
  170. */
  171. public function getStartTime()
  172. {
  173. return $this->startTime;
  174. }
  175. /**
  176. * @param DateTime $startTime
  177. * @return $this
  178. */
  179. public function setStartTime($startTime)
  180. {
  181. $this->startTime = $startTime;
  182. return $this;
  183. }
  184. /**
  185. * @return DateTime
  186. */
  187. public function getCreatedAt()
  188. {
  189. return $this->createdAt;
  190. }
  191. /**
  192. * @return mixed
  193. */
  194. public function getStatus()
  195. {
  196. return $this->status;
  197. }
  198. /**
  199. * @param mixed $status
  200. * @return $this
  201. */
  202. public function setStatus($status)
  203. {
  204. $this->status = $status;
  205. return $this;
  206. }
  207. /**
  208. * @return WorksheetUnit[]
  209. */
  210. public function getWorksheetUnits()
  211. {
  212. return $this->worksheetUnits;
  213. }
  214. /**
  215. * @param WorksheetUnit[] $worksheetUnits
  216. * @return $this
  217. */
  218. public function setWorksheetUnits($worksheetUnits)
  219. {
  220. $this->worksheetUnits = $worksheetUnits;
  221. return $this;
  222. }
  223. /**
  224. * @return $this
  225. */
  226. public function addWorksheetUnit(WorksheetUnit $worksheetUnit)
  227. {
  228. $worksheetUnit->setWorksheetSession($this);
  229. $this->worksheetUnits->add($worksheetUnit);
  230. return $this;
  231. }
  232. public function getEndTime()
  233. {
  234. if ($this->getDeadline() !== null) {
  235. return $this->getDeadline();
  236. }
  237. $endTime = clone $this->startTime;
  238. return $endTime->modify(sprintf('+%d minutes', $this->getDuration()));
  239. }
  240. /**
  241. * @param int $isEnabled
  242. */
  243. public function setIsEnabled($isEnabled)
  244. {
  245. $this->isEnabled = $isEnabled;
  246. }
  247. /**
  248. * @return int
  249. */
  250. public function getIsEnabled()
  251. {
  252. return $this->isEnabled;
  253. }
  254. public function getDeadline(): ?DateTime
  255. {
  256. return $this->deadline;
  257. }
  258. public function setDeadline(?DateTime $deadline = null)
  259. {
  260. $this->deadline = $deadline;
  261. }
  262. public function isDurationIgnored(): ?bool
  263. {
  264. return $this->durationIgnored;
  265. }
  266. public function setDurationIgnored(bool $durationIgnored)
  267. {
  268. $this->durationIgnored = $durationIgnored;
  269. }
  270. public function getDuration(): ?int
  271. {
  272. return $this->duration;
  273. }
  274. public function setDuration(?int $duration)
  275. {
  276. $this->duration = $duration;
  277. }
  278. public function isSolutionsAvailable(): ?bool
  279. {
  280. return $this->solutionsAvailable;
  281. }
  282. public function setSolutionsAvailable(?bool $solutionsAvailable)
  283. {
  284. $this->solutionsAvailable = $solutionsAvailable;
  285. }
  286. private function cmpWorksheetUnits(WorksheetUnit $a, WorksheetUnit $b): bool
  287. {
  288. return strcmp($a->getStudent()->getNickname(), $b->getStudent()->getNickname());
  289. }
  290. public function getName(): ?string
  291. {
  292. return $this->name;
  293. }
  294. public function setName(?string $name)
  295. {
  296. $this->name = $name;
  297. }
  298. public function isHomework(): bool
  299. {
  300. return boolval($this->deadline);
  301. }
  302. public function jsonSerialize(): mixed
  303. {
  304. return [
  305. "id" => $this->getId(),
  306. "name" => $this->getName(),
  307. "duration" => $this->getDuration(),
  308. "startTime" => $this->getStartTime(),
  309. ];
  310. }
  311. }