enrich computed data

This commit is contained in:
Baptiste Lemoine 2020-01-30 11:19:36 +01:00
parent 58c28474f8
commit 510e5303be
2 changed files with 43 additions and 15 deletions

View File

@ -87,7 +87,6 @@
->setTo( $founduser->getEmail() )
->setBody(
$this->renderView(
// templates/hello/email.txt.twig
'emails/owner-list.html.twig',
$templateVars
)
@ -95,11 +94,7 @@
$mailer->send( $message );
return $this->render( 'emails/owner-list.html.twig', $templateVars );
// return $this->json( [
// 'message' => 'here are your polls, ' . $email,
// 'data' => 'email was sent with a list of ' . count( $polls ) . ' polls',
// ],
// 200 );
} else {
return $this->json( [
'message' => 'no user found for email ' . $email,
@ -535,11 +530,27 @@
if ( $existingOwner ) {
$precision = ' from an existing owner : ' . $foundOwner->getEmail();
}
$comments = [];
$stacks = [];
$choices = [];
foreach ( $poll->getComments() as $c ) {
$comments[] = $c->display();
}
foreach ( $poll->getStacksOfVotes() as $c ) {
$stacks[] = $c->display();
}
foreach ( $poll->getChoices() as $c ) {
$choices[] = $c->display();
}
return $this->json( [
'message' => 'you created a vote stack' . $precision,
'poll' => $poll,
'vote_stack' => $stack->display(),
'stacks' => $stacks,
'comments' => $comments,
'choices' => $choices,
'choices_count' => $poll->computeAnswers(),
'vote_count' => count( $poll->getStacksOfVotes() ),
'owner_modifier_token' => $foundOwner->getModifierToken(),
'admin_key' => $poll->getAdminKey(),

View File

@ -155,34 +155,51 @@
public function computeAnswers() {
// counts each number of answer for this choice
$computedArray = [];
$people = [];
$maxScore = 0;
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' => [
$answer = $vote->getValue();
$choice_id = $vote->getChoice()->getId();
if ( ! isset( $computedArray[ $choice_id ] ) ) {
$computedArray[ $choice_id ] = [
'choice_id' => $choice_id,
'choice_text' => $vote->getChoice()->getName(),
'id' => $vote->getId(),
'score' => 0,
'yes' => [
'count' => 0,
'people' => [],
],
'maybe' => [
'maybe' => [
'count' => 0,
'people' => [],
],
'no' => [
'no' => [
'count' => 0,
'people' => [],
],
];
}
$computedArray[ $vote->getChoice()->getId() ][ $vote->getValue() ][ 'count' ] ++;
$computedArray[ $vote->getChoice()->getId() ][ $vote->getValue() ][ 'people' ][] = $stack_of_vote->getOwner()->getPseudo();
$computedArray[ $choice_id ][ $answer ][ 'count' ] ++;
$computedArray[ $choice_id ][ $answer ][ 'people' ][] = $stack_of_vote->getOwner()->getPseudo();
if ( $answer == 'yes' ) {
$computedArray[ $choice_id ][ 'score' ] += 1;
} elseif ( $answer == 'maybe' ) {
$computedArray[ $choice_id ][ 'score' ] += 0.5;
}
// compare with max value
if ( $computedArray[ $choice_id ][ 'score' ] > $maxScore ) {
$maxScore = $computedArray[ $choice_id ][ 'score' ];
}
}
}
return [
'counts' => $computedArray,
'counts' => $computedArray,
'maxScore' => $maxScore,
];
}