src/Entity/ElLession.php line 70

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use App\Repository\ElLessionRepository;
  5. use ApiPlatform\Core\Annotation\ApiFilter;
  6. use App\Entity\Traits\TimestampableEntity;
  7. use Doctrine\Common\Collections\Collection;
  8. use ApiPlatform\Core\Annotation\ApiResource;
  9. use App\Entity\Interfaces\OrderMappedInterface;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use App\Entity\Interfaces\ElCopyRestrictInterface;
  12. use App\Entity\Interfaces\ContainerMappedInterface;
  13. use Symfony\Component\Serializer\Annotation\Groups;
  14. use App\Entity\Interfaces\OperationLogMappedInterface;
  15. use Symfony\Component\Validator\Constraints as Assert;
  16. use App\Entity\Interfaces\ElCourseIndexUpdateInterface;
  17. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  18. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  19. use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslatable;
  20. use Locastic\ApiPlatformTranslationBundle\Model\TranslationInterface;
  21. /**
  22.  * @ApiResource(
  23.  *      attributes={"security"="is_granted('ROLE_OPERATOR')", "filters"={"translation.groups"}},
  24.  *      normalizationContext={"groups"={"ElLession:Read"}, "skip_null_values"=false},
  25.  *      denormalizationContext={"groups"={"ElLession:Write"}},
  26.  *      collectionOperations={
  27.  *          "get",
  28.  *          "get_for_timeline"={
  29.  *              "path"="/el_lessions/get-for-timeline",
  30.  *              "method"="GET",
  31.  *              "normalization_context"={"groups"={"ElLession:Timeline"}, "skip_null_values"=false},
  32.  *              "security"="is_granted('IS_AUTHENTICATED_FULLY', object)"
  33.  *          },
  34.  *          "get_for_copy"={
  35.  *              "path"="/el_lessions/get-for-copy",
  36.  *              "method"="GET",
  37.  *              "normalization_context"={"groups"={"ElLession:Select"}, "skip_null_values"=false},
  38.  *              "pagination_enabled"=false
  39.  *          },
  40.  *          "post"={
  41.  *              "security"="is_granted('ROLE_OPERATOR') or is_granted('ROLE_INSTRUCTOR')"
  42.  *          }
  43.  *      },
  44.  *      itemOperations={
  45.  *          "get"={"security"="is_granted('IS_CO_OPR', object) or is_granted('IS_CO_INS_SPE_COURSE', object.elCourse)"},
  46.  *          "get_detail"={
  47.  *              "path"="/el_lessions/{id}/get-detail",
  48.  *              "method"="GET",
  49.  *              "normalization_context"={"groups"={"ElLession:Read", "ElLession:Detail"}, "skip_null_values"=false, "enable_max_depth"=true},
  50.  *              "security"="is_granted('IS_AUTHENTICATED_FULLY', object)"
  51.  *          },
  52.  *          "put"={"security"="is_granted('IS_CO_OPR', object) or is_granted('IS_CO_INS_SPE_COURSE', object.elCourse)"},
  53.  *          "patch"={"security"="is_granted('IS_CO_OPR', object) or is_granted('IS_CO_INS_SPE_COURSE', object.elCourse)"},
  54.  *          "patch_change_ord"={
  55.  *              "path"="/el_lessions/{id}/change-ord",
  56.  *              "method"="PATCH",
  57.  *              "denormalization_context"={"groups"={"ElLession:ChangeOrd"}},
  58.  *              "security"="is_granted('IS_CO_OPR', object) or is_granted('IS_CO_INS_SPE_COURSE', object.elCourse)"
  59.  *          },
  60.  *          "delete"={"security"="is_granted('IS_CO_OPR', object) or is_granted('IS_CO_INS_SPE_COURSE', object.elCourse)"}
  61.  *     }
  62.  * )
  63.  * @ApiFilter(SearchFilter::class, properties={"id": "exact", "container.id": "exact", "elCourse.id": "exact", "translations.name": "partial"})
  64.  * @ApiFilter(OrderFilter::class, properties={"ord": "ASC", "translations.name", "id"})
  65.  * @ORM\Entity(repositoryClass=ElLessionRepository::class)
  66.  */
  67. class ElLession extends AbstractTranslatable implements ContainerMappedInterfaceOrderMappedInterfaceElCourseIndexUpdateInterfaceOperationLogMappedInterfaceElCopyRestrictInterface
  68. {
  69.     /**
  70.      * Hook timestampable behavior
  71.      * updates createdAt, updatedAt fields
  72.      */
  73.     use TimestampableEntity;
  74.     /**
  75.      * @ORM\OneToMany(targetEntity="ElLessionTranslation", mappedBy="translatable", fetch="EXTRA_LAZY", indexBy="locale", cascade={"PERSIST"}, orphanRemoval=true)
  76.      *
  77.      * @Groups({"ElLession:Write", "ElLessionTranslationGroup"})
  78.      * @Assert\Valid()
  79.      */
  80.     protected $translations;
  81.     /**
  82.      * @Groups({"ElLession:Read", "ElLession:Select", "ElLession:Timeline", "ElCourse:Detail", "ElExam:Read", "ElTask:Read", "ElLessionTopic:Read", "ElCourse:PIRead"})
  83.      */
  84.     private $name;
  85.     /**
  86.      * @Groups({"ElLession:Read", "ElCourse:Detail", "ElTask:Read", "ElCourse:PIRead"})
  87.      */
  88.     private $description;
  89.     /**
  90.      * @ORM\Id
  91.      * @ORM\GeneratedValue
  92.      * @ORM\Column(type="integer")
  93.      * @Groups({"ElLession:Read", "ElLession:Select", "ElLession:Timeline", "ElCourse:Detail", "ElExam:Read", "ElTask:Read", "ElLessionTopic:Read", "ElCourse:PIRead"})
  94.      */
  95.     private $id;
  96.     /**
  97.      * @ORM\Column(type="integer", nullable=true)
  98.      * @Groups({"ElLession:Read", "ElLession:Timeline", "ElLession:Write", "ElCourse:Detail", "ElTask:Read", "ElCourse:PIRead"})
  99.      */
  100.     private $ord;
  101.     /**
  102.      * @ORM\ManyToOne(targetEntity=Container::class)
  103.      * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
  104.      *
  105.      * @Groups({"ElLession:Write"})
  106.      */
  107.     private $container;
  108.     /**
  109.      * @ORM\ManyToOne(targetEntity=ElCourse::class, inversedBy="elLessions")
  110.      * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
  111.      *
  112.      * @Groups({"ElLession:Read", "ElLession:Write"})
  113.      */
  114.     public $elCourse;
  115.     /**
  116.      * @ORM\OneToMany(targetEntity=ElLessionTopic::class, mappedBy="elLession", orphanRemoval=true)
  117.      * @Groups({"ElLession:Detail"})
  118.      */
  119.     private $elLessionTopics;
  120.     /**
  121.      * @ORM\OneToMany(targetEntity=ElTask::class, mappedBy="elLession", orphanRemoval=true)
  122.      * @Groups({"ElLession:Detail"})
  123.      */
  124.     private $elTasks;
  125.     /**
  126.      * @ORM\OneToMany(targetEntity=ElExam::class, mappedBy="elLession")
  127.      * @Groups({"ElLession:Read", "ElLession:Detail"})
  128.      */
  129.     private $elExams;
  130.     /**
  131.      * @ORM\Column(type="string", length=16, nullable=true)
  132.      * @Groups({"ElLession:Read", "ElLession:Timeline", "ElCourse:Detail", "ElLession:Write", "ElTask:Read", "ElCourse:PIRead"})
  133.      */
  134.     private $color;
  135.     /**
  136.      * @Groups({"ElLession:ChangeOrd"})
  137.      */
  138.     private int $newOrd;
  139.     private $ordChangeDirection;
  140.     public function __construct()
  141.     {
  142.         parent::__construct();
  143.         $this->elLessionTopics = new ArrayCollection();
  144.         $this->elTasks = new ArrayCollection();
  145.         $this->elExams = new ArrayCollection();
  146.     }
  147.     protected function createTranslation(): TranslationInterface
  148.     {
  149.         return new ElLessionTranslation();
  150.     }
  151.     public function getName(): ?string
  152.     {
  153.         return $this->getTranslation()->getName();
  154.     }
  155.     public function setName(string $name): self
  156.     {
  157.         $this->getTranslation()->setName($name);
  158.         return $this;
  159.     }
  160.     public function getDescription(): ?string
  161.     {
  162.         return $this->getTranslation()->getDescription();
  163.     }
  164.     public function setDescription(string $description): self
  165.     {
  166.         $this->getTranslation()->setDescription($description);
  167.         return $this;
  168.     }
  169.     public function getId(): ?int
  170.     {
  171.         return $this->id;
  172.     }
  173.     public function getOrd(): ?int
  174.     {
  175.         return $this->ord;
  176.     }
  177.     public function setOrd(?int $ord): self
  178.     {
  179.         $this->ord $ord;
  180.         return $this;
  181.     }
  182.     public function getOrdParents(): array
  183.     {
  184.         return [
  185.             'container' => $this->getContainer(),
  186.             'elCourse' => $this->getElCourse()
  187.         ];
  188.     }
  189.     public function getOrdChangeDirection()
  190.     {
  191.         return $this->ordChangeDirection;
  192.     }
  193.     public function getContainer(): ?Container
  194.     {
  195.         return $this->container;
  196.     }
  197.     public function setContainer(?Container $container): self
  198.     {
  199.         $this->container $container;
  200.         return $this;
  201.     }
  202.     public function getElCourse(): ?ElCourse
  203.     {
  204.         return $this->elCourse;
  205.     }
  206.     public function setElCourse(?ElCourse $elCourse): self
  207.     {
  208.         $this->elCourse $elCourse;
  209.         return $this;
  210.     }
  211.     public function getElLession(): ?ElLession
  212.     {
  213.         return $this->elLession;
  214.     }
  215.     public function setElLession(?ElLession $elLession): self
  216.     {
  217.         $this->elLession $elLession;
  218.         return $this;
  219.     }
  220.     public function getElLessionTopics()
  221.     {
  222.         return $this->elLessionTopics;
  223.     }
  224.     public function addElLessionTopic(ElLessionTopic $elLessionTopics): self
  225.     {
  226.         if (!$this->elLessionTopics->contains($elLessionTopics)) {
  227.             $this->elLessionTopics[] = $elLessionTopics;
  228.             $elLessionTopics->setElLession($this);
  229.         }
  230.         return $this;
  231.     }
  232.     public function removeElLessionTopic(ElLessionTopic $elLessionTopics): self
  233.     {
  234.         if ($this->elLessions->removeElement($elLessionTopics)) {
  235.             // set the owning side to null (unless already changed)
  236.             if ($elLessionTopics->getElLession() === $this) {
  237.                 $elLessionTopics->setElLession(null);
  238.             }
  239.         }
  240.         return $this;
  241.     }
  242.     public function getElTasks($taskLevel null)
  243.     {
  244.         if ($taskLevel) {
  245.             $elTasks = [];
  246.             foreach ($this->getElTasks() as $task) {
  247.                 if ($task->getTaskLevel() === $taskLevel) {
  248.                     $elTasks[] = $task;
  249.                 }
  250.             }
  251.             return $elTasks;
  252.         }
  253.         return $this->elTasks;
  254.     }
  255.     public function addElTask(ElTask $elTask): self
  256.     {
  257.         if (!$this->elTasks->contains($elTask)) {
  258.             $this->elTasks[] = $elTask;
  259.             $elTask->setElLession($this);
  260.         }
  261.         return $this;
  262.     }
  263.     public function removeElTask(ElTask $elTask): self
  264.     {
  265.         if ($this->elTasks->removeElement($elTask)) {
  266.             // set the owning side to null (unless already changed)
  267.             if ($elTask->getElLession() === $this) {
  268.                 $elTask->setElLession(null);
  269.             }
  270.         }
  271.         return $this;
  272.     }
  273.     /**
  274.      * @Groups({"ElLession:Detail"})
  275.      */
  276.     public function getTotalElLessionTopics()
  277.     {
  278.         return (is_array($this->elLessionTopics)) ?? count($this->elLessionTopics);
  279.     }
  280.     /**
  281.      * @Groups({"ElLession:Detail"})
  282.      */
  283.     public function getTotalElTasks()
  284.     {
  285.         return (is_array($this->elTasks)) ?? count($this->elTasks);
  286.     }
  287.     /**
  288.      * @return Collection|ElExam[]
  289.      */
  290.     public function getElExams(): Collection
  291.     {
  292.         return $this->elExams;
  293.     }
  294.     public function addElExam(ElExam $elExam): self
  295.     {
  296.         if (!$this->elExams->contains($elExam)) {
  297.             $this->elExams[] = $elExam;
  298.             $elExam->setElLession($this);
  299.         }
  300.         return $this;
  301.     }
  302.     public function removeElExam(ElExam $elExam): self
  303.     {
  304.         if ($this->elExams->removeElement($elExam)) {
  305.             // set the owning side to null (unless already changed)
  306.             if ($elExam->getElLession() === $this) {
  307.                 $elExam->setElLession(null);
  308.             }
  309.         }
  310.         return $this;
  311.     }
  312.     public function getColor(): ?string
  313.     {
  314.         return $this->color;
  315.     }
  316.     public function setColor(?string $color): self
  317.     {
  318.         $this->color $color;
  319.         return $this;
  320.     }
  321.     public function getNewOrd(): int
  322.     {
  323.         return $this->newOrd ?? 0;
  324.     }
  325.     public function setNewOrd(int $newOrd): self
  326.     {
  327.         $oldOrd $this->ord;
  328.         $this->newOrd $newOrd;
  329.         $this->ord $newOrd;
  330.         $this->ordChangeDirection = ($this->newOrd $oldOrd) ? 'desc' 'asc';
  331.         return $this;
  332.     }
  333.     /**
  334.      * @Groups({"ElLession:Timeline"})
  335.      */
  336.     public function getMinDate()
  337.     {
  338.         $minDate null;
  339.         foreach ($this->elTasks as $task) {
  340.             if ($minDate === null || $minDate $task->getStart()) {
  341.                 $minDate $task->getStart();
  342.             }
  343.         }
  344.         return $minDate;
  345.     }
  346.     /**
  347.      * @Groups({"ElLession:Timeline"})
  348.      */
  349.     public function getMaxDate()
  350.     {
  351.         $maxDate null;
  352.         foreach ($this->elTasks as $task) {
  353.             if ($maxDate === null || $maxDate $task->getEnd()) {
  354.                 $maxDate $task->getEnd();
  355.             }
  356.         }
  357.         return $maxDate;
  358.     }
  359.     public function getMetadataTranslation()
  360.     {
  361.         $translations = [];
  362.         foreach ($this->getTranslations() as $translation) {
  363.             $translations[] = [
  364.                 $translation->getLocale() => [
  365.                     'name' => $translation->getName(),
  366.                     'description' => $translation->getDescription()
  367.                 ]
  368.             ];
  369.         }
  370.         return $translations;
  371.     }
  372.     public function getParentCourse()
  373.     {
  374.         return $this->elCourse;
  375.     }
  376.     public function getForIndex()
  377.     {
  378.         $translations = [];
  379.         foreach ($this->getTranslations() as $translation) {
  380.             $translations[] = [
  381.                 $translation->getLocale() => [
  382.                     'name' => $translation->getName()
  383.                 ]
  384.             ];
  385.         }
  386.         $elLessionTopics = [];
  387.         foreach ($this->getElLessionTopics() as $topic) {
  388.             $elLessionTopics[] = $topic->getForIndex();
  389.         }
  390.         $elExams = [];
  391.         foreach ($this->getElExams() as $exam) {
  392.             $elExams[] = $exam->getForIndex();
  393.         }
  394.         $elTasks = [];
  395.         foreach ($this->getElTasks() as $task) {
  396.             if ($task->getTaskLevel() === ElTask::TASKLEVEL_LESSION) {
  397.                 $elTasks[] = $task->getForIndex();
  398.             }
  399.         }
  400.         return [
  401.             'id' => $this->getId(),
  402.             'ord' => $this->getOrd(),
  403.             'color' => $this->getColor(),
  404.             'elCourse' => $this->getElCourse()->getId(),
  405.             'translations' => $translations,
  406.             'elLessionTopics' => $elLessionTopics,
  407.             'elExams' => $elExams,
  408.             'elTasks' => $elTasks
  409.         ];
  410.     }
  411. }