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 Doctrine\ORM\Mapping as ORM;
use ErrorException; use ErrorException;
use JMS\Serializer\Annotation as Serializer; use JMS\Serializer\Annotation as Serializer;
use RangeException;
/** /**
* @ORM\Entity(repositoryClass="App\Repository\PollRepository") * @ORM\Entity(repositoryClass="App\Repository\PollRepository")
@ -218,9 +217,6 @@ class Poll {
} }
public function displayForAdmin() { public function displayForAdmin() {
$content = $this->display(); $content = $this->display();
$content[ 'owner' ] = $this->getOwner()->displayForAdmin(); $content[ 'owner' ] = $this->getOwner()->displayForAdmin();
@ -268,29 +264,17 @@ class Poll {
'password_protected' => $this->getPassword() ? 'yes' : 'no', 'password_protected' => $this->getPassword() ? 'yes' : 'no',
'max_score' => $computedAnswers[ 'max_score' ], 'max_score' => $computedAnswers[ 'max_score' ],
'choices' => $displayedChoices, 'choices' => $displayedChoices,
'choices_stats' => $computedAnswers[ 'answersWithStats' ],
'stacks' => $displayedStackOfVotes, 'stacks' => $displayedStackOfVotes,
'comments' => $displayedComments, 'comments' => $displayedComments,
]; ];
} }
public function computeAnswers() {
// counts each number of answer for this choice // counts each number of answer for this choice
public function computeAnswers() {
$computedArray = []; $computedArray = [];
$maxScore = 0; $maxScore = 0;
foreach ( $this->getStacksOfVotes() as $stack_of_vote ) { $scoreInfos = ['score' => 0,
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,
'yes' => [ 'yes' => [
'count' => 0, 'count' => 0,
'people' => [], 'people' => [],
@ -302,7 +286,25 @@ class Poll {
'no' => [ 'no' => [
'count' => 0, 'count' => 0,
'people' => [], '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' ] ++; $computedArray[ $choice_id ][ $answer ][ 'count' ] ++;
@ -319,14 +321,9 @@ class Poll {
} }
} }
} }
$answersWithStats = [];
foreach ( $computedArray as $choice_stat ) {
$answersWithStats[] = $choice_stat;
}
return [ return [
'answers' => $computedArray, 'answers' => $computedArray,
'answersWithStats' => $answersWithStats,
'max_score' => $maxScore, 'max_score' => $maxScore,
]; ];
} }