src/CoreBundle/Entity/WorksheetSession.php line 15

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