2019-11-11 10:44:19 +01:00
|
|
|
<?php
|
|
|
|
|
2020-01-30 12:25:58 +01:00
|
|
|
namespace App\Entity;
|
|
|
|
|
2021-04-27 10:21:21 +02:00
|
|
|
use App\Traits\TimeStampableTrait;
|
2021-04-27 12:51:10 +02:00
|
|
|
use DateTime;
|
2020-01-30 12:25:58 +01:00
|
|
|
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 {
|
2021-04-27 10:21:21 +02:00
|
|
|
|
|
|
|
use TimeStampableTrait;
|
|
|
|
|
2020-01-30 12:25:58 +01:00
|
|
|
/**
|
|
|
|
* @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;
|
2021-04-27 10:22:16 +02:00
|
|
|
/**
|
|
|
|
* @ORM\Id()
|
|
|
|
* @ORM\GeneratedValue()
|
|
|
|
* @ORM\Column(type="integer")
|
|
|
|
*/
|
|
|
|
private $id;
|
2020-01-30 12:25:58 +01:00
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
|
2021-04-27 10:41:21 +02:00
|
|
|
/**
|
|
|
|
* @ORM\Column(type="string", length=255)
|
|
|
|
*/
|
|
|
|
private $ip;
|
|
|
|
|
2021-04-27 10:22:16 +02:00
|
|
|
public function __construct() {
|
2021-04-27 12:51:10 +02:00
|
|
|
$this->setCreatedAt( new DateTime() );
|
|
|
|
$this->votes = new ArrayCollection();
|
|
|
|
}
|
2020-01-30 12:25:58 +01:00
|
|
|
|
|
|
|
public function display() {
|
2021-04-27 12:51:10 +02:00
|
|
|
$votes = $this->getVotes();
|
|
|
|
|
|
|
|
$tab = [
|
|
|
|
// 'id' => $this->getId(),
|
|
|
|
// 'modifier_token' => $this->getOwner()->getModifierToken(),
|
|
|
|
'pseudo' => $this->getPseudo(),
|
|
|
|
'created_at' => $this->getCreatedAtAsString(),
|
|
|
|
'votes' => [],
|
|
|
|
];
|
|
|
|
// prefill votes with all choices ids
|
|
|
|
// foreach ( $this->getPoll()->getChoices() as $choice ) {
|
|
|
|
// $tab[ 'votes' ][ $choice->getId() ] = [
|
|
|
|
// 'choice_id' => $choice->getId(),
|
|
|
|
// 'value' => null,
|
|
|
|
// ];
|
|
|
|
// }
|
|
|
|
|
|
|
|
foreach ( $votes as $vote ) {
|
|
|
|
$tab[ 'votes' ][ $vote->getChoice()->getId() ] = $vote->display();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $tab;
|
|
|
|
}
|
2019-11-27 17:07:11 +01:00
|
|
|
|
2020-01-30 12:25:58 +01:00
|
|
|
/**
|
|
|
|
* @return Collection|poll[]
|
|
|
|
*/
|
|
|
|
public function getVotes(): Collection {
|
2021-04-27 12:51:10 +02:00
|
|
|
return $this->votes;
|
|
|
|
}
|
2019-11-12 11:26:19 +01:00
|
|
|
|
2020-01-30 12:25:58 +01:00
|
|
|
public function getPseudo(): ?string {
|
2021-04-27 12:51:10 +02:00
|
|
|
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 {
|
2021-04-27 12:51:10 +02:00
|
|
|
$this->pseudo = $pseudo;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2019-11-27 11:26:21 +01:00
|
|
|
|
2021-04-27 10:22:16 +02:00
|
|
|
/**
|
|
|
|
* @ORM\PrePersist
|
|
|
|
*/
|
|
|
|
public function prePersist() {
|
2021-04-27 12:51:10 +02:00
|
|
|
$this->setPseudo( $this->getOwner()->getPseudo() );
|
|
|
|
}
|
2021-04-27 10:22:16 +02:00
|
|
|
|
2020-01-30 12:25:58 +01:00
|
|
|
public function getOwner(): ?Owner {
|
2021-04-27 12:51:10 +02:00
|
|
|
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 {
|
2021-04-27 12:51:10 +02:00
|
|
|
$this->owner = $owner;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2019-11-27 11:26:21 +01:00
|
|
|
|
2021-04-27 10:22:16 +02:00
|
|
|
public function getId(): ?int {
|
2021-04-27 12:51:10 +02:00
|
|
|
return $this->id;
|
|
|
|
}
|
2021-04-27 10:22:16 +02:00
|
|
|
|
|
|
|
public function addVote( Vote $vote ): self {
|
2021-04-27 12:51:10 +02:00
|
|
|
if ( ! $this->votes->contains( $vote ) ) {
|
|
|
|
$vote->setPoll( $this->getPoll() );
|
|
|
|
|
|
|
|
$this->votes[] = $vote;
|
|
|
|
$vote->setStacksOfVotes( $this );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2021-04-27 10:22:16 +02:00
|
|
|
|
2020-01-30 12:25:58 +01:00
|
|
|
public function getPoll(): ?Poll {
|
2021-04-27 12:51:10 +02:00
|
|
|
return $this->poll;
|
|
|
|
}
|
2020-01-30 12:25:58 +01:00
|
|
|
|
|
|
|
public function setPoll( ?Poll $poll ): self {
|
2021-04-27 12:51:10 +02:00
|
|
|
$this->poll = $poll;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2021-04-27 10:22:16 +02:00
|
|
|
|
|
|
|
public function removeVote( Vote $vote ): self {
|
2021-04-27 12:51:10 +02:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getIp(): ?string {
|
|
|
|
return $this->ip;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setIp( string $ip ): self {
|
|
|
|
$this->ip = $ip;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2020-01-30 12:25:58 +01:00
|
|
|
}
|