src/Entity/SurveyFormQuestion.php line 35

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Entity;
  4. use ApiPlatform\Core\Annotation\ApiFilter;
  5. use ApiPlatform\Core\Annotation\ApiResource;
  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\PropertyFilter;
  10. use App\Validator\SurveyFormAnswerAssociation;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\Common\Collections\Collection;
  13. use Doctrine\ORM\Mapping as ORM;
  14. use Gedmo\Timestampable\Traits\TimestampableEntity;
  15. use Greg0ire\Enum\Bridge\Symfony\Validator\Constraint\Enum as EnumAssert;
  16. use Symfony\Component\Serializer\Annotation\Groups;
  17. use Symfony\Component\Validator\Constraints as Assert;
  18. /**
  19.  * @ORM\Entity(repositoryClass="App\Repository\SurveyFormQuestionRepository")
  20.  * @ORM\HasLifecycleCallbacks()
  21.  * @SurveyFormAnswerAssociation()
  22.  */
  23. #[ApiResource(
  24.     attributes: ['security' => "is_granted('ROLE_USER')"],
  25.     collectionOperations: ['get''post' => ['security' => "is_granted('ROLE_ADMIN')"]],
  26.     itemOperations: ['get''put' => ['security' => "is_granted('ROLE_ADMIN') or object.owner == user"], 'delete' => ['security' => "is_granted('ROLE_ADMIN')"]],
  27. )]
  28. #[ApiFilter(PropertyFilter::class)]
  29. #[ApiFilter(OrderFilter::class, properties: ['id''type''category.title''label''ratingActivated''isUpdateProfile''isActive''demographicsType''createdAt'], arguments: ['orderParameterName' => 'order'])]
  30. #[ApiFilter(BooleanFilter::class, properties: ['isUpdateProfile'])]
  31. class SurveyFormQuestion
  32. {
  33.     use TimestampableEntity;
  34.     /**
  35.      * @ORM\Id()
  36.      * @ORM\GeneratedValue()
  37.      * @ORM\Column(type="integer")
  38.      * @Groups({"pinpointCampaign:list"})
  39.      */
  40.     private $id;
  41.     /**
  42.      * @var string
  43.      *
  44.      * @ORM\Column(type="string", length=64)
  45.      * @Assert\Length(max="64")
  46.      * @Assert\NotBlank()
  47.      * @EnumAssert(class="App\Enum\SurveyFormQuestionType")
  48.      * @Groups({"pinpointCampaign:list"})
  49.      */
  50.     #[ApiFilter(SearchFilter::class, strategy'exact')]
  51.     private $type;
  52.     /**
  53.      * @var bool
  54.      *
  55.      * @ORM\Column(type="boolean")
  56.      */
  57.     private $ratingActivated;
  58.     /**
  59.      * @var string
  60.      *
  61.      * @ORM\Column(type="text")
  62.      * @Assert\NotBlank()
  63.      * @Groups({"pinpointCampaign:list"})
  64.      */
  65.     #[ApiFilter(SearchFilter::class, strategy'ipartial')]
  66.     private $label;
  67.     /**
  68.      * @var string
  69.      *
  70.      * @ORM\Column(type="string", length=2000, nullable=true)
  71.      * @Assert\Length(max="2000")
  72.      */
  73.     #[ApiFilter(SearchFilter::class, strategy'ipartial')]
  74.     private $info;
  75.     /**
  76.      * @var string
  77.      *
  78.      * @ORM\Column(type="string", length=128, nullable=true)
  79.      * @Assert\Length(max="128")
  80.      * @Groups({"pinpointCampaign:list"})
  81.      */
  82.     private $placeholder;
  83.     /**
  84.      * @var string
  85.      *
  86.      * @ORM\Column(type="string", length=128, nullable=true)
  87.      * @Assert\Length(max="128")
  88.      * @Groups({"pinpointCampaign:list"})
  89.      */
  90.     private $sliderLabelMin;
  91.     /**
  92.      * @var string
  93.      *
  94.      * @ORM\Column(type="string", length=128, nullable=true)
  95.      * @Assert\Length(max="128")
  96.      * @Groups({"pinpointCampaign:list"})
  97.      */
  98.     private $sliderLabelMax;
  99.     /**
  100.      * @var int
  101.      *
  102.      * @ORM\Column(type="integer")
  103.      * @Assert\NotBlank()
  104.      * @Assert\Type(type="integer")
  105.      * @Groups({"pinpointCampaign:list"})
  106.      */
  107.     private $position;
  108.     /**
  109.      * @var string
  110.      *
  111.      * @ORM\Column(type="string", length=64, nullable=true)
  112.      * @Assert\Length(max="64")
  113.      * @EnumAssert(class="App\Enum\SurveyFormType")
  114.      */
  115.     private $demographicsType;
  116.     /**
  117.      * @var SurveyFormStep
  118.      *
  119.      * @ORM\ManyToOne(targetEntity="App\Entity\SurveyFormStep")
  120.      * @ORM\JoinColumn(nullable=true, name="form_step_id", referencedColumnName="id")
  121.      */
  122.     private $formStep;
  123.     /**
  124.      * @var Category
  125.      *
  126.      * @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="surveyFormQuestions")
  127.      * @ORM\JoinColumn(nullable=true)
  128.      * @Groups({"pinpointCampaign:list"})
  129.      */
  130.     #[ApiFilter(SearchFilter::class)]
  131.     private $category;
  132.     /**
  133.      * @var int
  134.      *
  135.      * @ORM\Column(type="integer")
  136.      * @Assert\Type(type="integer")
  137.      * @Groups({"pinpointCampaign:list"})
  138.      */
  139.     private $maxSelection;
  140.     /**
  141.      * @ORM\Column(type="boolean")
  142.      * @Groups({"pinpointCampaign:list"})
  143.      */
  144.     private $isActive;
  145.     /**
  146.      * @ORM\Column(type="boolean")
  147.      * @Groups({"pinpointCampaign:list"})
  148.      */
  149.     private $isUpdateProfile;
  150.     /**
  151.      * @var string
  152.      *
  153.      * @ORM\Column(type="string", length=100, nullable=true)
  154.      * @Assert\Length(max="100")
  155.      * @Groups({"pinpointCampaign:list"})
  156.      */
  157.     private $profileFieldName;
  158.     /**
  159.      * @var string
  160.      *
  161.      * @ORM\Column(type="string", length=100, nullable=true)
  162.      * @Assert\Length(max="100")
  163.      * @Groups({"pinpointCampaign:list"})
  164.      */
  165.     private $otherAnswerText;
  166.     /**
  167.      * @var string
  168.      *
  169.      * @ORM\Column(type="string", length=100, nullable=true)
  170.      * @Assert\Length(max="100")
  171.      */
  172.     private $subType;
  173.     /**
  174.      * @var string
  175.      *
  176.      * @ORM\Column(type="string", length=10, nullable=true)
  177.      * @Assert\Length(max="10")
  178.      */
  179.     private $isSubType;
  180.     /**
  181.      * @var ArrayCollection|SurveyFormStep[]
  182.      *
  183.      * @ORM\ManyToMany(targetEntity="App\Entity\SurveyFormStep", mappedBy="formQuestions")
  184.      * @ORM\JoinColumn(name="survey_form_step_id", referencedColumnName="id")
  185.      * @ORM\OrderBy({"id" = "ASC"})
  186.      */
  187.     private $formSteps;
  188.     /**
  189.      * @var ArrayCollection|SurveyFormStepSurveyFormQuestion[]
  190.      *
  191.      * @ORM\OneToMany(targetEntity="App\Entity\SurveyFormStepSurveyFormQuestion", mappedBy="surveyFormQuestion", cascade={"persist"}))
  192.      * @ORM\JoinColumn(name="id", referencedColumnName="survey_form_question_id")
  193.      * @ORM\OrderBy({"id" = "ASC"})
  194.      */
  195.     private $surveyFormSteps;
  196.     /**
  197.      * @var ArrayCollection|SurveyCompanySurveyFormQuestion[]
  198.      *
  199.      * @ORM\OneToMany(targetEntity="App\Entity\SurveyCompanySurveyFormQuestion", mappedBy="surveyFormQuestion", cascade={"persist"}))
  200.      * @ORM\JoinColumn(name="id", referencedColumnName="survey_form_question_id")
  201.      * @ORM\OrderBy({"id" = "ASC"})
  202.      */
  203.     private $surveyCompanies;
  204.     /**
  205.      * @var ArrayCollection|SurveyFormAnswer[]
  206.      *
  207.      * @ORM\OneToMany(targetEntity="App\Entity\SurveyFormAnswer", mappedBy="formQuestion",orphanRemoval=true, cascade={"persist","remove"})
  208.      * @ORM\OrderBy({"other" = "ASC","position" = "ASC"})
  209.      * @Groups({"pinpointCampaign:list"})
  210.      */
  211.     private iterable $formAnswers;
  212.     /**
  213.      * @var DateTimeTypeAnswer
  214.      *
  215.      * @ORM\OneToOne(targetEntity=DateTimeTypeAnswer::class, mappedBy="formQuestion", orphanRemoval=true, cascade={"persist", "remove"})
  216.      * @Groups({"pinpointCampaign:list"})
  217.      */
  218.     private $dateTimeTypeAnswer;
  219.     /** @var FileTypeAnswer
  220.      * @ORM\OneToOne(targetEntity=FileTypeAnswer::class, mappedBy="formQuestion", orphanRemoval=true, cascade={"persist", "remove"})
  221.      * @Groups({"pinpointCampaign:list"})
  222.      */
  223.     private $fileTypeAnswer;
  224.     /**
  225.      * @ORM\Column(type="boolean", nullable=true)
  226.      */
  227.     private $hasOtherFormAnswer;
  228.     public function __construct()
  229.     {
  230.         $this->position 0;
  231.         $this->isActive true;
  232.         $this->isUpdateProfile false;
  233.         $this->ratingActivated false;
  234.         $this->maxSelection 0;
  235.         $this->isSubType 'no';
  236.         $this->hasOtherFormAnswer false;
  237.         $this->formSteps = new ArrayCollection();
  238.         $this->formAnswers = new ArrayCollection();
  239.         $this->surveyFormSteps = new ArrayCollection();
  240.         $this->surveyCompanies = new ArrayCollection();
  241.     }
  242.     public function __toString()
  243.     {
  244.         return $this->getId() . ') ' $this->getLabel();
  245.     }
  246.     public function getId(): ?int
  247.     {
  248.         return $this->id;
  249.     }
  250.     public function getType(): ?string
  251.     {
  252.         return $this->type;
  253.     }
  254.     public function setType(string $type): self
  255.     {
  256.         $this->type $type;
  257.         return $this;
  258.     }
  259.     public function getLabel(): ?string
  260.     {
  261.         return $this->label;
  262.     }
  263.     public function setLabel(string $label): self
  264.     {
  265.         $this->label $label;
  266.         return $this;
  267.     }
  268.     public function getInfo(): ?string
  269.     {
  270.         return $this->info;
  271.     }
  272.     public function setInfo(?string $info): self
  273.     {
  274.         $this->info $info;
  275.         return $this;
  276.     }
  277.     public function getPlaceholder(): ?string
  278.     {
  279.         return $this->placeholder;
  280.     }
  281.     public function setPlaceholder(?string $placeholder): self
  282.     {
  283.         $this->placeholder $placeholder;
  284.         return $this;
  285.     }
  286.     public function getSliderLabelMin(): ?string
  287.     {
  288.         return $this->sliderLabelMin;
  289.     }
  290.     public function setSliderLabelMin(?string $sliderLabelMin): self
  291.     {
  292.         $this->sliderLabelMin $sliderLabelMin;
  293.         return $this;
  294.     }
  295.     public function getSliderLabelMax(): ?string
  296.     {
  297.         return $this->sliderLabelMax;
  298.     }
  299.     public function setSliderLabelMax(?string $sliderLabelMax): self
  300.     {
  301.         $this->sliderLabelMax $sliderLabelMax;
  302.         return $this;
  303.     }
  304.     public function getPosition(): ?int
  305.     {
  306.         return $this->position;
  307.     }
  308.     public function setPosition(int $position): self
  309.     {
  310.         $this->position $position;
  311.         return $this;
  312.     }
  313.     public function getMaxSelection(): ?int
  314.     {
  315.         return $this->maxSelection;
  316.     }
  317.     public function setMaxSelection(?int $maxSelection): self
  318.     {
  319.         $this->maxSelection $maxSelection;
  320.         return $this;
  321.     }
  322.     /**
  323.      * @return Collection<int, SurveyFormAnswer>
  324.      */
  325.     public function getFormAnswers(): iterable
  326.     {
  327.         $this->formAnswers->filter(function ($item$i) {
  328.             if (!$item->getPosition() && $item->getOther()) {
  329.                 return $item->setPosition($i 3);
  330.             }
  331.             return $item;
  332.         });
  333.         return $this->formAnswers;
  334.     }
  335.     public function addFormAnswer(?SurveyFormAnswer $formAnswer): self
  336.     {
  337.         if (!$this->formAnswers->contains($formAnswer)) {
  338.             $this->formAnswers[] = $formAnswer;
  339.             $formAnswer->setFormQuestion($this);
  340.         }
  341.         return $this;
  342.     }
  343.     public function removeFormAnswer(?SurveyFormAnswer $formAnswer): self
  344.     {
  345.         if ($this->formAnswers->contains($formAnswer)) {
  346.             $this->formAnswers->removeElement($formAnswer);
  347.             // set the owning side to null (unless already changed)
  348.             if ($formAnswer->getFormQuestion() === $this) {
  349.                 $formAnswer->setFormQuestion(null);
  350.             }
  351.         }
  352.         return $this;
  353.     }
  354.     public function getOtherAnswerText(): ?string
  355.     {
  356.         return $this->otherAnswerText;
  357.     }
  358.     public function setOtherAnswerText(?string $otherAnswerText): self
  359.     {
  360.         $this->otherAnswerText $otherAnswerText;
  361.         return $this;
  362.     }
  363.     public function getRatingActivated(): ?bool
  364.     {
  365.         return $this->ratingActivated;
  366.     }
  367.     public function setRatingActivated(bool $ratingActivated): self
  368.     {
  369.         $this->ratingActivated $ratingActivated;
  370.         return $this;
  371.     }
  372.     public function getDemographicsType(): ?string
  373.     {
  374.         return $this->demographicsType;
  375.     }
  376.     public function setDemographicsType(?string $demographicsType): self
  377.     {
  378.         $this->demographicsType $demographicsType;
  379.         return $this;
  380.     }
  381.     public function getIsActive(): ?bool
  382.     {
  383.         return $this->isActive;
  384.     }
  385.     public function setIsActive(bool $isActive): self
  386.     {
  387.         $this->isActive $isActive;
  388.         return $this;
  389.     }
  390.     public function getIsUpdateProfile(): ?bool
  391.     {
  392.         return $this->isUpdateProfile;
  393.     }
  394.     public function setIsUpdateProfile(bool $isUpdateProfile): self
  395.     {
  396.         $this->isUpdateProfile $isUpdateProfile;
  397.         return $this;
  398.     }
  399.     public function getProfileFieldName(): ?string
  400.     {
  401.         return $this->profileFieldName;
  402.     }
  403.     public function setProfileFieldName(?string $profileFieldName): self
  404.     {
  405.         $this->profileFieldName $profileFieldName;
  406.         return $this;
  407.     }
  408.     public function getCategory(): ?Category
  409.     {
  410.         return $this->category;
  411.     }
  412.     public function setCategory(?Category $category): self
  413.     {
  414.         $this->category $category;
  415.         return $this;
  416.     }
  417.     /**
  418.      * @return Collection|SurveyFormStep[]
  419.      */
  420.     public function getFormSteps(): Collection
  421.     {
  422.         return $this->formSteps;
  423.     }
  424.     public function addFormStep(SurveyFormStep $formSteps): self
  425.     {
  426.         if (!$this->formSteps->contains($formSteps)) {
  427.             $this->formSteps[] = $formSteps;
  428.             $formSteps->addFormQuestion($this);
  429.         }
  430.         return $this;
  431.     }
  432.     public function removeFormStep(SurveyFormStep $formSteps): self
  433.     {
  434.         if ($this->formSteps->contains($formSteps)) {
  435.             $this->formSteps->removeElement($formSteps);
  436.             $formSteps->removeFormQuestion($this);
  437.         }
  438.         return $this;
  439.     }
  440.     /**
  441.      * @return Collection|SurveyFormStepSurveyFormQuestion[]
  442.      */
  443.     public function getSurveyFormSteps(): Collection
  444.     {
  445.         return $this->surveyFormSteps;
  446.     }
  447.     // public function addSurveyFormStep(SurveyFormStepSurveyFormQuestion $surveyFormStep): self
  448.     // {
  449.     //     if (!$this->surveyFormSteps->contains($surveyFormStep)) {
  450.     //         $surveyFormStep->setSurveyFormQuestion($this);
  451.     //         $this->surveyFormSteps[] = $surveyFormStep;
  452.     //     }
  453.     //     return $this;
  454.     // }
  455.     // public function removeSurveyFormStep(SurveyFormStepSurveyFormQuestion $surveyFormStep): self
  456.     // {
  457.     //     if ($this->surveyFormSteps->contains($surveyFormStep)) {
  458.     //         $this->surveyFormSteps->removeElement($surveyFormStep);
  459.     //         // set the owning side to null (unless already changed)
  460.     //         if ($surveyFormStep->getSurveyFormQuestion() === $this) {
  461.     //             $surveyFormStep->setSurveyFormQuestion(null);
  462.     //         }
  463.     //     }
  464.     //     return $this;
  465.     // }
  466.     /**
  467.      * @return Collection|SurveyCompanySurveyFormQuestion[]
  468.      */
  469.     public function getSurveyCompanies(): Collection
  470.     {
  471.         return $this->surveyCompanies;
  472.     }
  473.     public function addSurveyCompany(SurveyCompany $surveyCompany): self
  474.     {
  475.         if (!$this->surveyCompanies->contains($surveyCompany)) {
  476.             $surveyCompany->setSurveyCompany($this);
  477.             $this->surveyCompanies[] = $surveyCompany;
  478.         }
  479.         return $this;
  480.     }
  481.     public function removeSurveyCompany(SurveyCompany $surveyCompany): self
  482.     {
  483.         if ($this->surveyCompanies->contains($surveyCompany)) {
  484.             $this->surveyCompanies->removeElement($surveyCompany);
  485.             // set the owning side to null (unless already changed)
  486.             if ($surveyCompany->getSurveyCompany() === $this) {
  487.                 $surveyCompany->setSurveyCompany(null);
  488.             }
  489.         }
  490.         return $this;
  491.     }
  492.     public function getSubType(): ?string
  493.     {
  494.         return $this->subType;
  495.     }
  496.     public function setSubType(?string $subType): self
  497.     {
  498.         $this->subType $subType;
  499.         return $this;
  500.     }
  501.     public function getIsSubType(): ?string
  502.     {
  503.         return $this->isSubType;
  504.     }
  505.     public function setIsSubType(string $isSubType): self
  506.     {
  507.         $this->isSubType $isSubType;
  508.         return $this;
  509.     }
  510.     public function getDateTimeTypeAnswer(): ?DateTimeTypeAnswer
  511.     {
  512.         return $this->dateTimeTypeAnswer;
  513.     }
  514.     public function setDateTimeTypeAnswer(?DateTimeTypeAnswer $dateTimeTypeAnswer): self
  515.     {
  516.         // unset the owning side of the relation if necessary
  517.         if (null === $dateTimeTypeAnswer && null !== $this->dateTimeTypeAnswer) {
  518.             $this->dateTimeTypeAnswer->setFormQuestion(null);
  519.         }
  520.         // set the owning side of the relation if necessary
  521.         if (null !== $dateTimeTypeAnswer && $dateTimeTypeAnswer->getFormQuestion() && $dateTimeTypeAnswer->getFormQuestion()->getId() !== $this->getId()) {
  522.             $dateTimeTypeAnswer->setFormQuestion($this);
  523.             $this->dateTimeTypeAnswer $dateTimeTypeAnswer;
  524.         }
  525.         // set the owning side of the relation if necessary
  526.         if (null !== $dateTimeTypeAnswer && $dateTimeTypeAnswer->getFormQuestion() && $dateTimeTypeAnswer->getFormQuestion()->getId() == $this->getId()) {
  527.             $dateTimeTypeAnswer->setFormQuestion($this);
  528.             $this->dateTimeTypeAnswer->setDateFormat($dateTimeTypeAnswer->getDateFormat());
  529.             $this->dateTimeTypeAnswer->setDateTime($dateTimeTypeAnswer->getDateTime());
  530.             $this->dateTimeTypeAnswer->setErrorMessage($dateTimeTypeAnswer->getErrorMessage());
  531.         } elseif (null !== $dateTimeTypeAnswer && null === $dateTimeTypeAnswer->getFormQuestion()) {
  532.             $dateTimeTypeAnswer->setFormQuestion($this);
  533.             $this->dateTimeTypeAnswer $dateTimeTypeAnswer;
  534.         }
  535.         return $this;
  536.     }
  537.     public function getFileTypeAnswer(): ?FileTypeAnswer
  538.     {
  539.         return $this->fileTypeAnswer;
  540.     }
  541.     public function setFileTypeAnswer(?FileTypeAnswer $fileTypeAnswer): self
  542.     {
  543.         // unset the owning side of the relation if necessary
  544.         if (null === $fileTypeAnswer && null !== $this->fileTypeAnswer) {
  545.             $this->fileTypeAnswer->setFormQuestion(null);
  546.         }
  547.         // set the owning side of the relation if necessary
  548.         if (null !== $fileTypeAnswer && $fileTypeAnswer->getFormQuestion() && $fileTypeAnswer->getFormQuestion()->getId() !== $this->getId()) {
  549.             $fileTypeAnswer->setFormQuestion($this);
  550.             $this->fileTypeAnswer $fileTypeAnswer;
  551.         }
  552.         // set the owning side of the relation if necessary
  553.         if (null !== $fileTypeAnswer && $fileTypeAnswer->getFormQuestion() && $fileTypeAnswer->getFormQuestion()->getId() == $this->getId()) {
  554.             $this->fileTypeAnswer->setAllowedFiles($fileTypeAnswer->getAllowedFiles());
  555.             $this->fileTypeAnswer->setTypeErrorMessage($fileTypeAnswer->getTypeErrorMessage());
  556.         } elseif (null !== $fileTypeAnswer && null === $fileTypeAnswer->getFormQuestion()) {
  557.             $fileTypeAnswer->setFormQuestion($this);
  558.             $this->fileTypeAnswer $fileTypeAnswer;
  559.         }
  560.         return $this;
  561.     }
  562.     /**
  563.      * @var float
  564.      */
  565.     public function getAverageRating(): float
  566.     {
  567.         $maxScore 0.00;
  568.         if (array_search($this->type, ['choice-unique-collapsed''choice-unique-expanded']) && $this->formAnswers->count() > 0) {
  569.             $totalRating = [];
  570.             foreach ($this->formAnswers as $ans) {
  571.                 $totalRating[] = (float) $ans->getRating();
  572.             }
  573.             $maxScore max($totalRating);
  574.         }
  575.         return $maxScore;
  576.     }
  577.     public function getHasOtherFormAnswer(): ?bool
  578.     {
  579.         return $this->hasOtherFormAnswer;
  580.     }
  581.     public function setHasOtherFormAnswer(?bool $hasOtherFormAnswer): self
  582.     {
  583.         $this->hasOtherFormAnswer $hasOtherFormAnswer;
  584.         return $this;
  585.     }
  586. }