<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use App\Repository\ElLessionRepository;
use ApiPlatform\Core\Annotation\ApiFilter;
use App\Entity\Traits\TimestampableEntity;
use Doctrine\Common\Collections\Collection;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Entity\Interfaces\OrderMappedInterface;
use Doctrine\Common\Collections\ArrayCollection;
use App\Entity\Interfaces\ElCopyRestrictInterface;
use App\Entity\Interfaces\ContainerMappedInterface;
use Symfony\Component\Serializer\Annotation\Groups;
use App\Entity\Interfaces\OperationLogMappedInterface;
use Symfony\Component\Validator\Constraints as Assert;
use App\Entity\Interfaces\ElCourseIndexUpdateInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslatable;
use Locastic\ApiPlatformTranslationBundle\Model\TranslationInterface;
/**
* @ApiResource(
* attributes={"security"="is_granted('ROLE_OPERATOR')", "filters"={"translation.groups"}},
* normalizationContext={"groups"={"ElLession:Read"}, "skip_null_values"=false},
* denormalizationContext={"groups"={"ElLession:Write"}},
* collectionOperations={
* "get",
* "get_for_timeline"={
* "path"="/el_lessions/get-for-timeline",
* "method"="GET",
* "normalization_context"={"groups"={"ElLession:Timeline"}, "skip_null_values"=false},
* "security"="is_granted('IS_AUTHENTICATED_FULLY', object)"
* },
* "get_for_copy"={
* "path"="/el_lessions/get-for-copy",
* "method"="GET",
* "normalization_context"={"groups"={"ElLession:Select"}, "skip_null_values"=false},
* "pagination_enabled"=false
* },
* "post"={
* "security"="is_granted('ROLE_OPERATOR') or is_granted('ROLE_INSTRUCTOR')"
* }
* },
* itemOperations={
* "get"={"security"="is_granted('IS_CO_OPR', object) or is_granted('IS_CO_INS_SPE_COURSE', object.elCourse)"},
* "get_detail"={
* "path"="/el_lessions/{id}/get-detail",
* "method"="GET",
* "normalization_context"={"groups"={"ElLession:Read", "ElLession:Detail"}, "skip_null_values"=false, "enable_max_depth"=true},
* "security"="is_granted('IS_AUTHENTICATED_FULLY', object)"
* },
* "put"={"security"="is_granted('IS_CO_OPR', object) or is_granted('IS_CO_INS_SPE_COURSE', object.elCourse)"},
* "patch"={"security"="is_granted('IS_CO_OPR', object) or is_granted('IS_CO_INS_SPE_COURSE', object.elCourse)"},
* "patch_change_ord"={
* "path"="/el_lessions/{id}/change-ord",
* "method"="PATCH",
* "denormalization_context"={"groups"={"ElLession:ChangeOrd"}},
* "security"="is_granted('IS_CO_OPR', object) or is_granted('IS_CO_INS_SPE_COURSE', object.elCourse)"
* },
* "delete"={"security"="is_granted('IS_CO_OPR', object) or is_granted('IS_CO_INS_SPE_COURSE', object.elCourse)"}
* }
* )
* @ApiFilter(SearchFilter::class, properties={"id": "exact", "container.id": "exact", "elCourse.id": "exact", "translations.name": "partial"})
* @ApiFilter(OrderFilter::class, properties={"ord": "ASC", "translations.name", "id"})
* @ORM\Entity(repositoryClass=ElLessionRepository::class)
*/
class ElLession extends AbstractTranslatable implements ContainerMappedInterface, OrderMappedInterface, ElCourseIndexUpdateInterface, OperationLogMappedInterface, ElCopyRestrictInterface
{
/**
* Hook timestampable behavior
* updates createdAt, updatedAt fields
*/
use TimestampableEntity;
/**
* @ORM\OneToMany(targetEntity="ElLessionTranslation", mappedBy="translatable", fetch="EXTRA_LAZY", indexBy="locale", cascade={"PERSIST"}, orphanRemoval=true)
*
* @Groups({"ElLession:Write", "ElLessionTranslationGroup"})
* @Assert\Valid()
*/
protected $translations;
/**
* @Groups({"ElLession:Read", "ElLession:Select", "ElLession:Timeline", "ElCourse:Detail", "ElExam:Read", "ElTask:Read", "ElLessionTopic:Read", "ElCourse:PIRead"})
*/
private $name;
/**
* @Groups({"ElLession:Read", "ElCourse:Detail", "ElTask:Read", "ElCourse:PIRead"})
*/
private $description;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"ElLession:Read", "ElLession:Select", "ElLession:Timeline", "ElCourse:Detail", "ElExam:Read", "ElTask:Read", "ElLessionTopic:Read", "ElCourse:PIRead"})
*/
private $id;
/**
* @ORM\Column(type="integer", nullable=true)
* @Groups({"ElLession:Read", "ElLession:Timeline", "ElLession:Write", "ElCourse:Detail", "ElTask:Read", "ElCourse:PIRead"})
*/
private $ord;
/**
* @ORM\ManyToOne(targetEntity=Container::class)
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
*
* @Groups({"ElLession:Write"})
*/
private $container;
/**
* @ORM\ManyToOne(targetEntity=ElCourse::class, inversedBy="elLessions")
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
*
* @Groups({"ElLession:Read", "ElLession:Write"})
*/
public $elCourse;
/**
* @ORM\OneToMany(targetEntity=ElLessionTopic::class, mappedBy="elLession", orphanRemoval=true)
* @Groups({"ElLession:Detail"})
*/
private $elLessionTopics;
/**
* @ORM\OneToMany(targetEntity=ElTask::class, mappedBy="elLession", orphanRemoval=true)
* @Groups({"ElLession:Detail"})
*/
private $elTasks;
/**
* @ORM\OneToMany(targetEntity=ElExam::class, mappedBy="elLession")
* @Groups({"ElLession:Read", "ElLession:Detail"})
*/
private $elExams;
/**
* @ORM\Column(type="string", length=16, nullable=true)
* @Groups({"ElLession:Read", "ElLession:Timeline", "ElCourse:Detail", "ElLession:Write", "ElTask:Read", "ElCourse:PIRead"})
*/
private $color;
/**
* @Groups({"ElLession:ChangeOrd"})
*/
private int $newOrd;
private $ordChangeDirection;
public function __construct()
{
parent::__construct();
$this->elLessionTopics = new ArrayCollection();
$this->elTasks = new ArrayCollection();
$this->elExams = new ArrayCollection();
}
protected function createTranslation(): TranslationInterface
{
return new ElLessionTranslation();
}
public function getName(): ?string
{
return $this->getTranslation()->getName();
}
public function setName(string $name): self
{
$this->getTranslation()->setName($name);
return $this;
}
public function getDescription(): ?string
{
return $this->getTranslation()->getDescription();
}
public function setDescription(string $description): self
{
$this->getTranslation()->setDescription($description);
return $this;
}
public function getId(): ?int
{
return $this->id;
}
public function getOrd(): ?int
{
return $this->ord;
}
public function setOrd(?int $ord): self
{
$this->ord = $ord;
return $this;
}
public function getOrdParents(): array
{
return [
'container' => $this->getContainer(),
'elCourse' => $this->getElCourse()
];
}
public function getOrdChangeDirection()
{
return $this->ordChangeDirection;
}
public function getContainer(): ?Container
{
return $this->container;
}
public function setContainer(?Container $container): self
{
$this->container = $container;
return $this;
}
public function getElCourse(): ?ElCourse
{
return $this->elCourse;
}
public function setElCourse(?ElCourse $elCourse): self
{
$this->elCourse = $elCourse;
return $this;
}
public function getElLession(): ?ElLession
{
return $this->elLession;
}
public function setElLession(?ElLession $elLession): self
{
$this->elLession = $elLession;
return $this;
}
public function getElLessionTopics()
{
return $this->elLessionTopics;
}
public function addElLessionTopic(ElLessionTopic $elLessionTopics): self
{
if (!$this->elLessionTopics->contains($elLessionTopics)) {
$this->elLessionTopics[] = $elLessionTopics;
$elLessionTopics->setElLession($this);
}
return $this;
}
public function removeElLessionTopic(ElLessionTopic $elLessionTopics): self
{
if ($this->elLessions->removeElement($elLessionTopics)) {
// set the owning side to null (unless already changed)
if ($elLessionTopics->getElLession() === $this) {
$elLessionTopics->setElLession(null);
}
}
return $this;
}
public function getElTasks($taskLevel = null)
{
if ($taskLevel) {
$elTasks = [];
foreach ($this->getElTasks() as $task) {
if ($task->getTaskLevel() === $taskLevel) {
$elTasks[] = $task;
}
}
return $elTasks;
}
return $this->elTasks;
}
public function addElTask(ElTask $elTask): self
{
if (!$this->elTasks->contains($elTask)) {
$this->elTasks[] = $elTask;
$elTask->setElLession($this);
}
return $this;
}
public function removeElTask(ElTask $elTask): self
{
if ($this->elTasks->removeElement($elTask)) {
// set the owning side to null (unless already changed)
if ($elTask->getElLession() === $this) {
$elTask->setElLession(null);
}
}
return $this;
}
/**
* @Groups({"ElLession:Detail"})
*/
public function getTotalElLessionTopics()
{
return (is_array($this->elLessionTopics)) ?? count($this->elLessionTopics);
}
/**
* @Groups({"ElLession:Detail"})
*/
public function getTotalElTasks()
{
return (is_array($this->elTasks)) ?? count($this->elTasks);
}
/**
* @return Collection|ElExam[]
*/
public function getElExams(): Collection
{
return $this->elExams;
}
public function addElExam(ElExam $elExam): self
{
if (!$this->elExams->contains($elExam)) {
$this->elExams[] = $elExam;
$elExam->setElLession($this);
}
return $this;
}
public function removeElExam(ElExam $elExam): self
{
if ($this->elExams->removeElement($elExam)) {
// set the owning side to null (unless already changed)
if ($elExam->getElLession() === $this) {
$elExam->setElLession(null);
}
}
return $this;
}
public function getColor(): ?string
{
return $this->color;
}
public function setColor(?string $color): self
{
$this->color = $color;
return $this;
}
public function getNewOrd(): int
{
return $this->newOrd ?? 0;
}
public function setNewOrd(int $newOrd): self
{
$oldOrd = $this->ord;
$this->newOrd = $newOrd;
$this->ord = $newOrd;
$this->ordChangeDirection = ($this->newOrd < $oldOrd) ? 'desc' : 'asc';
return $this;
}
/**
* @Groups({"ElLession:Timeline"})
*/
public function getMinDate()
{
$minDate = null;
foreach ($this->elTasks as $task) {
if ($minDate === null || $minDate > $task->getStart()) {
$minDate = $task->getStart();
}
}
return $minDate;
}
/**
* @Groups({"ElLession:Timeline"})
*/
public function getMaxDate()
{
$maxDate = null;
foreach ($this->elTasks as $task) {
if ($maxDate === null || $maxDate < $task->getEnd()) {
$maxDate = $task->getEnd();
}
}
return $maxDate;
}
public function getMetadataTranslation()
{
$translations = [];
foreach ($this->getTranslations() as $translation) {
$translations[] = [
$translation->getLocale() => [
'name' => $translation->getName(),
'description' => $translation->getDescription()
]
];
}
return $translations;
}
public function getParentCourse()
{
return $this->elCourse;
}
public function getForIndex()
{
$translations = [];
foreach ($this->getTranslations() as $translation) {
$translations[] = [
$translation->getLocale() => [
'name' => $translation->getName()
]
];
}
$elLessionTopics = [];
foreach ($this->getElLessionTopics() as $topic) {
$elLessionTopics[] = $topic->getForIndex();
}
$elExams = [];
foreach ($this->getElExams() as $exam) {
$elExams[] = $exam->getForIndex();
}
$elTasks = [];
foreach ($this->getElTasks() as $task) {
if ($task->getTaskLevel() === ElTask::TASKLEVEL_LESSION) {
$elTasks[] = $task->getForIndex();
}
}
return [
'id' => $this->getId(),
'ord' => $this->getOrd(),
'color' => $this->getColor(),
'elCourse' => $this->getElCourse()->getId(),
'translations' => $translations,
'elLessionTopics' => $elLessionTopics,
'elExams' => $elExams,
'elTasks' => $elTasks
];
}
}