group date and slices in display of poll
This commit is contained in:
parent
723c774d51
commit
c0791d52ac
|
@ -61,12 +61,23 @@ class Choice {
|
||||||
}
|
}
|
||||||
|
|
||||||
public function display( $kind = 'text' ): array {
|
public function display( $kind = 'text' ): array {
|
||||||
|
if($kind == 'text'){
|
||||||
$fields = [
|
$fields = [
|
||||||
'id' => $this->getId(),
|
'id' => $this->getId(),
|
||||||
'created_at' => $this->getCreatedAtAsString(),
|
'created_at' => $this->getCreatedAtAsString(),
|
||||||
'name' => $this->getName(),
|
'name' => $this->getName(),
|
||||||
'url' => $this->getUrl(),
|
'url' => $this->getUrl(),
|
||||||
];
|
];
|
||||||
|
}
|
||||||
|
elseif($kind=='date'){
|
||||||
|
$fields = [
|
||||||
|
'id' => $this->getId(),
|
||||||
|
'created_at' => $this->getCreatedAtAsString(),
|
||||||
|
'name' => $this->getName(),
|
||||||
|
// 'url' => $this->getUrl(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
return $fields;
|
return $fields;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,22 +1,22 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Entity;
|
namespace App\Entity;
|
||||||
|
|
||||||
use App\Traits\RandomTrait;
|
use App\Traits\RandomTrait;
|
||||||
use App\Traits\TimeStampableTrait;
|
use App\Traits\TimeStampableTrait;
|
||||||
use DateTime;
|
use DateTime;
|
||||||
use DateTimeInterface;
|
use DateTimeInterface;
|
||||||
use Doctrine\Common\Collections\ArrayCollection;
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
use Doctrine\Common\Collections\Collection;
|
use Doctrine\Common\Collections\Collection;
|
||||||
use Doctrine\ORM\Mapping as ORM;
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
use ErrorException;
|
use ErrorException;
|
||||||
use JMS\Serializer\Annotation as Serializer;
|
use JMS\Serializer\Annotation as Serializer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ORM\Entity(repositoryClass="App\Repository\PollRepository")
|
* @ORM\Entity(repositoryClass="App\Repository\PollRepository")
|
||||||
* @Serializer\ExclusionPolicy("all")
|
* @Serializer\ExclusionPolicy("all")
|
||||||
*/
|
*/
|
||||||
class Poll {
|
class Poll {
|
||||||
|
|
||||||
use RandomTrait;
|
use RandomTrait;
|
||||||
|
|
||||||
|
@ -244,7 +244,7 @@
|
||||||
}
|
}
|
||||||
$displayedChoices = [];
|
$displayedChoices = [];
|
||||||
foreach ( $this->getChoices() as $choice ) {
|
foreach ( $this->getChoices() as $choice ) {
|
||||||
$displayedChoices[] = $choice->display($this->getKind());
|
$displayedChoices[] = $choice->display( $this->getKind() );
|
||||||
}
|
}
|
||||||
$displayedComments = [];
|
$displayedComments = [];
|
||||||
foreach ( $this->getComments() as $comment ) {
|
foreach ( $this->getComments() as $comment ) {
|
||||||
|
@ -277,14 +277,38 @@
|
||||||
'comments' => $displayedComments,
|
'comments' => $displayedComments,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
if ( $this->getKind() == 'text' ) {
|
||||||
|
$resp[ 'choices' ] = $computedAnswers[ 'answers' ];
|
||||||
|
} elseif ( $this->getKind() == 'date' ) {
|
||||||
|
$resp[ 'choices_grouped' ] = $computedAnswers[ 'grouped_dates' ];
|
||||||
|
}
|
||||||
|
|
||||||
return $resp;
|
return $resp;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function computeAnswers() {
|
public function computeAnswers() {
|
||||||
$computedArray = [];
|
$computedArray = [];
|
||||||
|
$grouped_dates = [];
|
||||||
$maxScore = 0;
|
$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 = [
|
$scoreInfos = [
|
||||||
'score' => 0,
|
'score' => 0,
|
||||||
'yes' => [
|
'yes' => [
|
||||||
|
@ -351,9 +375,24 @@
|
||||||
foreach ( $computedArray as $choice_stat ) {
|
foreach ( $computedArray as $choice_stat ) {
|
||||||
$answersWithStats[] = $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 [
|
return [
|
||||||
'answers' => $answersWithStats,
|
'answers' => $answersWithStats,
|
||||||
|
'grouped_dates' => $groupsOfDates,
|
||||||
'max_score' => $maxScore,
|
'max_score' => $maxScore,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@ -716,4 +755,4 @@
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue