1
0
mirror of https://framagit.org/tykayn/date-poll-api synced 2023-08-25 08:23:11 +02:00

group date and slices in display of poll

This commit is contained in:
Tykayn 2021-04-30 15:34:04 +02:00 committed by tykayn
parent 723c774d51
commit c0791d52ac
2 changed files with 655 additions and 605 deletions

View File

@ -61,12 +61,23 @@ class Choice {
}
public function display( $kind = 'text' ): array {
if($kind == 'text'){
$fields = [
'id' => $this->getId(),
'created_at' => $this->getCreatedAtAsString(),
'name' => $this->getName(),
'url' => $this->getUrl(),
];
}
elseif($kind=='date'){
$fields = [
'id' => $this->getId(),
'created_at' => $this->getCreatedAtAsString(),
'name' => $this->getName(),
// 'url' => $this->getUrl(),
];
}
return $fields;
}

View File

@ -1,22 +1,22 @@
<?php
namespace App\Entity;
namespace App\Entity;
use App\Traits\RandomTrait;
use App\Traits\TimeStampableTrait;
use DateTime;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use ErrorException;
use JMS\Serializer\Annotation as Serializer;
use App\Traits\RandomTrait;
use App\Traits\TimeStampableTrait;
use DateTime;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use ErrorException;
use JMS\Serializer\Annotation as Serializer;
/**
/**
* @ORM\Entity(repositoryClass="App\Repository\PollRepository")
* @Serializer\ExclusionPolicy("all")
*/
class Poll {
class Poll {
use RandomTrait;
@ -244,7 +244,7 @@
}
$displayedChoices = [];
foreach ( $this->getChoices() as $choice ) {
$displayedChoices[] = $choice->display($this->getKind());
$displayedChoices[] = $choice->display( $this->getKind() );
}
$displayedComments = [];
foreach ( $this->getComments() as $comment ) {
@ -277,14 +277,38 @@
'comments' => $displayedComments,
];
if ( $this->getKind() == 'text' ) {
$resp[ 'choices' ] = $computedAnswers[ 'answers' ];
} elseif ( $this->getKind() == 'date' ) {
$resp[ 'choices_grouped' ] = $computedAnswers[ 'grouped_dates' ];
}
return $resp;
}
public function computeAnswers() {
$computedArray = [];
$grouped_dates = [];
$maxScore = 0;
if ( $this->getKind() == 'date' ) {
foreach ( $this->getChoices() as $choice ) {
$boom = explode( ' >>> ', $choice->getName() );
if ( ! isset( $grouped_dates[ $boom[ 0 ] ] ) ) {
$grouped_dates[ $boom[ 0 ] ] = [
"date_string" => $boom[ 0 ],
"time_slices" => [],
];
}
$grouped_dates[ $boom[ 0 ] ][ "time_slices" ][] = [
"choice_id" => $choice->getId(),
"name" => $boom[ 1 ] ];
}
}
$scoreInfos = [
'score' => 0,
'yes' => [
@ -351,9 +375,24 @@
foreach ( $computedArray as $choice_stat ) {
$answersWithStats[] = $choice_stat;
}
$groupsOfDates = [];
foreach ( $grouped_dates as $group ) {
$ii =0;
foreach ( $group["time_slices"] as $slice ) {
$slice['score'] = $computedArray[ $slice['choice_id'] ]['score'];
$slice['yes'] = $computedArray[ $slice['choice_id'] ]['yes'];
$slice['maybe'] = $computedArray[ $slice['choice_id'] ]['maybe'];
$slice['no'] = $computedArray[ $slice['choice_id'] ]['no'];
$group["time_slices"][$ii] = $slice;
$ii++;
}
$groupsOfDates[] = $group ;
}
return [
'answers' => $answersWithStats,
'grouped_dates' => $groupsOfDates,
'max_score' => $maxScore,
];
}
@ -716,4 +755,4 @@
return $this;
}
}
}