src/Entity/LearningCourses/LearningCourse.php line 34

Open in your IDE?
  1. <?php
  2. namespace App\Entity\LearningCourses;
  3. use App\Annotation\SiteAware;
  4. use App\Entity\AbstractBase;
  5. use App\Entity\Interfaces\SiteInterface;
  6. use App\Entity\MiniAbstractBase;
  7. use App\Entity\Traits\ActiveTrait;
  8. use App\Entity\Traits\DescriptionTrait;
  9. use App\Entity\Traits\ImageFileNameTrait;
  10. use App\Entity\Traits\ImageFileTrait;
  11. use App\Entity\Traits\NameTrait;
  12. use App\Entity\Traits\SiteTrait;
  13. use App\Entity\User;
  14. use App\Enum\LearningCourseTypeEnum;
  15. use App\Repository\LearningCourses\LearningCourseRepository;
  16. use DateTimeImmutable;
  17. use DateTimeInterface;
  18. use Doctrine\Common\Collections\ArrayCollection;
  19. use Doctrine\Common\Collections\Collection;
  20. use Doctrine\ORM\Mapping as ORM;
  21. use Gedmo\Mapping\Annotation as Gedmo;
  22. use Symfony\Component\HttpFoundation\File\File;
  23. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  24. /**
  25.  * @ORM\Table(name="vulco_learning_course", indexes={@ORM\Index(name="learning_course_site_idx", columns={"site"})})
  26.  * @ORM\Entity(repositoryClass=LearningCourseRepository::class)
  27.  * @Gedmo\SoftDeleteable(fieldName="removedAt", timeAware=false)
  28.  * @SiteAware(siteFieldName="site")
  29.  * @Vich\Uploadable
  30.  */
  31. class LearningCourse extends AbstractBase implements SiteInterface
  32. {
  33.     use ActiveTrait;
  34.     use DescriptionTrait;
  35.     use ImageFileTrait;
  36.     use ImageFileNameTrait;
  37.     use NameTrait;
  38.     use SiteTrait;
  39.     /**
  40.      * @ORM\ManyToMany(targetEntity="App\Entity\User")
  41.      * @ORM\JoinTable(name="vulco_learning_courses_users",
  42.      *     joinColumns={@ORM\JoinColumn(name="learning_course_id", referencedColumnName="id")},
  43.      *     inverseJoinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}
  44.      * )
  45.      */
  46.     private ?Collection $associatedUsers;
  47.     /**
  48.      * @ORM\ManyToOne(targetEntity="LearningCourseCategory", inversedBy="courses")
  49.      */
  50.     private ?LearningCourseCategory $category null;
  51.     /**
  52.      * @ORM\OneToMany(targetEntity="LearningCourseRegistration", mappedBy="course", cascade={"persist", "remove"})
  53.      */
  54.     private ?Collection $registrations// order here is important because cascade remove will remove registrations before sessions
  55.     /**
  56.      * @ORM\OneToMany(targetEntity="LearningCourseSession", mappedBy="course", cascade={"persist", "remove"})
  57.      * @ORM\OrderBy({"date": "ASC"})
  58.      */
  59.     private ?Collection $sessions;
  60.     /**
  61.      * @ORM\Column(type="string", length=255, nullable=false)
  62.      */
  63.     private string $name;
  64.     /**
  65.      * @ORM\Column(type="text", length=10000, nullable=true)
  66.      */
  67.     private ?string $description;
  68.     /**
  69.      * @Vich\UploadableField(mapping="learning_course_image", fileNameProperty="imageFileName")
  70.      */
  71.     private ?File $imageFile null;
  72.     /**
  73.      * @ORM\Column(type="string", length=255, nullable=true)
  74.      */
  75.     private ?string $imageFileName null;
  76.     /**
  77.      * @ORM\Column(type="integer", nullable=false, options={"default": 0})
  78.      */
  79.     private int $type 0;
  80.     /**
  81.      * @ORM\Column(type="integer", nullable=false, options={"default": 0})
  82.      */
  83.     private int $status 0;
  84.     /**
  85.      * @ORM\Column(type="string", length=255, nullable=true)
  86.      */
  87.     private ?string $totalHours '';
  88.     /**
  89.      * @ORM\Column(type="integer", nullable=false, options={"default": 0})
  90.      */
  91.     private int $maximumCapacity 0;
  92.     /**
  93.      * @ORM\Column(type="datetime", nullable=true)
  94.      */
  95.     private ?DateTimeInterface $endRegistrationPeriod;
  96.     /**
  97.      * @ORM\Column(type="boolean", nullable=false, options={"default": 1})
  98.      */
  99.     private bool $active true;
  100.     public function __construct()
  101.     {
  102.         $this->associatedUsers = new ArrayCollection();
  103.         $this->sessions = new ArrayCollection();
  104.         $this->registrations = new ArrayCollection();
  105.     }
  106.     public function getAssociatedUsersAmount(): int
  107.     {
  108.         return $this->getAssociatedUsers()->count();
  109.     }
  110.     public function getAssociatedUsers(): Collection
  111.     {
  112.         return $this->associatedUsers;
  113.     }
  114.     public function setAssociatedUsers(?Collection $associatedUsers): self
  115.     {
  116.         $this->associatedUsers $associatedUsers;
  117.         return $this;
  118.     }
  119.     public function addAssociatedUser(User $associatedUser): self
  120.     {
  121.         if (!$this->associatedUsers->contains($associatedUser)) {
  122.             $this->associatedUsers->add($associatedUser);
  123.         }
  124.         return $this;
  125.     }
  126.     public function removeAssociatedUser(User $associatedUser): self
  127.     {
  128.         if ($this->associatedUsers->contains($associatedUser)) {
  129.             $this->associatedUsers->removeElement($associatedUser);
  130.         }
  131.         return $this;
  132.     }
  133.     public function getSessions(): Collection
  134.     {
  135.         return $this->sessions;
  136.     }
  137.     public function addSession(LearningCourseSession $session): self
  138.     {
  139.         if (!$this->getSessions()->contains($session)) {
  140.             $session->setCourse($this);
  141.             $this->sessions->add($session);
  142.         }
  143.         return $this;
  144.     }
  145.     public function removeSession(LearningCourseSession $session): self
  146.     {
  147.         if ($this->getSessions()->contains($session)) {
  148.             $this->sessions->removeElement($session);
  149.         }
  150.         return $this;
  151.     }
  152.     public function getSessionsAmount(): int
  153.     {
  154.         return $this->getSessions()->count();
  155.     }
  156.     public function getRegistrations(): Collection
  157.     {
  158.         return $this->registrations;
  159.     }
  160.     public function getRegistrationsAmount(): int
  161.     {
  162.         return $this->getRegistrations()->count();
  163.     }
  164.     public function addRegistration(LearningCourseRegistration $registration): self
  165.     {
  166.         if (!$this->getRegistrations()->contains($registration)) {
  167.             $registration->setCourse($this);
  168.             $this->getRegistrations()->add($registration);
  169.         }
  170.         return $this;
  171.     }
  172.     public function removeRegistration(LearningCourseRegistration $registration): self
  173.     {
  174.         if ($this->getRegistrations()->contains($registration)) {
  175.             $this->getRegistrations()->removeElement($registration);
  176.         }
  177.         return $this;
  178.     }
  179.     public function getCategory(): ?LearningCourseCategory
  180.     {
  181.         return $this->category;
  182.     }
  183.     public function getCategoryName(): string
  184.     {
  185.         return $this->getCategory() ? $this->getCategory()->getName() : MiniAbstractBase::DEFAULT_EMPTY_STRING;
  186.     }
  187.     public function setCategory(?LearningCourseCategory $category): self
  188.     {
  189.         $this->category $category;
  190.         return $this;
  191.     }
  192.     public function getType(): int
  193.     {
  194.         return $this->type;
  195.     }
  196.     public function getTypeAsTranlationKeyString(): string
  197.     {
  198.         return LearningCourseTypeEnum::getTranslatedEnumArray()[$this->getType()];
  199.     }
  200.     public function setType(int $type): self
  201.     {
  202.         $this->type $type;
  203.         return $this;
  204.     }
  205.     public function getStatus(): int
  206.     {
  207.         return $this->status;
  208.     }
  209.     public function setStatus(int $status): self
  210.     {
  211.         $this->status $status;
  212.         return $this;
  213.     }
  214.     public function getTotalHours(): ?string
  215.     {
  216.         return $this->totalHours;
  217.     }
  218.     public function setTotalHours(?string $totalHours): self
  219.     {
  220.         $this->totalHours $totalHours;
  221.         return $this;
  222.     }
  223.     public function getMaximumCapacity(): int
  224.     {
  225.         return $this->maximumCapacity;
  226.     }
  227.     public function getCurrentCapacity(): int
  228.     {
  229.         return $this->getRegistrationsAmount();
  230.     }
  231.     public function getAvailableRegistersAmount(): int
  232.     {
  233.         return $this->getMaximumCapacity() - $this->getRegistrationsAmount();
  234.     }
  235.     public function isFull(): bool
  236.     {
  237.         return $this->getAvailableRegistersAmount() <= 0;
  238.     }
  239.     public function setMaximumCapacity(int $maximumCapacity): self
  240.     {
  241.         $this->maximumCapacity $maximumCapacity;
  242.         return $this;
  243.     }
  244.     public function getEndRegistrationPeriod(): ?DateTimeInterface
  245.     {
  246.         return $this->endRegistrationPeriod;
  247.     }
  248.     public function isEndRegistrationPeriodExpired(): bool
  249.     {
  250.         return $this->getEndRegistrationPeriod() && (new DateTimeImmutable())->format('Y/m/d') > $this->getEndRegistrationPeriod()->format('Y/m/d');
  251.     }
  252.     public function getEndRegistrationPeriodAsString(): string
  253.     {
  254.         return $this->getDateAsString($this->endRegistrationPeriod);
  255.     }
  256.     public function setEndRegistrationPeriod(?DateTimeInterface $endRegistrationPeriod): self
  257.     {
  258.         $this->endRegistrationPeriod $endRegistrationPeriod;
  259.         return $this;
  260.     }
  261.     public function getCourseBeginDate(): ?DateTimeInterface
  262.     {
  263.         $firstDate null;
  264.         if ($this->getSessionsAmount() > 0) {
  265.             /** @var LearningCourseSession $session */
  266.             foreach ($this->getSessions() as $session) {
  267.                 if (is_null($firstDate)) {
  268.                     $firstDate $session->getDate();
  269.                 } elseif ($firstDate->format(MiniAbstractBase::DEFAULT_DATE_DATABASE_FORMAT) > $session->getDate()->format(MiniAbstractBase::DEFAULT_DATE_DATABASE_FORMAT)) {
  270.                     $firstDate $session->getDate();
  271.                 }
  272.             }
  273.         }
  274.         return $firstDate;
  275.     }
  276.     public function getCourseBeginDateAsString(): string
  277.     {
  278.         return $this->getDateAsString($this->getCourseBeginDate());
  279.     }
  280.     public function getCourseEndDate(): ?DateTimeInterface
  281.     {
  282.         $lastDate null;
  283.         if ($this->getSessionsAmount() > 0) {
  284.             /** @var LearningCourseSession $session */
  285.             foreach ($this->getSessions() as $session) {
  286.                 if (is_null($lastDate)) {
  287.                     $lastDate $session->getDate();
  288.                 } elseif ($lastDate->format(MiniAbstractBase::DEFAULT_DATE_DATABASE_FORMAT) <= $session->getDate()->format(MiniAbstractBase::DEFAULT_DATE_DATABASE_FORMAT)) {
  289.                     $lastDate $session->getDate();
  290.                 }
  291.             }
  292.         }
  293.         return $lastDate;
  294.     }
  295.     public function getCourseEndDateAsString(): string
  296.     {
  297.         return $this->getDateAsString($this->getCourseEndDate());
  298.     }
  299.     public function isExpired(): bool
  300.     {
  301.         return $this->getCourseEndDate() && (new DateTimeImmutable())->format('Y/m/d') > $this->getCourseEndDate()->format('Y/m/d');
  302.     }
  303. }