diff --git a/src/Controller/api/PollController.php b/src/Controller/api/PollController.php index 5249e38..d56e61e 100644 --- a/src/Controller/api/PollController.php +++ b/src/Controller/api/PollController.php @@ -38,41 +38,44 @@ class PollController extends EmailsController { $data = $pollRepository->findAll(); - $polls = $data; - $titles=[]; + $polls = $data; + $titles = []; $pollData = [ 'message' => 'here are your polls', - 'count' => count($polls), + 'count' => count( $polls ), ]; - $debug=1; + $debug = 1; - if($debug){ + if ( $debug ) { foreach ( $polls as $poll ) { - $titles[] = ['title' => $poll->getTitle(), - 'slug' => $poll->getCustomUrl() + $titles[] = [ + 'title' => $poll->getTitle(), + 'slug' => $poll->getCustomUrl(), ]; } - $pollData['polls'] = $titles; + $pollData[ 'polls' ] = $titles; } - return $this->json( $pollData); + return $this->json( $pollData ); } /** * @param $id * message when the poll is not found + * * @return JsonResponse */ - public function notFoundPoll($id): Response{ + public function notFoundPoll( $id ): Response { return $this->json( [ 'message' => $id . ' : poll not found', ], 404 ); } + /** * get a poll config by its custom URL, we do not want polls to be reachable by their numeric id * @Get( @@ -89,7 +92,7 @@ class PollController extends EmailsController { SerializerInterface $serializer, $customUrl, Request $request - ): Response { + ) { $repository = $this->getDoctrine()->getRepository( Poll::class ); $poll = $repository->findOneByCustomUrl( $customUrl ); @@ -109,7 +112,7 @@ class PollController extends EmailsController { 'stacks' => $poll->getStacksOfVotes(), 'choices_count' => $poll->computeAnswers(), 'choices' => $poll->getChoices(), - 'comments' => $comments, + 'comments' => $comments, 'comments_count' => count( $comments ), ]; @@ -119,13 +122,15 @@ class PollController extends EmailsController { if ( $pass ) { // no password possibly given by this route return $this->json( [ - 'message' => 'this is protected by a password,but you did not provide the encoded password parameter, and you should feel bad. ' , + 'message' => 'this is protected by a password,but you did not provide the encoded password parameter, and you should feel bad. ', ], 403 ); } else { // free access to poll - return $this->returnPollData( $poll, $serializer ); +// return $this->returnPollData( $poll, $serializer ); +// return $this->json($returnedPoll); + return $this->json($poll); } } @@ -142,22 +147,22 @@ class PollController extends EmailsController { * * @return JsonResponse|Response */ - function getProtectedPoll($customUrl,$md5, SerializerInterface $serializer){ + function getProtectedPoll( $customUrl, $md5, SerializerInterface $serializer ) { $repository = $this->getDoctrine()->getRepository( Poll::class ); $poll = $repository->findOneByCustomUrl( $customUrl ); if ( ! $poll ) { - return $this->notFoundPoll($customUrl); + return $this->notFoundPoll( $customUrl ); } - if ( $poll->getPassword() === $md5 ) { + if ( $poll->getPassword() === $md5 ) { // good matching pass return $this->returnPollData( $poll, $serializer ); } else { // wrong pass return $this->json( [ - 'message' => 'this is protected by a password, your password "' . $md5 . '" is wrong, and you should feel bad', - 'md5' => md5($md5), + 'message' => 'this is protected by a password, your password hash "' . $md5 . '" is wrong, and you should feel bad', + 'md5' => md5( $md5 ), 'data' => null, ], 403 ); @@ -203,6 +208,7 @@ class PollController extends EmailsController { return $this->json( [ 'message' => 'you updated the poll ' . $poll->getTitle(), + "poll" => $poll, ], 200 ); } diff --git a/src/DataFixtures/AppPollFixtures.php b/src/DataFixtures/AppPollFixtures.php index 36bde47..a499990 100755 --- a/src/DataFixtures/AppPollFixtures.php +++ b/src/DataFixtures/AppPollFixtures.php @@ -14,6 +14,9 @@ use Doctrine\Persistence\ObjectManager; class AppPollFixtures extends Fixture { public const POLL_FIXTURE_ONE = 'citron-poll-fixture'; + public const POLL_FIXTURE_TWO = 'aujourdhui-ou-demain'; + public const POLL_FIXTURE_THREE = 'citron'; + public const POLL_FIXTURE_FOUR = 'demo'; public function load( ObjectManager $manager ) { @@ -41,7 +44,6 @@ class AppPollFixtures extends Fixture { $pollCitronOrange->setTitle( 'citron ou orange' ) ->setCustomUrl('citron') ->setDescription( 'votre sorbert préféré' ) - ->setAdminKey( uniqid() ) ->setModificationPolicy( 'nobody' ) ->setPassword('le pass woute woute'); @@ -120,7 +122,6 @@ class AppPollFixtures extends Fixture { ->setCustomUrl('demo') ->setDescription( 'description du sondage 2' ); - $pollCitronOrange->setAdminKey( uniqid() ); $pollCitronOrange->setModificationPolicy( 'self' ); $pollCitronOrange->setMailOnComment( true ); @@ -154,16 +155,16 @@ class AppPollFixtures extends Fixture { ->addChoice( $choice ) ->addChoice( $choice2 ) ->addChoice( $choice3 ) - ->setModificationPolicy( 'self' ); + ->setModificationPolicy( 'everybody' ); $manager->persist( $pollCitronOrange ); // poll with cartoon choices - $pollCitronOrange = new Poll(); - $pollCitronOrange->setTitle( 'dessin animé préféré' ) + $pollCartoons = new Poll(); + $pollCartoons->setTitle( 'dessin animé préféré' ) ->setCustomUrl('dessin-anime') ->setDescription( 'choisissez votre animé préféré' ) ->setOwner( $owner ) - ->setModificationPolicy( 'self' ) + ->setModificationPolicy( 'nobody' ) ->addTextChoiceArray( [ "Vic le viking", "Boumbo petite automobile", @@ -179,61 +180,72 @@ class AppPollFixtures extends Fixture { ->setPseudo('un gens qui commente') ->setText( "allez boumbo!" ) ->setOwner( $commenterMan ); - $pollCitronOrange->addComment( $someoneComment ); + $pollCartoons->addComment( $someoneComment ); $someoneComment2 = new Comment(); $someoneComment2 ->setPseudo('un gens qui commente') ->setText( "je suis pour la team rocket de digimon" ) ->setOwner( $owner ); - $pollCitronOrange->addComment( $someoneComment2 ); + $pollCartoons->addComment( $someoneComment2 ); - $manager->persist( $pollCitronOrange ); + $manager->persist( $pollCartoons ); $stack = new StackOfVotes(); $stack->setPseudo( 'Wulfila' ); $stack - ->setPoll( $pollCitronOrange ) + ->setPoll( $pollCartoons ) ->setOwner( $voter ); + $pollCartoons->addStackOfVote($stack); $vote = new Vote(); $vote - ->setPoll( $pollCitronOrange ) + ->setPoll( $pollCartoons ) ->setStacksOfVotes( $stack ) ->setValue( "yes" ) - ->setChoice( $pollCitronOrange->getChoices()[ 2 ] ); + ->setChoice( $pollCartoons->getChoices()[ 2 ] ); + $pollCartoons->addVote($vote); + $vote = new Vote(); $vote - ->setPoll( $pollCitronOrange ) + ->setPoll( $pollCartoons ) ->setStacksOfVotes( $stack ) ->setValue( "maybe" ) - ->setChoice( $pollCitronOrange->getChoices()[ 1 ] ); + ->setChoice( $pollCartoons->getChoices()[ 1 ] ); + $pollCartoons->addVote($vote); $manager->persist( $stack ); $stack = new StackOfVotes(); $stack->setPseudo( 'Tykayn' ); $stack - ->setPoll( $pollCitronOrange ) + ->setPoll( $pollCartoons ) ->setOwner( $voter ); + $vote = new Vote(); $vote - ->setPoll( $pollCitronOrange ) + ->setPoll( $pollCartoons ) ->setStacksOfVotes( $stack ) ->setValue( "yes" ) - ->setChoice( $pollCitronOrange->getChoices()[ 1 ] ); + ->setChoice( $pollCartoons->getChoices()[ 1 ] ); + $pollCartoons->addVote($vote); + $vote = new Vote(); $vote - ->setPoll( $pollCitronOrange ) + ->setPoll( $pollCartoons ) ->setStacksOfVotes( $stack ) ->setValue( "yes" ) - ->setChoice( $pollCitronOrange->getChoices()[ 2 ] ); + ->setChoice( $pollCartoons->getChoices()[ 2 ] ); + $pollCartoons->addVote($vote); + $vote = new Vote(); $vote - ->setPoll( $pollCitronOrange ) + ->setPoll( $pollCartoons ) ->setStacksOfVotes( $stack ) ->setValue( "no" ) - ->setChoice( $pollCitronOrange->getChoices()[ 2 ] ); + ->setChoice( $pollCartoons->getChoices()[ 2 ] ); + $pollCartoons->addVote($vote); + $manager->persist( $pollCartoons ); $manager->persist( $stack ); diff --git a/src/Entity/Poll.php b/src/Entity/Poll.php index 5b3c39d..355ad96 100755 --- a/src/Entity/Poll.php +++ b/src/Entity/Poll.php @@ -79,7 +79,7 @@ class Poll { * @Serializer\Type("boolean") * @Serializer\Expose() */ - public $votesAllowed; + public $votesAllowed = true; /** * max number of stack of votes possible. * limits the number of people who can answer. as long as you trust the people to give only one answer with a reliable system. @@ -87,7 +87,7 @@ class Poll { * @Serializer\Type("smallint") * @Serializer\Expose() */ - public $votesMax; + public $votesMax = 1024; /** * max number of choices people can answer in a stack of vote. for text polls only, not date kind. @@ -97,7 +97,7 @@ class Poll { * @Serializer\Type("smallint") * @Serializer\Expose() */ - public $choicesMax; + public $choicesMax = -1; /** * people can add comments @@ -105,7 +105,7 @@ class Poll { * @Serializer\Type("boolean") * @Serializer\Expose() */ - public $commentsAllowed; + public $commentsAllowed = true; /** * kind of way the people can modify the poll @@ -116,35 +116,35 @@ class Poll { * @Serializer\Type("string") * @Serializer\Expose() */ - public $modificationPolicy = 'nobody'; + public $modificationPolicy = 'everybody'; /** * send a mail on a new comment * @ORM\Column(type="boolean", nullable=true) * @Serializer\Type("boolean") * @Serializer\Expose() */ - public $mailOnComment; + public $mailOnComment = true; /** * send a mail on a new vote * @ORM\Column(type="boolean", nullable=true) * @Serializer\Type("boolean") * @Serializer\Expose() */ - public $mailOnVote; + public $mailOnVote = false; /** * hide publicly results * @ORM\Column(type="boolean", nullable=true) * @Serializer\Type("boolean") * @Serializer\Expose() */ - public $hideResults; + public $hideResults = false; /** * show publicly results even if there is a password to access the vote * @ORM\Column(type="boolean", nullable=true) * @Serializer\Type("boolean") * @Serializer\Expose() */ - public $showResultEvenIfPasswords; + public $showResultEvenIfPasswords = false; /** * @ORM\OneToMany(targetEntity="App\Entity\Vote", mappedBy="poll", orphanRemoval=true, cascade={"persist", "remove"}) * @Serializer\Type("ArrayCollection") @@ -247,22 +247,23 @@ class Poll { } public function __construct() { - $this->votes = new ArrayCollection(); - $this->stacksOfVotes = new ArrayCollection(); - $this->choices = new ArrayCollection(); - $this->comments = new ArrayCollection(); + $this->initiate(); } private function initiate() { - $this->adminKey = $this->generateAdminKey(); + $this->votes = new ArrayCollection(); + $this->stacksOfVotes = new ArrayCollection(); + $this->choices = new ArrayCollection(); + $this->comments = new ArrayCollection(); + $this->setAdminKey( $this->generateAdminKey() ); $this->setCreationDate( new \DateTime() ); $this->setExpiracyDate( $this->addDaysToDate( new \DateTime(), $this->defaultExpiracyDaysFromNow ) ); - $this->setAllowedAnswers( [ 'yes' ] ); + $this->setAllowedAnswers( [ 'yes', 'maybe', 'no' ] ); } @@ -324,7 +325,7 @@ class Poll { } public function addDaysToDate( \DateTime $date, int $days ) { - $st = strtotime( $date->getTimestamp() . ' + ' . $days . ' days' ); + $st = strtotime( $date->getTimestamp() ); return new \DateTime( $st ); }