src/Entity/Gos/Uniqskills/Test.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Gos\Uniqskills;
  3. use App\Entity\Gos\CertificatePath;
  4. use App\Entity\Gos\User;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Gedmo\Mapping\Annotation as Gedmo;
  9. /**
  10.  * @ORM\Table()
  11.  * @ORM\Entity(repositoryClass="App\Repository\Gos\Uniqskills\TestRepository")
  12.  * @ORM\HasLifecycleCallbacks
  13.  */
  14. class Test
  15. {
  16.     /**
  17.      * @var integer
  18.      *
  19.      * @ORM\Column(type="integer")
  20.      * @ORM\Id
  21.      * @ORM\GeneratedValue(strategy="AUTO")
  22.      */
  23.     private $id;
  24.     /**
  25.      * @var bool
  26.      * @ORM\Column(type="boolean")
  27.      */
  28.     private $isActive;
  29.     /**
  30.      * @var bool
  31.      * @ORM\Column(type="boolean")
  32.      */
  33.     private $isOneQuestionPerPage;
  34.     /**
  35.      * @ORM\Column(type="string")
  36.      */
  37.     private $name;
  38.     /**
  39.      * @Gedmo\Slug(fields={"name"})
  40.      * @ORM\Column(type="string", length=191, unique=true)
  41.      */
  42.     private $slug;
  43.     /**
  44.      * @ORM\Column(type="string")
  45.      */
  46.     private $type;
  47.     /**
  48.      * @ORM\Column(type="string", nullable=true)
  49.      */
  50.     private $share;
  51.     /**
  52.      * @ORM\Column(type="datetime", nullable=true)
  53.      */
  54.     private $dateStart;
  55.     /**
  56.      * @ORM\Column(type="integer", nullable=true)
  57.      */
  58.     private $shareDays;
  59.     /**
  60.      * @ORM\Column(type="float")
  61.      */
  62.     private $pointsGoodAnswer;
  63.     /**
  64.      * @ORM\Column(type="float")
  65.      */
  66.     private $pointsWrongAnswer;
  67.     /**
  68.      * @ORM\Column(type="float")
  69.      */
  70.     private $minimumScore;
  71.     /**
  72.      * @ORM\Column(type="float", nullable=true)
  73.      */
  74.     private $educationPointsPerScorePoint;
  75.     /**
  76.      * @ORM\Column(type="integer")
  77.      */
  78.     private $numberRepetitions;
  79.     /**
  80.      * @ORM\Column(type="boolean", nullable=true)
  81.      */
  82.     private $infiniteRepeatTest;
  83.     /**
  84.      * @ORM\Column(type="integer", nullable=true)
  85.      */
  86.     private $timeToRepeatTest;
  87.     /**
  88.      * @var \DateTime
  89.      *
  90.      * @ORM\Column(type="datetime", nullable=true)
  91.      */
  92.     private $createdAt;
  93.     /**
  94.      * @var \DateTime
  95.      *
  96.      * @ORM\Column(type="datetime", nullable=true)
  97.      */
  98.     private $updatedAt;
  99.     /**
  100.      * @var \DateTime
  101.      *
  102.      * @ORM\Column(type="datetime", nullable=true)
  103.      */
  104.     private $deletedAt;
  105.     /**
  106.      * @ORM\OneToOne(targetEntity="Module", inversedBy="test")
  107.      * @ORM\JoinColumn(name="module_id", referencedColumnName="id", onDelete="cascade")
  108.      */
  109.     private $module;
  110.     /**
  111.      * @ORM\ManyToOne(targetEntity="Course", inversedBy="test")
  112.      * @ORM\JoinColumn(name="course_id", referencedColumnName="id", onDelete="cascade")
  113.      */
  114.     private $course;
  115.     /**
  116.      * @ORM\OneToMany(targetEntity="Question", mappedBy="test")
  117.      */
  118.     private $question;
  119.     /**
  120.      * @ORM\OneToMany(targetEntity="UserTest", mappedBy="test")
  121.      */
  122.     private $userTest;
  123.     /**
  124.      * @ORM\OneToMany(targetEntity="UserAnswer", mappedBy="test")
  125.      */
  126.     private $userAnswer;
  127. //    /**
  128. //     * @ORM\OneToOne(targetEntity="Certificate", mappedBy="test")
  129. //     */
  130. //    private $certificate;
  131.     /**
  132.      * @ORM\ManyToOne(targetEntity="App\Entity\Gos\User", inversedBy="test")
  133.      * @ORM\JoinColumn()
  134.      */
  135.     private $user;
  136.     /**
  137.      * @ORM\OneToMany(targetEntity=CertificatePath::class, mappedBy="test")
  138.      */
  139.     private $certificatePaths;
  140.     public function __construct()
  141.     {
  142.         $this->question = new ArrayCollection();
  143.         $this->userTest = new ArrayCollection();
  144.         $this->userAnswer = new ArrayCollection();
  145.         $this->certificatePaths = new ArrayCollection();
  146.     }
  147.     /** @ORM\PrePersist() */
  148.     public function prePersist()
  149.     {
  150.         $this->createdAt = new \DateTime();
  151.     }
  152.     /** @ORM\PreUpdate() */
  153.     public function preUpdate()
  154.     {
  155.         $this->updatedAt = new \DateTime();
  156.     }
  157.     public function __toString()
  158.     {
  159.         return (string) $this->name;
  160.     }
  161.     public function isCompletedTestByUser($user)
  162.     {
  163.         if (!$this->hasQuestions())
  164.         {
  165.             return true;
  166.         }
  167.         foreach ($this->userTest as $userTest)
  168.         {
  169.             if ($userTest->getUser() && $userTest->getUser()->getId() == $user->getId())
  170.             {
  171.                 return true;
  172.             }
  173.         }
  174.         return false;
  175.     }
  176.     public function canUserRepeatTest($user)
  177.     {
  178.         if ($this->infiniteRepeatTest && $this->hasQuestions())
  179.         {
  180.             return true;
  181.         }
  182.         if ($this->numberRepetitions && $this->hasQuestions())
  183.         {
  184.             foreach ($this->userTest as $userTest)
  185.             {
  186.                 if ($userTest->getUser() && $userTest->getUser()->getId() == $user->getId())
  187.                 {
  188.                     $date           = (new \DateTime())->format('U');
  189.                     $userTestTime   $userTest->getCreatedAt()->format('U');
  190.                     if (
  191.                         (
  192.                             ($date $userTestTime) / 3600 $this->timeToRepeatTest
  193.                         )
  194.                         && $userTest->getSumCompleted() <= $this->numberRepetitions
  195.                     )
  196.                     {
  197.                         return true;
  198.                     }
  199.                 }
  200.             }
  201.         }
  202.         return false;
  203.     }
  204.     public function getClientTestByUser($user)
  205.     {
  206.         foreach ($this->userTest as $userTest)
  207.         {
  208.             if ($userTest->getUser() && $userTest->getUser()->getId() == $user->getId())
  209.             {
  210.                 return $userTest;
  211.             }
  212.         }
  213.         return null;
  214.     }
  215.     public function hasOpenQuestions()
  216.     {
  217.         foreach ($this->question as $question)
  218.         {
  219.             if (count($question->getAnswer()) === 0)
  220.             {
  221.                 return true;
  222.             }
  223.         }
  224.         return false;
  225.     }
  226.     public function __clone()
  227.     {
  228.         $this->id           null;
  229.         $this->updatedAt    null;
  230.     }
  231.     public function getId(): ?int
  232.     {
  233.         return $this->id;
  234.     }
  235.     public function getIsActive(): ?bool
  236.     {
  237.         return $this->isActive;
  238.     }
  239.     public function setIsActive(bool $isActive): self
  240.     {
  241.         $this->isActive $isActive;
  242.         return $this;
  243.     }
  244.     public function getIsOneQuestionPerPage(): ?bool
  245.     {
  246.         return $this->isOneQuestionPerPage;
  247.     }
  248.     public function setIsOneQuestionPerPage(bool $isOneQuestionPerPage): self
  249.     {
  250.         $this->isOneQuestionPerPage $isOneQuestionPerPage;
  251.         return $this;
  252.     }
  253.     public function getName(): ?string
  254.     {
  255.         return $this->name;
  256.     }
  257.     public function setName(string $name): self
  258.     {
  259.         $this->name $name;
  260.         return $this;
  261.     }
  262.     public function getSlug(): ?string
  263.     {
  264.         return $this->slug;
  265.     }
  266.     public function setSlug(string $slug): self
  267.     {
  268.         $this->slug $slug;
  269.         return $this;
  270.     }
  271.     public function getType(): ?string
  272.     {
  273.         return $this->type;
  274.     }
  275.     public function setType(string $type): self
  276.     {
  277.         $this->type $type;
  278.         return $this;
  279.     }
  280.     public function getShare(): ?string
  281.     {
  282.         return $this->share;
  283.     }
  284.     public function setShare(string $share): self
  285.     {
  286.         $this->share $share;
  287.         return $this;
  288.     }
  289.     public function getDateStart(): ?\DateTimeInterface
  290.     {
  291.         return $this->dateStart;
  292.     }
  293.     public function setDateStart(?\DateTimeInterface $dateStart): self
  294.     {
  295.         $this->dateStart $dateStart;
  296.         return $this;
  297.     }
  298.     public function getShareDays(): ?int
  299.     {
  300.         return $this->shareDays;
  301.     }
  302.     public function setShareDays(?int $shareDays): self
  303.     {
  304.         $this->shareDays $shareDays;
  305.         return $this;
  306.     }
  307.     public function getPointsGoodAnswer(): ?float
  308.     {
  309.         return $this->pointsGoodAnswer;
  310.     }
  311.     public function setPointsGoodAnswer(float $pointsGoodAnswer): self
  312.     {
  313.         $this->pointsGoodAnswer $pointsGoodAnswer;
  314.         return $this;
  315.     }
  316.     public function getPointsWrongAnswer(): ?float
  317.     {
  318.         return $this->pointsWrongAnswer;
  319.     }
  320.     public function setPointsWrongAnswer(float $pointsWrongAnswer): self
  321.     {
  322.         $this->pointsWrongAnswer $pointsWrongAnswer;
  323.         return $this;
  324.     }
  325.     public function getMinimumScore(): ?float
  326.     {
  327.         return $this->minimumScore;
  328.     }
  329.     public function setMinimumScore(float $minimumScore): self
  330.     {
  331.         $this->minimumScore $minimumScore;
  332.         return $this;
  333.     }
  334.     public function getEducationPointsPerScorePoint(): ?float
  335.     {
  336.         return $this->educationPointsPerScorePoint;
  337.     }
  338.     public function setEducationPointsPerScorePoint(?float $educationPointsPerScorePoint): self
  339.     {
  340.         $this->educationPointsPerScorePoint $educationPointsPerScorePoint;
  341.         return $this;
  342.     }
  343.     public function getNumberRepetitions(): ?int
  344.     {
  345.         return $this->numberRepetitions;
  346.     }
  347.     public function setNumberRepetitions(int $numberRepetitions): self
  348.     {
  349.         $this->numberRepetitions $numberRepetitions;
  350.         return $this;
  351.     }
  352.     public function getInfiniteRepeatTest(): ?bool
  353.     {
  354.         return $this->infiniteRepeatTest;
  355.     }
  356.     public function setInfiniteRepeatTest(?bool $infiniteRepeatTest): self
  357.     {
  358.         $this->infiniteRepeatTest $infiniteRepeatTest;
  359.         return $this;
  360.     }
  361.     public function getTimeToRepeatTest(): ?int
  362.     {
  363.         return $this->timeToRepeatTest;
  364.     }
  365.     public function setTimeToRepeatTest(?int $timeToRepeatTest): self
  366.     {
  367.         $this->timeToRepeatTest $timeToRepeatTest;
  368.         return $this;
  369.     }
  370.     public function getCreatedAt(): ?\DateTimeInterface
  371.     {
  372.         return $this->createdAt;
  373.     }
  374.     public function setCreatedAt(?\DateTimeInterface $createdAt): self
  375.     {
  376.         $this->createdAt $createdAt;
  377.         return $this;
  378.     }
  379.     public function getUpdatedAt(): ?\DateTimeInterface
  380.     {
  381.         return $this->updatedAt;
  382.     }
  383.     public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
  384.     {
  385.         $this->updatedAt $updatedAt;
  386.         return $this;
  387.     }
  388.     public function getDeletedAt(): ?\DateTimeInterface
  389.     {
  390.         return $this->deletedAt;
  391.     }
  392.     public function setDeletedAt(?\DateTimeInterface $deletedAt): self
  393.     {
  394.         $this->deletedAt $deletedAt;
  395.         return $this;
  396.     }
  397.     public function getModule(): ?Module
  398.     {
  399.         return $this->module;
  400.     }
  401.     public function setModule(?Module $module): self
  402.     {
  403.         $this->module $module;
  404.         return $this;
  405.     }
  406.     public function getCourse(): ?Course
  407.     {
  408.         return $this->course;
  409.     }
  410.     public function getCourseName(): string
  411.     {
  412.         if ($this->getCourse())
  413.         {
  414.             return $this->getCourse()->getName();
  415.         }
  416.         elseif ($this->getModule())
  417.         {
  418.             return $this->getModule()->getCourse()->getName();
  419.         }
  420.         return '';
  421.     }
  422.     public function setCourse(?Course $course): self
  423.     {
  424.         $this->course $course;
  425.         return $this;
  426.     }
  427.     /**
  428.      * @return Collection|Question[]
  429.      */
  430.     public function getQuestion(): Collection
  431.     {
  432.         return $this->question;
  433.     }
  434.     public function hasQuestions(): bool
  435.     {
  436.         return !$this->getQuestion()->isEmpty();
  437.     }
  438.     public function addQuestion(Question $question): self
  439.     {
  440.         if (!$this->question->contains($question)) {
  441.             $this->question[] = $question;
  442.             $question->setTest($this);
  443.         }
  444.         return $this;
  445.     }
  446.     public function removeQuestion(Question $question): self
  447.     {
  448.         if ($this->question->contains($question)) {
  449.             $this->question->removeElement($question);
  450.             // set the owning side to null (unless already changed)
  451.             if ($question->getTest() === $this) {
  452.                 $question->setTest(null);
  453.             }
  454.         }
  455.         return $this;
  456.     }
  457.     /**
  458.      * @return Collection|UserTest[]
  459.      */
  460.     public function getUserTest(): Collection
  461.     {
  462.         return $this->userTest;
  463.     }
  464.     public function addUserTest(UserTest $userTest): self
  465.     {
  466.         if (!$this->userTest->contains($userTest)) {
  467.             $this->userTest[] = $userTest;
  468.             $userTest->setTest($this);
  469.         }
  470.         return $this;
  471.     }
  472.     public function removeUserTest(UserTest $userTest): self
  473.     {
  474.         if ($this->userTest->contains($userTest)) {
  475.             $this->userTest->removeElement($userTest);
  476.             // set the owning side to null (unless already changed)
  477.             if ($userTest->getTest() === $this) {
  478.                 $userTest->setTest(null);
  479.             }
  480.         }
  481.         return $this;
  482.     }
  483.     /**
  484.      * @return Collection|UserAnswer[]
  485.      */
  486.     public function getUserAnswer(): Collection
  487.     {
  488.         return $this->userAnswer;
  489.     }
  490.     public function addUserAnswer(UserAnswer $userAnswer): self
  491.     {
  492.         if (!$this->userAnswer->contains($userAnswer)) {
  493.             $this->userAnswer[] = $userAnswer;
  494.             $userAnswer->setTest($this);
  495.         }
  496.         return $this;
  497.     }
  498.     public function removeUserAnswer(UserAnswer $userAnswer): self
  499.     {
  500.         if ($this->userAnswer->contains($userAnswer)) {
  501.             $this->userAnswer->removeElement($userAnswer);
  502.             // set the owning side to null (unless already changed)
  503.             if ($userAnswer->getTest() === $this) {
  504.                 $userAnswer->setTest(null);
  505.             }
  506.         }
  507.         return $this;
  508.     }
  509.     public function getUser(): ?User
  510.     {
  511.         return $this->user;
  512.     }
  513.     public function setUser(?User $user): self
  514.     {
  515.         $this->user $user;
  516.         return $this;
  517.     }
  518.     /**
  519.      * @return Collection|CertificatePath[]
  520.      */
  521.     public function getCertificatePaths(): Collection
  522.     {
  523.         return $this->certificatePaths;
  524.     }
  525.     public function addCertificatePath(CertificatePath $certificatePath): self
  526.     {
  527.         if (!$this->certificatePaths->contains($certificatePath)) {
  528.             $this->certificatePaths[] = $certificatePath;
  529.             $certificatePath->setTest($this);
  530.         }
  531.         return $this;
  532.     }
  533.     public function removeCertificatePath(CertificatePath $certificatePath): self
  534.     {
  535.         if ($this->certificatePaths->contains($certificatePath)) {
  536.             $this->certificatePaths->removeElement($certificatePath);
  537.             // set the owning side to null (unless already changed)
  538.             if ($certificatePath->getTest() === $this) {
  539.                 $certificatePath->setTest(null);
  540.             }
  541.         }
  542.         return $this;
  543.     }
  544. }