src/Entity/Gos/Uniqskills/Question.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Gos\Uniqskills;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Component\Validator\Constraints as Assert;
  7. use Gedmo\Mapping\Annotation as Gedmo;
  8. /**
  9.  * @ORM\Table()
  10.  * @ORM\Entity(repositoryClass="App\Repository\Gos\Uniqskills\QuestionRepository")
  11.  * @ORM\HasLifecycleCallbacks
  12.  */
  13. class Question
  14. {
  15.     /**
  16.      * @var integer
  17.      *
  18.      * @ORM\Column(type="integer")
  19.      * @ORM\Id
  20.      * @ORM\GeneratedValue(strategy="AUTO")
  21.      */
  22.     private $id;
  23.     /**
  24.      * @ORM\Column(type="text")
  25.      */
  26.     private $name;
  27.     /**
  28.      * @Gedmo\Slug(fields={"name"})
  29.      * @ORM\Column(type="string", length=191, unique=true)
  30.      */
  31.     private $slug;
  32.     /**
  33.      * @var integer
  34.      * @ORM\Column(type="integer")
  35.      */
  36.     private $position;
  37.     /**
  38.      * @var integer
  39.      * @ORM\Column(type="integer", nullable=true)
  40.      */
  41.     private $points;
  42.     
  43.     /**
  44.      * @var bool
  45.      * @ORM\Column(type="boolean")
  46.      */
  47.     private $fileWithAnswer;
  48.     /**
  49.      * @var \DateTime
  50.      *
  51.      * @ORM\Column(type="datetime", nullable=true)
  52.      */
  53.     private $createdAt;
  54.     /**
  55.      * @var \DateTime
  56.      *
  57.      * @ORM\Column(type="datetime", nullable=true)
  58.      */
  59.     private $updatedAt;
  60.     /**
  61.      * @ORM\Column(type="string", nullable=true)
  62.      *
  63.      * @Assert\File(mimeTypes={ "image/png", "image/jpeg", "image/gif" })
  64.      */
  65.     private $image;
  66.     /**
  67.      * @ORM\ManyToOne(targetEntity="Test", inversedBy="question")
  68.      * @ORM\JoinColumn(name="test_id", referencedColumnName="id", onDelete="CASCADE")
  69.      */
  70.     private $test;
  71.     /**
  72.      * @ORM\OneToMany(targetEntity="Answer", mappedBy="question", cascade={"persist"}, orphanRemoval=true)
  73.      */
  74.     private $answer;
  75.     /**
  76.      * @ORM\OneToMany(targetEntity="UserAnswer", mappedBy="question")
  77.      */
  78.     private $userAnswer;
  79.     /**
  80.      * @ORM\Column(type="string", length=255, nullable=true)
  81.      */
  82.     private $sendAnswerTo;
  83.     public function __construct()
  84.     {
  85.         $this->answer       = new ArrayCollection();
  86.         $this->userAnswer   = new ArrayCollection();
  87.     }
  88.     public function __clone()
  89.     {
  90.         $this->id           null;
  91.         $this->updatedAt    null;
  92.     }
  93.     public function getImage()
  94.     {
  95.         return $this->image;
  96.     }
  97.     public function setImage($image)
  98.     {
  99.         $this->image $image;
  100.         return $this;
  101.     }
  102.     public function getImageLink($dir)
  103.     {
  104.         return $dir.'/'.$this->getTest()->getId().'/'.$this->image;
  105.     }
  106.     /** @ORM\PrePersist() */
  107.     public function prePersist()
  108.     {
  109.         $this->createdAt = new \DateTime();
  110.     }
  111.     /** @ORM\PreUpdate() */
  112.     public function preUpdate()
  113.     {
  114.         $this->updatedAt = new \DateTime();
  115.     }
  116.     public function __toString()
  117.     {
  118.         return (string) $this->name;
  119.     }
  120.     public function setAnswers($answers)
  121.     {
  122.         if (count($answers) > 0) {
  123.             foreach ($answers as $a) {
  124.                 $this->addAnswer($a);
  125.             }
  126.         }
  127.         return $this;
  128.     }
  129.     /**
  130.      * Add answer
  131.      *
  132.      * @param Answer $answer
  133.      * @return Question
  134.      */
  135.     public function addAnswer(Answer $answer)
  136.     {
  137.         $answer->setQuestion($this);
  138.         $this->answer[] = $answer;
  139.         return $this;
  140.     }
  141.     /**
  142.      * Remove answer
  143.      *
  144.      * @param Answer $answer
  145.      */
  146.     public function removeAnswer(Answer $answer)
  147.     {
  148.         $this->answer->removeElement($answer);
  149.     }
  150.     /**
  151.      * Get answer
  152.      *
  153.      * @return \Doctrine\Common\Collections\Collection
  154.      */
  155.     public function getAnswer()
  156.     {
  157.         return $this->answer;
  158.     }
  159.     public function getId(): ?int
  160.     {
  161.         return $this->id;
  162.     }
  163.     public function getName(): ?string
  164.     {
  165.         return $this->name;
  166.     }
  167.     public function setName(string $name): self
  168.     {
  169.         $this->name $name;
  170.         return $this;
  171.     }
  172.     public function getSlug(): ?string
  173.     {
  174.         return $this->slug;
  175.     }
  176.     public function setSlug($slug): self
  177.     {
  178.         $this->slug $slug;
  179.         return $this;
  180.     }
  181.     public function getPosition(): ?int
  182.     {
  183.         return $this->position;
  184.     }
  185.     public function setPosition(int $position): self
  186.     {
  187.         $this->position $position;
  188.         return $this;
  189.     }
  190.     public function getPoints(): ?int
  191.     {
  192.         return $this->points;
  193.     }
  194.     public function setPoints(?int $points): self
  195.     {
  196.         $this->points $points;
  197.         return $this;
  198.     }
  199.     public function getFileWithAnswer(): ?bool
  200.     {
  201.         return $this->fileWithAnswer;
  202.     }
  203.     public function setFileWithAnswer(bool $fileWithAnswer): self
  204.     {
  205.         $this->fileWithAnswer $fileWithAnswer;
  206.         return $this;
  207.     }
  208.     public function getCreatedAt(): ?\DateTimeInterface
  209.     {
  210.         return $this->createdAt;
  211.     }
  212.     public function setCreatedAt(?\DateTimeInterface $createdAt): self
  213.     {
  214.         $this->createdAt $createdAt;
  215.         return $this;
  216.     }
  217.     public function getUpdatedAt(): ?\DateTimeInterface
  218.     {
  219.         return $this->updatedAt;
  220.     }
  221.     public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
  222.     {
  223.         $this->updatedAt $updatedAt;
  224.         return $this;
  225.     }
  226.     public function getTest(): ?Test
  227.     {
  228.         return $this->test;
  229.     }
  230.     public function setTest(?Test $test): self
  231.     {
  232.         $this->test $test;
  233.         return $this;
  234.     }
  235.     /**
  236.      * @return Collection|UserAnswer[]
  237.      */
  238.     public function getUserAnswer(): Collection
  239.     {
  240.         return $this->userAnswer;
  241.     }
  242.     public function addUserAnswer(UserAnswer $userAnswer): self
  243.     {
  244.         if (!$this->userAnswer->contains($userAnswer)) {
  245.             $this->userAnswer[] = $userAnswer;
  246.             $userAnswer->setQuestion($this);
  247.         }
  248.         return $this;
  249.     }
  250.     public function removeUserAnswer(UserAnswer $userAnswer): self
  251.     {
  252.         if ($this->userAnswer->contains($userAnswer)) {
  253.             $this->userAnswer->removeElement($userAnswer);
  254.             // set the owning side to null (unless already changed)
  255.             if ($userAnswer->getQuestion() === $this) {
  256.                 $userAnswer->setQuestion(null);
  257.             }
  258.         }
  259.         return $this;
  260.     }
  261.     public function getSendAnswerTo(): ?string
  262.     {
  263.         return $this->sendAnswerTo;
  264.     }
  265.     public function setSendAnswerTo(?string $sendAnswerTo): self
  266.     {
  267.         $this->sendAnswerTo $sendAnswerTo;
  268.         return $this;
  269.     }
  270.     public function getType(): string
  271.     {
  272.         $counter 0;
  273.         foreach ($this->getAnswer() as $answer)
  274.         {
  275.             if ($answer->getIsTrue()) $counter++;
  276.         }
  277.         if ($counter === 0)
  278.         {
  279.             return 'textarea';
  280.         }
  281.         if ($counter === 1)
  282.         {
  283.             return 'radio';
  284.         }
  285.         return 'checkbox';
  286.     }
  287. }