<?php
namespace App\Entity;
use App\Repository\CourseRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
#[ORM\Entity(repositoryClass: CourseRepository::class)]
class Course
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;
#[ORM\Column(type: 'string', length: 255)]
#[Groups(['LogService'])]
private $title;
#[ORM\Column(type: 'text', nullable: true)]
private $description;
#[ORM\Column(type: 'text', nullable: true)]
private $excerpt;
#[ORM\Column(type: 'integer', nullable: true)]
private $duration;
#[ORM\Column(type: 'integer', nullable: true)]
private $dueDay;
#[ORM\Column(type: 'integer', nullable: true)]
#[Groups(['LogService'])]
private $minScore;
#[ORM\Column(type: 'boolean', nullable: true)]
private $randomizeOrder = false;
#[ORM\Column(type: 'boolean', nullable: true)]
private $randomizeQuestion;
#[ORM\Column(type: 'boolean', nullable: true)]
private $isOnboarding;
#[ORM\Column(type: 'boolean', nullable: true)]
private $needApproval;
// #[ORM\Column(type: 'boolean')]
// private $bypass = 1;
#[ORM\Column(type: 'integer', nullable: true)]
private $retryInterval = 0;
#[ORM\ManyToMany(targetEntity: Department::class, inversedBy: 'courses')]
private $departments;
#[ORM\ManyToMany(targetEntity: Office::class, inversedBy: 'courses')]
private $offices;
#[ORM\Column(type: 'text', nullable: true)]
#[Groups(['LogService'])]
private $type;
#[ORM\Column(type: 'boolean', nullable: true)]
private $isRequired;
#[ORM\Column(type: 'array', nullable: true)]
private $roles = [];
#[ORM\Column(type: 'integer', nullable: true)]
private $quizTimer;
#[ORM\OneToMany(targetEntity: CourseContent::class, mappedBy: 'course')]
private $courseContents;
#[ORM\Column(type: 'string', length: 16, nullable: true)]
private $level;
#[ORM\OneToMany(targetEntity: CourseAutoEnrollment::class, mappedBy: 'course')]
private $courseAutoEnrollments;
#[ORM\Column(type: 'datetime_immutable')]
private $createdAt;
#[ORM\ManyToOne(targetEntity: User::class)]
private $createdBy;
#[ORM\Column(type: 'datetime_immutable', nullable: true)]
#[Groups(['LogService'])]
private $updatedAt;
#[ORM\Column(type: 'datetime_immutable', nullable: true)]
private $deletedAt;
#[ORM\OneToMany(targetEntity: CourseQuizQuestion::class, mappedBy: 'course')]
private $courseQuizQuestions;
#[ORM\OneToMany(targetEntity: CourseUserEnrollment::class, mappedBy: 'course')]
private $courseUserEnrollments;
#[ORM\Column(type: 'boolean', options: ['default' => false])]
private $isPublished;
public function __construct()
{
$this->departments = new ArrayCollection();
$this->offices = new ArrayCollection();
$this->courseContents = new ArrayCollection();
$this->courseAutoEnrollments = new ArrayCollection();
$this->courseQuizQuestions = new ArrayCollection();
$this->courseUserEnrollments = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getExcerpt(): ?string
{
return $this->excerpt;
}
public function setExcerpt(?string $excerpt): self
{
$this->excerpt = $excerpt;
return $this;
}
public function getDuration(): ?int
{
return $this->duration;
}
public function setDuration(?int $duration): self
{
$this->duration = $duration;
return $this;
}
public function getDueDay(): ?int
{
return $this->dueDay;
}
public function setDueDay(?int $dueDay): self
{
$this->dueDay = $dueDay;
return $this;
}
public function getMinScore(): ?int
{
return $this->minScore;
}
public function setMinScore(?int $minScore): self
{
$this->minScore = $minScore;
return $this;
}
public function getRandomizeOrder(): ?int
{
return $this->randomizeOrder;
}
public function setRandomizeOrder(?int $randomizeOrder): self
{
$this->randomizeOrder = $randomizeOrder;
return $this;
}
public function getRandomizeQuestion(): ?int
{
return $this->randomizeQuestion;
}
public function setRandomizeQuestion(?int $randomizeQuestion): self
{
$this->randomizeQuestion = $randomizeQuestion;
return $this;
}
public function getNeedApproval(): ?int
{
return $this->needApproval;
}
public function setNeedApproval(?int $needApproval): self
{
$this->needApproval = $needApproval;
return $this;
}
/*
public function getBypass(): ?int
{
return $this->bypass;
}
public function setBypass(?int $bypass): self
{
$this->bypass = $bypass;
return $this;
}
*/
public function getIsOnboarding(): ?int
{
return $this->isOnboarding;
}
public function setIsOnboarding(?int $isOnboarding): self
{
$this->isOnboarding = $isOnboarding;
return $this;
}
public function getRetryInterval(): ?int
{
return $this->retryInterval;
}
public function setRetryInterval(?int $retryInterval): self
{
$this->retryInterval = $retryInterval;
return $this;
}
/**
* @return Collection|Department[]
*/
public function getDepartments(): Collection
{
return $this->departments;
}
public function addDepartment(Department $department): self
{
if (!$this->departments->contains($department)) {
$this->departments[] = $department;
}
return $this;
}
public function removeDepartment(Department $department): self
{
$this->departments->removeElement($department);
return $this;
}
/**
* @return Collection|Office[]
*/
public function getOffices(): Collection
{
return $this->offices;
}
public function addOffice(Office $office): self
{
if (!$this->offices->contains($office)) {
$this->offices[] = $office;
}
return $this;
}
public function removeOffice(Office $office): self
{
$this->offices->removeElement($office);
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(?string $type): self
{
$this->type = $type;
return $this;
}
public function getIsRequired(): ?bool
{
return $this->isRequired;
}
public function setIsRequired(?bool $isRequired): self
{
$this->isRequired = $isRequired;
return $this;
}
public function getRoles(): ?array
{
return $this->roles;
}
public function setRoles(?array $roles): self
{
$this->roles = $roles;
return $this;
}
public function getQuizTimer(): ?int
{
return $this->quizTimer;
}
public function setQuizTimer(?int $quizTimer): self
{
$this->quizTimer = $quizTimer;
return $this;
}
/**
* @return Collection|CourseContent[]
*/
public function getCourseContent(): Collection
{
return $this->courseContents;
}
public function getLevel(): ?string
{
return $this->level;
}
public function setLevel(?string $level): self
{
$this->level = $level;
return $this;
}
public function addCreatedAt(CourseContent $courseContent): self
{
if (!$this->courseContents->contains($courseContent)) {
$this->courseContents[] = $courseContent;
$courseContent->setCourse($this);
}
return $this;
}
public function removeCreatedAt(CourseContent $courseContent): self
{
if ($this->courseContents->removeElement($courseContent)) {
// set the owning side to null (unless already changed)
if ($courseContent->getCourse() === $this) {
$courseContent->setCourse(null);
}
}
return $this;
}
/**
* @return Collection|CourseAutoEnrollment[]
*/
public function getCourseAutoEnrollments(): Collection
{
return $this->courseAutoEnrollments;
}
public function addCourseAutoEnrollment(CourseAutoEnrollment $CourseAutoEnrollment): self
{
if (!$this->courseAutoEnrollments->contains($CourseAutoEnrollment)) {
$this->courseAutoEnrollments[] = $CourseAutoEnrollment;
$CourseAutoEnrollment->setCourse($this);
}
return $this;
}
public function removeCourseAutoEnrollment(CourseAutoEnrollment $CourseAutoEnrollment): self
{
if ($this->courseAutoEnrollments->removeElement($CourseAutoEnrollment)) {
// set the owning side to null (unless already changed)
if ($CourseAutoEnrollment->getCourse() === $this) {
$CourseAutoEnrollment->setCourse(null);
}
}
return $this;
}
/**
* Get the latest CourseAutoEnrollment associated with this Course entity.
*
* @return CourseAutoEnrollment|null The latest CourseAutoEnrollment or null if none exists.
*/
public function getLatestAutoEnrollment(): ?CourseAutoEnrollment
{
$latestAutoEnrollment = null;
// Sort the course auto enrollments by createdAt in descending order
$sortedEnrollments = $this->getCourseAutoEnrollments()->toArray();
usort($sortedEnrollments, function (CourseAutoEnrollment $a, CourseAutoEnrollment $b) {
return $b->getCreatedAt() <=> $a->getCreatedAt();
});
// Get the first element from the sorted array (latest enrollment)
if (!empty($sortedEnrollments)) {
$latestAutoEnrollment = reset($sortedEnrollments);
}
return $latestAutoEnrollment;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getCreatedBy(): ?User
{
return $this->createdBy;
}
public function setCreatedBy(?User $createdBy): self
{
$this->createdBy = $createdBy;
return $this;
}
public function getUpdatedAt(): ?\DateTimeImmutable
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeImmutable $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getDeletedAt(): ?\DateTimeImmutable
{
return $this->deletedAt;
}
public function setDeletedAt(?\DateTimeImmutable $deletedAt): self
{
$this->deletedAt = $deletedAt;
return $this;
}
/**
* @return Collection|CourseQuizQuestion[]
*/
public function getCourseQuizQuestions(): Collection
{
return $this->courseQuizQuestions;
}
public function addCourseQuizQuestion(CourseQuizQuestion $courseQuizQuestion): self
{
if (!$this->courseQuizQuestions->contains($courseQuizQuestion)) {
$this->courseQuizQuestions[] = $courseQuizQuestion;
$courseQuizQuestion->setCourse($this);
}
return $this;
}
public function removeCourseQuizQuestion(CourseQuizQuestion $courseQuizQuestion): self
{
if ($this->courseQuizQuestions->removeElement($courseQuizQuestion)) {
// set the owning side to null (unless already changed)
if ($courseQuizQuestion->getCourse() === $this) {
$courseQuizQuestion->setCourse(null);
}
}
return $this;
}
/**
* @return Collection|CourseUserEnrollment[]
*/
public function getCourseUserEnrollments(): Collection
{
// return $this->courseUserEnrollments;
return $this->courseUserEnrollments->filter(function (CourseUserEnrollment $enrollment) {
return !$enrollment->isDeleted();
});
}
public function addCourseUserEnrollment(CourseUserEnrollment $courseUserEnrollment): self
{
if (!$this->courseUserEnrollments->contains($courseUserEnrollment)) {
$this->courseUserEnrollments[] = $courseUserEnrollment;
$courseUserEnrollment->setCourse($this);
}
return $this;
}
public function removeCourseUserEnrollment(CourseUserEnrollment $courseUserEnrollment): self
{
if ($this->courseUserEnrollments->removeElement($courseUserEnrollment)) {
// set the owning side to null (unless already changed)
if ($courseUserEnrollment->getCourse() === $this) {
$courseUserEnrollment->setCourse(null);
}
}
return $this;
}
public function getCourseReady(): bool
{
if ($this->getCourseContent()->isEmpty()) {
return false;
}
if ($this->getCourseQuizQuestions()->isEmpty()) {
return false;
}
return true;
}
public function getIsPublished(): ?int
{
return $this->isPublished;
}
public function setIsPublished(?int $isPublished): self
{
$this->isPublished = $isPublished;
return $this;
}
}