diff --git a/src/Controller/api/PollController.php b/src/Controller/api/PollController.php index 30a3190..bd8d213 100644 --- a/src/Controller/api/PollController.php +++ b/src/Controller/api/PollController.php @@ -103,30 +103,10 @@ class PollController extends EmailsController { $comments = $poll->getComments(); $stacks = $poll->getStacksOfVotes(); - $displayedComments = []; - foreach ( $comments as $comment ) { - $displayedComments[] = $comment->display(); - } - $displayedStackOfVotes = []; - foreach ( $stacks as $stack ) { - $displayedStackOfVotes[] = $stack->display(); - } - $displayedChoices = []; - foreach ( $poll->getChoices() as $choice - ) { - $displayedChoices[] = $choice->display(); - } + + $pass = $poll->getPassword(); - $returnedPoll = [ - 'message' => 'your poll config for ' . $poll->getTitle(), - - 'poll' => $poll->display(), - // TODO do not render sub objects of owner, it returns too many thing - 'stacks' => $displayedStackOfVotes, - 'choices' => $displayedChoices, - 'comments' => $displayedComments, - ]; /** * password protected content @@ -140,9 +120,7 @@ class PollController extends EmailsController { } else { // free access to poll -// return $this->returnPollData( $poll, $serializer ); - return $this->json( $returnedPoll ); -// return $this->json($poll); + return $this->json( $poll->display() ); } } @@ -169,7 +147,7 @@ class PollController extends EmailsController { if ( $poll->getPassword() === $md5 ) { // good matching pass - return $this->returnPollData( $poll, $serializer ); + return $this->json( $poll->display() ); } else { // wrong pass return $this->json( [ @@ -218,10 +196,8 @@ class PollController extends EmailsController { $em->persist( $poll ); $em->flush(); - return $this->json( [ - 'message' => 'you updated the poll ' . $poll->getTitle(), - "poll" => $poll, - ], + return $this->json( $poll->displayForAdmin() + , 200 ); } @@ -303,7 +279,6 @@ class PollController extends EmailsController { $newChoice = new Choice(); $newChoice ->setPoll( $newpoll ) -// ->setUrl( $c[ 'url' ] ) ->setName( $c[ 'literal' ] ); $em->persist( $newChoice ); // TODO add also choices for each time range in a day @@ -331,10 +306,8 @@ class PollController extends EmailsController { return $this->json( [ 'message' => 'you created a poll ' . $precision, - 'poll' => $newpoll, + 'poll' => $newpoll->displayForAdmin, 'password_protected' => is_string( $newpoll->getPassword() ), - 'admin_key' => $newpoll->getAdminKey(), - 'owner_modifier_token' => $foundOwner->getModifierToken(), ], 201 ); diff --git a/src/Entity/Poll.php b/src/Entity/Poll.php index 9129903..a3b913c 100755 --- a/src/Entity/Poll.php +++ b/src/Entity/Poll.php @@ -197,12 +197,13 @@ class Poll { 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 ] = [ - 'choice_id' => $choice_id, - 'choice_text' => $vote->getChoice()->getName(), - 'id' => $vote->getId(), + 'id' => $choice_id, + 'url' => $choice_url, + 'name' => $vote->getChoice()->getName(), 'score' => 0, 'yes' => [ 'count' => 0, @@ -233,16 +234,53 @@ class Poll { } } return [ - 'counts' => $computedArray, - 'maxScore' => $maxScore, + 'answers' => $computedArray, + 'max_score' => $maxScore, ]; } + public function displayForAdmin() { + $content = $this->display(); + $content['owner_modifier_token'] = $this->getOwner()->getModifierToken(); + $content['admin_key'] = $this->getAdminKey(); + $content['password_hash'] = $this->getPassword(); + $content['id'] = $this->getId(); + return $content; + } public function display() { + + $computedAnswers = $this->computeAnswers(); + $displayedStackOfVotes = []; + foreach ( $this->getStacksOfVotes() as $stack ) { + $displayedStackOfVotes[] = $stack->display(); + } + $displayedComments = []; + foreach ( $this->getComments() as $comment ) { + $displayedComments[] = $comment->display(); + } + + + return [ - 'config' => $this, + 'title' => $this->getTitle(), + 'creation_date' => $this->getCreationDate()->format('c'), + 'expiracy_date' => $this->getExpiracyDate()->format('c'), + 'votes_max' => $this->getVotesMax(), + 'choices_max' => $this->getChoicesMax(), + 'kind' => $this->getKind(), + 'allowed_answers' => $this->getAllowedAnswers(), + 'votes_allowed' => $this->getVotesAllowed(), + 'modification_policy' => $this->getModificationPolicy(), + 'hide_results' => $this->getHideResults(), + 'show_results_even_if_password' => $this->getShowResultEvenIfPasswords(), + 'owner' => [ + 'pseudo' => $this->getOwner()->getPseudo()] + , 'password_protected' => $this->getPassword() ? 'yes' : 'no', - 'answers' => $this->computeAnswers(), + 'max_score' => $computedAnswers['max_score'], + 'choices' => $computedAnswers['answers'], + 'stacks' => $displayedStackOfVotes, + 'comments' => $displayedComments, ]; }