2019-11-11 10:44:19 +01:00
|
|
|
<?php
|
|
|
|
|
2020-01-30 12:25:58 +01:00
|
|
|
namespace App\Entity;
|
|
|
|
|
|
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
|
|
|
use Doctrine\Common\Collections\Collection;
|
|
|
|
use Doctrine\ORM\Mapping as ORM;
|
|
|
|
use JMS\Serializer\Annotation as Serializer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* contains the votes for one answer to a poll
|
|
|
|
* @ORM\Entity(repositoryClass="App\Repository\StackOfVotesRepository")
|
|
|
|
* @ORM\HasLifecycleCallbacks()
|
2020-04-26 12:33:11 +02:00
|
|
|
* @Serializer\ExclusionPolicy("all")
|
2020-01-30 12:25:58 +01:00
|
|
|
*/
|
|
|
|
class StackOfVotes {
|
|
|
|
/**
|
|
|
|
* @ORM\Id()
|
|
|
|
* @ORM\GeneratedValue()
|
|
|
|
* @ORM\Column(type="integer")
|
|
|
|
*/
|
|
|
|
private $id;
|
|
|
|
/**
|
|
|
|
* @ORM\Column(type="string", length=255)
|
|
|
|
* @Serializer\Type("string")
|
|
|
|
* @Serializer\Expose()
|
|
|
|
*/
|
|
|
|
public $pseudo;
|
|
|
|
/**
|
|
|
|
* @ORM\OneToMany(targetEntity="App\Entity\Vote", mappedBy="stacksOfVotes", cascade={"persist","remove"})
|
|
|
|
* @Serializer\Expose()
|
|
|
|
*/
|
|
|
|
public $votes;
|
|
|
|
/**
|
|
|
|
* @ORM\ManyToOne(targetEntity="App\Entity\Poll", inversedBy="stacksOfVotes", cascade={"persist"})
|
|
|
|
*/
|
|
|
|
private $poll;
|
2019-11-12 11:26:19 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-30 12:25:58 +01:00
|
|
|
* @ORM\ManyToOne(targetEntity="App\Entity\Owner", inversedBy="stackOfVotes", cascade={"persist"})
|
|
|
|
* @Serializer\Expose()
|
2019-11-12 11:26:19 +01:00
|
|
|
*/
|
2020-01-30 12:25:58 +01:00
|
|
|
private $owner;
|
|
|
|
|
|
|
|
|
|
|
|
public function display() {
|
|
|
|
$tab = [
|
2020-02-04 12:38:07 +01:00
|
|
|
'id' => $this->getId(),
|
2021-04-26 15:14:25 +02:00
|
|
|
// 'modifier_token' => $this->getOwner()->getModifierToken(),
|
2020-02-04 12:38:07 +01:00
|
|
|
'pseudo' => '',
|
|
|
|
'creation_date' => '',
|
|
|
|
'votes' => [],
|
2020-01-30 12:25:58 +01:00
|
|
|
];
|
|
|
|
// prefill votes with all choices ids
|
|
|
|
foreach ( $this->getPoll()->getChoices() as $choice ) {
|
|
|
|
$tab[ 'votes' ][ $choice->getId() ] = [
|
|
|
|
'choice_id' => $choice->getId(),
|
2021-04-26 15:14:25 +02:00
|
|
|
'name' => $choice->getName(),
|
2020-01-21 10:46:29 +01:00
|
|
|
];
|
2020-01-29 17:26:16 +01:00
|
|
|
}
|
2019-11-12 11:26:19 +01:00
|
|
|
|
2020-01-30 12:25:58 +01:00
|
|
|
foreach ( $this->getVotes() as $vote ) {
|
2021-04-26 15:14:25 +02:00
|
|
|
// $tab[ 'votes' ][ $vote->getChoice()->getId() ] = $vote->display();
|
2020-02-04 12:38:07 +01:00
|
|
|
$tab[ 'votes' ][ $vote->getChoice()->getId() ][ 'stack_id' ] = $this->getId();
|
2021-04-26 15:14:25 +02:00
|
|
|
$tab[ 'pseudo' ] = $this->getPseudo();
|
2020-02-04 12:38:07 +01:00
|
|
|
$tab[ 'creation_date' ] = $vote->getCreationDate();
|
2020-01-29 17:26:16 +01:00
|
|
|
}
|
2019-11-12 11:26:19 +01:00
|
|
|
|
2020-01-30 12:25:58 +01:00
|
|
|
return $tab;
|
|
|
|
}
|
2019-11-27 17:07:11 +01:00
|
|
|
|
2020-01-30 12:25:58 +01:00
|
|
|
/**
|
|
|
|
* @ORM\PrePersist
|
|
|
|
*/
|
|
|
|
public function prePersist() {
|
|
|
|
$this->setPseudo( $this->getOwner()->getPseudo() );
|
|
|
|
}
|
2019-11-12 11:26:19 +01:00
|
|
|
|
2020-01-30 12:25:58 +01:00
|
|
|
public function __construct() {
|
|
|
|
$this->votes = new ArrayCollection();
|
|
|
|
}
|
2019-11-12 11:26:19 +01:00
|
|
|
|
2020-01-30 12:25:58 +01:00
|
|
|
public function getId(): ?int {
|
|
|
|
return $this->id;
|
|
|
|
}
|
2020-01-29 17:26:16 +01:00
|
|
|
|
2020-01-30 12:25:58 +01:00
|
|
|
/**
|
|
|
|
* @return Collection|poll[]
|
|
|
|
*/
|
|
|
|
public function getVotes(): Collection {
|
|
|
|
return $this->votes;
|
|
|
|
}
|
2019-11-12 11:26:19 +01:00
|
|
|
|
2020-01-30 12:25:58 +01:00
|
|
|
public function addVote( Vote $vote ): self {
|
|
|
|
if ( ! $this->votes->contains( $vote ) ) {
|
|
|
|
$vote->setPoll( $this->getPoll() );
|
2019-11-12 11:26:19 +01:00
|
|
|
|
2020-01-30 12:25:58 +01:00
|
|
|
$this->votes[] = $vote;
|
|
|
|
$vote->setStacksOfVotes( $this );
|
2020-01-29 17:26:16 +01:00
|
|
|
}
|
2019-11-12 11:26:19 +01:00
|
|
|
|
2020-01-30 12:25:58 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function removeVote( Vote $vote ): self {
|
|
|
|
if ( $this->votes->contains( $vote ) ) {
|
|
|
|
$this->votes->removeElement( $vote );
|
|
|
|
// set the owning side to null (unless already changed)
|
|
|
|
if ( $vote->getStacksOfVotes() === $this ) {
|
|
|
|
$vote->setStacksOfVotes( null );
|
|
|
|
}
|
2020-01-29 17:26:16 +01:00
|
|
|
}
|
2019-11-12 11:26:19 +01:00
|
|
|
|
2020-01-30 12:25:58 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2019-11-12 11:26:19 +01:00
|
|
|
|
2020-01-30 12:25:58 +01:00
|
|
|
public function getPseudo(): ?string {
|
|
|
|
return $this->pseudo;
|
|
|
|
}
|
2019-11-12 11:26:19 +01:00
|
|
|
|
2020-01-30 12:25:58 +01:00
|
|
|
public function setPseudo( ?string $pseudo ): self {
|
|
|
|
$this->pseudo = $pseudo;
|
2019-11-12 11:26:19 +01:00
|
|
|
|
2020-01-30 12:25:58 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2019-11-27 11:26:21 +01:00
|
|
|
|
2020-01-30 12:25:58 +01:00
|
|
|
public function getOwner(): ?Owner {
|
|
|
|
return $this->owner;
|
|
|
|
}
|
2020-01-29 17:26:16 +01:00
|
|
|
|
2020-01-30 12:25:58 +01:00
|
|
|
public function setOwner( ?Owner $owner ): self {
|
|
|
|
$this->owner = $owner;
|
2019-11-27 11:26:21 +01:00
|
|
|
|
2020-01-30 12:25:58 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2019-11-27 11:26:21 +01:00
|
|
|
|
2020-01-30 12:25:58 +01:00
|
|
|
public function getPoll(): ?Poll {
|
|
|
|
return $this->poll;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setPoll( ?Poll $poll ): self {
|
|
|
|
$this->poll = $poll;
|
|
|
|
|
|
|
|
return $this;
|
2019-11-27 11:26:21 +01:00
|
|
|
}
|
2020-01-30 12:25:58 +01:00
|
|
|
}
|