From 31c75f69b24e37aa382e5140a97db18fb55721a7 Mon Sep 17 00:00:00 2001 From: Tykayn Date: Thu, 10 Jun 2021 12:46:20 +0200 Subject: [PATCH] display also empty votes in stacks --- src/Controller/api/v1/PollController.php | 4 +++- src/Entity/StackOfVotes.php | 19 +++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/Controller/api/v1/PollController.php b/src/Controller/api/v1/PollController.php index 9d9494b..1b37a04 100644 --- a/src/Controller/api/v1/PollController.php +++ b/src/Controller/api/v1/PollController.php @@ -179,7 +179,9 @@ class PollController extends EmailsController { // wrong pass return $this->json( [ 'message' => 'this is protected by a password, your password hash "' . $md5 . '" is wrong, and you should feel bad', -// 'md5' => md5( $md5 ), + 'pass' => $md5 , + 'md5' => md5( $md5 ), + 'md5( $poll->getPassword() )' => md5( $poll->getPassword() ), 'data' => null, ], 403 ); diff --git a/src/Entity/StackOfVotes.php b/src/Entity/StackOfVotes.php index 4547434..b6d1ccc 100755 --- a/src/Entity/StackOfVotes.php +++ b/src/Entity/StackOfVotes.php @@ -59,15 +59,30 @@ class StackOfVotes { public function display() { + + // fill all votes by choices + $choices = $this->getPoll()->getChoices(); + $defaultVotes = []; + foreach ( $choices as $choice ) { + $defaultVotes[ $choice->getId() ] = [ "choice_id" => $choice->getId(), "value" => "" ]; + } + $tab = [ 'id' => $this->getId(), 'pseudo' => $this->getPseudo(), 'created_at' => $this->getCreatedAtAsString(), - 'votes' => [], + 'votes' => $defaultVotes, ]; foreach ( $this->getVotes() as $vote ) { - $tab[ 'votes' ][] = $vote->display(); + $tab[ 'votes' ][ $vote->getChoice()->getId() ] = $vote->display(); } + // flatten the votes array + $flatVotes = []; + foreach ( $tab[ 'votes' ] as $vote ) { + $flatVotes[] = $vote; + } + $tab[ 'votes' ] = $flatVotes; + $tab[ 'owner' ] = $this->getOwner()->display(); return $tab;