mirror of
https://framagit.org/tykayn/date-poll-api
synced 2023-08-25 08:23:11 +02:00
Merge branch 'master' of https://framagit.org/tykayn/date-poll-api
This commit is contained in:
commit
1341b06478
@ -275,7 +275,7 @@ class DefaultController extends AbstractController {
|
|||||||
'poll' => $poll,
|
'poll' => $poll,
|
||||||
'stacks_count' => count( $poll->getStacksOfVotes() ),
|
'stacks_count' => count( $poll->getStacksOfVotes() ),
|
||||||
'stacks' => $stacks,
|
'stacks' => $stacks,
|
||||||
'choices_count' => count( $poll->getChoices() ),
|
'choices_count' => $poll->computeAnswers(),
|
||||||
'choices' => $choices,
|
'choices' => $choices,
|
||||||
'comments' => $comments,
|
'comments' => $comments,
|
||||||
'comments_count' => count( $comments ),
|
'comments_count' => count( $comments ),
|
||||||
|
@ -61,6 +61,7 @@ class Choice {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function display() {
|
public function display() {
|
||||||
return [
|
return [
|
||||||
'id' => $this->getId(),
|
'id' => $this->getId(),
|
||||||
|
@ -152,6 +152,47 @@ class Poll {
|
|||||||
public $defaultExpiracyDaysFromNow = 60;
|
public $defaultExpiracyDaysFromNow = 60;
|
||||||
private $maxChoicesLimit = 25;
|
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' => [
|
||||||
|
'count' => 0,
|
||||||
|
'people' => [],
|
||||||
|
],
|
||||||
|
'maybe' => [
|
||||||
|
'count' => 0,
|
||||||
|
'people' => [],
|
||||||
|
],
|
||||||
|
'no' => [
|
||||||
|
'count' => 0,
|
||||||
|
'people' => [],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
$computedArray[ $vote->getChoice()->getId() ][ $vote->getValue() ][ 'count' ] ++;
|
||||||
|
$computedArray[ $vote->getChoice()->getId() ][ $vote->getValue() ][ 'people' ][] = $stack_of_vote->getOwner()->getPseudo();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'counts' => $computedArray,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function display() {
|
||||||
|
return [
|
||||||
|
'poll' => $this,
|
||||||
|
'answers' => $this->computeAnswers(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
$this->votes = new ArrayCollection();
|
$this->votes = new ArrayCollection();
|
||||||
$this->stacksOfVotes = new ArrayCollection();
|
$this->stacksOfVotes = new ArrayCollection();
|
||||||
@ -171,6 +212,7 @@ class Poll {
|
|||||||
$this->setAllowedAnswers( [ 'yes' ] );
|
$this->setAllowedAnswers( [ 'yes' ] );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function generateAdminKey() {
|
public function generateAdminKey() {
|
||||||
$rand = random_int( PHP_INT_MIN, PHP_INT_MAX );
|
$rand = random_int( PHP_INT_MIN, PHP_INT_MAX );
|
||||||
|
|
||||||
|
@ -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(),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user