compute vote counts in get poll config

This commit is contained in:
Baptiste Lemoine 2020-01-28 20:43:16 +01:00
parent 64a770d99a
commit 86fe1d5720
3 changed files with 1266 additions and 1232 deletions

View File

@ -275,7 +275,7 @@ class DefaultController extends AbstractController {
'poll' => $poll,
'stacks_count' => count( $poll->getStacksOfVotes() ),
'stacks' => $stacks,
'choices_count' => count( $poll->getChoices() ),
'choices_count' => $poll->computeAnswers(),
'choices' => $choices,
'comments' => $comments,
'comments_count' => count( $comments ),

View File

@ -61,6 +61,7 @@ class Choice {
}
}
public function display() {
return [
'id' => $this->getId(),

View File

@ -152,6 +152,38 @@ class Poll {
public $defaultExpiracyDaysFromNow = 60;
private $maxChoicesLimit = 25;
public function computeAnswers() {
// counts each number of answer for this choice
$computedArray = [];
$people = [];
foreach ( $this->getStacksOfVotes() as $stack_of_vote ) {
foreach ( $stack_of_vote->getVotes() as $vote ) {
if ( ! isset( $computedArray[ $vote->getChoice()->getId() ] ) ) {
$computedArray[ $vote->getChoice()->getId() ] = [
'yes' => 0,
'maybe' => 0,
'no' => 0,
];
}
$computedArray[ $vote->getChoice()->getId() ][ $vote->getValue() ] ++;
$people[ $stack_of_vote->getOwner()->getPseudo() ] = $vote->getValue();
}
}
return [
'counts' => $computedArray,
'people' => $people,
];
}
public function display() {
return [
'poll' => $this,
'answers' => $this->computeAnswers(),
];
}
public function __construct() {
$this->votes = new ArrayCollection();
$this->stacksOfVotes = new ArrayCollection();
@ -171,6 +203,7 @@ class Poll {
$this->setAllowedAnswers( [ 'yes' ] );
}
public function generateAdminKey() {
$rand = random_int( PHP_INT_MIN, PHP_INT_MAX );