src/CoreBundle/Entity/Classroom.php line 14

Open in your IDE?
  1. <?php
  2. namespace CoreBundle\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use FOS\UserBundle\Model\User;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use JsonSerializable;
  7. /**
  8. * @ORM\Table("classroom")
  9. * @ORM\Entity(repositoryClass="CoreBundle\Entity\ClassroomRepository")
  10. */
  11. class Classroom 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 string
  23. *
  24. * @ORM\Column(name="name", type="string", length=255, nullable = false)
  25. */
  26. private $name;
  27. /**
  28. * @var string
  29. *
  30. * @ORM\Column(name="password", type="string", length=255)
  31. */
  32. private $password;
  33. /**
  34. * @var User[]
  35. *
  36. * @ORM\ManyToMany(targetEntity="UserBundle\Entity\User")
  37. * @ORM\JoinTable(name="classroom_teacher",
  38. * joinColumns={@ORM\JoinColumn(name="classroom", referencedColumnName="id")},
  39. * inverseJoinColumns={@ORM\JoinColumn(name="teacher", referencedColumnName="id")})
  40. */
  41. private $teachers;
  42. /**
  43. * @var ClassroomStudent[]
  44. *
  45. * @ORM\OneToMany(targetEntity="ClassroomStudent", mappedBy="classroom", cascade={"persist","remove"})
  46. */
  47. private $members;
  48. /**
  49. * @var Session[]
  50. *
  51. * @ORM\OneToMany(targetEntity="Session", mappedBy="classroom", cascade={"persist"})
  52. */
  53. private $sessions;
  54. /**
  55. * @var ArrayCollection PeerSession
  56. *
  57. * @ORM\OneToMany(targetEntity="CoreBundle\Entity\PeerSession", mappedBy="classroom", cascade={"persist"})
  58. */
  59. private $peerSessions;
  60. /**
  61. * @var QuizSession[]
  62. *
  63. * @ORM\OneToMany(targetEntity="CoreBundle\Entity\QuizSession", mappedBy="classroom", cascade={"persist"})
  64. */
  65. private $quizSessions;
  66. /**
  67. * @var bool
  68. *
  69. * @ORM\Column(name="is_demo", type="boolean", options={"default" : false})
  70. */
  71. private $isDemo;
  72. /**
  73. * @var bool
  74. *
  75. * @ORM\Column(name="is_active", type="boolean", options={"default" : true})
  76. */
  77. private $isActive;
  78. /**
  79. * @var bool
  80. *
  81. * @ORM\Column(type="boolean", nullable=true)
  82. */
  83. private $customer;
  84. /**
  85. * @var Institution
  86. *
  87. * @ORM\ManyToOne(targetEntity="CoreBundle\Entity\Institution")
  88. * @ORM\JoinColumn(name="institution", referencedColumnName="id", onDelete="CASCADE", nullable=true)
  89. */
  90. protected $institution = null;
  91. /**
  92. * @var ClassroomType
  93. *
  94. * @ORM\ManyToOne(targetEntity="CoreBundle\Entity\ClassroomType")
  95. * @ORM\JoinColumn(name="type", referencedColumnName="id", nullable=true)
  96. */
  97. private $type = null;
  98. /**
  99. * @var \DateTime
  100. *
  101. * @ORM\Column(name="timestamp", type="datetime", nullable=true)
  102. */
  103. private $timestamp;
  104. public function __construct()
  105. {
  106. $this->members = new ArrayCollection();
  107. $this->teachers = new ArrayCollection();
  108. $this->sessions = new ArrayCollection();
  109. $this->peerSessions = new ArrayCollection();
  110. $this->quizSessions = new ArrayCollection();
  111. $bytes = random_bytes(4);
  112. $this->password = bin2hex($bytes);
  113. }
  114. /**
  115. * Get id.
  116. *
  117. * @return int
  118. */
  119. public function getId()
  120. {
  121. return $this->id;
  122. }
  123. /**
  124. * Set name.
  125. *
  126. * @param string $name
  127. *
  128. * @return Classroom
  129. */
  130. public function setName($name)
  131. {
  132. $this->name = $name;
  133. return $this;
  134. }
  135. public function setNameForDemoClassroom(User $user)
  136. {
  137. $this->name = "Testklasse ({$user->getUsername()})";
  138. }
  139. /**
  140. * Get name.
  141. *
  142. * @return string
  143. */
  144. public function getName()
  145. {
  146. return $this->name;
  147. }
  148. public function getTeachers()
  149. {
  150. return $this->teachers;
  151. }
  152. /**
  153. * @param User $teacher
  154. *
  155. * @return Classroom
  156. */
  157. public function addTeacher(User $teacher)
  158. {
  159. $this->teachers->add($teacher);
  160. return $this;
  161. }
  162. /**
  163. * @param User $teacher
  164. *
  165. * @return bool
  166. */
  167. public function removeTeacher(User $teacher)
  168. {
  169. return $this->teachers->removeElement($teacher);
  170. }
  171. /**
  172. * @return ClassroomStudent[]
  173. */
  174. public function getMembers()
  175. {
  176. return $this->members;
  177. }
  178. /**
  179. * @return array
  180. */
  181. public function getMembersIds()
  182. {
  183. $membersIds = [];
  184. foreach ($this->members as $member) {
  185. $membersIds[] = $member->getStudent()->getId();
  186. }
  187. return $membersIds;
  188. }
  189. /**
  190. * @param ClassroomStudent $ClassroomStudent
  191. *
  192. * @return Classroom
  193. */
  194. public function addMember(ClassroomStudent $ClassroomStudent)
  195. {
  196. $ClassroomStudent->setClassroom($this);
  197. $this->members->add($ClassroomStudent);
  198. return $this;
  199. }
  200. /**
  201. * @param ClassroomStudent $ClassroomStudent
  202. *
  203. * @return bool
  204. */
  205. public function removeMember(ClassroomStudent $ClassroomStudent)
  206. {
  207. $ClassroomStudent->setClassroom(null);
  208. return $this->members->removeElement($ClassroomStudent);
  209. }
  210. /**
  211. * @return Session[]
  212. */
  213. public function getSessions()
  214. {
  215. return $this->sessions;
  216. }
  217. /**
  218. * @param Session $session
  219. *
  220. * @return Classroom
  221. */
  222. public function addSession(Session $session)
  223. {
  224. $session->setClassroom($this);
  225. $this->sessions->add($session);
  226. return $this;
  227. }
  228. /**
  229. * @param Session $session
  230. *
  231. * @return bool
  232. */
  233. public function removeSession(Session $session)
  234. {
  235. $session->setClassroom(null);
  236. return $this->sessions->removeElement($session);
  237. }
  238. /**
  239. * @param \FOS\UserBundle\Model\User[] $teachers
  240. *
  241. * @return $this
  242. */
  243. public function setTeachers(array $teachers)
  244. {
  245. $this->teachers = $teachers;
  246. return $this;
  247. }
  248. /**
  249. * @return Institution
  250. */
  251. public function getInstitution()
  252. {
  253. return $this->institution;
  254. }
  255. /**
  256. * @param Institution $institution
  257. *
  258. * @return $this
  259. */
  260. public function setInstitution(Institution $institution)
  261. {
  262. $this->institution = $institution;
  263. return $this;
  264. }
  265. /**
  266. * @return string
  267. */
  268. public function getPassword()
  269. {
  270. return $this->password;
  271. }
  272. /**
  273. * @param string $password
  274. *
  275. * @return $this
  276. */
  277. public function setPassword($password)
  278. {
  279. $this->password = $password;
  280. return $this;
  281. }
  282. /**
  283. * @return ArrayCollection
  284. */
  285. public function getPeerSessions()
  286. {
  287. return $this->peerSessions;
  288. }
  289. /**
  290. * @return bool
  291. */
  292. public function isDemo()
  293. {
  294. return $this->isDemo;
  295. }
  296. /**
  297. * @param bool $isDemo
  298. *
  299. * @return $this
  300. */
  301. public function setIsDemo(bool $isDemo)
  302. {
  303. $this->isDemo = $isDemo;
  304. return $this;
  305. }
  306. /**
  307. * @return bool
  308. */
  309. public function isActive()
  310. {
  311. return $this->isActive;
  312. }
  313. /**
  314. * @param bool $isActive
  315. */
  316. public function setIsActive($isActive)
  317. {
  318. $this->isActive = $isActive;
  319. }
  320. /**
  321. * @return \DateTime
  322. */
  323. public function getTimestamp()
  324. {
  325. return $this->timestamp;
  326. }
  327. /**
  328. * @param \DateTime $timestamp
  329. */
  330. public function setTimestamp($timestamp)
  331. {
  332. $this->timestamp = $timestamp;
  333. }
  334. public function isCustomer(): ?bool
  335. {
  336. return $this->customer;
  337. }
  338. public function setCustomer(bool $customer)
  339. {
  340. $this->customer = $customer;
  341. }
  342. public function getType(): ?ClassroomType
  343. {
  344. return $this->type;
  345. }
  346. public function setType(?ClassroomType $type): void
  347. {
  348. $this->type = $type;
  349. }
  350. public function hasTeacher(User $user): bool
  351. {
  352. foreach ($this->getTeachers() as $teacher) {
  353. if ($teacher->getId() === $user->getId())
  354. return true;
  355. }
  356. return false;
  357. }
  358. public function jsonSerialize(): mixed
  359. {
  360. return [
  361. "id" => $this->getId(),
  362. "name" => $this->getName(),
  363. "type" => $this->getType(),
  364. "timeStamp" => $this->getTimestamp(),
  365. "institution" => $this->getInstitution(),
  366. "members" => $this->getMembers()->toArray(),
  367. "memberIds" => $this->getMembersIds(),
  368. "teachers" => $this->getTeachers()->toArray()
  369. ];
  370. }
  371. }