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

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