src/Entity/Survey.php line 127

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use ApiPlatform\Core\Annotation\ApiSubresource;
  5. use ApiPlatform\Core\Annotation\ApiFilter;
  6. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ExistsFilter;
  7. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  8. use ApiPlatform\Core\Serializer\Filter\GroupFilter;
  9. use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  10. use App\Repository\SurveyRepository;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\Common\Collections\Collection;
  13. use Doctrine\ORM\Mapping as ORM;
  14. use Gedmo\Mapping\Annotation as Gedmo;
  15. use Symfony\Component\Serializer\Annotation\Groups;
  16. use Symfony\Component\Validator\Constraints as Assert;
  17. /**
  18.  * @ORM\Entity(repositoryClass=SurveyRepository::class)
  19.  * @ApiResource(
  20.  *     normalizationContext={
  21.  *         "groups"={"survey:read"}
  22.  *     },
  23.  *     denormalizationContext={
  24.  *          "groups"={"survey:write"}
  25.  *     },
  26.  *     collectionOperations={
  27.  *          "get"={
  28.  *              "security"="is_granted('ROLE_USER')"
  29.  *          },
  30.  *          "post"={
  31.  *              "security"="is_granted('ROLE_ADMIN')"
  32.  *          },
  33.  *          "get_surveys_homepage"={
  34.  *              "security"="is_granted('ROLE_USER')",
  35.  *              "path"="/surveys_homepage",
  36.  *              "method"="GET",
  37.  *              "controller"="App\Controller\Api\SurveyController::getHomepageSurvey",
  38.  *              "openapi_context"={
  39.  *                  "summary"="Fetches homepage survey",
  40.  *              },
  41.  *          },
  42.  *          "get_answers_surveys_homepage"={
  43.  *              "security"="is_granted('ROLE_USER')",
  44.  *              "path"="/surveys_homepage/answers",
  45.  *              "method"="GET",
  46.  *              "controller"="App\Controller\Api\SurveyController::getAnswersHomepageSurveyBO",
  47.  *              "openapi_context"={
  48.  *                  "summary"="Fetches answers homepage survey",
  49.  *              },
  50.  *          },
  51.  *          "get_surveys_programs"={
  52.  *              "security"="is_granted('ROLE_USER')",
  53.  *              "path"="/surveys_program",
  54.  *              "method"="GET",
  55.  *              "controller"="App\Controller\Api\SurveyController::getProgramsSurvey",
  56.  *              "openapi_context"={
  57.  *                  "summary"="Fetches all program survey",
  58.  *              },
  59.  *          },
  60.  *          "get_answers_surveys_programs"={
  61.  *              "security"="is_granted('ROLE_USER')",
  62.  *              "path"="/programs/{id}/surveys/answers",
  63.  *              "method"="GET",
  64.  *              "controller"="App\Controller\Api\SurveyController::getAnswersProgramsSurvey",
  65.  *              "openapi_context"={
  66.  *                  "summary"="Fetches all program survey",
  67.  *              },
  68.  *          },
  69.  *          "display_program_survey"={
  70.  *              "security"="is_granted('ROLE_USER')",
  71.  *              "path"="/programs/{id}/surveys/display",
  72.  *              "method"="GET",
  73.  *              "controller"="App\Controller\Api\SurveyController::displayProgramSurvey",
  74.  *              "openapi_context"={
  75.  *                  "summary"="Fetches all program survey",
  76.  *              },
  77.  *          },
  78.  *          "get_count_submited_survey"={
  79.  *              "security"="is_granted('ROLE_USER')",
  80.  *              "path"="/surveys/{id}/count-submited",
  81.  *              "method"="GET",
  82.  *              "controller"="App\Controller\Api\SurveyController::countSumbitedSurvey",
  83.  *              "openapi_context"={
  84.  *                  "summary"="Count number of submited survey",
  85.  *              },
  86.  *          },
  87.  *          "create_vitality_tag_survey"={
  88.  *              "description"="Create new survey type = vitality_survey",
  89.  *              "path"="/vitality_surveys",
  90.  *              "method"="POST",
  91.  *              "controller"="App\Controller\Api\SurveyController::createVitalityTagSurvey",
  92.  *              "security"="is_granted('ROLE_ADMIN')"
  93.  *          },
  94.  *     },
  95.  *     itemOperations={
  96.  *          "get"={
  97.  *              "security"="is_granted('ROLE_USER')"
  98.  *          },
  99.  *          "delete"={
  100.  *              "security"="is_granted('ROLE_ADMIN')"
  101.  *          },
  102.  *          "patch"={
  103.  *              "security"="is_granted('ROLE_ADMIN')"
  104.  *          }
  105.  *     }
  106.  * )
  107.  * @ApiFilter(SearchFilter::class, strategy="exact", properties={
  108.  *      "id", "type","questions.active","questions.answers.active", "active", "tvTag.id"
  109.  * })
  110.  * @ApiFilter(ExistsFilter::class, properties={"tvTag"})
  111.  * @ORM\HasLifecycleCallbacks
  112.  * @ApiFilter(GroupFilter::class, arguments={
  113.  *      "parameterName": "groups", 
  114.  *      "overrideDefaultGroups": true, 
  115.  *      "whitelist": {"survey_list", "survey_vitality_front", "recommendation_survey_edit"}
  116.  * })
  117.  * @ApiFilter(PropertyFilter::class, 
  118.  *      arguments={
  119.  *          "parameterName"="fields", 
  120.  *          "overrideDefaultProperties"=true
  121.  *     }
  122.  * )
  123.  */
  124. class Survey
  125. {
  126.     const TYPE_HOMEPAGE 'homepage_survey';
  127.     const TYPE_PROGRAM 'program_survey';
  128.     const TYPE_VITALITY 'vitality_survey';
  129.     const TYPE_TEAMPLAY_QUIZ 'teamplay_quiz';
  130.     const TYPES = [
  131.         self::TYPE_HOMEPAGE,
  132.         self::TYPE_PROGRAM,
  133.         self::TYPE_VITALITY,
  134.         self::TYPE_TEAMPLAY_QUIZ,
  135.     ];
  136.     /**
  137.      * @ORM\Id
  138.      * @ORM\GeneratedValue
  139.      * @ORM\Column(type="integer")
  140.      * @Groups({"survey:read:id", "survey:read", "question:read", "survey_list", "survey_vitality_front", "recommendation_survey_edit", "teamplay_challenge:read", "program:read"})
  141.      */
  142.     private $id;
  143.     /**
  144.      * @ORM\Column(type="string", length=255)
  145.      * @Groups({"survey:read:name", "survey:read", "survey:write", "question:read", "survey_list", "survey_vitality_front", "recommendation_survey_edit", "teamplay_challenge:write", "teamplay_challenge:read", "program:read"})
  146.      */
  147.     private $name;
  148.     /**
  149.      * Defines the origin of the survey in witch page will it be displayed
  150.      * (ex: a type 'homepage_survey' is a survey to be displayed in an HP)
  151.      * @ORM\Column(type="string", length=255)
  152.      * @Assert\Choice(choices=self::TYPES)
  153.      * @Assert\NotBlank
  154.      * @Groups({"survey:read:type", "survey:read", "survey:write", "question:read", "survey_list", "teamplay_challenge:write", "teamplay_challenge:read"})
  155.      */
  156.     private $type;
  157.     /**
  158.      * @ORM\Column(type="text", nullable=true)
  159.      * @Groups({"survey:read:description", "survey:read", "survey:write", "survey_list", "survey_vitality_front", "recommendation_survey_edit", "teamplay_challenge:write", "teamplay_challenge:read"})
  160.      */
  161.     private $description;
  162.     /**
  163.      * @ORM\OneToMany(targetEntity=Question::class, mappedBy="survey", orphanRemoval=true, cascade={"persist", "refresh", "remove"})
  164.      * @ApiSubresource(maxDepth=1)
  165.      * @Groups({"survey:read:questions", "survey:read", "program:read", "survey_vitality_front", "teamplay_challenge:write", "teamplay_challenge:read"})
  166.      */
  167.     private $questions;
  168.     /**
  169.      * @ORM\ManyToMany(targetEntity=Program::class, inversedBy="surveys")
  170.      * @Groups({"survey:read:programs", "survey:read", "survey:write"})
  171.      */
  172.     private $programs;
  173.     /**
  174.      * @ORM\Column(type="boolean", options={"default": 0})
  175.      * @Groups({"survey:read:active", "survey:read", "survey:write", "survey_list", "recommendation_survey_edit", "teamplay_challenge:write", "teamplay_challenge:read", "program:read"})
  176.      */
  177.     private $active false;
  178.     /**
  179.      * @ORM\ManyToOne(targetEntity=TvTag::class)
  180.      * @Groups({"survey:read:tvTag", "survey:read", "survey:write", "survey_list", "survey_vitality_front", "recommendation_survey_edit"})
  181.      */
  182.     private $tvTag null;
  183.     /**
  184.      * @ORM\OneToMany(targetEntity=SurveyConfig::class, mappedBy="survey")
  185.      * @Groups({"survey:read:configs", "survey:read"})
  186.      */
  187.     private $configs;
  188.     /**
  189.      * @ORM\Column(type="datetime", nullable=true)
  190.      * @Gedmo\Timestampable(on="update")
  191.      */
  192.     private $updatedAt;
  193.     /**
  194.      * @ORM\Column(type="datetime")
  195.      * @Gedmo\Timestampable(on="create")
  196.      */
  197.     private $createdAt;
  198.     /**
  199.      * @ORM\OneToMany(targetEntity=UserResponse::class, mappedBy="survey")
  200.      */
  201.     private $userResponses;
  202.     /**
  203.      * @Groups({"survey:read:nbSubmited", "survey:read", "program:read", "teamplay_challenge:read"})
  204.      */
  205.     public $nbSubmited 0;
  206.     /**
  207.      * @Groups({"survey:read:tvTags", "survey:read"})
  208.      */
  209.     public $tvTags null;
  210.     /**
  211.      * @Groups({"survey:read:tvTags", "survey:read", "program:read", "teamplay_challenge:read"})
  212.      */
  213.     public $stopAsk false;
  214.     /**
  215.      * @ORM\OneToMany(targetEntity=TeamplayChallenge::class, mappedBy="survey", cascade={"persist", "refresh", "remove"}, orphanRemoval=true)
  216.      */
  217.     private $teamplayChallenges;
  218.     /**
  219.      * @Groups({"survey:read:motivationPhrase", "survey:read", "program:read", "teamplay_challenge:read"})
  220.      */
  221.     private $motivationPhrase
  222.     /**
  223.      * @Groups({"survey:read:motivationPhrase1", "survey:read", "program:read", "teamplay_challenge:read"})
  224.      */
  225.     private $motivationPhrase1
  226.     /**
  227.      * @Groups({"survey:read:motivationPhrase2", "survey:read", "program:read", "teamplay_challenge:read"})
  228.      */
  229.     private $motivationPhrase2
  230.     /**
  231.      * @Groups({"survey:read:motivationPhrase3", "survey:read", "program:read", "teamplay_challenge:read"})
  232.      */    
  233.     private $motivationPhrase3
  234.     /**
  235.      * @Groups({"survey:read:motivationPhrase4", "survey:read", "program:read", "teamplay_challenge:read"})
  236.      */
  237.     private $motivationPhrase4
  238.     public function __construct()
  239.     {
  240.         $this->questions = new ArrayCollection();
  241.         $this->programs = new ArrayCollection();
  242.         $this->configs = new ArrayCollection();
  243.         $this->userResponses = new ArrayCollection();
  244.         $this->teamplayChallenges = new ArrayCollection();
  245.     }
  246.     public function __clone()
  247.     {
  248.         $this->id null;
  249.         $this->programs = new ArrayCollection();
  250.         $this->configs = new ArrayCollection();
  251.         $this->userResponses = new ArrayCollection();
  252.         $this->teamplayChallenges = new ArrayCollection();
  253.         $this->active false;
  254.         $this->createdAt = new \DateTime();
  255.         $this->updatedAt = new \DateTime();
  256.         // Copy des Questions
  257.         $questions = new ArrayCollection();
  258.         foreach ($this->questions->toArray() as $question) {
  259.             $newQuestion = clone $question;
  260.             $newQuestion->setSurvey($this);
  261.             $questions->add($newQuestion);
  262.         }
  263.         $this->questions $questions;
  264.     }
  265.     public function getId(): ?int
  266.     {
  267.         return $this->id;
  268.     }
  269.     public function setId(?int $id): self
  270.     {
  271.         $this->id $id;
  272.         return $this;
  273.     }
  274.     public function getName(): ?string
  275.     {
  276.         return $this->name;
  277.     }
  278.     public function setName(string $name): Survey
  279.     {
  280.         $this->name $name;
  281.         return $this;
  282.     }
  283.     public function getDescription(): ?string
  284.     {
  285.         return $this->description;
  286.     }
  287.     public function setDescription(?string $description): Survey
  288.     {
  289.         $this->description $description;
  290.         return $this;
  291.     }
  292.     public function getUpdatedAt(): ?\DateTimeInterface
  293.     {
  294.         return $this->updatedAt;
  295.     }
  296.     public function setUpdatedAt(?\DateTimeInterface $updatedAt): Survey
  297.     {
  298.         $this->updatedAt $updatedAt;
  299.         return $this;
  300.     }
  301.     public function getCreatedAt(): ?\DateTimeInterface
  302.     {
  303.         return $this->createdAt;
  304.     }
  305.     public function setCreatedAt(\DateTimeInterface $createdAt): Survey
  306.     {
  307.         $this->createdAt $createdAt;
  308.         return $this;
  309.     }
  310.     /**
  311.      * @return Collection|Question[]
  312.      */
  313.     public function getQuestions(): Collection
  314.     {
  315.         return $this->questions;
  316.     }
  317.     public function addQuestion(Question $question): Survey
  318.     {
  319.         if (!$this->questions->contains($question)) {
  320.             $this->questions[] = $question;
  321.             $question->setSurvey($this);
  322.         }
  323.         return $this;
  324.     }
  325.     public function removeQuestion(Question $question): Survey
  326.     {
  327.         if ($this->questions->removeElement($question)) {
  328.             // set the owning side to null (unless already changed)
  329.             if ($question->getSurvey() === $this) {
  330.                 $question->setSurvey(null);
  331.             }
  332.         }
  333.         return $this;
  334.     }
  335.     /**
  336.      * @return Collection|Program[]
  337.      */
  338.     public function getPrograms(): Collection
  339.     {
  340.         return $this->programs;
  341.     }
  342.     public function addProgram(Program $program): Survey
  343.     {
  344.         if (!$this->programs->contains($program)) {
  345.             $this->programs[] = $program;
  346.         }
  347.         return $this;
  348.     }
  349.     public function removeProgram(Program $program): Survey
  350.     {
  351.         $this->programs->removeElement($program);
  352.         return $this;
  353.     }
  354.     /**
  355.      * @return mixed
  356.      */
  357.     public function getType()
  358.     {
  359.         return $this->type;
  360.     }
  361.     /**
  362.      * @param mixed $type
  363.      * @return Survey
  364.      */
  365.     public function setType($type): Survey
  366.     {
  367.         $this->type $type;
  368.         return $this;
  369.     }
  370.     public function getActive(): ?bool
  371.     {
  372.         return $this->active;
  373.     }
  374.     public function setActive(bool $active): self
  375.     {
  376.         $this->active $active;
  377.         return $this;
  378.     }
  379.     public function getTvTag(): ?TvTag
  380.     {
  381.         return $this->tvTag;
  382.     }
  383.     public function setTvTag(?TvTag $tvTag): self
  384.     {
  385.         $this->tvTag $tvTag;
  386.         return $this;
  387.     }
  388.     /**
  389.      * @return Collection|SurveyConfig[]
  390.      */
  391.     public function getConfigs(): Collection
  392.     {
  393.         return $this->configs;
  394.     }
  395.     public function addConfig(SurveyConfig $config): self
  396.     {
  397.         if (!$this->configs->contains($config)) {
  398.             $this->configs[] = $config;
  399.             $config->setSurvey($this);
  400.         }
  401.         return $this;
  402.     }
  403.     public function removeConfig(SurveyConfig $config): self
  404.     {
  405.         if ($this->configs->removeElement($config)) {
  406.             // set the owning side to null (unless already changed)
  407.             if ($config->getSurvey() === $this) {
  408.                 $config->setSurvey(null);
  409.             }
  410.         }
  411.         return $this;
  412.     }
  413.     /**
  414.      * @Groups({"recommendation_survey_edit"})
  415.      */
  416.     public function getMotivationPhrase(): ?string
  417.     {
  418.         $this->motivationPhrase null;
  419.         if(!empty($this->configs->toArray())) {
  420.             foreach ($this->configs->toArray() as $config) {
  421.                 if($config instanceof SurveyConfig && $config->getType() === SurveyConfig::RECOMMENDED_OBJECTIVES 
  422.                     && in_array($config->getSubType(), SurveyConfig::INTERVALLES)){
  423.                        $this->motivationPhrase $config->getValue(); 
  424.                 }
  425.             }
  426.         }
  427.         return $this->motivationPhrase;
  428.     }
  429.     /**
  430.      * @Groups({"recommendation_survey_edit"})
  431.      */
  432.     public function getMotivationPhrase1(): ?string
  433.     {
  434.         $this->motivationPhrase1 null;
  435.         if(!empty($this->configs->toArray())) {
  436.             foreach ($this->configs->toArray() as $config) {
  437.                 if($config instanceof SurveyConfig && $config->getType() === SurveyConfig::RECOMMENDED_OBJECTIVES 
  438.                     && $config->getSubType() === SurveyConfig::INTERVALLE_1){
  439.                        $this->motivationPhrase1 $config->getValue(); 
  440.                        break;
  441.                 }
  442.             }
  443.         }
  444.         return $this->motivationPhrase1;
  445.     }
  446.     /**
  447.      * @Groups({"recommendation_survey_edit"})
  448.      */
  449.     public function getMotivationPhrase2(): ?string
  450.     {
  451.         $this->motivationPhrase2 null;
  452.         if(!empty($this->configs->toArray())) {
  453.             foreach ($this->configs->toArray() as $config) {
  454.                 if($config instanceof SurveyConfig && $config->getType() === SurveyConfig::RECOMMENDED_OBJECTIVES 
  455.                     && $config->getSubType() === SurveyConfig::INTERVALLE_2){
  456.                        $this->motivationPhrase2 $config->getValue(); 
  457.                        break;
  458.                 }
  459.             }
  460.         }
  461.         return $this->motivationPhrase2;
  462.     }
  463.     /**
  464.      * @Groups({"recommendation_survey_edit"})
  465.      */
  466.     public function getMotivationPhrase3(): ?string
  467.     {
  468.         $this->motivationPhrase3 null;
  469.         if(!empty($this->configs->toArray())) {
  470.             foreach ($this->configs->toArray() as $config) {
  471.                 if($config instanceof SurveyConfig && $config->getType() === SurveyConfig::RECOMMENDED_OBJECTIVES 
  472.                     && $config->getSubType() === SurveyConfig::INTERVALLE_3){
  473.                        $this->motivationPhrase3 $config->getValue(); 
  474.                        break;
  475.                 }
  476.             }
  477.         }
  478.         return $this->motivationPhrase3;
  479.     }
  480.     /**
  481.      * @Groups({"recommendation_survey_edit"})
  482.      */
  483.     public function getMotivationPhrase4(): ?string
  484.     {
  485.         $this->motivationPhrase4 null;
  486.         if(!empty($this->configs->toArray())) {
  487.             foreach ($this->configs->toArray() as $config) {
  488.                 if($config instanceof SurveyConfig && $config->getType() === SurveyConfig::RECOMMENDED_OBJECTIVES 
  489.                     && $config->getSubType() === SurveyConfig::INTERVALLE_4){
  490.                        $this->motivationPhrase4 $config->getValue(); 
  491.                        break;
  492.                 }
  493.             }
  494.         }
  495.         return $this->motivationPhrase4;
  496.     }
  497.     /**
  498.      * @return Collection|UserResponse[]
  499.      */
  500.     public function getUserResponses(): Collection
  501.     {
  502.         return $this->userResponses;
  503.     }
  504.     public function addUserResponse(UserResponse $userResponse): self
  505.     {
  506.         if (!$this->userResponses->contains($userResponse)) {
  507.             $this->userResponses[] = $userResponse;
  508.             $userResponse->setSurvey($this);
  509.         }
  510.         return $this;
  511.     }
  512.     public function removeUserResponse(UserResponse $userResponse): self
  513.     {
  514.         if ($this->userResponses->removeElement($userResponse)) {
  515.             // set the owning side to null (unless already changed)
  516.             if ($userResponse->getSurvey() === $this) {
  517.                 $userResponse->setSurvey(null);
  518.             }
  519.         }
  520.         return $this;
  521.     }
  522.     /**
  523.      * @return Collection|TeamplayChallenge[]
  524.      */
  525.     public function getTeamplayChallenges(): Collection
  526.     {
  527.         return $this->teamplayChallenges;
  528.     }
  529.     public function addTeamplayChallenge(TeamplayChallenge $teamplayChallenge): self
  530.     {
  531.         if (!$this->teamplayChallenges->contains($teamplayChallenge)) {
  532.             $this->teamplayChallenges[] = $teamplayChallenge;
  533.             $teamplayChallenge->setSurvey($this);
  534.         }
  535.         return $this;
  536.     }
  537.     public function removeTeamplayChallenge(TeamplayChallenge $teamplayChallenge): self
  538.     {
  539.         if ($this->teamplayChallenges->removeElement($teamplayChallenge)) {
  540.             // set the owning side to null (unless already changed)
  541.             if ($teamplayChallenge->getSurvey() === $this) {
  542.                 $teamplayChallenge->setSurvey(null);
  543.             }
  544.         }
  545.         return $this;
  546.     }
  547. }