enrich stats for choices

This commit is contained in:
Tykayn 2021-04-27 11:11:34 +02:00 committed by tykayn
parent 6831d8d1ae
commit 665cf6d173
1 changed files with 368 additions and 371 deletions

View File

@ -10,7 +10,6 @@ use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use ErrorException;
use JMS\Serializer\Annotation as Serializer;
use RangeException;
/**
* @ORM\Entity(repositoryClass="App\Repository\PollRepository")
@ -218,9 +217,6 @@ class Poll {
}
public function displayForAdmin() {
$content = $this->display();
$content[ 'owner' ] = $this->getOwner()->displayForAdmin();
@ -268,29 +264,17 @@ class Poll {
'password_protected' => $this->getPassword() ? 'yes' : 'no',
'max_score' => $computedAnswers[ 'max_score' ],
'choices' => $displayedChoices,
'choices_stats' => $computedAnswers[ 'answersWithStats' ],
'stacks' => $displayedStackOfVotes,
'comments' => $displayedComments,
];
}
public function computeAnswers() {
// counts each number of answer for this choice
public function computeAnswers() {
$computedArray = [];
$maxScore = 0;
foreach ( $this->getStacksOfVotes() as $stack_of_vote ) {
foreach ( $stack_of_vote->getVotes() as $vote ) {
$answer = $vote->getValue();
$choice_id = $vote->getChoice()->getId();
$choice_url = $vote->getChoice()->getUrl();
if ( ! isset( $computedArray[ $choice_id ] ) ) {
$computedArray[ $choice_id ] = [
'id' => $choice_id,
'url' => $choice_url,
'name' => $vote->getChoice()->getName(),
'score' => 0,
$scoreInfos = ['score' => 0,
'yes' => [
'count' => 0,
'people' => [],
@ -302,7 +286,25 @@ class Poll {
'no' => [
'count' => 0,
'people' => [],
],
]];
// first, prefill all choices
foreach ( $this->getChoices() as $choice ) {
$computedArray[ $choice->getId() ] = array_merge($scoreInfos, $choice->display());
}
// then, compute stack of votes scores on each choice
foreach ( $this->getStacksOfVotes() as $stack_of_vote ) {
foreach ( $stack_of_vote->getVotes() as $vote ) {
$answer = $vote->getValue();
$choice_id = $vote->getChoice()->getId();
$choice_url = $vote->getChoice()->getUrl();
if ( ! isset( $computedArray[ $choice_id ] ) ) {
$computedArray[ $choice_id ] = [
'id' => $choice_id,
'url' => $choice_url,
'name' => $vote->getChoice()->getName(),
];
}
$computedArray[ $choice_id ][ $answer ][ 'count' ] ++;
@ -319,14 +321,9 @@ class Poll {
}
}
}
$answersWithStats = [];
foreach ( $computedArray as $choice_stat ) {
$answersWithStats[] = $choice_stat;
}
return [
'answers' => $computedArray,
'answersWithStats' => $answersWithStats,
'max_score' => $maxScore,
];
}