⚡ compute vote counts in get poll config
This commit is contained in:
parent
64a770d99a
commit
86fe1d5720
|
@ -1,32 +1,32 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Controller;
|
namespace App\Controller;
|
||||||
|
|
||||||
use App\Entity\Choice;
|
use App\Entity\Choice;
|
||||||
use App\Entity\Comment;
|
use App\Entity\Comment;
|
||||||
use App\Entity\Owner;
|
use App\Entity\Owner;
|
||||||
use App\Entity\Poll;
|
use App\Entity\Poll;
|
||||||
use App\Entity\StackOfVotes;
|
use App\Entity\StackOfVotes;
|
||||||
use App\Entity\Vote;
|
use App\Entity\Vote;
|
||||||
use DateTime;
|
use DateTime;
|
||||||
use DateTimeZone;
|
use DateTimeZone;
|
||||||
use FOS\RestBundle\Controller\Annotations\Delete;
|
use FOS\RestBundle\Controller\Annotations\Delete;
|
||||||
use FOS\RestBundle\Controller\Annotations\Get;
|
use FOS\RestBundle\Controller\Annotations\Get;
|
||||||
use FOS\RestBundle\Controller\Annotations\Post;
|
use FOS\RestBundle\Controller\Annotations\Post;
|
||||||
use FOS\RestBundle\Controller\Annotations\Put;
|
use FOS\RestBundle\Controller\Annotations\Put;
|
||||||
use FOS\RestBundle\Controller\Annotations\Route;
|
use FOS\RestBundle\Controller\Annotations\Route;
|
||||||
use JMS\Serializer\SerializerBuilder;
|
use JMS\Serializer\SerializerBuilder;
|
||||||
use Swift_Message;
|
use Swift_Message;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class DefaultController
|
* Class DefaultController
|
||||||
* @package App\Controller
|
* @package App\Controller
|
||||||
* @Route("/api/v1",name="api_")
|
* @Route("/api/v1",name="api_")
|
||||||
*/
|
*/
|
||||||
class DefaultController extends AbstractController {
|
class DefaultController extends AbstractController {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -275,7 +275,7 @@ class DefaultController extends AbstractController {
|
||||||
'poll' => $poll,
|
'poll' => $poll,
|
||||||
'stacks_count' => count( $poll->getStacksOfVotes() ),
|
'stacks_count' => count( $poll->getStacksOfVotes() ),
|
||||||
'stacks' => $stacks,
|
'stacks' => $stacks,
|
||||||
'choices_count' => count( $poll->getChoices() ),
|
'choices_count' => $poll->computeAnswers(),
|
||||||
'choices' => $choices,
|
'choices' => $choices,
|
||||||
'comments' => $comments,
|
'comments' => $comments,
|
||||||
'comments_count' => count( $comments ),
|
'comments_count' => count( $comments ),
|
||||||
|
@ -646,4 +646,4 @@ class DefaultController extends AbstractController {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,20 +1,20 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Entity;
|
namespace App\Entity;
|
||||||
|
|
||||||
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 JMS\Serializer\Annotation as Serializer;
|
use JMS\Serializer\Annotation as Serializer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* one poll choice, could be a text or a date
|
* one poll choice, could be a text or a date
|
||||||
* @ORM\Entity(repositoryClass="App\Repository\ChoiceRepository")
|
* @ORM\Entity(repositoryClass="App\Repository\ChoiceRepository")
|
||||||
* @Serializer\ExclusionPolicy("all")
|
* @Serializer\ExclusionPolicy("all")
|
||||||
*/
|
*/
|
||||||
class Choice {
|
class Choice {
|
||||||
/**
|
/**
|
||||||
* @ORM\Id()
|
* @ORM\Id()
|
||||||
* @ORM\GeneratedValue()
|
* @ORM\GeneratedValue()
|
||||||
|
@ -61,6 +61,7 @@ class Choice {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function display() {
|
public function display() {
|
||||||
return [
|
return [
|
||||||
'id' => $this->getId(),
|
'id' => $this->getId(),
|
||||||
|
@ -141,4 +142,4 @@ class Choice {
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,18 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Entity;
|
namespace App\Entity;
|
||||||
|
|
||||||
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 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 {
|
||||||
/**
|
/**
|
||||||
* @ORM\Id()
|
* @ORM\Id()
|
||||||
* @ORM\GeneratedValue()
|
* @ORM\GeneratedValue()
|
||||||
|
@ -152,6 +152,38 @@ class Poll {
|
||||||
public $defaultExpiracyDaysFromNow = 60;
|
public $defaultExpiracyDaysFromNow = 60;
|
||||||
private $maxChoicesLimit = 25;
|
private $maxChoicesLimit = 25;
|
||||||
|
|
||||||
|
public function computeAnswers() {
|
||||||
|
// counts each number of answer for this choice
|
||||||
|
$computedArray = [];
|
||||||
|
$people = [];
|
||||||
|
|
||||||
|
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' => 0,
|
||||||
|
'maybe' => 0,
|
||||||
|
'no' => 0,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
$computedArray[ $vote->getChoice()->getId() ][ $vote->getValue() ] ++;
|
||||||
|
$people[ $stack_of_vote->getOwner()->getPseudo() ] = $vote->getValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'counts' => $computedArray,
|
||||||
|
'people' => $people,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function display() {
|
||||||
|
return [
|
||||||
|
'poll' => $this,
|
||||||
|
'answers' => $this->computeAnswers(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
$this->votes = new ArrayCollection();
|
$this->votes = new ArrayCollection();
|
||||||
$this->stacksOfVotes = new ArrayCollection();
|
$this->stacksOfVotes = new ArrayCollection();
|
||||||
|
@ -171,6 +203,7 @@ class Poll {
|
||||||
$this->setAllowedAnswers( [ 'yes' ] );
|
$this->setAllowedAnswers( [ 'yes' ] );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function generateAdminKey() {
|
public function generateAdminKey() {
|
||||||
$rand = random_int( PHP_INT_MIN, PHP_INT_MAX );
|
$rand = random_int( PHP_INT_MIN, PHP_INT_MAX );
|
||||||
|
|
||||||
|
@ -543,4 +576,4 @@ class Poll {
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue