src/Entity/Core/Worksheet/Worksheet.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Core\Worksheet;
  3. use App\Entity\Core\Classroom\ClassroomType;
  4. use App\Entity\Core\Mathproblem;
  5. use App\Entity\User\User;
  6. use DateTimeImmutable;
  7. use DateTimeZone;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\ORM\Mapping as ORM;
  11. #[ORM\Table('worksheet')]
  12. #[ORM\Entity(repositoryClass: WorksheetRepository::class)]
  13. class Worksheet
  14. {
  15. /**
  16. * @var int
  17. */
  18. #[ORM\Column(name: 'id', type: 'integer')]
  19. #[ORM\Id]
  20. #[ORM\GeneratedValue(strategy: 'AUTO')]
  21. private $id;
  22. /**
  23. * @var int
  24. */
  25. #[ORM\Column(type: 'integer', nullable: false, options: ['default' => 0])]
  26. private $duration;
  27. /**
  28. * @var string
  29. */
  30. #[ORM\Column(name: 'name', type: 'string', length: 255, nullable: true)]
  31. private $name;
  32. /**
  33. * @var Collection
  34. */
  35. #[ORM\JoinTable(name: 'worksheet_problem')]
  36. #[ORM\JoinColumn(name: 'worksheet_id', referencedColumnName: 'id')]
  37. #[ORM\InverseJoinColumn(name: 'mathproblem_id', referencedColumnName: 'id')]
  38. #[ORM\ManyToMany(targetEntity: Mathproblem::class)]
  39. private $problems;
  40. /**
  41. * @var array
  42. */
  43. #[ORM\Column(name: 'problem_order', type: 'simple_array', nullable: true)]
  44. private $problemOrder;
  45. /**
  46. * @var bool
  47. */
  48. #[ORM\Column(name: 'reusable', type: 'boolean', nullable: false)]
  49. private $reusable;
  50. /**
  51. * @var bool
  52. */
  53. #[ORM\Column(name: 'abacus', type: 'boolean', nullable: true, options: ['default' => 0])]
  54. private $abacus;
  55. /**
  56. * @var DateTimeImmutable
  57. */
  58. #[ORM\Column(name: 'created_at', type: 'datetime_immutable', nullable: false)]
  59. private $createdAt;
  60. /**
  61. * @var User
  62. */
  63. #[ORM\JoinColumn(name: 'created_by', referencedColumnName: 'id')]
  64. #[ORM\ManyToOne(targetEntity: User::class)]
  65. private $createdBy;
  66. /**
  67. * @var User[]
  68. */
  69. #[ORM\JoinTable(name: 'worksheet_teacher')]
  70. #[ORM\JoinColumn(name: 'worksheet_id', referencedColumnName: 'id')]
  71. #[ORM\InverseJoinColumn(name: 'teacher_id', referencedColumnName: 'id')]
  72. #[ORM\ManyToMany(targetEntity: User::class)]
  73. private $teachers;
  74. /**
  75. * @var WorksheetSession[]
  76. */
  77. #[ORM\OneToMany(targetEntity: WorksheetSession::class, mappedBy: 'worksheet', cascade: ['persist'])]
  78. private $worksheetSessions;
  79. /**
  80. * @var ClassroomType
  81. */
  82. #[ORM\JoinColumn(name: 'level', referencedColumnName: 'id', nullable: true)]
  83. #[ORM\ManyToOne(targetEntity: ClassroomType::class)]
  84. private $level;
  85. /**
  86. * @var array
  87. */
  88. #[ORM\Column(name: 'mathproblems_for_templates', type: 'simple_array', nullable: true)]
  89. private $mathproblemsForTemplates;
  90. public function __construct()
  91. {
  92. $this->problems = new ArrayCollection();
  93. $this->teachers = new ArrayCollection();
  94. $this->worksheetSessions = new ArrayCollection();
  95. $this->createdAt = new DateTimeImmutable('now', new DateTimeZone('Europe/Copenhagen'));
  96. }
  97. /**
  98. * Get id.
  99. *
  100. * @return int
  101. */
  102. public function getId()
  103. {
  104. return $this->id;
  105. }
  106. /**
  107. * @return mixed
  108. */
  109. public function getName()
  110. {
  111. return $this->name;
  112. }
  113. /**
  114. * @param string $name
  115. *
  116. * @return $this
  117. */
  118. public function setName($name)
  119. {
  120. $this->name = $name;
  121. return $this;
  122. }
  123. /**
  124. * Set duration.
  125. *
  126. * @param int $duration
  127. *
  128. * @return $this
  129. */
  130. public function setDuration($duration)
  131. {
  132. $this->duration = $duration;
  133. return $this;
  134. }
  135. /**
  136. * Get duration.
  137. *
  138. * @return int
  139. */
  140. public function getDuration()
  141. {
  142. return $this->duration;
  143. }
  144. public function getProblems()
  145. {
  146. return $this->problems;
  147. }
  148. public function getSortedProblems($problemOrder = -1)
  149. {
  150. if ($problemOrder == -1) {
  151. $problemOrder = $this->problemOrder;
  152. }
  153. $problems = $this->problems;
  154. if ($problemOrder && count($problemOrder) == count($problems)) {
  155. $newmathproblems = [];
  156. foreach ($problems as $mp) {
  157. $newkey = array_search($mp->getID(), $problemOrder);
  158. $newmathproblems[$newkey] = $mp;
  159. }
  160. ksort($newmathproblems);
  161. $problems = $newmathproblems;
  162. return $problems;
  163. } else {
  164. return $this->problems->toArray(); // fallback
  165. }
  166. }
  167. /**
  168. * @param Mathproblem[]
  169. *
  170. * @return $this
  171. */
  172. public function setProblems($problems)
  173. {
  174. $this->problems = $problems;
  175. return $this;
  176. }
  177. /**
  178. * @param Mathproblem
  179. *
  180. * @return $this
  181. */
  182. public function addProblem($problem)
  183. {
  184. $this->problems->add($problem);
  185. return $this;
  186. }
  187. /**
  188. * @return bool
  189. */
  190. public function getReusable()
  191. {
  192. return $this->reusable;
  193. }
  194. /**
  195. * @param bool $reusable
  196. *
  197. * @return $this
  198. */
  199. public function setReusable($reusable)
  200. {
  201. $this->reusable = $reusable;
  202. return $this;
  203. }
  204. public function getCreatedAt(): DateTimeImmutable
  205. {
  206. return $this->createdAt;
  207. }
  208. public function getCreatedBy(): ?User
  209. {
  210. return $this->createdBy;
  211. }
  212. /**
  213. * @param User $createdBy
  214. *
  215. * @return $this
  216. */
  217. public function setCreatedBy($createdBy)
  218. {
  219. $this->createdBy = $createdBy;
  220. return $this;
  221. }
  222. public function getTeachers()
  223. {
  224. return $this->teachers;
  225. }
  226. public function addTeacher(User $teacher)
  227. {
  228. $this->teachers->add($teacher);
  229. return $this;
  230. }
  231. public function getWorksheetSessions(): Collection
  232. {
  233. return $this->worksheetSessions;
  234. }
  235. public function addWorksheetSession(WorksheetSession $worksheetSession)
  236. {
  237. $this->worksheetSessions->add($worksheetSession);
  238. $worksheetSession->setWorksheet($this);
  239. return $this;
  240. }
  241. /**
  242. * @return bool
  243. */
  244. public function isAbacus()
  245. {
  246. return $this->abacus;
  247. }
  248. /**
  249. * @param bool $abacus
  250. */
  251. public function setAbacus($abacus)
  252. {
  253. $this->abacus = $abacus;
  254. }
  255. public function getLevel(): ?ClassroomType
  256. {
  257. return $this->level;
  258. }
  259. public function setLevel(?ClassroomType $level): void
  260. {
  261. $this->level = $level;
  262. }
  263. public function getProblemOrder(): ?array
  264. {
  265. return $this->problemOrder;
  266. }
  267. public function setProblemOrder(array $problemOrder)
  268. {
  269. $this->problemOrder = $problemOrder;
  270. }
  271. /**
  272. * @return array|null
  273. */
  274. public function getMathproblemsForTemplates()
  275. {
  276. return $this->mathproblemsForTemplates;
  277. }
  278. /**
  279. * @param $mathproblemsForTemplates
  280. */
  281. public function setMathproblemsForTemplates($mathproblemsForTemplates): void
  282. {
  283. $this->mathproblemsForTemplates = $mathproblemsForTemplates;
  284. }
  285. }