src/Entity/Core/Classroom/Classroom.php line 19

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity\Core\Classroom;
  4. use App\Entity\Core\Institution;
  5. use App\Entity\Core\Quiz\QuizSession;
  6. use App\Entity\Core\Session\Session;
  7. use App\Entity\User\User;
  8. use DateTime;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. use Doctrine\ORM\Mapping as ORM;
  11. use Doctrine\ORM\PersistentCollection;
  12. use JsonSerializable;
  13. #[ORM\Table('classroom')]
  14. #[ORM\Entity(repositoryClass: ClassroomRepository::class)]
  15. class Classroom implements JsonSerializable
  16. {
  17. #[ORM\Column(name: 'id', type: 'integer')]
  18. #[ORM\Id]
  19. #[ORM\GeneratedValue(strategy: 'AUTO')]
  20. private int $id;
  21. #[ORM\Column(name: 'name', type: 'string', length: 255, nullable: false)]
  22. private string $name;
  23. #[ORM\Column(name: 'password', type: 'string', length: 255)]
  24. private string $password;
  25. /**
  26. * @var User[]
  27. */
  28. #[ORM\JoinTable(name: 'classroom_teacher')]
  29. #[ORM\JoinColumn(name: 'classroom', referencedColumnName: 'id')]
  30. #[ORM\InverseJoinColumn(name: 'teacher', referencedColumnName: 'id')]
  31. #[ORM\ManyToMany(targetEntity: User::class)]
  32. private array|ArrayCollection|PersistentCollection $teachers;
  33. /**
  34. * @var ClassroomStudent[]
  35. */
  36. #[ORM\OneToMany(targetEntity: ClassroomStudent::class, mappedBy: 'classroom', cascade: ['persist', 'remove'])]
  37. private array|ArrayCollection|PersistentCollection $members;
  38. /**
  39. * @var Session[]
  40. */
  41. #[ORM\OneToMany(targetEntity: \App\Entity\Core\Session\Session::class, mappedBy: 'classroom', cascade: ['persist'])]
  42. private array|ArrayCollection|PersistentCollection $sessions;
  43. #[ORM\OneToMany(targetEntity: \App\Entity\Core\Peer\PeerSession::class, mappedBy: 'classroom', cascade: ['persist'])]
  44. private ArrayCollection|PersistentCollection $peerSessions;
  45. /**
  46. * @var QuizSession[]
  47. */
  48. #[ORM\OneToMany(targetEntity: \App\Entity\Core\Quiz\QuizSession::class, mappedBy: 'classroom', cascade: ['persist'])]
  49. private array|ArrayCollection|PersistentCollection $quizSessions;
  50. #[ORM\Column(name: 'is_demo', type: 'boolean', options: ['default' => false])]
  51. private bool $isDemo;
  52. #[ORM\Column(name: 'is_active', type: 'boolean', options: ['default' => true])]
  53. private bool $isActive;
  54. #[ORM\Column(type: 'boolean', nullable: true)]
  55. private ?bool $customer;
  56. #[ORM\JoinColumn(name: 'institution', referencedColumnName: 'id', onDelete: 'CASCADE', nullable: true)]
  57. #[ORM\ManyToOne(targetEntity: \App\Entity\Core\Institution::class)]
  58. protected ?Institution $institution = null;
  59. #[ORM\JoinColumn(name: 'type', referencedColumnName: 'id', nullable: true)]
  60. #[ORM\ManyToOne(targetEntity: \App\Entity\Core\Classroom\ClassroomType::class)]
  61. private ?ClassroomType $type = null;
  62. #[ORM\Column(name: 'timestamp', type: 'datetime', nullable: true)]
  63. private ?DateTime $timestamp;
  64. public function __construct()
  65. {
  66. $this->members = new ArrayCollection();
  67. $this->teachers = new ArrayCollection();
  68. $this->sessions = new ArrayCollection();
  69. $this->peerSessions = new ArrayCollection();
  70. $this->quizSessions = new ArrayCollection();
  71. $bytes = random_bytes(4);
  72. $this->password = bin2hex($bytes);
  73. }
  74. public function getId(): ?int
  75. {
  76. return $this->id;
  77. }
  78. public function setName(string $name): Classroom
  79. {
  80. $this->name = $name;
  81. return $this;
  82. }
  83. public function setNameForDemoClassroom(User $user): void
  84. {
  85. $this->name = "Testklasse ({$user->getUsername()})";
  86. }
  87. public function getName(): string
  88. {
  89. return $this->name;
  90. }
  91. public function getTeachers(): ArrayCollection|Array|PersistentCollection
  92. {
  93. return $this->teachers;
  94. }
  95. public function addTeacher(User $teacher): Classroom
  96. {
  97. $this->teachers->add($teacher);
  98. return $this;
  99. }
  100. public function removeTeacher(User $teacher): bool
  101. {
  102. return $this->teachers->removeElement($teacher);
  103. }
  104. /**
  105. * @return ClassroomStudent[]
  106. */
  107. public function getMembers(): ArrayCollection|PersistentCollection
  108. {
  109. return $this->members;
  110. }
  111. /**
  112. * @return array|ArrayCollection
  113. */
  114. public function getMembersIds(): array|ArrayCollection
  115. {
  116. $membersIds = [];
  117. foreach ($this->members as $member) {
  118. $membersIds[] = $member->getStudent()->getId();
  119. }
  120. return $membersIds;
  121. }
  122. public function addMember(ClassroomStudent $ClassroomStudent): Classroom
  123. {
  124. $ClassroomStudent->setClassroom($this);
  125. $this->members->add($ClassroomStudent);
  126. return $this;
  127. }
  128. public function removeMember(ClassroomStudent $ClassroomStudent): bool
  129. {
  130. $ClassroomStudent->setClassroom(null);
  131. return $this->members->removeElement($ClassroomStudent);
  132. }
  133. public function getSessions(): array|ArrayCollection
  134. {
  135. return $this->sessions;
  136. }
  137. public function addSession(Session $session): Classroom
  138. {
  139. $session->setClassroom($this);
  140. $this->sessions->add($session);
  141. return $this;
  142. }
  143. public function removeSession(Session $session): bool
  144. {
  145. $session->setClassroom(null);
  146. return $this->sessions->removeElement($session);
  147. }
  148. /**
  149. * @param User[] $teachers
  150. *
  151. * @return $this
  152. */
  153. public function setTeachers(array $teachers): Classroom
  154. {
  155. $this->teachers = $teachers;
  156. return $this;
  157. }
  158. public function getInstitution(): ?Institution
  159. {
  160. return $this->institution;
  161. }
  162. public function setInstitution(Institution $institution): Classroom
  163. {
  164. $this->institution = $institution;
  165. return $this;
  166. }
  167. public function getPassword(): string
  168. {
  169. return $this->password;
  170. }
  171. public function setPassword(string $password): Classroom
  172. {
  173. $this->password = $password;
  174. return $this;
  175. }
  176. public function getPeerSessions(): ArrayCollection
  177. {
  178. return $this->peerSessions;
  179. }
  180. public function isDemo(): bool
  181. {
  182. return $this->isDemo;
  183. }
  184. public function setIsDemo(bool $isDemo): Classroom
  185. {
  186. $this->isDemo = $isDemo;
  187. return $this;
  188. }
  189. public function isActive(): bool
  190. {
  191. return $this->isActive;
  192. }
  193. public function setIsActive($isActive): void
  194. {
  195. $this->isActive = $isActive;
  196. }
  197. public function getTimestamp(): ?DateTime
  198. {
  199. return $this->timestamp;
  200. }
  201. public function setTimestamp(DateTime $timestamp): void
  202. {
  203. $this->timestamp = $timestamp;
  204. }
  205. public function isCustomer(): ?bool
  206. {
  207. return $this->customer;
  208. }
  209. public function setCustomer(bool $customer): void
  210. {
  211. $this->customer = $customer;
  212. }
  213. public function getType(): ?ClassroomType
  214. {
  215. return $this->type;
  216. }
  217. public function setType(?ClassroomType $type): void
  218. {
  219. $this->type = $type;
  220. }
  221. public function hasTeacher(User $user): bool
  222. {
  223. foreach ($this->getTeachers() as $teacher) {
  224. if ($teacher->getId() === $user->getId())
  225. return true;
  226. }
  227. return false;
  228. }
  229. public function jsonSerialize(): mixed
  230. {
  231. return [
  232. "id" => $this->getId(),
  233. "name" => $this->getName(),
  234. "type" => $this->getType(),
  235. "timeStamp" => $this->getTimestamp(),
  236. "institution" => $this->getInstitution(),
  237. "members" => $this->getMembers()->toArray(),
  238. "memberIds" => $this->getMembersIds(),
  239. "teachers" => $this->getTeachers()->toArray()
  240. ];
  241. }
  242. }