<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use function Symfony\Component\String\u;
;
/**
* @ORM\Table(name="course_session")
* @ORM\Entity(repositoryClass="App\Repository\SessionRepository")
*/
class Session
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank()
*/
private $displayName;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Teacher", inversedBy="sessionsCourses")
* @Assert\NotBlank()
*/
private $teacher;
/**
* @ORM\Column(type="decimal", precision=25, scale=15)
*
*/
private $price;
/**
* @ORM\Column(type="datetime")
*/
private $startDate;
/**
* @ORM\Column(type="datetime")
*/
private $endDate;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $midLevelExamDate;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $endLevelExamDate;
/**
* @ORM\Column(type="integer")
*/
private $nbCourse;
/**
* @ORM\OneToMany(targetEntity="App\Entity\TeacherSchedule", mappedBy="sessionCourse")
* @Assert\Valid()
*/
private $TeacherSchedule;
/**
* @ORM\OneToMany(targetEntity="App\Entity\TeacherSchedule", mappedBy="nextSessionCourse")
* @Assert\Valid()
*/
private $nextTeacherSchedule;
/**
* @ORM\Column(type="string", length=255)
*/
private $displaySchedule;
/**
* @ORM\Column(type="integer")
*/
private $seat;
/**
* @ORM\OneToMany(targetEntity="SessionOrder", mappedBy="session")
*/
private $sessionOrder;
/**
* @ORM\OneToMany(targetEntity="App\Entity\PaidCourseList", mappedBy="sessionGroup")
*/
private $courseList;
/**
* @ORM\Column(type="string", length=255)
*/
private $displayScheduleEn;
/**
* @ORM\Column(type="string", length=255)
*/
private $displayNameEn;
private $subject;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\SubjectLevel", inversedBy="groupSessions")
* @ORM\JoinColumn(nullable=false)
*/
private $level;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $totalLostMinutes;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $isClose;
/**
* @ORM\Column(type="string", length=100, nullable=true)
*/
private $period;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $courseDuration;
/**
* @ORM\Column(type="boolean")
*/
private $isCustom = false;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $isDeleted;
public function __construct()
{
$this->TeacherSchedule = new ArrayCollection();
$this->nextTeacherSchedule = new ArrayCollection();
$this->sessionOrder = new ArrayCollection();
$this->courseList = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getDisplayName(): ?string
{
return $this->displayName;
}
public function setDisplayName(?string $displayName): self
{
$this->displayName = $displayName;
return $this;
}
public function getTeacher(): ?Teacher
{
return $this->teacher;
}
public function setTeacher(?Teacher $teacher): self
{
$this->teacher = $teacher;
return $this;
}
public function getPrice(): ?float
{
return $this->price;
}
public function setPrice(float $price): self
{
$this->price = round($price, 2);
return $this;
}
public function getStartDate(): ?\DateTimeInterface
{
return $this->startDate;
}
public function setStartDate(?\DateTimeInterface $startDate): self
{
$this->startDate = $startDate;
return $this;
}
public function getEndDate(): ?\DateTimeInterface
{
return $this->endDate;
}
public function setEndDate(?\DateTimeInterface $endDate): self
{
$this->endDate = $endDate;
return $this;
}
public function getMidLevelExamDate(): ?\DateTimeInterface
{
return $this->midLevelExamDate;
}
public function setMidLevelExamDate(\DateTimeInterface $midLevelExamDate): self
{
$this->midLevelExamDate = $midLevelExamDate;
return $this;
}
public function getEndLevelExamDate(): ?\DateTimeInterface
{
return $this->endLevelExamDate;
}
public function setEndLevelExamDate(\DateTimeInterface $endLevelExamDate): self
{
$this->endLevelExamDate = $endLevelExamDate;
return $this;
}
public function getNbCourse(): ?int
{
return $this->nbCourse;
}
public function setNbCourse(int $nbCourse): self
{
$this->nbCourse = $nbCourse;
return $this;
}
/**
* @return Collection|TeacherSchedule[]
*/
public function getTeacherSchedule(): Collection
{
return $this->TeacherSchedule;
}
public function addTeacherSchedule(TeacherSchedule $schedule): self
{
if ( ! $this->TeacherSchedule->contains($schedule)) {
$this->TeacherSchedule[] = $schedule;
$schedule->setSessionCourse($this);
}
return $this;
}
public function removeTeacherSchedule(TeacherSchedule $schedule): self
{
if ($this->TeacherSchedule->contains($schedule)) {
$this->TeacherSchedule->removeElement($schedule);
// set the owning side to null (unless already changed)
if ($schedule->getSessionCourse() === $this) {
$schedule->setSessionCourse(null);
}
}
return $this;
}
public function getNextTeacherSchedule(): Collection
{
return $this->nextTeacherSchedule;
}
public function addNexteacherSchedule(TeacherSchedule $schedule): self
{
if ( ! $this->nextTeacherSchedule->contains($schedule)) {
$this->nextTeacherSchedule[] = $schedule;
$schedule->setSessionCourse($this);
}
return $this;
}
public function removeNextTeacherSchedule(TeacherSchedule $schedule): self
{
if ($this->nextTeacherSchedule->contains($schedule)) {
$this->nextTeacherSchedule->removeElement($schedule);
// set the owning side to null (unless already changed)
if ($schedule->getSessionCourse() === $this) {
$schedule->setSessionCourse(null);
}
}
return $this;
}
public function getDisplaySchedule(): ?string
{
return $this->displaySchedule;
}
public function setDisplaySchedule(string $displaySchedule): self
{
$this->displaySchedule = $displaySchedule;
return $this;
}
public function getSeat(): ?int
{
return $this->seat;
}
public function setSeat(int $seat): self
{
$this->seat = $seat;
return $this;
}
/**
* @return Collection|SessionOrder[]
*/
public function getSessionOrder(): Collection
{
return $this->sessionOrder;
}
public function addSessionOrder(SessionOrder $sessionOrder): self
{
if ( ! $this->sessionOrder->contains($sessionOrder)) {
$this->sessionOrder[] = $sessionOrder;
$sessionOrder->setSession($this);
}
return $this;
}
public function removeSessionOrder(SessionOrder $sessionOrder): self
{
if ($this->sessionOrder->contains($sessionOrder)) {
$this->sessionOrder->removeElement($sessionOrder);
// set the owning side to null (unless already changed)
if ($sessionOrder->getSession() === $this) {
$sessionOrder->setSession(null);
}
}
return $this;
}
public function decrementSeat()
{
$this->seat = $this->seat - 1;
}
public function incrementSeat()
{
$this->seat = $this->seat + 1;
}
public function __toString()
{
return $this->getDisplayNameEn();
}
/**
* @return Collection|PaidCourseList[]
*/
public function getCourseList(): Collection
{
return $this->courseList;
}
public function addCourseList(PaidCourseList $courseList): self
{
if ( ! $this->courseList->contains($courseList)) {
$this->courseList[] = $courseList;
$courseList->setSessionGroup($this);
}
return $this;
}
public function removeCourseList(PaidCourseList $courseList): self
{
if ($this->courseList->contains($courseList)) {
$this->courseList->removeElement($courseList);
// set the owning side to null (unless already changed)
if ($courseList->getSessionGroup() === $this) {
$courseList->setSessionGroup(null);
}
}
return $this;
}
public function getDisplayScheduleEn(): ?string
{
return $this->displayScheduleEn;
}
public function setDisplayScheduleEn(string $displayScheduleEn): self
{
$this->displayScheduleEn = $displayScheduleEn;
return $this;
}
public function getDisplayNameEn(): ?string
{
return $this->displayNameEn;
}
public function setDisplayNameEn(?string $displayNameEn): self
{
$this->displayNameEn = $displayNameEn;
return $this;
}
/**
* @return mixed
*/
public function getSubject()
{
return $this->subject;
}
/**
* @param mixed $subject
*/
public function setSubject($subject): self
{
$this->subject = $subject;
return $this;
}
public function getLevel(): ?SubjectLevel
{
return $this->level;
}
public function setLevel(?SubjectLevel $level): self
{
$this->level = $level;
if ($level !== null) {
$this->setCourseDuration($level->getSessionDuration());
$this->setPrice($level->getSessionPrice());
$this->setNbCourse($level->getSessionNbCourse());
$this->setSeat($level->getSessionSeats());
}
return $this;
}
public function getTotalLostMinutes(): ?int
{
return $this->totalLostMinutes;
}
public function setTotalLostMinutes(?int $totalLostMinutes): self
{
$this->totalLostMinutes = $totalLostMinutes;
return $this;
}
/**
* Quand des minutes de cours sont perdues, j'augmente le total avec les minutes perdues
*
* @param $lostMinutes
*
* @return mixed
*/
public function incrementTotalListMinutes($lostMinutes)
{
$newTotal = $this->totalLostMinutes + $lostMinutes;
return $newTotal;
}
/**
* Quand des minutes sont rattrapées, je les déduit de ce total
*
* @param $minutesCaughtUp
*
* @return mixed
*/
public function decrementTotalListMinutes($minutesCaughtUp)
{
$newTotal = $this->totalLostMinutes - $minutesCaughtUp;
return $newTotal;
}
public function getIsClose(): ?bool
{
return $this->isClose;
}
public function setIsClose(?bool $isClose): self
{
$this->isClose = $isClose;
return $this;
}
public function getPeriod(): ?string
{
return $this->period;
}
public function setPeriod(?string $period): self
{
$this->period = $period;
return $this;
}
public function getCourseDuration(): ?int
{
return $this->courseDuration;
}
public function setCourseDuration(?int $courseDuration): self
{
$this->courseDuration = $courseDuration;
return $this;
}
public function getIsDeleted(): ?bool
{
return $this->isDeleted;
}
public function setIsDeleted(?bool $isDeleted): self
{
$this->isDeleted = $isDeleted;
return $this;
}
public function getIsCustom(): bool
{
return $this->isCustom;
}
public function setIsCustom(bool $isCustom): self
{
$this->isCustom = $isCustom;
return $this;
}
/** @Assert\Callback() */
function validateSession(ExecutionContextInterface $context, $payload)
{
$teacherSchedules = [];
foreach ($this->getTeacherSchedule()->toArray() as $ts) {
/** @var TeacherSchedule $ts */
$teacherSchedules[$ts->getScheduleInstitute()->getDayNb().$ts->getScheduleInstitute()->getStartTime()] = $ts;
}
if($this->getId() !== null) {
foreach ($this->getNextTeacherSchedule()->toArray() as $ts) {
/** @var TeacherSchedule $ts */
$teacherSchedules[$ts->getScheduleInstitute()->getDayNb().$ts->getScheduleInstitute()->getStartTime()] = $ts;
}
}
ksort($teacherSchedules);
foreach ($teacherSchedules as $i => $teacherSchedule) {
if ($teacherSchedule->getRoom() === null) {
$context->buildViolation('You need to add a room on selected teacherSchedule.')
->atPath('teacherSchedule')
->addViolation();
break;
}
}
if (count($teacherSchedules) === 0) {
$context->buildViolation('You need to add more than one.')
->atPath('teacherSchedule')
->addViolation();
} elseif ($this->getCourseDuration() === 150) {
if (count($teacherSchedules) === 1) {
$context->buildViolation('You need to add 2 teacherSchedule consecutive.')
->atPath('teacherSchedule')
->addViolation();
} elseif (count($teacherSchedules) % 2 === 1) {
$context->buildViolation('You need to add 2 teacherSchedule consecutive.')
->atPath('teacherSchedule')
->addViolation();
} elseif ($this->getLevel() === null) {
$context->buildViolation('You need to select a level.')
->atPath('level')
->addViolation();
} elseif ($this->getIsCustom() !== true && ($this->getLevel()->getSessionNbCourseMinByWeek() * 2) != count($teacherSchedules) && $this->getId() === null) {
$context->buildViolation('You need to select '.($this->getLevel()->getSessionNbCourseMinByWeek() * 2).' teacherSchedules in group of 2 for this level.')
->atPath('teacherSchedule')
->addViolation();
} else {
$firstTeacherSchedule = null;
foreach ($teacherSchedules as $i => $teacherSchedule) {
if ($i % 2 === 0) {
$firstTeacherSchedule = $teacherSchedule;
if (isset($dateFirstSchedule)) {
if ($dateFirstSchedule === $firstTeacherSchedule->getScheduleInstitute()->getDay()) {
$context->buildViolation('You need to group schedule on different day.')
->atPath('teacherSchedule')
->addViolation();
}
}
} else {
$dateFirstSchedule = $firstTeacherSchedule->getScheduleInstitute()->getDay();
$endTimeFirstSchedule = $firstTeacherSchedule->getScheduleInstitute()->getEndTime();
$dateSchedule = $teacherSchedule->getScheduleInstitute()->getDay();
$startTimeSchedule = $teacherSchedule->getScheduleInstitute()->getStartTime();
if ($startTimeSchedule <= (45 * 60)) {
$startTimeSchedule += (24 * 60 * 60);
}
$endTimeSchedule = $teacherSchedule->getScheduleInstitute()->getEndTime();
if ($endTimeSchedule <= (45 * 60)) {
$endTimeSchedule += (24 * 60 * 60);
}
$calculHour = $endTimeFirstSchedule + (30 * 60);
if (u($dateFirstSchedule)->lower()->toString() !== u($dateSchedule)->lower()->toString()) {
if ($calculHour > (24 * 60 * 60)) {
$context->buildViolation('You need to add 2 teacherSchedule on same day.')
->atPath('teacherSchedule')
->addViolation();
}
} elseif ($calculHour < $startTimeSchedule || $calculHour > $endTimeSchedule) {
$context->buildViolation('You need to add 2 teacherSchedule consecutive.')
->atPath('teacherSchedule')
->addViolation();
}
if ($firstTeacherSchedule->getRoom() !== null && $teacherSchedule->getRoom() != null) {
if ($firstTeacherSchedule->getRoom()->getId() != $teacherSchedule->getRoom()->getId()) {
$context->buildViolation('You need to have same room on consecutive teacherSchedule.')
->atPath('teacherSchedule')
->addViolation();
}
}
}
}
}
} elseif ($this->getCourseDuration() === 75) {
if ($this->getIsCustom() !== true && $this->getLevel() !== null && $this->getLevel()->getSessionNbCourseMinByWeek() != count($teacherSchedules)) {
$context->buildViolation('You need to select '.($this->getLevel()->getSessionNbCourseMinByWeek()).' teacherSchedules for this level.')
->atPath('teacherSchedule')
->addViolation();
}
$oldTeacherSchedule = null;
foreach ($teacherSchedules as $i => $teacherSchedule) {
if ($oldTeacherSchedule !== null && $teacherSchedule->getScheduleInstitute()->getDay() === $oldTeacherSchedule->getScheduleInstitute()->getDay()) {
$context->buildViolation('You need to add schedule on different day.')
->atPath('teacherSchedule')
->addViolation();
}
$oldTeacherSchedule = $teacherSchedule;
}
}
}
}