src/Entity/ElExamAttempt.php line 66

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use App\Filter\ElExamAttemptFilter;
  5. use ApiPlatform\Core\Annotation\ApiFilter;
  6. use App\Repository\ElExamAttemptRepository;
  7. use Doctrine\Common\Collections\Collection;
  8. use ApiPlatform\Core\Annotation\ApiResource;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. use App\Entity\Interfaces\ContainerMappedInterface;
  11. use Symfony\Component\Serializer\Annotation\Groups;
  12. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter;
  13. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  14. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  15. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\BooleanFilter;
  16. /**
  17.  * @ApiResource(
  18.  *      collectionOperations={
  19.  *          "get"={
  20.  *              "security"="is_granted('IS_AUTHENTICATED_FULLY')",
  21.  *              "normalization_context"={"groups"={"ElExamAttempt:Read"}, "skip_null_values"=false}
  22.  *          },
  23.  *          "post_start_exam"={
  24.  *              "route_name"="api_el_exam_attempts_start_exam",
  25.  *              "method"="POST",
  26.  *              "security"="is_granted('IS_AUTHENTICATED_FULLY')",
  27.  *              "denormalization_context"={"groups"={"ElExamAttempt:Start-Exam"}}
  28.  *          }
  29.  *      },
  30.  *      itemOperations={
  31.  *          "get"={
  32.  *              "security"="is_granted('IS_CO_ANY_SPE', object)",
  33.  *              "normalization_context"={"groups"={"ElExamAttempt:Read"}, "skip_null_values"=false}
  34.  *          },
  35.  *          "patch_end_exam"={
  36.  *              "route_name"="api_el_exam_attempts_end_exam",
  37.  *              "method"="PATCH",
  38.  *              "security"="is_granted('IS_CO_ANY_SPE', object)",
  39.  *              "denormalization_context"={"groups"={"ElExamAttempt:End-Exam"}}
  40.  *          },
  41.  *          "patch_restart_exam"={
  42.  *              "route_name"="api_el_exams_restart_exam",
  43.  *              "method"="PATCH",
  44.  *              "security"="is_granted('ROLE_OPERATOR') || is_granted('IS_CO_ANY_SPE', object)",
  45.  *              "denormalization_context"={"groups"={""}}
  46.  *          },
  47.  *          "patch_assessment_complete"={
  48.  *              "route_name"="api_el_exams_assessment_complete",
  49.  *              "method"="PATCH",
  50.  *              "security"="is_granted('IS_CO_OPR', object)",
  51.  *              "denormalization_context"={"groups"={""}}
  52.  *          },
  53.  *          "delete"={"security"="is_granted('IS_CO_ANY_SPE', object)"}
  54.  *     }
  55.  * )
  56.  * @ApiFilter(SearchFilter::class, properties={"id": "exact", "container.id": "exact", "elExam.elCourseAlways.id": "exact", "elExam.id": "exact", "elExam.elLession.id": "exact", "elExam.elLessionTopic.id": "exact", "elExam.elTask.id": "exact", "elExam.translations.title": "partial", "elExam.examLevel": "exact", "user.id": "exact", "status": "exact", "resultStatus": "exact"})
  57.  * @ApiFilter(BooleanFilter::class, properties={"elExam.isConsiderForPass"})
  58.  * @ApiFilter(DateFilter::class, properties={"start", "end"})
  59.  * @ApiFilter(ElExamAttemptFilter::class, properties={"search": "partial"})
  60.  * @ApiFilter(OrderFilter::class, properties={"id", "start", "end", "status"})
  61.  * @ORM\Entity(repositoryClass=ElExamAttemptRepository::class)
  62.  */
  63. class ElExamAttempt implements ContainerMappedInterface
  64. {
  65.     /**
  66.      * @ORM\Id
  67.      * @ORM\GeneratedValue
  68.      * @ORM\Column(type="integer")
  69.      * @Groups({"ElExamAttempt:Read", "User:Me"})
  70.      */
  71.     private $id;
  72.     /**
  73.      * @ORM\Column(type="datetime", nullable=true)
  74.      * @Groups({"ElExamAttempt:Read", "User:Me"})
  75.      */
  76.     private $start;
  77.     /**
  78.      * @ORM\Column(type="datetime", nullable=true)
  79.      * @Groups({"ElExamAttempt:Read", "User:Me"})
  80.      */
  81.     private $end;
  82.     /**
  83.      * @ORM\Column(type="string", length=16)
  84.      * @Groups({"ElExamAttempt:Read", "User:Me"})
  85.      */
  86.     private $status;
  87.     public const STATUSEXAMATTEMPT_STARTED "STARTED";
  88.     public const STATUSEXAMATTEMPT_COMPLETED "COMPLETED";
  89.     public const STATUSEXAMATTEMPT_EXPIRED "EXPIRED";
  90.     /**
  91.      * @ORM\ManyToOne(targetEntity=User::class, inversedBy="elExamAttempts")
  92.      * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
  93.      * @Groups({"ElExamAttempt:Read"})
  94.      */
  95.     private $user;
  96.     /**
  97.      * @ORM\ManyToOne(targetEntity=ElExam::class, inversedBy="elExamAttempts")
  98.      * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
  99.      * @Groups({"ElExamAttempt:Read", "ElExamAttempt:Start-Exam", "User:Me"})
  100.      */
  101.     private $elExam;
  102.     /**
  103.      * @ORM\ManyToOne(targetEntity=Container::class)
  104.      * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
  105.      * @Groups({"ElExamAttempt:Start-Exam"})
  106.      */
  107.     private $container;
  108.     /**
  109.      * @ORM\OneToMany(targetEntity=ElExamAnswer::class, mappedBy="elExamAttempt")
  110.      */
  111.     private $elExamAnswers;
  112.     /**
  113.      * @ORM\Column(type="integer", nullable=true)
  114.      * @Groups({"ElExamAttempt:Read", "User:Me", "ElExamAttempt:Current-Question"})
  115.      */
  116.     private $currentQuestionId;
  117.     /**
  118.      * @ORM\Column(type="integer", nullable=true)
  119.      * @Groups({"ElExamAttempt:Read", "User:Me"})
  120.      */
  121.     private $totalPoint;
  122.     /**
  123.      * @ORM\Column(type="decimal", precision=10, scale=2, nullable=true)
  124.      * @Groups({"ElExamAttempt:Read", "User:Me"})
  125.      */
  126.     private $obtainPoint;
  127.     /**
  128.      * @ORM\Column(type="string", length=16, nullable=true)
  129.      * @Groups({"ElExamAttempt:Read", "User:Me"})
  130.      */
  131.     private $resultStatus;
  132.     public const RESULTSTATUSEXAMATTEMPT_PASS "PASS";
  133.     public const RESULTSTATUSEXAMATTEMPT_FAIL "FAIL";
  134.     /**
  135.      * @Groups({"ElExamAttempt:Complete-Bulk"})
  136.      */
  137.     private $elExamAttemptIds = [];
  138.     /**
  139.      * @ORM\Column(type="boolean", nullable=true)
  140.      * @Groups({"ElExamAttempt:Read", "User:Me"})
  141.      */
  142.     private $isResultSend;
  143.     public function __construct()
  144.     {
  145.         $this->elExamAnswers = new ArrayCollection();
  146.         $this->elExamAttemptIds = [];
  147.     }
  148.     public function getId(): ?int
  149.     {
  150.         return $this->id;
  151.     }
  152.     public function getStart(): ?\DateTimeInterface
  153.     {
  154.         return $this->start;
  155.     }
  156.     public function setStart(?\DateTimeInterface $start): self
  157.     {
  158.         $this->start $start;
  159.         return $this;
  160.     }
  161.     public function getEnd(): ?\DateTimeInterface
  162.     {
  163.         return $this->end;
  164.     }
  165.     public function setEnd(?\DateTimeInterface $end): self
  166.     {
  167.         $this->end $end;
  168.         return $this;
  169.     }
  170.     public function getStatus(): ?string
  171.     {
  172.         return $this->status;
  173.     }
  174.     public function setStatus(string $status): self
  175.     {
  176.         $this->status $status;
  177.         return $this;
  178.     }
  179.     public function getUser(): ?User
  180.     {
  181.         return $this->user;
  182.     }
  183.     public function setUser(?User $user): self
  184.     {
  185.         $this->user $user;
  186.         return $this;
  187.     }
  188.     public function getElExam(): ?ElExam
  189.     {
  190.         return $this->elExam;
  191.     }
  192.     public function setElExam(?ElExam $elExam): self
  193.     {
  194.         $this->elExam $elExam;
  195.         return $this;
  196.     }
  197.     public function getContainer(): ?Container
  198.     {
  199.         return $this->container;
  200.     }
  201.     public function setContainer(?Container $container): self
  202.     {
  203.         $this->container $container;
  204.         return $this;
  205.     }
  206.     /**
  207.      * @return Collection|ElExamAnswer[]
  208.      */
  209.     public function getElExamAnswers(): Collection
  210.     {
  211.         return $this->elExamAnswers;
  212.     }
  213.     public function addElExamAnswer(ElExamAnswer $elExamAnswer): self
  214.     {
  215.         if (!$this->elExamAnswers->contains($elExamAnswer)) {
  216.             $this->elExamAnswers[] = $elExamAnswer;
  217.             $elExamAnswer->setElExamAttempt($this);
  218.         }
  219.         return $this;
  220.     }
  221.     public function removeElExamAnswer(ElExamAnswer $elExamAnswer): self
  222.     {
  223.         if ($this->elExamAnswers->removeElement($elExamAnswer)) {
  224.             // set the owning side to null (unless already changed)
  225.             if ($elExamAnswer->getElExamAttempt() === $this) {
  226.                 $elExamAnswer->setElExamAttempt(null);
  227.             }
  228.         }
  229.         return $this;
  230.     }
  231.     /**
  232.      * @Groups({"ElExamAttempt:Read", "User:Me"})
  233.      */
  234.     public function getRemainingDuration()
  235.     {
  236.         if ($this->status === self::STATUSEXAMATTEMPT_STARTED) {
  237.             if ($this->getElExam()) {
  238.                 if ($this->getElExam()->getDuration() < 1) {
  239.                     return -1;
  240.                 }
  241.                 $duration = ($this->getElExam()->getDuration() * 60);
  242.                 $now = new \DateTime();
  243.                 $passTime = ($now->getTimestamp() - $this->getStart()->getTimestamp());
  244.                 $remainingTime = ($duration $passTime);
  245.                 return ($remainingTime 0) ? $remainingTime 0;
  246.             }
  247.         }
  248.         return 0;
  249.     }
  250.     public function getCurrentQuestionId(): ?int
  251.     {
  252.         return $this->currentQuestionId;
  253.     }
  254.     public function setCurrentQuestionId(?int $currentQuestionId): self
  255.     {
  256.         $this->currentQuestionId $currentQuestionId;
  257.         return $this;
  258.     }
  259.     public function getTotalPoint(): ?int
  260.     {
  261.         return $this->totalPoint;
  262.     }
  263.     public function setTotalPoint(?int $totalPoint): self
  264.     {
  265.         $this->totalPoint $totalPoint;
  266.         return $this;
  267.     }
  268.     public function getObtainPoint(): ?string
  269.     {
  270.         return $this->obtainPoint;
  271.     }
  272.     public function setObtainPoint(?string $obtainPoint): self
  273.     {
  274.         $this->obtainPoint $obtainPoint;
  275.         return $this;
  276.     }
  277.     public function getResultStatus(): ?string
  278.     {
  279.         return $this->resultStatus;
  280.     }
  281.     public function setResultStatus(?string $resultStatus): self
  282.     {
  283.         $this->resultStatus $resultStatus;
  284.         return $this;
  285.     }
  286.     public function getElExamAttemptIds()
  287.     {
  288.         return $this->elExamAttemptIds;
  289.     }
  290.     public function setElExamAttemptIds($elExamAttemptIds)
  291.     {
  292.         $this->elExamAttemptIds $elExamAttemptIds;
  293.         return $this;
  294.     }
  295.     public function getIsResultSend(): ?bool
  296.     {
  297.         return $this->isResultSend;
  298.     }
  299.     public function setIsResultSend(?bool $isResultSend): self
  300.     {
  301.         $this->isResultSend $isResultSend;
  302.         return $this;
  303.     }
  304. }