<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use App\Repository\SurveyRepository;
use ApiPlatform\Core\Annotation\ApiFilter;
use App\Entity\Traits\TimestampableEntity;
use Doctrine\Common\Collections\Collection;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\Common\Collections\ArrayCollection;
use App\Entity\Interfaces\ContainerMappedInterface;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\BooleanFilter;
/**
* @ApiResource(
* attributes={"security"="is_granted('ROLE_OPERATOR')"},
* normalizationContext={"groups"={"Survey:Read", "TZ:Read"}, "skip_null_values"=false},
* denormalizationContext={"groups"={"Survey:Write"}},
* collectionOperations={
* "get",
* "get_active"={
* "path"="/surveys/get-active",
* "method"="GET",
* "security"="is_granted('IS_AUTHENTICATED_FULLY')",
* "pagination_enabled"=false
* },
* "post"
* },
* itemOperations={
* "get",
* "put",
* "patch",
* "delete"
* }
* )
* @ApiFilter(SearchFilter::class, properties={"container.id": "exact", "name": "partial", "surveyTriggers.triggerType": "exact", "surveyTriggers.elTask.id": "exact", "surveyTriggers.elExam.id": "exact"})
* @ApiFilter(BooleanFilter::class, properties={"isActive", "isMandatory"})
* @ApiFilter(OrderFilter::class, properties={"id", "name", "createdAt"})
* @ORM\Entity(repositoryClass=SurveyRepository::class)
*/
class Survey implements ContainerMappedInterface
{
/**
* Hook timestampable behavior
* updates createdAt, updatedAt fields
*/
use TimestampableEntity;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"Survey:Read"})
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Assert\NotBlank(message="validation.Survey:name.notBlank")
* @Groups({"Survey:Read", "Survey:Write"})
*/
private $name;
/**
* @ORM\Column(type="boolean", nullable=true)
* @Groups({"Survey:Read", "Survey:Write"})
*/
private $isActive;
/**
* @ORM\ManyToOne(targetEntity=Container::class)
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
* @Groups({"Survey:Write"})
*/
private $container;
/**
* @ORM\OneToMany(targetEntity=SurveyTrigger::class, mappedBy="survey", orphanRemoval=true, cascade={"persist"})
* @Assert\Valid()
* @Groups({"Survey:Read", "Survey:Write"})
*/
private $surveyTriggers;
/**
* @ORM\OneToMany(targetEntity=SurveyQuestion::class, mappedBy="survey", orphanRemoval=true)
*/
private $surveyQuestions;
/**
* @ORM\ManyToMany(targetEntity=UserGroup::class, inversedBy="surveys")
* @Groups({"Survey:Read", "Survey:Write"})
*/
private $userGroups;
/**
* @ORM\Column(type="boolean", nullable=true)
* @Groups({"Survey:Read", "Survey:Write"})
*/
private $isShowCancel;
/**
* @ORM\Column(type="string", length=32)
* @Groups({"Survey:Read", "Survey:Write"})
*/
private $visibility;
public const SURVEYTRIGGERVISIBILITY_ONE_TIME = "ONE_TIME";
public const SURVEYTRIGGERVISIBILITY_ONE_TIME_PER_TRIGGER = "ONE_TIME_PER_TRIGGER";
public const SURVEYTRIGGERVISIBILITY_ALWAYS = "ALWAYS";
/**
* @ORM\OneToMany(targetEntity=SurveyPage::class, mappedBy="survey")
*/
private $surveyPages;
/**
* @ORM\Column(type="boolean", nullable=true)
* @Groups({"Survey:Read", "Survey:Write"})
*/
private $isMandatory;
public function __construct()
{
$this->surveyTriggers = new ArrayCollection();
$this->surveyQuestions = new ArrayCollection();
$this->userGroups = new ArrayCollection();
$this->surveyPages = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getIsActive(): ?bool
{
return $this->isActive;
}
public function setIsActive(?bool $isActive): self
{
$this->isActive = $isActive;
return $this;
}
public function getContainer(): ?Container
{
return $this->container;
}
public function setContainer(?Container $container): self
{
$this->container = $container;
return $this;
}
/**
* @return Collection|SurveyTrigger[]
*/
public function getSurveyTriggers(): Collection
{
return $this->surveyTriggers;
}
public function addSurveyTrigger(SurveyTrigger $surveyTrigger): self
{
if (!$this->surveyTriggers->contains($surveyTrigger)) {
$this->surveyTriggers[] = $surveyTrigger;
$surveyTrigger->setSurvey($this);
}
return $this;
}
public function removeSurveyTrigger(SurveyTrigger $surveyTrigger): self
{
if ($this->surveyTriggers->removeElement($surveyTrigger)) {
// set the owning side to null (unless already changed)
if ($surveyTrigger->getSurvey() === $this) {
$surveyTrigger->setSurvey(null);
}
}
return $this;
}
public function removeAllSurveyTriggers(): self
{
foreach ($this->surveyTriggers as $surveyTrigger) {
$this->removeSurveyTrigger($surveyTrigger);
}
return $this;
}
/**
* @return Collection|SurveyQuestion[]
*/
public function getSurveyQuestions(): Collection
{
return $this->surveyQuestions;
}
public function addSurveyQuestion(SurveyQuestion $surveyQuestion): self
{
if (!$this->surveyQuestions->contains($surveyQuestion)) {
$this->surveyQuestions[] = $surveyQuestion;
$surveyQuestion->setSurvey($this);
}
return $this;
}
public function removeSurveyQuestion(SurveyQuestion $surveyQuestion): self
{
if ($this->surveyQuestions->removeElement($surveyQuestion)) {
// set the owning side to null (unless already changed)
if ($surveyQuestion->getSurvey() === $this) {
$surveyQuestion->setSurvey(null);
}
}
return $this;
}
/**
* @return Collection|UserGroup[]
*/
public function getUserGroups(): Collection
{
return $this->userGroups;
}
public function addUserGroup(UserGroup $userGroup): self
{
if (!$this->userGroups->contains($userGroup)) {
$this->userGroups[] = $userGroup;
}
return $this;
}
public function removeUserGroup(UserGroup $userGroup): self
{
$this->userGroups->removeElement($userGroup);
return $this;
}
public function removeAllUserGroups(): self
{
foreach ($this->userGroups as $userGroup) {
$this->removeUserGroup($userGroup);
}
return $this;
}
public function getIsShowCancel(): ?bool
{
return $this->isShowCancel;
}
public function setIsShowCancel(?bool $isShowCancel): self
{
$this->isShowCancel = $isShowCancel;
return $this;
}
public function getVisibility(): ?string
{
return $this->visibility;
}
public function setVisibility(string $visibility): self
{
$this->visibility = $visibility;
return $this;
}
/**
* @return Collection|SurveyPage[]
*/
public function getSurveyPages(): Collection
{
return $this->surveyPages;
}
public function addSurveyPage(SurveyPage $surveyPage): self
{
if (!$this->surveyPages->contains($surveyPage)) {
$this->surveyPages[] = $surveyPage;
$surveyPage->setSurvey($this);
}
return $this;
}
public function removeSurveyPage(SurveyPage $surveyPage): self
{
if ($this->surveyPages->removeElement($surveyPage)) {
// set the owning side to null (unless already changed)
if ($surveyPage->getSurvey() === $this) {
$surveyPage->setSurvey(null);
}
}
return $this;
}
public function isIsMandatory(): ?bool
{
return $this->isMandatory;
}
public function setIsMandatory(?bool $isMandatory): self
{
$this->isMandatory = $isMandatory;
return $this;
}
}