src/Entity/CourseUserEnrollment.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\CourseUserEnrollmentRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\Serializer\Annotation\Groups;
  8. #[ORM\Entity(repositoryClassCourseUserEnrollmentRepository::class)]
  9. class CourseUserEnrollment
  10. {
  11.     #[ORM\Id]
  12.     #[ORM\GeneratedValue]
  13.     #[ORM\Column(type'integer')]
  14.     private $id;
  15.     #[ORM\ManyToOne(targetEntityUser::class, inversedBy'courseUserEnrollments')]
  16.     #[Groups(['LogService'])]
  17.     private $user;
  18.     
  19.     #[ORM\Column(type'datetime_immutable'nullabletrue)]
  20.     private $createdAt;
  21.     #[ORM\Column(type'datetime_immutable'nullabletrue)]
  22.     #[Groups(['LogService'])]
  23.     private $enrolledAt;
  24.     #[ORM\Column(type'datetime_immutable'nullabletrue)]
  25.     #[Groups(['LogService'])]
  26.     private $dueAt;
  27.     #[ORM\Column(type'string'length255nullabletrue)]
  28.     #[Groups(['LogService'])]
  29.     private $status;
  30.     #[ORM\Column(type'string'length255)]
  31.     #[Groups(['LogService'])]
  32.     private $enrollType;
  33.     #[ORM\ManyToOne(targetEntityCourse::class, inversedBy'courseUserEnrollments')]
  34.     private $course;
  35.     #[ORM\OneToMany(targetEntityCourseQuizResult::class, mappedBy'enrollment'cascade: ['remove'])]
  36.     private $courseQuizResults;
  37.     #[ORM\Column(type'json'nullabletrue)]
  38.     private $attributes = [];
  39.     #[ORM\Column(type'datetime_immutable'nullabletrue)]
  40.     private $deletedAt;
  41.     #[ORM\Column(type'array'nullabletrue)]
  42.     private $reminder = [];
  43.     public function __construct()
  44.     {
  45.         $this->courseQuizResults = new ArrayCollection();
  46.     }
  47.     public function getId(): ?int
  48.     {
  49.         return $this->id;
  50.     }
  51.     public function getUser(): ?User
  52.     {
  53.         return $this->user;
  54.     }
  55.     public function setUser(?User $user): self
  56.     {
  57.         $this->user $user;
  58.         return $this;
  59.     }
  60.     public function getCreatedAt(): ?\DateTimeImmutable
  61.     {
  62.         return $this->createdAt;
  63.     }
  64.     public function setCreatedAt(\DateTimeImmutable $createdAt): self
  65.     {
  66.         $this->createdAt $createdAt;
  67.         return $this;
  68.     }
  69.     public function getEnrolledAt(): ?\DateTimeImmutable
  70.     {
  71.         return $this->enrolledAt;
  72.     }
  73.     public function setEnrolledAt(\DateTimeImmutable $enrolledAt): self
  74.     {
  75.         $this->enrolledAt $enrolledAt;
  76.         return $this;
  77.     }
  78.     public function getDueAt(): ?\DateTimeImmutable
  79.     {
  80.         return $this->dueAt;
  81.     }
  82.     public function setDueAt(?\DateTimeImmutable $dueAt): self
  83.     {
  84.         $this->dueAt $dueAt;
  85.         return $this;
  86.     }
  87.     public function getStatus(): ?string
  88.     {
  89.         return $this->status;
  90.     }
  91.     public function setStatus(?string $status): self
  92.     {
  93.         $this->status $status;
  94.         return $this;
  95.     }
  96.     public function getEnrollType(): ?string
  97.     {
  98.         return $this->enrollType;
  99.     }
  100.     public function setEnrollType(string $enrollType): self
  101.     {
  102.         $this->enrollType $enrollType;
  103.         return $this;
  104.     }
  105.     public function getCourse(): ?Course
  106.     {
  107.         return $this->course;
  108.     }
  109.     public function setCourse(?Course $course): self
  110.     {
  111.         $this->course $course;
  112.         return $this;
  113.     }
  114.     /**
  115.      * @return Collection|CourseQuizResult[]
  116.      */
  117.     public function getCourseQuizResults(): Collection
  118.     {
  119.         // return $this->courseQuizResults;
  120.         return $this->courseQuizResults->filter(function (CourseQuizResult $quizResult) {
  121.             return !$quizResult->isDeleted();
  122.         });
  123.     }
  124.     // /**
  125.     //  * Get the courseQuizResults with status 'failed'.
  126.     //  *
  127.     //  * @return Collection|CourseQuizResult[]
  128.     //  */
  129.     // public function getFailedCourseQuizResults(): Collection
  130.     // {
  131.     //     return $this->courseQuizResults->filter(function (CourseQuizResult $quizResult) {
  132.     //         return $quizResult->getStatus() === 'failed' && !$quizResult->isDeleted();
  133.     //     });
  134.     // }
  135.     /**
  136.      * Get the count of courseQuizResults with status 'failed'.
  137.      *
  138.      * @return int
  139.      */
  140.     public function getFailedCourseQuizResultsCount(): int
  141.     {
  142.         return $this->courseQuizResults->filter(function (CourseQuizResult $quizResult) {
  143.             return $quizResult->getStatus() === 'failed' && !$quizResult->isDeleted();
  144.         })->count();
  145.     }
  146.     public function addCourseQuizResult(CourseQuizResult $courseQuizResult): self
  147.     {
  148.         if (!$this->courseQuizResults->contains($courseQuizResult)) {
  149.             $this->courseQuizResults[] = $courseQuizResult;
  150.             $courseQuizResult->setEnrollment($this);
  151.         }
  152.         return $this;
  153.     }
  154.     public function removeCourseQuizResult(CourseQuizResult $courseQuizResult): self
  155.     {
  156.         /*
  157.         if ($this->courseQuizResults->removeElement($courseQuizResult)) {
  158.             // set the owning side to null (unless already changed)
  159.             if ($courseQuizResult->getEnrollment() === $this) {
  160.                 $courseQuizResult->setEnrollment(null);
  161.             }
  162.         }
  163.         */
  164.         if (!$courseQuizResult->isDeleted()) {
  165.             $courseQuizResult->setDeletedAt(new \DateTimeImmutable());
  166.         }
  167.         return $this;
  168.     }
  169.     /**
  170.      * Get the latest course quiz result for this enrollment.
  171.      *
  172.      * @return CourseQuizResult|null The latest course quiz result or null if none found.
  173.      */
  174.     public function getLatestCourseQuizResult($status null): ?CourseQuizResult
  175.     {
  176.         $latestQuizResult null;
  177.         $latestCreatedAt null;
  178.         /*
  179.         foreach ($this->courseQuizResults as $quizResult) {
  180.             $quizResultCreatedAt = $quizResult->getCreatedAt();
  181.             if (!$latestCreatedAt || $quizResultCreatedAt > $latestCreatedAt) {
  182.                 $latestQuizResult = $quizResult;
  183.                 $latestCreatedAt = $quizResultCreatedAt;
  184.             }
  185.         }
  186.         */
  187.         foreach ($this->courseQuizResults as $quizResult) {
  188.             if (!$quizResult->isDeleted() && ($status === null || $quizResult->getStatus() == $status)) {
  189.                 $quizResultCreatedAt $quizResult->getCreatedAt();
  190.                 
  191.                 if (!$latestCreatedAt || $quizResultCreatedAt $latestCreatedAt) {
  192.                     $latestQuizResult $quizResult;
  193.                     $latestCreatedAt $quizResultCreatedAt;
  194.                 }
  195.             }
  196.         }
  197.         return $latestQuizResult;
  198.     }
  199.     public function getAttributes(): ?array
  200.     {
  201.         return $this->attributes;
  202.     }
  203.     public function setAttributes(?array $attributes): self
  204.     {
  205.         $this->attributes $attributes;
  206.         return $this;
  207.     }
  208.     public function getDeletedAt(): ?\DateTimeImmutable
  209.     {
  210.         return $this->deletedAt;
  211.     }
  212.     /**
  213.      * Check if the CourseQuizResult has been soft deleted.
  214.      *
  215.      * @return bool
  216.      */
  217.     public function isDeleted(): bool
  218.     {   
  219.         return $this->deletedAt !== null;
  220.     }
  221.     public function setDeletedAt(?\DateTimeImmutable $deletedAt): self
  222.     {
  223.         $this->deletedAt $deletedAt;
  224.         return $this;
  225.     }
  226.     public function getReminder(): ?array
  227.     {
  228.         return $this->reminder;
  229.     }
  230.     public function setReminder(?array $reminder): self
  231.     {
  232.         $this->reminder $reminder;
  233.         return $this;
  234.     }
  235. }