<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiSubresource;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ExistsFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Serializer\Filter\GroupFilter;
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
use App\Repository\SurveyRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass=SurveyRepository::class)
* @ApiResource(
* normalizationContext={
* "groups"={"survey:read"}
* },
* denormalizationContext={
* "groups"={"survey:write"}
* },
* collectionOperations={
* "get"={
* "security"="is_granted('ROLE_USER')"
* },
* "post"={
* "security"="is_granted('ROLE_ADMIN')"
* },
* "get_surveys_homepage"={
* "security"="is_granted('ROLE_USER')",
* "path"="/surveys_homepage",
* "method"="GET",
* "controller"="App\Controller\Api\SurveyController::getHomepageSurvey",
* "openapi_context"={
* "summary"="Fetches homepage survey",
* },
* },
* "get_answers_surveys_homepage"={
* "security"="is_granted('ROLE_USER')",
* "path"="/surveys_homepage/answers",
* "method"="GET",
* "controller"="App\Controller\Api\SurveyController::getAnswersHomepageSurveyBO",
* "openapi_context"={
* "summary"="Fetches answers homepage survey",
* },
* },
* "get_surveys_programs"={
* "security"="is_granted('ROLE_USER')",
* "path"="/surveys_program",
* "method"="GET",
* "controller"="App\Controller\Api\SurveyController::getProgramsSurvey",
* "openapi_context"={
* "summary"="Fetches all program survey",
* },
* },
* "get_answers_surveys_programs"={
* "security"="is_granted('ROLE_USER')",
* "path"="/programs/{id}/surveys/answers",
* "method"="GET",
* "controller"="App\Controller\Api\SurveyController::getAnswersProgramsSurvey",
* "openapi_context"={
* "summary"="Fetches all program survey",
* },
* },
* "display_program_survey"={
* "security"="is_granted('ROLE_USER')",
* "path"="/programs/{id}/surveys/display",
* "method"="GET",
* "controller"="App\Controller\Api\SurveyController::displayProgramSurvey",
* "openapi_context"={
* "summary"="Fetches all program survey",
* },
* },
* "get_count_submited_survey"={
* "security"="is_granted('ROLE_USER')",
* "path"="/surveys/{id}/count-submited",
* "method"="GET",
* "controller"="App\Controller\Api\SurveyController::countSumbitedSurvey",
* "openapi_context"={
* "summary"="Count number of submited survey",
* },
* },
* "create_vitality_tag_survey"={
* "description"="Create new survey type = vitality_survey",
* "path"="/vitality_surveys",
* "method"="POST",
* "controller"="App\Controller\Api\SurveyController::createVitalityTagSurvey",
* "security"="is_granted('ROLE_ADMIN')"
* },
* },
* itemOperations={
* "get"={
* "security"="is_granted('ROLE_USER')"
* },
* "delete"={
* "security"="is_granted('ROLE_ADMIN')"
* },
* "patch"={
* "security"="is_granted('ROLE_ADMIN')"
* }
* }
* )
* @ApiFilter(SearchFilter::class, strategy="exact", properties={
* "id", "type","questions.active","questions.answers.active", "active", "tvTag.id"
* })
* @ApiFilter(ExistsFilter::class, properties={"tvTag"})
* @ORM\HasLifecycleCallbacks
* @ApiFilter(GroupFilter::class, arguments={
* "parameterName": "groups",
* "overrideDefaultGroups": true,
* "whitelist": {"survey_list", "survey_vitality_front", "recommendation_survey_edit"}
* })
* @ApiFilter(PropertyFilter::class,
* arguments={
* "parameterName"="fields",
* "overrideDefaultProperties"=true
* }
* )
*/
class Survey
{
const TYPE_HOMEPAGE = 'homepage_survey';
const TYPE_PROGRAM = 'program_survey';
const TYPE_VITALITY = 'vitality_survey';
const TYPE_TEAMPLAY_QUIZ = 'teamplay_quiz';
const TYPES = [
self::TYPE_HOMEPAGE,
self::TYPE_PROGRAM,
self::TYPE_VITALITY,
self::TYPE_TEAMPLAY_QUIZ,
];
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"survey:read:id", "survey:read", "question:read", "survey_list", "survey_vitality_front", "recommendation_survey_edit", "teamplay_challenge:read", "program:read"})
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @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"})
*/
private $name;
/**
* Defines the origin of the survey in witch page will it be displayed
* (ex: a type 'homepage_survey' is a survey to be displayed in an HP)
* @ORM\Column(type="string", length=255)
* @Assert\Choice(choices=self::TYPES)
* @Assert\NotBlank
* @Groups({"survey:read:type", "survey:read", "survey:write", "question:read", "survey_list", "teamplay_challenge:write", "teamplay_challenge:read"})
*/
private $type;
/**
* @ORM\Column(type="text", nullable=true)
* @Groups({"survey:read:description", "survey:read", "survey:write", "survey_list", "survey_vitality_front", "recommendation_survey_edit", "teamplay_challenge:write", "teamplay_challenge:read"})
*/
private $description;
/**
* @ORM\OneToMany(targetEntity=Question::class, mappedBy="survey", orphanRemoval=true, cascade={"persist", "refresh", "remove"})
* @ApiSubresource(maxDepth=1)
* @Groups({"survey:read:questions", "survey:read", "program:read", "survey_vitality_front", "teamplay_challenge:write", "teamplay_challenge:read"})
*/
private $questions;
/**
* @ORM\ManyToMany(targetEntity=Program::class, inversedBy="surveys")
* @Groups({"survey:read:programs", "survey:read", "survey:write"})
*/
private $programs;
/**
* @ORM\Column(type="boolean", options={"default": 0})
* @Groups({"survey:read:active", "survey:read", "survey:write", "survey_list", "recommendation_survey_edit", "teamplay_challenge:write", "teamplay_challenge:read", "program:read"})
*/
private $active = false;
/**
* @ORM\ManyToOne(targetEntity=TvTag::class)
* @Groups({"survey:read:tvTag", "survey:read", "survey:write", "survey_list", "survey_vitality_front", "recommendation_survey_edit"})
*/
private $tvTag = null;
/**
* @ORM\OneToMany(targetEntity=SurveyConfig::class, mappedBy="survey")
* @Groups({"survey:read:configs", "survey:read"})
*/
private $configs;
/**
* @ORM\Column(type="datetime", nullable=true)
* @Gedmo\Timestampable(on="update")
*/
private $updatedAt;
/**
* @ORM\Column(type="datetime")
* @Gedmo\Timestampable(on="create")
*/
private $createdAt;
/**
* @ORM\OneToMany(targetEntity=UserResponse::class, mappedBy="survey")
*/
private $userResponses;
/**
* @Groups({"survey:read:nbSubmited", "survey:read", "program:read", "teamplay_challenge:read"})
*/
public $nbSubmited = 0;
/**
* @Groups({"survey:read:tvTags", "survey:read"})
*/
public $tvTags = null;
/**
* @Groups({"survey:read:tvTags", "survey:read", "program:read", "teamplay_challenge:read"})
*/
public $stopAsk = false;
/**
* @ORM\OneToMany(targetEntity=TeamplayChallenge::class, mappedBy="survey", cascade={"persist", "refresh", "remove"}, orphanRemoval=true)
*/
private $teamplayChallenges;
/**
* @Groups({"survey:read:motivationPhrase", "survey:read", "program:read", "teamplay_challenge:read"})
*/
private $motivationPhrase;
/**
* @Groups({"survey:read:motivationPhrase1", "survey:read", "program:read", "teamplay_challenge:read"})
*/
private $motivationPhrase1;
/**
* @Groups({"survey:read:motivationPhrase2", "survey:read", "program:read", "teamplay_challenge:read"})
*/
private $motivationPhrase2;
/**
* @Groups({"survey:read:motivationPhrase3", "survey:read", "program:read", "teamplay_challenge:read"})
*/
private $motivationPhrase3;
/**
* @Groups({"survey:read:motivationPhrase4", "survey:read", "program:read", "teamplay_challenge:read"})
*/
private $motivationPhrase4;
public function __construct()
{
$this->questions = new ArrayCollection();
$this->programs = new ArrayCollection();
$this->configs = new ArrayCollection();
$this->userResponses = new ArrayCollection();
$this->teamplayChallenges = new ArrayCollection();
}
public function __clone()
{
$this->id = null;
$this->programs = new ArrayCollection();
$this->configs = new ArrayCollection();
$this->userResponses = new ArrayCollection();
$this->teamplayChallenges = new ArrayCollection();
$this->active = false;
$this->createdAt = new \DateTime();
$this->updatedAt = new \DateTime();
// Copy des Questions
$questions = new ArrayCollection();
foreach ($this->questions->toArray() as $question) {
$newQuestion = clone $question;
$newQuestion->setSurvey($this);
$questions->add($newQuestion);
}
$this->questions = $questions;
}
public function getId(): ?int
{
return $this->id;
}
public function setId(?int $id): self
{
$this->id = $id;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): Survey
{
$this->name = $name;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): Survey
{
$this->description = $description;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeInterface $updatedAt): Survey
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): Survey
{
$this->createdAt = $createdAt;
return $this;
}
/**
* @return Collection|Question[]
*/
public function getQuestions(): Collection
{
return $this->questions;
}
public function addQuestion(Question $question): Survey
{
if (!$this->questions->contains($question)) {
$this->questions[] = $question;
$question->setSurvey($this);
}
return $this;
}
public function removeQuestion(Question $question): Survey
{
if ($this->questions->removeElement($question)) {
// set the owning side to null (unless already changed)
if ($question->getSurvey() === $this) {
$question->setSurvey(null);
}
}
return $this;
}
/**
* @return Collection|Program[]
*/
public function getPrograms(): Collection
{
return $this->programs;
}
public function addProgram(Program $program): Survey
{
if (!$this->programs->contains($program)) {
$this->programs[] = $program;
}
return $this;
}
public function removeProgram(Program $program): Survey
{
$this->programs->removeElement($program);
return $this;
}
/**
* @return mixed
*/
public function getType()
{
return $this->type;
}
/**
* @param mixed $type
* @return Survey
*/
public function setType($type): Survey
{
$this->type = $type;
return $this;
}
public function getActive(): ?bool
{
return $this->active;
}
public function setActive(bool $active): self
{
$this->active = $active;
return $this;
}
public function getTvTag(): ?TvTag
{
return $this->tvTag;
}
public function setTvTag(?TvTag $tvTag): self
{
$this->tvTag = $tvTag;
return $this;
}
/**
* @return Collection|SurveyConfig[]
*/
public function getConfigs(): Collection
{
return $this->configs;
}
public function addConfig(SurveyConfig $config): self
{
if (!$this->configs->contains($config)) {
$this->configs[] = $config;
$config->setSurvey($this);
}
return $this;
}
public function removeConfig(SurveyConfig $config): self
{
if ($this->configs->removeElement($config)) {
// set the owning side to null (unless already changed)
if ($config->getSurvey() === $this) {
$config->setSurvey(null);
}
}
return $this;
}
/**
* @Groups({"recommendation_survey_edit"})
*/
public function getMotivationPhrase(): ?string
{
$this->motivationPhrase = null;
if(!empty($this->configs->toArray())) {
foreach ($this->configs->toArray() as $config) {
if($config instanceof SurveyConfig && $config->getType() === SurveyConfig::RECOMMENDED_OBJECTIVES
&& in_array($config->getSubType(), SurveyConfig::INTERVALLES)){
$this->motivationPhrase = $config->getValue();
}
}
}
return $this->motivationPhrase;
}
/**
* @Groups({"recommendation_survey_edit"})
*/
public function getMotivationPhrase1(): ?string
{
$this->motivationPhrase1 = null;
if(!empty($this->configs->toArray())) {
foreach ($this->configs->toArray() as $config) {
if($config instanceof SurveyConfig && $config->getType() === SurveyConfig::RECOMMENDED_OBJECTIVES
&& $config->getSubType() === SurveyConfig::INTERVALLE_1){
$this->motivationPhrase1 = $config->getValue();
break;
}
}
}
return $this->motivationPhrase1;
}
/**
* @Groups({"recommendation_survey_edit"})
*/
public function getMotivationPhrase2(): ?string
{
$this->motivationPhrase2 = null;
if(!empty($this->configs->toArray())) {
foreach ($this->configs->toArray() as $config) {
if($config instanceof SurveyConfig && $config->getType() === SurveyConfig::RECOMMENDED_OBJECTIVES
&& $config->getSubType() === SurveyConfig::INTERVALLE_2){
$this->motivationPhrase2 = $config->getValue();
break;
}
}
}
return $this->motivationPhrase2;
}
/**
* @Groups({"recommendation_survey_edit"})
*/
public function getMotivationPhrase3(): ?string
{
$this->motivationPhrase3 = null;
if(!empty($this->configs->toArray())) {
foreach ($this->configs->toArray() as $config) {
if($config instanceof SurveyConfig && $config->getType() === SurveyConfig::RECOMMENDED_OBJECTIVES
&& $config->getSubType() === SurveyConfig::INTERVALLE_3){
$this->motivationPhrase3 = $config->getValue();
break;
}
}
}
return $this->motivationPhrase3;
}
/**
* @Groups({"recommendation_survey_edit"})
*/
public function getMotivationPhrase4(): ?string
{
$this->motivationPhrase4 = null;
if(!empty($this->configs->toArray())) {
foreach ($this->configs->toArray() as $config) {
if($config instanceof SurveyConfig && $config->getType() === SurveyConfig::RECOMMENDED_OBJECTIVES
&& $config->getSubType() === SurveyConfig::INTERVALLE_4){
$this->motivationPhrase4 = $config->getValue();
break;
}
}
}
return $this->motivationPhrase4;
}
/**
* @return Collection|UserResponse[]
*/
public function getUserResponses(): Collection
{
return $this->userResponses;
}
public function addUserResponse(UserResponse $userResponse): self
{
if (!$this->userResponses->contains($userResponse)) {
$this->userResponses[] = $userResponse;
$userResponse->setSurvey($this);
}
return $this;
}
public function removeUserResponse(UserResponse $userResponse): self
{
if ($this->userResponses->removeElement($userResponse)) {
// set the owning side to null (unless already changed)
if ($userResponse->getSurvey() === $this) {
$userResponse->setSurvey(null);
}
}
return $this;
}
/**
* @return Collection|TeamplayChallenge[]
*/
public function getTeamplayChallenges(): Collection
{
return $this->teamplayChallenges;
}
public function addTeamplayChallenge(TeamplayChallenge $teamplayChallenge): self
{
if (!$this->teamplayChallenges->contains($teamplayChallenge)) {
$this->teamplayChallenges[] = $teamplayChallenge;
$teamplayChallenge->setSurvey($this);
}
return $this;
}
public function removeTeamplayChallenge(TeamplayChallenge $teamplayChallenge): self
{
if ($this->teamplayChallenges->removeElement($teamplayChallenge)) {
// set the owning side to null (unless already changed)
if ($teamplayChallenge->getSurvey() === $this) {
$teamplayChallenge->setSurvey(null);
}
}
return $this;
}
}