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 ) {
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,
];
}

View File

@ -1,17 +1,17 @@
<?php
namespace App\Entity;
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;
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")
*/
class StackOfVotes {
class StackOfVotes {
/**
* @ORM\Id()
* @ORM\GeneratedValue()
@ -49,8 +49,15 @@ class StackOfVotes {
'creation_date' => '',
'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 ) {
$tab[ 'votes' ][] = [
$tab[ 'votes' ][ $vote->getChoice()->getId() ] = [
'id' => $this->getId(),
'vote_id' => $vote->getId(),
'value' => $vote->getValue(),
@ -131,4 +138,4 @@ class StackOfVotes {
return $this;
}
}
}