src/Entity/Session.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Component\Validator\Constraints as Assert;
  7. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  8. use function Symfony\Component\String\u;
  9. ;
  10. /**
  11. * @ORM\Table(name="course_session")
  12. * @ORM\Entity(repositoryClass="App\Repository\SessionRepository")
  13. */
  14. class Session
  15. {
  16. /**
  17. * @ORM\Id()
  18. * @ORM\GeneratedValue()
  19. * @ORM\Column(type="integer")
  20. */
  21. private $id;
  22. /**
  23. * @ORM\Column(type="string", length=255)
  24. * @Assert\NotBlank()
  25. */
  26. private $displayName;
  27. /**
  28. * @ORM\ManyToOne(targetEntity="App\Entity\Teacher", inversedBy="sessionsCourses")
  29. * @Assert\NotBlank()
  30. */
  31. private $teacher;
  32. /**
  33. * @ORM\Column(type="decimal", precision=25, scale=15)
  34. *
  35. */
  36. private $price;
  37. /**
  38. * @ORM\Column(type="datetime")
  39. */
  40. private $startDate;
  41. /**
  42. * @ORM\Column(type="datetime")
  43. */
  44. private $endDate;
  45. /**
  46. * @ORM\Column(type="datetime", nullable=true)
  47. */
  48. private $midLevelExamDate;
  49. /**
  50. * @ORM\Column(type="datetime", nullable=true)
  51. */
  52. private $endLevelExamDate;
  53. /**
  54. * @ORM\Column(type="integer")
  55. */
  56. private $nbCourse;
  57. /**
  58. * @ORM\OneToMany(targetEntity="App\Entity\TeacherSchedule", mappedBy="sessionCourse")
  59. * @Assert\Valid()
  60. */
  61. private $TeacherSchedule;
  62. /**
  63. * @ORM\OneToMany(targetEntity="App\Entity\TeacherSchedule", mappedBy="nextSessionCourse")
  64. * @Assert\Valid()
  65. */
  66. private $nextTeacherSchedule;
  67. /**
  68. * @ORM\Column(type="string", length=255)
  69. */
  70. private $displaySchedule;
  71. /**
  72. * @ORM\Column(type="integer")
  73. */
  74. private $seat;
  75. /**
  76. * @ORM\OneToMany(targetEntity="SessionOrder", mappedBy="session")
  77. */
  78. private $sessionOrder;
  79. /**
  80. * @ORM\OneToMany(targetEntity="App\Entity\PaidCourseList", mappedBy="sessionGroup")
  81. */
  82. private $courseList;
  83. /**
  84. * @ORM\Column(type="string", length=255)
  85. */
  86. private $displayScheduleEn;
  87. /**
  88. * @ORM\Column(type="string", length=255)
  89. */
  90. private $displayNameEn;
  91. private $subject;
  92. /**
  93. * @ORM\ManyToOne(targetEntity="App\Entity\SubjectLevel", inversedBy="groupSessions")
  94. * @ORM\JoinColumn(nullable=false)
  95. */
  96. private $level;
  97. /**
  98. * @ORM\Column(type="integer", nullable=true)
  99. */
  100. private $totalLostMinutes;
  101. /**
  102. * @ORM\Column(type="boolean", nullable=true)
  103. */
  104. private $isClose;
  105. /**
  106. * @ORM\Column(type="string", length=100, nullable=true)
  107. */
  108. private $period;
  109. /**
  110. * @ORM\Column(type="integer", nullable=true)
  111. */
  112. private $courseDuration;
  113. /**
  114. * @ORM\Column(type="boolean")
  115. */
  116. private $isCustom = false;
  117. /**
  118. * @ORM\Column(type="boolean", nullable=true)
  119. */
  120. private $isDeleted;
  121. public function __construct()
  122. {
  123. $this->TeacherSchedule = new ArrayCollection();
  124. $this->nextTeacherSchedule = new ArrayCollection();
  125. $this->sessionOrder = new ArrayCollection();
  126. $this->courseList = new ArrayCollection();
  127. }
  128. public function getId(): ?int
  129. {
  130. return $this->id;
  131. }
  132. public function getDisplayName(): ?string
  133. {
  134. return $this->displayName;
  135. }
  136. public function setDisplayName(?string $displayName): self
  137. {
  138. $this->displayName = $displayName;
  139. return $this;
  140. }
  141. public function getTeacher(): ?Teacher
  142. {
  143. return $this->teacher;
  144. }
  145. public function setTeacher(?Teacher $teacher): self
  146. {
  147. $this->teacher = $teacher;
  148. return $this;
  149. }
  150. public function getPrice(): ?float
  151. {
  152. return $this->price;
  153. }
  154. public function setPrice(float $price): self
  155. {
  156. $this->price = round($price, 2);
  157. return $this;
  158. }
  159. public function getStartDate(): ?\DateTimeInterface
  160. {
  161. return $this->startDate;
  162. }
  163. public function setStartDate(?\DateTimeInterface $startDate): self
  164. {
  165. $this->startDate = $startDate;
  166. return $this;
  167. }
  168. public function getEndDate(): ?\DateTimeInterface
  169. {
  170. return $this->endDate;
  171. }
  172. public function setEndDate(?\DateTimeInterface $endDate): self
  173. {
  174. $this->endDate = $endDate;
  175. return $this;
  176. }
  177. public function getMidLevelExamDate(): ?\DateTimeInterface
  178. {
  179. return $this->midLevelExamDate;
  180. }
  181. public function setMidLevelExamDate(\DateTimeInterface $midLevelExamDate): self
  182. {
  183. $this->midLevelExamDate = $midLevelExamDate;
  184. return $this;
  185. }
  186. public function getEndLevelExamDate(): ?\DateTimeInterface
  187. {
  188. return $this->endLevelExamDate;
  189. }
  190. public function setEndLevelExamDate(\DateTimeInterface $endLevelExamDate): self
  191. {
  192. $this->endLevelExamDate = $endLevelExamDate;
  193. return $this;
  194. }
  195. public function getNbCourse(): ?int
  196. {
  197. return $this->nbCourse;
  198. }
  199. public function setNbCourse(int $nbCourse): self
  200. {
  201. $this->nbCourse = $nbCourse;
  202. return $this;
  203. }
  204. /**
  205. * @return Collection|TeacherSchedule[]
  206. */
  207. public function getTeacherSchedule(): Collection
  208. {
  209. return $this->TeacherSchedule;
  210. }
  211. public function addTeacherSchedule(TeacherSchedule $schedule): self
  212. {
  213. if ( ! $this->TeacherSchedule->contains($schedule)) {
  214. $this->TeacherSchedule[] = $schedule;
  215. $schedule->setSessionCourse($this);
  216. }
  217. return $this;
  218. }
  219. public function removeTeacherSchedule(TeacherSchedule $schedule): self
  220. {
  221. if ($this->TeacherSchedule->contains($schedule)) {
  222. $this->TeacherSchedule->removeElement($schedule);
  223. // set the owning side to null (unless already changed)
  224. if ($schedule->getSessionCourse() === $this) {
  225. $schedule->setSessionCourse(null);
  226. }
  227. }
  228. return $this;
  229. }
  230. public function getNextTeacherSchedule(): Collection
  231. {
  232. return $this->nextTeacherSchedule;
  233. }
  234. public function addNexteacherSchedule(TeacherSchedule $schedule): self
  235. {
  236. if ( ! $this->nextTeacherSchedule->contains($schedule)) {
  237. $this->nextTeacherSchedule[] = $schedule;
  238. $schedule->setNextSessionCourse($this);
  239. }
  240. return $this;
  241. }
  242. public function removeNextTeacherSchedule(TeacherSchedule $schedule): self
  243. {
  244. if ($this->nextTeacherSchedule->contains($schedule)) {
  245. $this->nextTeacherSchedule->removeElement($schedule);
  246. // set the owning side to null (unless already changed)
  247. if ($schedule->getNextSessionCourse() === $this) {
  248. $schedule->setNextSessionCourse(null);
  249. }
  250. }
  251. return $this;
  252. }
  253. public function getDisplaySchedule(): ?string
  254. {
  255. return $this->displaySchedule;
  256. }
  257. public function setDisplaySchedule(string $displaySchedule): self
  258. {
  259. $this->displaySchedule = $displaySchedule;
  260. return $this;
  261. }
  262. public function getSeat(): ?int
  263. {
  264. return $this->seat;
  265. }
  266. public function setSeat(?int $seat): self
  267. {
  268. $this->seat = $seat;
  269. return $this;
  270. }
  271. /**
  272. * @return Collection|SessionOrder[]
  273. */
  274. public function getSessionOrder(): Collection
  275. {
  276. return $this->sessionOrder;
  277. }
  278. public function addSessionOrder(SessionOrder $sessionOrder): self
  279. {
  280. if ( ! $this->sessionOrder->contains($sessionOrder)) {
  281. $this->sessionOrder[] = $sessionOrder;
  282. $sessionOrder->setSession($this);
  283. }
  284. return $this;
  285. }
  286. public function removeSessionOrder(SessionOrder $sessionOrder): self
  287. {
  288. if ($this->sessionOrder->contains($sessionOrder)) {
  289. $this->sessionOrder->removeElement($sessionOrder);
  290. // set the owning side to null (unless already changed)
  291. if ($sessionOrder->getSession() === $this) {
  292. $sessionOrder->setSession(null);
  293. }
  294. }
  295. return $this;
  296. }
  297. public function decrementSeat()
  298. {
  299. $this->seat = $this->seat - 1;
  300. }
  301. public function incrementSeat()
  302. {
  303. $this->seat = $this->seat + 1;
  304. }
  305. public function __toString()
  306. {
  307. return $this->getDisplayNameEn();
  308. }
  309. /**
  310. * @return Collection|PaidCourseList[]
  311. */
  312. public function getCourseList(): Collection
  313. {
  314. return $this->courseList;
  315. }
  316. public function addCourseList(PaidCourseList $courseList): self
  317. {
  318. if ( ! $this->courseList->contains($courseList)) {
  319. $this->courseList[] = $courseList;
  320. $courseList->setSessionGroup($this);
  321. }
  322. return $this;
  323. }
  324. public function removeCourseList(PaidCourseList $courseList): self
  325. {
  326. if ($this->courseList->contains($courseList)) {
  327. $this->courseList->removeElement($courseList);
  328. // set the owning side to null (unless already changed)
  329. if ($courseList->getSessionGroup() === $this) {
  330. $courseList->setSessionGroup(null);
  331. }
  332. }
  333. return $this;
  334. }
  335. public function getDisplayScheduleEn(): ?string
  336. {
  337. return $this->displayScheduleEn;
  338. }
  339. public function setDisplayScheduleEn(string $displayScheduleEn): self
  340. {
  341. $this->displayScheduleEn = $displayScheduleEn;
  342. return $this;
  343. }
  344. public function getDisplayNameEn(): ?string
  345. {
  346. return $this->displayNameEn;
  347. }
  348. public function setDisplayNameEn(?string $displayNameEn): self
  349. {
  350. $this->displayNameEn = $displayNameEn;
  351. return $this;
  352. }
  353. /**
  354. * @return mixed
  355. */
  356. public function getSubject()
  357. {
  358. return $this->subject;
  359. }
  360. /**
  361. * @param mixed $subject
  362. */
  363. public function setSubject($subject): self
  364. {
  365. $this->subject = $subject;
  366. return $this;
  367. }
  368. public function getLevel(): ?SubjectLevel
  369. {
  370. return $this->level;
  371. }
  372. public function setLevel(?SubjectLevel $level): self
  373. {
  374. $this->level = $level;
  375. if ($level !== null) {
  376. $this->setCourseDuration($level->getSessionDuration());
  377. $this->setPrice($level->getSessionPrice());
  378. $this->setNbCourse($level->getSessionNbCourse());
  379. $this->setSeat($level->getSessionSeats());
  380. }
  381. return $this;
  382. }
  383. public function getTotalLostMinutes(): ?int
  384. {
  385. return $this->totalLostMinutes;
  386. }
  387. public function setTotalLostMinutes(?int $totalLostMinutes): self
  388. {
  389. $this->totalLostMinutes = $totalLostMinutes;
  390. return $this;
  391. }
  392. /**
  393. * Quand des minutes de cours sont perdues, j'augmente le total avec les minutes perdues
  394. *
  395. * @param $lostMinutes
  396. *
  397. * @return mixed
  398. */
  399. public function incrementTotalListMinutes($lostMinutes)
  400. {
  401. $newTotal = $this->totalLostMinutes + $lostMinutes;
  402. return $newTotal;
  403. }
  404. /**
  405. * Quand des minutes sont rattrapées, je les déduit de ce total
  406. *
  407. * @param $minutesCaughtUp
  408. *
  409. * @return mixed
  410. */
  411. public function decrementTotalListMinutes($minutesCaughtUp)
  412. {
  413. $newTotal = $this->totalLostMinutes - $minutesCaughtUp;
  414. return $newTotal;
  415. }
  416. public function getIsClose(): ?bool
  417. {
  418. return $this->isClose;
  419. }
  420. public function setIsClose(?bool $isClose): self
  421. {
  422. $this->isClose = $isClose;
  423. return $this;
  424. }
  425. public function getPeriod(): ?string
  426. {
  427. return $this->period;
  428. }
  429. public function setPeriod(?string $period): self
  430. {
  431. $this->period = $period;
  432. return $this;
  433. }
  434. public function getCourseDuration(): ?int
  435. {
  436. return $this->courseDuration;
  437. }
  438. public function setCourseDuration(?int $courseDuration): self
  439. {
  440. $this->courseDuration = $courseDuration;
  441. return $this;
  442. }
  443. public function getIsDeleted(): ?bool
  444. {
  445. return $this->isDeleted;
  446. }
  447. public function setIsDeleted(?bool $isDeleted): self
  448. {
  449. $this->isDeleted = $isDeleted;
  450. return $this;
  451. }
  452. public function getIsCustom(): bool
  453. {
  454. return $this->isCustom;
  455. }
  456. public function setIsCustom(bool $isCustom): self
  457. {
  458. $this->isCustom = $isCustom;
  459. return $this;
  460. }
  461. /** @Assert\Callback() */
  462. function validateSession(ExecutionContextInterface $context, $payload)
  463. {
  464. $teacherSchedules = [];
  465. foreach ($this->getTeacherSchedule()->toArray() as $ts) {
  466. /** @var TeacherSchedule $ts */
  467. $teacherSchedules[$ts->getScheduleInstitute()->getDayNb().$ts->getScheduleInstitute()->getStartTime()] = $ts;
  468. }
  469. if($this->getId() !== null) {
  470. foreach ($this->getNextTeacherSchedule()->toArray() as $ts) {
  471. /** @var TeacherSchedule $ts */
  472. $teacherSchedules[$ts->getScheduleInstitute()->getDayNb().$ts->getScheduleInstitute()->getStartTime()] = $ts;
  473. }
  474. }
  475. ksort($teacherSchedules);
  476. foreach ($teacherSchedules as $i => $teacherSchedule) {
  477. if ($teacherSchedule->getRoom() === null) {
  478. $context->buildViolation('You need to add a room on selected teacherSchedule.')
  479. ->atPath('teacherSchedule')
  480. ->addViolation();
  481. break;
  482. }
  483. }
  484. if (count($teacherSchedules) === 0) {
  485. $context->buildViolation('You need to add more than one.')
  486. ->atPath('teacherSchedule')
  487. ->addViolation();
  488. } elseif ($this->getCourseDuration() === 150) {
  489. if (count($teacherSchedules) === 1) {
  490. $context->buildViolation('You need to add 2 teacherSchedule consecutive.')
  491. ->atPath('teacherSchedule')
  492. ->addViolation();
  493. } elseif (count($teacherSchedules) % 2 === 1) {
  494. $context->buildViolation('You need to add 2 teacherSchedule consecutive.')
  495. ->atPath('teacherSchedule')
  496. ->addViolation();
  497. } elseif ($this->getLevel() === null) {
  498. $context->buildViolation('You need to select a level.')
  499. ->atPath('level')
  500. ->addViolation();
  501. } elseif ($this->getIsCustom() !== true && ($this->getLevel()->getSessionNbCourseMinByWeek() * 2) != count($teacherSchedules) && $this->getId() === null) {
  502. $context->buildViolation('You need to select '.($this->getLevel()->getSessionNbCourseMinByWeek() * 2).' teacherSchedules in group of 2 for this level.')
  503. ->atPath('teacherSchedule')
  504. ->addViolation();
  505. } else {
  506. $firstTeacherSchedule = null;
  507. foreach ($teacherSchedules as $i => $teacherSchedule) {
  508. if ($i % 2 === 0) {
  509. $firstTeacherSchedule = $teacherSchedule;
  510. if (isset($dateFirstSchedule)) {
  511. if ($dateFirstSchedule === $firstTeacherSchedule->getScheduleInstitute()->getDay()) {
  512. $context->buildViolation('You need to group schedule on different day.')
  513. ->atPath('teacherSchedule')
  514. ->addViolation();
  515. }
  516. }
  517. } else {
  518. $dateFirstSchedule = $firstTeacherSchedule->getScheduleInstitute()->getDay();
  519. $endTimeFirstSchedule = $firstTeacherSchedule->getScheduleInstitute()->getEndTime();
  520. $dateSchedule = $teacherSchedule->getScheduleInstitute()->getDay();
  521. $startTimeSchedule = $teacherSchedule->getScheduleInstitute()->getStartTime();
  522. if ($startTimeSchedule <= (45 * 60)) {
  523. $startTimeSchedule += (24 * 60 * 60);
  524. }
  525. $endTimeSchedule = $teacherSchedule->getScheduleInstitute()->getEndTime();
  526. if ($endTimeSchedule <= (45 * 60)) {
  527. $endTimeSchedule += (24 * 60 * 60);
  528. }
  529. $calculHour = $endTimeFirstSchedule + (30 * 60);
  530. if (u($dateFirstSchedule)->lower()->toString() !== u($dateSchedule)->lower()->toString()) {
  531. if ($calculHour > (24 * 60 * 60)) {
  532. $context->buildViolation('You need to add 2 teacherSchedule on same day.')
  533. ->atPath('teacherSchedule')
  534. ->addViolation();
  535. }
  536. } elseif ($calculHour < $startTimeSchedule || $calculHour > $endTimeSchedule) {
  537. $context->buildViolation('You need to add 2 teacherSchedule consecutive.')
  538. ->atPath('teacherSchedule')
  539. ->addViolation();
  540. }
  541. if ($firstTeacherSchedule->getRoom() !== null && $teacherSchedule->getRoom() != null) {
  542. if ($firstTeacherSchedule->getRoom()->getId() != $teacherSchedule->getRoom()->getId()) {
  543. $context->buildViolation('You need to have same room on consecutive teacherSchedule.')
  544. ->atPath('teacherSchedule')
  545. ->addViolation();
  546. }
  547. }
  548. }
  549. }
  550. }
  551. } elseif ($this->getCourseDuration() === 75) {
  552. if ($this->getIsCustom() !== true && $this->getLevel() !== null && $this->getLevel()->getSessionNbCourseMinByWeek() != count($teacherSchedules)) {
  553. $context->buildViolation('You need to select '.($this->getLevel()->getSessionNbCourseMinByWeek()).' teacherSchedules for this level.')
  554. ->atPath('teacherSchedule')
  555. ->addViolation();
  556. }
  557. $oldTeacherSchedule = null;
  558. foreach ($teacherSchedules as $i => $teacherSchedule) {
  559. if ($oldTeacherSchedule !== null && $teacherSchedule->getScheduleInstitute()->getDay() === $oldTeacherSchedule->getScheduleInstitute()->getDay()) {
  560. $context->buildViolation('You need to add schedule on different day.')
  561. ->atPath('teacherSchedule')
  562. ->addViolation();
  563. }
  564. $oldTeacherSchedule = $teacherSchedule;
  565. }
  566. }
  567. }
  568. }