fill missing ids in stacks

This commit is contained in:
Baptiste Lemoine 2020-01-29 17:26:16 +01:00
parent 86fe1d5720
commit 58c28474f8
2 changed files with 143 additions and 127 deletions

View File

@ -161,19 +161,28 @@
foreach ( $stack_of_vote->getVotes() as $vote ) { foreach ( $stack_of_vote->getVotes() as $vote ) {
if ( ! isset( $computedArray[ $vote->getChoice()->getId() ] ) ) { if ( ! isset( $computedArray[ $vote->getChoice()->getId() ] ) ) {
$computedArray[ $vote->getChoice()->getId() ] = [ $computedArray[ $vote->getChoice()->getId() ] = [
'yes' => 0, 'yes' => [
'maybe' => 0, 'count' => 0,
'no' => 0, 'people' => [],
],
'maybe' => [
'count' => 0,
'people' => [],
],
'no' => [
'count' => 0,
'people' => [],
],
]; ];
} }
$computedArray[ $vote->getChoice()->getId() ][ $vote->getValue() ] ++; $computedArray[ $vote->getChoice()->getId() ][ $vote->getValue() ][ 'count' ] ++;
$people[ $stack_of_vote->getOwner()->getPseudo() ] = $vote->getValue(); $computedArray[ $vote->getChoice()->getId() ][ $vote->getValue() ][ 'people' ][] = $stack_of_vote->getOwner()->getPseudo();
} }
} }
return [ return [
'counts' => $computedArray, 'counts' => $computedArray,
'people' => $people,
]; ];
} }

View File

@ -1,17 +1,17 @@
<?php <?php
namespace App\Entity; namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer; use JMS\Serializer\Annotation as Serializer;
/** /**
* contains the votes for one answer to a poll * contains the votes for one answer to a poll
* @ORM\Entity(repositoryClass="App\Repository\StackOfVotesRepository") * @ORM\Entity(repositoryClass="App\Repository\StackOfVotesRepository")
*/ */
class StackOfVotes { class StackOfVotes {
/** /**
* @ORM\Id() * @ORM\Id()
* @ORM\GeneratedValue() * @ORM\GeneratedValue()
@ -49,8 +49,15 @@ class StackOfVotes {
'creation_date' => '', 'creation_date' => '',
'votes' => [], 'votes' => [],
]; ];
// 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 ) { foreach ( $this->getVotes() as $vote ) {
$tab[ 'votes' ][] = [ $tab[ 'votes' ][ $vote->getChoice()->getId() ] = [
'id' => $this->getId(), 'id' => $this->getId(),
'vote_id' => $vote->getId(), 'vote_id' => $vote->getId(),
'value' => $vote->getValue(), 'value' => $vote->getValue(),
@ -131,4 +138,4 @@ class StackOfVotes {
return $this; return $this;
} }
} }