src/Entity/Question.php line 63

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use ApiPlatform\Core\Annotation\ApiSubresource;
  6. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\BooleanFilter;
  7. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  8. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  9. use ApiPlatform\Core\Serializer\Filter\GroupFilter;
  10. use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  11. use App\Repository\QuestionRepository;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use Doctrine\Common\Collections\Collection;
  14. use Doctrine\ORM\Mapping as ORM;
  15. use Gedmo\Mapping\Annotation as Gedmo;
  16. use Symfony\Component\Serializer\Annotation\Groups;
  17. /**
  18.  * @ORM\Entity(repositoryClass=QuestionRepository::class)
  19.  * @ApiResource(
  20.  *     normalizationContext={
  21.  *         "groups"={"question:read"}
  22.  *     },
  23.  *     denormalizationContext={
  24.  *          "groups"={"question:write"}
  25.  *     },
  26.  *     collectionOperations={
  27.  *          "get"={
  28.  *              "security"="is_granted('ROLE_USER') or is_granted('ROLE_ADMIN')" 
  29.  *          },
  30.  *          "post"={
  31.  *              "security"="is_granted('ROLE_ADMIN')"
  32.  *          }
  33.  *     },
  34.  *     itemOperations={
  35.  *          "get"={
  36.  *              "security"="is_granted('ROLE_USER') or is_granted('ROLE_ADMIN')" 
  37.  *          },
  38.  *          "delete"={
  39.  *              "security"="is_granted('ROLE_ADMIN')"
  40.  *          },
  41.  *          "patch"={
  42.  *              "security"="is_granted('ROLE_ADMIN')"
  43.  *          }
  44.  *     },
  45.  *     attributes={"order"={"rank": "ASC", "tvTag.id": "DESC", "id": "ASC"}}
  46.  * )
  47.  * @ApiFilter(SearchFilter::class, properties={"id": "exact", "name": "partial", "survey.type": "exact", "survey.id": "exact", "survey.active": "exact", "survey.name": "partial", "tvTag.id": "exact", "status": "exact"})
  48.  * @ApiFilter(OrderFilter::class, properties={"id", "name", "survey.id", "tvTag.id", "tvTag.name", "active", "rank", "survey.name"})
  49.  * @ApiFilter(GroupFilter::class, arguments={
  50.  *      "parameterName": "groups", 
  51.  *      "overrideDefaultGroups": true
  52.  * })
  53.  * @ApiFilter(PropertyFilter::class, 
  54.  *      arguments={
  55.  *          "parameterName"="fields", 
  56.  *          "overrideDefaultProperties"=true
  57.  *     }
  58.  * )
  59.  */
  60. class Question
  61. {
  62.     /**
  63.      * @ORM\Id
  64.      * @ORM\GeneratedValue
  65.      * @ORM\Column(type="integer")
  66.      * @Groups({"question:read:id", "question:read", "survey:read", "survey_category:read", "question_list", "survey_vitality_front", "answer_recommended_objectives_edit", "teamplay_challenge:read"})
  67.      */
  68.     private $id;
  69.     /**
  70.      * @ORM\Column(type="string", length=255)
  71.      * @Groups({"question:read:name", "question:read", "question:write", "survey:read", "survey_category:read", "question_list", "survey_vitality_front", "answer_recommended_objectives_edit", "teamplay_challenge:write", "teamplay_challenge:read"})
  72.      */
  73.     private $name;
  74.     /**
  75.      * @ORM\Column(type="text", nullable=true)
  76.      * @Groups({"question:read:id", "question:description", "question:write", "survey:read", "question_list", "survey_vitality_front", "teamplay_challenge:write", "teamplay_challenge:read"})
  77.      */
  78.     private $description;
  79.     /**
  80.      * @ORM\Column(type="datetime", nullable=true)
  81.      * @Gedmo\Timestampable(on="update")
  82.      */
  83.     private $updatedAt;
  84.     /**
  85.      * @ORM\Column(type="datetime")
  86.      * @Gedmo\Timestampable(on="create")
  87.      */
  88.     private $createdAt;
  89.     /**
  90.      * @ORM\ManyToOne(targetEntity=Survey::class, inversedBy="questions")
  91.      * @ORM\JoinColumn(nullable=false)
  92.      * @Groups({"question:read:survey", "question:read", "question:write"})
  93.      */
  94.     private $survey;
  95.     /**
  96.      * @ORM\OneToMany(targetEntity=Answer::class, mappedBy="question", orphanRemoval=true, cascade={"persist", "refresh", "remove"})
  97.      * @Groups({"question:read", "question:read:answers", "survey:read", "survey_vitality_front", "teamplay_challenge:write", "teamplay_challenge:read"})
  98.      */
  99.     private $answers;
  100.     /**
  101.      * @ORM\OneToMany(targetEntity=UserResponse::class, mappedBy="question", orphanRemoval=true)
  102.      */
  103.     private $userResponses;
  104.     /**
  105.      * @ORM\ManyToOne(targetEntity=TvTag::class, inversedBy="questions")
  106.      * @Groups({"question:read:tvTag", "question:read", "question:write", "survey:read"})
  107.      */
  108.     private $tvTag;
  109.     /**
  110.      * @ORM\Column(type="boolean", options={"default": "1"})
  111.      * @Groups({"question:read:active", "question:read", "question:write", "survey:read", "question_list", "survey_vitality_front", "teamplay_challenge:write", "teamplay_challenge:read"})
  112.      */
  113.     private $active true;
  114.     /**
  115.      * @ORM\Column(name="`rank`", type="integer")
  116.      * @Groups({"question:read:rank", "question:read", "question:write", "survey:read", "question_list", "teamplay_challenge:write", "teamplay_challenge:read"})
  117.      */
  118.     private $rank;
  119.     /**
  120.      * @ORM\Column(type="boolean", options={"comment":"Rend la question obligatoire", "default": "1"})
  121.      * @Groups({"question:read:isRequired", "question:read", "question:write", "survey:read", "question_list", "survey_vitality_front", "teamplay_challenge:write", "teamplay_challenge:read"})
  122.      */
  123.     private $isRequired true;
  124.     /**
  125.      * @ORM\Column(type="boolean", options={"comment":"Question à choix multiples", "default": "0"})
  126.      * @Groups({"question:read:isMultiple", "question:read", "question:write", "survey:read", "question_list", "survey_vitality_front", "teamplay_challenge:write", "teamplay_challenge:read"})
  127.      */
  128.     private $isMultiple false;
  129.     /**
  130.      * @var ?int
  131.      * @Groups({"question:read:nbAsked", "question:read", "survey:read", "teamplay_challenge:read"})
  132.      */
  133.     public $nbAsked 0;
  134.     /**
  135.      * @var ?bool
  136.      * @Groups({"question:read:isAsked", "question:read", "survey:read", "teamplay_challenge:read"})
  137.      */
  138.     public $isAsked false;
  139.     public function __construct()
  140.     {
  141.         $this->answers = new ArrayCollection();
  142.         $this->userResponses = new ArrayCollection();
  143.         $this->active true;
  144.         $this->rank 999;
  145.     }
  146.     public function __clone()
  147.     {
  148.         $this->id null;
  149.         $this->userResponses = new ArrayCollection();
  150.         $this->active true;
  151.         $this->rank 999;
  152.         $this->createdAt = new \DateTime();
  153.         $this->updatedAt = new \DateTime();
  154.         // Copy des Answers
  155.         $answers = new ArrayCollection();
  156.         foreach ($this->answers->toArray() as $answer) {
  157.             $newAnswer = clone $answer;
  158.             $newAnswer->setQuestion($this);
  159.             $answers->add($newAnswer);
  160.         }
  161.         $this->answers $answers;
  162.     }
  163.     public function getId(): ?int
  164.     {
  165.         return $this->id;
  166.     }
  167.     public function setId(?int $id): self
  168.     {
  169.         $this->id $id;
  170.         return $this;
  171.     }
  172.     public function getName(): ?string
  173.     {
  174.         return $this->name;
  175.     }
  176.     public function setName(string $name): self
  177.     {
  178.         $this->name $name;
  179.         return $this;
  180.     }
  181.     public function getDescription(): ?string
  182.     {
  183.         return $this->description;
  184.     }
  185.     public function setDescription(?string $description): self
  186.     {
  187.         $this->description $description;
  188.         return $this;
  189.     }
  190.     public function getUpdatedAt(): ?\DateTimeInterface
  191.     {
  192.         return $this->updatedAt;
  193.     }
  194.     public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
  195.     {
  196.         $this->updatedAt $updatedAt;
  197.         return $this;
  198.     }
  199.     public function getCreatedAt(): ?\DateTimeInterface
  200.     {
  201.         return $this->createdAt;
  202.     }
  203.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  204.     {
  205.         $this->createdAt $createdAt;
  206.         return $this;
  207.     }
  208.     public function getSurvey(): ?Survey
  209.     {
  210.         return $this->survey;
  211.     }
  212.     public function setSurvey(?Survey $survey): self
  213.     {
  214.         $this->survey $survey;
  215.         return $this;
  216.     }
  217.     /**
  218.      * @return Collection|Answer[]
  219.      */
  220.     public function getAnswers(): Collection
  221.     {
  222.         return $this->answers;
  223.     }
  224.     public function addAnswer(Answer $answer): self
  225.     {
  226.         if (!$this->answers->contains($answer)) {
  227.             $this->answers[] = $answer;
  228.             $answer->setQuestion($this);
  229.         }
  230.         return $this;
  231.     }
  232.     public function removeAnswer(Answer $answer): self
  233.     {
  234.         if ($this->answers->removeElement($answer)) {
  235.             // set the owning side to null (unless already changed)
  236.             if ($answer->getQuestion() === $this) {
  237.                 $answer->setQuestion(null);
  238.             }
  239.         }
  240.         return $this;
  241.     }
  242.     /**
  243.      * @return Collection|UserResponse[]
  244.      */
  245.     public function getUserResponses(): Collection
  246.     {
  247.         return $this->userResponses;
  248.     }
  249.     public function addUserResponse(UserResponse $userResponse): self
  250.     {
  251.         if (!$this->userResponses->contains($userResponse)) {
  252.             $this->userResponses[] = $userResponse;
  253.             $userResponse->setQuestion($this);
  254.         }
  255.         return $this;
  256.     }
  257.     public function removeUserResponse(UserResponse $userResponse): self
  258.     {
  259.         if ($this->userResponses->removeElement($userResponse)) {
  260.             // set the owning side to null (unless already changed)
  261.             if ($userResponse->getQuestion() === $this) {
  262.                 $userResponse->setQuestion(null);
  263.             }
  264.         }
  265.         return $this;
  266.     }
  267.     public function getTvTag(): ?TvTag
  268.     {
  269.         return $this->tvTag;
  270.     }
  271.     public function setTvTag(?TvTag $tvTag): self
  272.     {
  273.         $this->tvTag $tvTag;
  274.         return $this;
  275.     }
  276.     public function getActive(): ?bool
  277.     {
  278.         return $this->active;
  279.     }
  280.     public function setActive(bool $active): self
  281.     {
  282.         $this->active $active;
  283.         return $this;
  284.     }
  285.     public function getRank(): ?int
  286.     {
  287.         return $this->rank;
  288.     }
  289.     public function setRank(int $rank): self
  290.     {
  291.         $this->rank $rank;
  292.         return $this;
  293.     }
  294.     public function getIsRequired(): ?bool
  295.     {
  296.         return $this->isRequired;
  297.     }
  298.     public function setIsRequired(bool $isRequired): self
  299.     {
  300.         $this->isRequired $isRequired;
  301.         return $this;
  302.     }
  303.     public function getIsMultiple(): ?bool
  304.     {
  305.         return $this->isMultiple;
  306.     }
  307.     public function isMultiple(): ?bool
  308.     {
  309.         return $this->isMultiple;
  310.     }
  311.     public function setIsMultiple(bool $isMultiple): self
  312.     {
  313.         $this->isMultiple $isMultiple;
  314.         return $this;
  315.     }
  316. }