From 58c28474f8935ec1fc6376b1b9fd0feccbd56caa Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Wed, 29 Jan 2020 17:26:16 +0100 Subject: [PATCH] fill missing ids in stacks --- src/Entity/Poll.php | 21 ++- src/Entity/StackOfVotes.php | 249 ++++++++++++++++++------------------ 2 files changed, 143 insertions(+), 127 deletions(-) diff --git a/src/Entity/Poll.php b/src/Entity/Poll.php index 38e847d..777e07f 100644 --- a/src/Entity/Poll.php +++ b/src/Entity/Poll.php @@ -161,19 +161,28 @@ foreach ( $stack_of_vote->getVotes() as $vote ) { if ( ! isset( $computedArray[ $vote->getChoice()->getId() ] ) ) { $computedArray[ $vote->getChoice()->getId() ] = [ - 'yes' => 0, - 'maybe' => 0, - 'no' => 0, + 'yes' => [ + 'count' => 0, + 'people' => [], + ], + 'maybe' => [ + 'count' => 0, + 'people' => [], + ], + 'no' => [ + 'count' => 0, + 'people' => [], + ], ]; } - $computedArray[ $vote->getChoice()->getId() ][ $vote->getValue() ] ++; - $people[ $stack_of_vote->getOwner()->getPseudo() ] = $vote->getValue(); + $computedArray[ $vote->getChoice()->getId() ][ $vote->getValue() ][ 'count' ] ++; + $computedArray[ $vote->getChoice()->getId() ][ $vote->getValue() ][ 'people' ][] = $stack_of_vote->getOwner()->getPseudo(); + } } return [ 'counts' => $computedArray, - 'people' => $people, ]; } diff --git a/src/Entity/StackOfVotes.php b/src/Entity/StackOfVotes.php index 8b7707f..9bb40d6 100644 --- a/src/Entity/StackOfVotes.php +++ b/src/Entity/StackOfVotes.php @@ -1,134 +1,141 @@ $this->getId(), - 'pseudo' => '', - 'creation_date' => '', - 'votes' => [], - ]; - foreach ( $this->getVotes() as $vote ) { - $tab[ 'votes' ][] = [ - 'id' => $this->getId(), - 'vote_id' => $vote->getId(), - 'value' => $vote->getValue(), - 'choice_id' => $vote->getChoice()->getId(), - 'text' => $vote->getChoice()->getName(), + public function display() { + $tab = [ + 'id' => $this->getId(), + 'pseudo' => '', + 'creation_date' => '', + 'votes' => [], ]; - $tab[ 'pseudo' ] = $this->getOwner()->getPseudo(); - $tab[ 'creation_date' ] = $vote->getCreationDate(); - } - - return $tab; - } - - public function __construct() { - $this->votes = new ArrayCollection(); - } - - public function getId(): ?int { - return $this->id; - } - - /** - * @return Collection|poll[] - */ - public function getVotes(): Collection { - return $this->votes; - } - - public function addVote( Vote $vote ): self { - if ( ! $this->votes->contains( $vote ) ) { - $vote->setPoll( $this->getPoll() ); - - $this->votes[] = $vote; - $vote->setStacksOfVotes( $this ); - } - - 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 ); + // prefill votes with all choices ids + foreach ( $this->getPoll()->getChoices() as $choice ) { + $tab[ 'votes' ][ $choice->getId() ] = [ + 'choice_id' => $choice->getId(), + ]; } + + foreach ( $this->getVotes() as $vote ) { + $tab[ 'votes' ][ $vote->getChoice()->getId() ] = [ + 'id' => $this->getId(), + 'vote_id' => $vote->getId(), + 'value' => $vote->getValue(), + 'choice_id' => $vote->getChoice()->getId(), + 'text' => $vote->getChoice()->getName(), + ]; + $tab[ 'pseudo' ] = $this->getOwner()->getPseudo(); + $tab[ 'creation_date' ] = $vote->getCreationDate(); + } + + return $tab; } - return $this; + public function __construct() { + $this->votes = new ArrayCollection(); + } + + public function getId(): ?int { + return $this->id; + } + + /** + * @return Collection|poll[] + */ + public function getVotes(): Collection { + return $this->votes; + } + + public function addVote( Vote $vote ): self { + if ( ! $this->votes->contains( $vote ) ) { + $vote->setPoll( $this->getPoll() ); + + $this->votes[] = $vote; + $vote->setStacksOfVotes( $this ); + } + + 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 ); + } + } + + return $this; + } + + public function getPseudo(): ?string { + return $this->pseudo; + } + + public function setPseudo( ?string $pseudo ): self { + $this->pseudo = $pseudo; + + return $this; + } + + public function getOwner(): ?Owner { + return $this->owner; + } + + public function setOwner( ?Owner $owner ): self { + $this->owner = $owner; + + return $this; + } + + public function getPoll(): ?Poll { + return $this->poll; + } + + public function setPoll( ?Poll $poll ): self { + $this->poll = $poll; + + return $this; + } } - - public function getPseudo(): ?string { - return $this->pseudo; - } - - public function setPseudo( ?string $pseudo ): self { - $this->pseudo = $pseudo; - - return $this; - } - - public function getOwner(): ?Owner { - return $this->owner; - } - - public function setOwner( ?Owner $owner ): self { - $this->owner = $owner; - - return $this; - } - - public function getPoll(): ?Poll { - return $this->poll; - } - - public function setPoll( ?Poll $poll ): self { - $this->poll = $poll; - - return $this; - } -}