From 510e5303be4384245602e3341cdb3279e2452df8 Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Thu, 30 Jan 2020 11:19:36 +0100 Subject: [PATCH 1/5] :zap: enrich computed data --- src/Controller/DefaultController.php | 23 +++++++++++++----- src/Entity/Poll.php | 35 +++++++++++++++++++++------- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/src/Controller/DefaultController.php b/src/Controller/DefaultController.php index 6b5189b..68c9851 100644 --- a/src/Controller/DefaultController.php +++ b/src/Controller/DefaultController.php @@ -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(), diff --git a/src/Entity/Poll.php b/src/Entity/Poll.php index 777e07f..900341d 100644 --- a/src/Entity/Poll.php +++ b/src/Entity/Poll.php @@ -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, ]; } From 39722aba29a40e757284975c1949c9934154361c Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Thu, 30 Jan 2020 11:28:24 +0100 Subject: [PATCH 2/5] :zap: split controllers --- README.md | 2 +- src/Controller/CommentController.php | 160 ++++++ src/Controller/DefaultController.php | 697 +++------------------------ src/Controller/PollController.php | 307 ++++++++++++ src/Controller/VoteController.php | 165 +++++++ 5 files changed, 687 insertions(+), 644 deletions(-) create mode 100644 src/Controller/CommentController.php create mode 100644 src/Controller/PollController.php create mode 100644 src/Controller/VoteController.php diff --git a/README.md b/README.md index 9978515..6bca0f5 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ this file is not versionned and should stay like this. ## cronjob to delete expired polls add this line in your crontab to run the clearance of expired polls everyday at 0h00. ``` -0 0 * * * wget http://MYWEBSITE/api/v1/clean-polls +0 0 * * * wget http://MYWEBSITE/api/v1/poll/clean-polls ``` you can open your crontabl in command line with : ``` diff --git a/src/Controller/CommentController.php b/src/Controller/CommentController.php new file mode 100644 index 0000000..1ee0f17 --- /dev/null +++ b/src/Controller/CommentController.php @@ -0,0 +1,160 @@ +json( [ + 'message' => 'here are your comments of the poll', + 'data' => $poll->getComments(), + ], + 200 ); + } + + /** + * add a comment on a poll + * @Post( + * path = "/poll/{id}/comment", + * name = "new_comment", + * requirements = {"content"="\w+", "poll_id"="\d+"} + * ) + */ + public + function newCommentAction( + Poll $poll, + Request $request + ) { + if ( ! $poll ) { + return $this->json( [ 'message' => 'poll not found' ], 404 ); + } + $data = $request->getContent(); + + $serializer = SerializerBuilder::create()->build(); + $comment = $serializer->deserialize( $data, 'App\Entity\Comment', 'json' ); + + $em = $this->getDoctrine()->getRepository( Owner::class ); + + $data = json_decode( $data, true ); + + $foundOwner = $em->findOneByEmail( $data[ 'email' ] ); + // manage existing or new Owner + if ( ! $foundOwner ) { + $foundOwner = new Owner(); + $foundOwner->setPseudo( $data[ 'owner' ][ 'email' ] ) + ->setEmail( $data[ 'owner' ][ 'email' ] ) + ->setModifierToken( uniqid() ); + } + // anti flood + $seconds_limit_lastpost = 5; + $emComment = $this->getDoctrine()->getRepository( Comment::class ); + $lastCommentOfOwner = $emComment->findBy( [ 'owner' => $foundOwner ], [ 'id' => 'desc' ] ); + + // TODO anti flood by session / IP + + if ( $lastCommentOfOwner ) { + + + // check time of last comment + $now = new \DateTime(); + $now = $now->format( 'Y-m-d H:i:s' ); + $date_first = strtotime( $lastCommentOfOwner[ 0 ]->getCreatedAt()->format( 'Y-m-d H:i:s' ) ); + $date_second = strtotime( $now ); + + if ( ( $date_second - $date_first ) < $seconds_limit_lastpost ) { + return $this->json( [ + 'message' => 'anti flood déclenché', + 'details' => 'votre deriner commentaire a été envoyé il y a moins de ' . $seconds_limit_lastpost . ' secondes', + ], + 403 ); + } + + // check similar text content + if ( $lastCommentOfOwner[ 0 ]->getText() == $comment->getText() ) { + return $this->json( [ + 'message' => 'anti flood déclenché', + 'details' => 'votre deriner commentaire a exactement le même contenu que celui ci, il n\'a donc pas été créé', + ], + 403 ); + } + } + $comment->setOwner( $foundOwner ) + ->setCreatedAt( new \DateTime() ) + ->setPoll( $poll ); + $foundOwner->addComment( $comment ); + + $em = $this->getDoctrine()->getManager(); + $em->persist( $foundOwner ); + $em->persist( $comment ); + $em->flush(); + + return $this->json( [ + 'message' => 'you created a comment', + 'data' => [ + 'your_comment' => $comment->display(), + ], + ], + 201 ); + } + + /** + * Erase all comments of a poll + * @Delete( + * path = "/poll/{id}/comments", + * name = "poll_comments_delete", + * requirements = {"accessToken"="\w+", "poll_id"="\d+"} + * ) + * + * @param Poll $poll + * @param $accessToken + * + * @return JsonResponse + */ + public + function deletePollCommentsAction( + Poll $poll, + $accessToken + ) { + if ( $accessToken == $poll->getAdminKey() ) { + $em = $this->getDoctrine()->getManager(); + $length = count( $poll->getComments() ); + $em->remove( $poll->getComments() ); + $em->flush(); + + return $this->json( [ + 'message' => 'boom! les ' . $length . ' commentaires du sondage ont été supprimés', + ] ); + } else { + return $this->json( [ + 'message' => 'le token d\'autorisation est invalide, vous ne pouvez pas modifier ce sondage', + ] ); + } + } + +} diff --git a/src/Controller/DefaultController.php b/src/Controller/DefaultController.php index 68c9851..6933d5f 100644 --- a/src/Controller/DefaultController.php +++ b/src/Controller/DefaultController.php @@ -1,660 +1,71 @@ getDoctrine()->getRepository( Owner::class ); + $founduser = $repository->findOneBy( [ 'email' => $email ] ); - - /** - * @Get( - * path = "/my-polls", - * name = "get_my_polls", - * requirements = {"access_token"="\w+"} - * ) - */ - public function showMyPollsAction() { - return $this->json( [ - 'message' => 'here are your polls', - 'poll' => new Poll(), - ] ); - } - - /** - * @Get( - * path = "/polls", - * name = "get_all_polls" - * ) - */ - public function getAllPollsAction() { - $repository = $this->getDoctrine()->getRepository( Poll::class ); - $data = $repository->findall(); - - - return $this->json( [ - 'message' => 'here are your polls', - 'poll' => $data, - ], - 200 ); - } - - /** - * @Get( - * path = "/send-polls-to-user/{email}", - * name = "send_user_polls" - * ) - * @param $email - * @param \Swift_Mailer $mailer - * - * @return JsonResponse - */ - public function sendPollsToUser( $email, \Swift_Mailer $mailer ) { - $repository = $this->getDoctrine()->getRepository( Owner::class ); - $founduser = $repository->findOneBy( [ 'email' => $email ] ); - - if ( $founduser ) { - $polls = $founduser->getPolls(); - $templateVars = [ - 'owner' => $founduser, - 'polls' => $polls, - ]; - - $message = ( new Swift_Message( 'Framadate - mes sondages' ) ) - ->setFrom( 'ne-pas-repondre@framadate-api.cipherbliss.com' ) - ->setTo( $founduser->getEmail() ) - ->setBody( - $this->renderView( - 'emails/owner-list.html.twig', - $templateVars - ) - ); - $mailer->send( $message ); - - return $this->render( 'emails/owner-list.html.twig', $templateVars ); - - } else { - return $this->json( [ - 'message' => 'no user found for email ' . $email, - 'data' => '', - ], - 400 ); - } - // find user by email - // send email - // user not found case - - - } - - /** - * @Post( - * path = "/poll", - * name = "new_poll", - * requirements = {"creator"="\w+"} - * ) - * @param Request $request - * - * @return JsonResponse - */ - public function newPollAction( Request $request ) { - - $data = $request->getContent(); - - $serializer = SerializerBuilder::create()->build(); - $newpoll = $serializer->deserialize( $data, 'App\Entity\Poll', 'json' ); - $newpoll - ->setAdminKey( $newpoll->generateAdminKey() ) - ->setCreationDate( new DateTime() ) - ->setModificationPolicy( 'nobody' ); - $timeStamp = time() + ( 3600 * 24 * 90 ); // 90 days by default - $newpoll->setExpiracyDate( ( new DateTime() )->setTimestamp( $timeStamp ), - new DateTimeZone( 'Europe/Paris' ) ); - $data = json_decode( $data, true ); - $em = $this->getDoctrine()->getRepository( Owner::class ); - $foundOwner = $em->findOneBy( [ 'email' => $data[ 'owner' ][ 'email' ] ] ); - - - $userWasFound = false; - if ( ! $foundOwner ) { - //create a new owner - $owner = new Owner(); - - $owner->setPseudo( $data[ 'owner' ][ 'pseudo' ] ); - $owner->setEmail( $data[ 'owner' ][ 'email' ] ); - $foundOwner = $owner; - } else { - $userWasFound = true; - } - // link the owner and the poll - $newpoll->setOwner( $foundOwner ); - $foundOwner->addPoll( $newpoll ); - - - $em = $this->getDoctrine()->getManager(); - $em->persist( $newpoll ); - $em->persist( $foundOwner ); - - // emails - $newpoll->setMailOnComment( true ); - $newpoll->setMailOnVote( true ); - $newpoll->setHideResults( false ); - // possible answers - $newpoll->setAllowedAnswers( [ 'yes' ] ); - if ( $data[ 'voteChoices' ] ) { - switch ( $data[ 'voteChoices' ] ) { - case "only_yes": - default: - - break; - } - } - // setup the password, converting the raw with md5 hash - if ( $data[ 'password' ] ) { - $newpoll->setPassword( $data[ 'password' ] ); - } - // manage choices - // text kind of answers, dates are below - if ( $data[ 'pollType' ] == 'classic' ) { - $choices = $data[ 'dateList' ]; - foreach ( $choices as $c ) { - $newChoice = new Choice(); - $newChoice - ->setPoll( $newpoll ) -// ->setUrl( $c[ 'url' ] ) - ->setName( $c[ 'literal' ] ); - $em->persist( $newChoice ); - // TODO add also choices for each time range in a day - } - } elseif ( $data[ 'pollType' ] == 'dates' ) { - if ( $data[ 'allowSeveralHours' ] == true ) { - // different hours spans - $choices = $data[ 'dateList' ]; - } else { - -// all days have the same hour spans - } - - - } - - $em->persist( $newpoll ); - $em->flush(); - $precision = ''; - if ( $userWasFound ) { - $precision = 'from an existing user : ' . $foundOwner->getEmail(); - } - - return $this->json( [ - 'message' => 'you created a poll ' . $precision, - 'poll' => $newpoll, - 'password_protected' => is_string( $newpoll->getPassword() ), - 'admin_key' => $newpoll->getAdminKey(), - 'owner_modifier_token' => $foundOwner->getModifierToken(), - - ], - 201 ); - - } - - /** - * @Get( - * path = "/poll/{id}/comments", - * name = "get_poll_comment", - * requirements = {"poll_id"="\d+"} - * ) - */ - public - function getPollCommentsAction( - Poll $poll - ) { - return $this->json( [ - 'message' => 'here are your comments of the poll', - 'data' => $poll->getComments(), - ], - 200 ); - } - - /** - * @Get( - * path = "/poll/{id}", - * name = "get_poll", - * requirements = {"poll_id"="\d+"} - * ) - */ - public - function getPollConfig( - Poll $poll, - Request $request - ) { - $pass = $poll->getPassword(); - $data = $request->getContent(); - $data = json_decode( $data, true ); - - $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(); - } - $returnedPoll = [ - 'message' => 'your poll config', - 'poll' => $poll, - 'stacks_count' => count( $poll->getStacksOfVotes() ), - 'stacks' => $stacks, - 'choices_count' => $poll->computeAnswers(), - 'choices' => $choices, - 'comments' => $comments, - 'comments_count' => count( $comments ), + if ( $founduser ) { + $polls = $founduser->getPolls(); + $templateVars = [ + 'owner' => $founduser, + 'polls' => $polls, ]; - /** - * password protected content - */ - if ( $pass ) { - if ( $pass == md5( $data[ 'password_input' ] ) ) { - return $this->json( - $returnedPoll, - 200 ); - } else { - return $this->json( [ - 'message' => 'your password ' . $data[ 'password_input' ] . ' is wrong, and you should feel bad', - 'data' => null, - ], - 403 ); - } - } else { - return $this->json( - $returnedPoll - , - 200 ); - } + $message = ( new Swift_Message( 'Framadate - mes sondages' ) ) + ->setFrom( 'ne-pas-repondre@framadate-api.cipherbliss.com' ) + ->setTo( $founduser->getEmail() ) + ->setBody( + $this->renderView( + 'emails/owner-list.html.twig', + $templateVars + ) + ); + $mailer->send( $message ); - } - - /** - * Delete all expired polls and their children - * @Get( - * path = "/clean-polls", - * name = "clean_expired_polls", - * ) - */ - public - function cleanExpiredPolls() { - $em = $this->getDoctrine()->getManager(); - $emPoll = $this->getDoctrine()->getRepository( Poll::class ); - - $queryFind = $em->createQuery( - 'SELECT p - FROM App\Entity\Poll p - WHERE p.expiracyDate < CURRENT_DATE()' - ); - $queryDelete = $em->createQuery( - 'DELETE - FROM App\Entity\Poll p - WHERE p.expiracyDate < CURRENT_DATE()' - ); - - $foundPolls = $queryFind->getResult(); - - $em->flush(); + return $this->render( 'emails/owner-list.html.twig', $templateVars ); + } else { return $this->json( [ - 'message' => 'clean routine has been done, here are the numbers of polls deleted: ' . count( $foundPolls ), - 'data' => [ - 'count' => count( $foundPolls ), - ], + 'message' => 'no user found for email ' . $email, + 'data' => '', ], - 200 ); + 400 ); } + // find user by email + // send email + // user not found case - /** - * @Put( - * path = "/poll/{id}", - * name = "update_poll", - * requirements = {"content"="\w+", "poll_id"="\d+"} - * ) - */ - public - function updatePollConfig( - Poll $poll, - Request $request - ) { - - // TODO check validity of request - // update only if we have the admin key - $em = $this->getDoctrine()->getManager(); - $em->persist( $poll ); - $em->flush(); - - return $this->json( [ - 'message' => 'you updated the poll ' . $poll->getTitle(), - ] ); - } - - /** - * add a comment on a poll - * @Post( - * path = "/poll/{id}/comment", - * name = "new_comment", - * requirements = {"content"="\w+", "poll_id"="\d+"} - * ) - */ - public - function newCommentAction( - Poll $poll, - Request $request - ) { - if ( ! $poll ) { - return $this->json( [ 'message' => 'poll not found' ], 404 ); - } - $data = $request->getContent(); - - $serializer = SerializerBuilder::create()->build(); - $comment = $serializer->deserialize( $data, 'App\Entity\Comment', 'json' ); - - $em = $this->getDoctrine()->getRepository( Owner::class ); - - $data = json_decode( $data, true ); - - $foundOwner = $em->findOneByEmail( $data[ 'email' ] ); - // manage existing or new Owner - if ( ! $foundOwner ) { - $foundOwner = new Owner(); - $foundOwner->setPseudo( $data[ 'owner' ][ 'email' ] ) - ->setEmail( $data[ 'owner' ][ 'email' ] ) - ->setModifierToken( uniqid() ); - } - // anti flood - $seconds_limit_lastpost = 5; - $emComment = $this->getDoctrine()->getRepository( Comment::class ); - $lastCommentOfOwner = $emComment->findBy( [ 'owner' => $foundOwner ], [ 'id' => 'desc' ] ); - - // TODO anti flood by session / IP - - if ( $lastCommentOfOwner ) { - - - // check time of last comment - $now = new \DateTime(); - $now = $now->format( 'Y-m-d H:i:s' ); - $date_first = strtotime( $lastCommentOfOwner[ 0 ]->getCreatedAt()->format( 'Y-m-d H:i:s' ) ); - $date_second = strtotime( $now ); - - if ( ( $date_second - $date_first ) < $seconds_limit_lastpost ) { - return $this->json( [ - 'message' => 'anti flood déclenché', - 'details' => 'votre deriner commentaire a été envoyé il y a moins de ' . $seconds_limit_lastpost . ' secondes', - ], - 403 ); - } - - // check similar text content - if ( $lastCommentOfOwner[ 0 ]->getText() == $comment->getText() ) { - return $this->json( [ - 'message' => 'anti flood déclenché', - 'details' => 'votre deriner commentaire a exactement le même contenu que celui ci, il n\'a donc pas été créé', - ], - 403 ); - } - } - $comment->setOwner( $foundOwner ) - ->setCreatedAt( new \DateTime() ) - ->setPoll( $poll ); - $foundOwner->addComment( $comment ); - - $em = $this->getDoctrine()->getManager(); - $em->persist( $foundOwner ); - $em->persist( $comment ); - $em->flush(); - - return $this->json( [ - 'message' => 'you created a comment', - 'data' => [ - 'your_comment' => $comment->display(), - ], - ], - 201 ); - } - - /** - * add a vote on a poll - * @Post( - * path = "/poll/{id}/vote", - * name = "new_vote_stack", - * requirements = {"content"="\w+", "poll_id"="\d+"} - * ) - */ - public - function newVoteStackAction( - Poll $poll, - Request $request - ) { - if ( ! $poll ) { - return $this->json( [ 'message' => 'poll not found' ], 404 ); - } - - $em = $this->getDoctrine()->getManager(); - $data = $request->getContent(); - $data = json_decode( $data, true ); - - - $emOwner = $this->getDoctrine()->getRepository( Owner::class ); - $emChoice = $this->getDoctrine()->getRepository( Choice::class ); - $existingOwner = false; - $foundOwner = $emOwner->findOneByEmail( trim( $data[ 'email' ] ) ); - // manage existing or new Owner - if ( ! $foundOwner ) { - $foundOwner = new Owner(); - $foundOwner - ->setEmail( $data[ 'email' ] ) - ->setPseudo( $data[ 'pseudo' ] ); - } else { - $existingOwner = true; - } - // TODO anti flood - $foundOwner - ->setModifierToken( $poll->generateAdminKey() ); - $stack = new StackOfVotes(); - $stack - ->setOwner( $foundOwner ) - ->setPseudo( $data[ 'pseudo' ] ) - ->setPoll( $poll ); - foreach ( $data[ 'votes' ] as $voteInfo ) { - - if ( ! isset( $voteInfo[ 'value' ] ) ) { - continue; - } - $allowedValuesToAnswer = [ 'yes', 'maybe', 'no' ]; - - if ( ! in_array( $voteInfo[ 'value' ], $allowedValuesToAnswer ) ) { - return $this->json( [ - 'message' => 'answer ' . $voteInfo[ 'value' ] . ' is not allowed. should be yes, maybe, or no.', - 'vote_stack' => $stack, - ], - 404 ); - } - $vote = new Vote(); - $foundChoice = $emChoice->find( $voteInfo[ 'choice_id' ] ); - if ( ! $foundChoice ) { - return $this->json( [ - 'message' => 'choice ' . $voteInfo[ 'choice_id' ] . ' was not found', - 'vote_stack' => $stack, - ], - 404 ); - } - $vote->setPoll( $poll ) - ->setChoice( $foundChoice ) - ->setValue( $voteInfo[ 'value' ] ); - $vote->setPoll( $poll ); - $stack->addVote( $vote ); - $poll->addVote( $vote ); - $em->persist( $vote ); - $em->persist( $foundChoice ); - } - - // find poll from choices - $poll->addStackOfVote( $stack ); - $em->persist( $stack ); - $em->persist( $poll ); - $em->flush(); - $precision = ''; - 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(), - 'json_you_sent' => $data, - ], - 201 ); - } - - - /** - * @Delete( - * path = "/poll/{id}", - * name = "poll_delete", - * requirements = {"accessToken"="\w+", "poll_id"="\d+"} - * ) - * @param Poll $poll - * @param $accessToken - * - * @return JsonResponse - */ - public - function deletePollAction( - Poll $poll, - $accessToken - ) { - - if ( $accessToken == $poll->getAdminKey() ) { - $em = $this->getDoctrine()->getManager(); - $em->remove( $poll ); - $em->flush(); - - return $this->json( [ - 'message' => 'boom! le sondage et ses objets assocités a été supprimé', - ] ); - } else { - return $this->json( [ - 'message' => 'le token d\'autorisation est invalide, vous ne pouvez pas modifier ce sondage', - ] ); - } - - } - - /** - * Erase all comments of a poll - * @Delete( - * path = "/poll/{id}/comments", - * name = "poll_comments_delete", - * requirements = {"accessToken"="\w+", "poll_id"="\d+"} - * ) - * - * @param Poll $poll - * @param $accessToken - * - * @return JsonResponse - */ - public - function deletePollCommentsAction( - Poll $poll, - $accessToken - ) { - if ( $accessToken == $poll->getAdminKey() ) { - $em = $this->getDoctrine()->getManager(); - $length = count( $poll->getComments() ); - $em->remove( $poll->getComments() ); - $em->flush(); - - return $this->json( [ - 'message' => 'boom! les ' . $length . ' commentaires du sondage ont été supprimés', - ] ); - } else { - return $this->json( [ - 'message' => 'le token d\'autorisation est invalide, vous ne pouvez pas modifier ce sondage', - ] ); - } - } - - /** - * @Delete( - * path = "/poll/{id}/votes/{accessToken}", - * name = "poll_votes_delete", - * requirements = {"accessToken"="\w+", "poll_id"="\d+"} - * ) - * @return JsonResponse - */ - public - function deletePollVotesAction( - Poll $poll, - $accessToken - ) { - if ( $accessToken == $poll->getAdminKey() ) { - $em = $this->getDoctrine()->getManager(); - $length = count( $poll->getVotes() ); - $em->remove( $poll->getVotes() ); - $em->flush(); - - return $this->json( [ - 'message' => 'boom! les ' . $length . ' votes du sondage ont été supprimés', - ] ); - } else { - return $this->json( [ - 'message' => 'le token d\'autorisation est invalide, vous ne pouvez pas modifier ce sondage', - ] ); - } - - } } + + +} diff --git a/src/Controller/PollController.php b/src/Controller/PollController.php new file mode 100644 index 0000000..63895de --- /dev/null +++ b/src/Controller/PollController.php @@ -0,0 +1,307 @@ +getDoctrine()->getRepository( Poll::class ); + $data = $repository->findall(); + + + return $this->json( [ + 'message' => 'here are your polls', + 'poll' => $data, + ], + 200 ); + } + + /** + * @Get( + * path = "/{id}", + * name = "get_poll", + * requirements = {"poll_id"="\d+"} + * ) + */ + public + function getPollConfig( + Poll $poll, + Request $request + ) { + $pass = $poll->getPassword(); + $data = $request->getContent(); + $data = json_decode( $data, true ); + + $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(); + } + $returnedPoll = [ + 'message' => 'your poll config', + 'poll' => $poll, + 'stacks_count' => count( $poll->getStacksOfVotes() ), + 'stacks' => $stacks, + 'choices_count' => $poll->computeAnswers(), + 'choices' => $choices, + 'comments' => $comments, + 'comments_count' => count( $comments ), + ]; + /** + * password protected content + */ + if ( $pass ) { + + if ( $pass == md5( $data[ 'password_input' ] ) ) { + return $this->json( + $returnedPoll, + 200 ); + } else { + return $this->json( [ + 'message' => 'your password ' . $data[ 'password_input' ] . ' is wrong, and you should feel bad', + 'data' => null, + ], + 403 ); + } + } else { + return $this->json( + $returnedPoll + , + 200 ); + } + + } + + /** + * @Put( + * path = "/{id}", + * name = "update_poll", + * requirements = {"content"="\w+", "poll_id"="\d+"} + * ) + */ + public + function updatePollConfig( + Poll $poll, + Request $request + ) { + + // TODO check validity of request + // update only if we have the admin key + $em = $this->getDoctrine()->getManager(); + $em->persist( $poll ); + $em->flush(); + + return $this->json( [ + 'message' => 'you updated the poll ' . $poll->getTitle(), + ] ); + } + + /** + * @Post( + * path = "/", + * name = "new_poll", + * requirements = {"creator"="\w+"} + * ) + * @param Request $request + * + * @return JsonResponse + */ + public function newPollAction( Request $request ) { + + $data = $request->getContent(); + + $serializer = SerializerBuilder::create()->build(); + $newpoll = $serializer->deserialize( $data, 'App\Entity\Poll', 'json' ); + $newpoll + ->setAdminKey( $newpoll->generateAdminKey() ) + ->setCreationDate( new DateTime() ) + ->setModificationPolicy( 'nobody' ); + $timeStamp = time() + ( 3600 * 24 * 90 ); // 90 days by default + $newpoll->setExpiracyDate( ( new DateTime() )->setTimestamp( $timeStamp ), + new DateTimeZone( 'Europe/Paris' ) ); + $data = json_decode( $data, true ); + $em = $this->getDoctrine()->getRepository( Owner::class ); + $foundOwner = $em->findOneBy( [ 'email' => $data[ 'owner' ][ 'email' ] ] ); + + + $userWasFound = false; + if ( ! $foundOwner ) { + //create a new owner + $owner = new Owner(); + + $owner->setPseudo( $data[ 'owner' ][ 'pseudo' ] ); + $owner->setEmail( $data[ 'owner' ][ 'email' ] ); + $foundOwner = $owner; + } else { + $userWasFound = true; + } + // link the owner and the poll + $newpoll->setOwner( $foundOwner ); + $foundOwner->addPoll( $newpoll ); + + + $em = $this->getDoctrine()->getManager(); + $em->persist( $newpoll ); + $em->persist( $foundOwner ); + + // emails + $newpoll->setMailOnComment( true ); + $newpoll->setMailOnVote( true ); + $newpoll->setHideResults( false ); + // possible answers + $newpoll->setAllowedAnswers( [ 'yes' ] ); + if ( $data[ 'voteChoices' ] ) { + switch ( $data[ 'voteChoices' ] ) { + case "only_yes": + default: + + break; + } + } + // setup the password, converting the raw with md5 hash + if ( $data[ 'password' ] ) { + $newpoll->setPassword( $data[ 'password' ] ); + } + // manage choices + // text kind of answers, dates are below + if ( $data[ 'pollType' ] == 'classic' ) { + $choices = $data[ 'dateList' ]; + foreach ( $choices as $c ) { + $newChoice = new Choice(); + $newChoice + ->setPoll( $newpoll ) +// ->setUrl( $c[ 'url' ] ) + ->setName( $c[ 'literal' ] ); + $em->persist( $newChoice ); + // TODO add also choices for each time range in a day + } + } elseif ( $data[ 'pollType' ] == 'dates' ) { + if ( $data[ 'allowSeveralHours' ] == true ) { + // different hours spans + $choices = $data[ 'dateList' ]; + } else { + +// all days have the same hour spans + } + + + } + + $em->persist( $newpoll ); + $em->flush(); + $precision = ''; + if ( $userWasFound ) { + $precision = 'from an existing user : ' . $foundOwner->getEmail(); + } + + return $this->json( [ + 'message' => 'you created a poll ' . $precision, + 'poll' => $newpoll, + 'password_protected' => is_string( $newpoll->getPassword() ), + 'admin_key' => $newpoll->getAdminKey(), + 'owner_modifier_token' => $foundOwner->getModifierToken(), + + ], + 201 ); + + } + + + /** + * @Delete( + * path = "/{id}", + * name = "poll_delete", + * requirements = {"accessToken"="\w+", "poll_id"="\d+"} + * ) + * @param Poll $poll + * @param $accessToken + * + * @return JsonResponse + */ + public + function deletePollAction( + Poll $poll, + $accessToken + ) { + + if ( $accessToken == $poll->getAdminKey() ) { + $em = $this->getDoctrine()->getManager(); + $em->remove( $poll ); + $em->flush(); + + return $this->json( [ + 'message' => 'boom! le sondage et ses objets assocités a été supprimé', + ] ); + } else { + return $this->json( [ + 'message' => 'le token d\'autorisation est invalide, vous ne pouvez pas modifier ce sondage', + ] ); + } + + } + + /** + * Delete all expired polls and their children + * @Get( + * path = "/clean-polls", + * name = "clean_expired_polls", + * ) + */ + public + function cleanExpiredPolls() { + $em = $this->getDoctrine()->getManager(); + $emPoll = $this->getDoctrine()->getRepository( Poll::class ); + + $queryFind = $em->createQuery( + 'SELECT p + FROM App\Entity\Poll p + WHERE p.expiracyDate < CURRENT_DATE()' + ); + $queryDelete = $em->createQuery( + 'DELETE + FROM App\Entity\Poll p + WHERE p.expiracyDate < CURRENT_DATE()' + ); + + $foundPolls = $queryFind->getResult(); + + $em->flush(); + + return $this->json( [ + 'message' => 'clean routine has been done, here are the numbers of polls deleted: ' . count( $foundPolls ), + 'data' => [ + 'count' => count( $foundPolls ), + ], + ], + 200 ); + } + +} diff --git a/src/Controller/VoteController.php b/src/Controller/VoteController.php new file mode 100644 index 0000000..8f20658 --- /dev/null +++ b/src/Controller/VoteController.php @@ -0,0 +1,165 @@ +json( [ 'message' => 'poll not found' ], 404 ); + } + + $em = $this->getDoctrine()->getManager(); + $data = $request->getContent(); + $data = json_decode( $data, true ); + + + $emOwner = $this->getDoctrine()->getRepository( Owner::class ); + $emChoice = $this->getDoctrine()->getRepository( Choice::class ); + $existingOwner = false; + $foundOwner = $emOwner->findOneByEmail( trim( $data[ 'email' ] ) ); + // manage existing or new Owner + if ( ! $foundOwner ) { + $foundOwner = new Owner(); + $foundOwner + ->setEmail( $data[ 'email' ] ) + ->setPseudo( $data[ 'pseudo' ] ); + } else { + $existingOwner = true; + } + // TODO anti flood + $foundOwner + ->setModifierToken( $poll->generateAdminKey() ); + $stack = new StackOfVotes(); + $stack + ->setOwner( $foundOwner ) + ->setPseudo( $data[ 'pseudo' ] ) + ->setPoll( $poll ); + foreach ( $data[ 'votes' ] as $voteInfo ) { + + if ( ! isset( $voteInfo[ 'value' ] ) ) { + continue; + } + $allowedValuesToAnswer = [ 'yes', 'maybe', 'no' ]; + + if ( ! in_array( $voteInfo[ 'value' ], $allowedValuesToAnswer ) ) { + return $this->json( [ + 'message' => 'answer ' . $voteInfo[ 'value' ] . ' is not allowed. should be yes, maybe, or no.', + 'vote_stack' => $stack, + ], + 404 ); + } + $vote = new Vote(); + $foundChoice = $emChoice->find( $voteInfo[ 'choice_id' ] ); + if ( ! $foundChoice ) { + return $this->json( [ + 'message' => 'choice ' . $voteInfo[ 'choice_id' ] . ' was not found', + 'vote_stack' => $stack, + ], + 404 ); + } + $vote->setPoll( $poll ) + ->setChoice( $foundChoice ) + ->setValue( $voteInfo[ 'value' ] ); + $vote->setPoll( $poll ); + $stack->addVote( $vote ); + $poll->addVote( $vote ); + $em->persist( $vote ); + $em->persist( $foundChoice ); + } + + // find poll from choices + $poll->addStackOfVote( $stack ); + $em->persist( $stack ); + $em->persist( $poll ); + $em->flush(); + $precision = ''; + 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(), + 'json_you_sent' => $data, + ], + 201 ); + } + + /** + * @Delete( + * path = "/poll/{id}/votes/{accessToken}", + * name = "poll_votes_delete", + * requirements = {"accessToken"="\w+", "poll_id"="\d+"} + * ) + * @return JsonResponse + */ + public + function deletePollVotesAction( + Poll $poll, + $accessToken + ) { + if ( $accessToken == $poll->getAdminKey() ) { + $em = $this->getDoctrine()->getManager(); + $length = count( $poll->getVotes() ); + $em->remove( $poll->getVotes() ); + $em->flush(); + + return $this->json( [ + 'message' => 'boom! les ' . $length . ' votes du sondage ont été supprimés', + ] ); + } else { + return $this->json( [ + 'message' => 'le token d\'autorisation est invalide, vous ne pouvez pas modifier ce sondage', + ] ); + } + + } +} From 7566c75af850ce9889f24d36177ba2ab8454df28 Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Thu, 30 Jan 2020 11:31:23 +0100 Subject: [PATCH 3/5] :bug: fix imports --- src/Controller/CommentController.php | 2 ++ src/Controller/PollController.php | 3 +++ src/Controller/VoteController.php | 3 ++- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Controller/CommentController.php b/src/Controller/CommentController.php index 1ee0f17..9f0f68b 100644 --- a/src/Controller/CommentController.php +++ b/src/Controller/CommentController.php @@ -5,7 +5,9 @@ namespace App\Controller; use App\Entity\Comment; use App\Entity\Owner; use App\Entity\Poll; +use FOS\RestBundle\Controller\Annotations\Delete; use FOS\RestBundle\Controller\Annotations\Get; +use FOS\RestBundle\Controller\Annotations\Post; use FOS\RestBundle\Controller\Annotations\Route; use JMS\Serializer\SerializerBuilder; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; diff --git a/src/Controller/PollController.php b/src/Controller/PollController.php index 63895de..0180cbf 100644 --- a/src/Controller/PollController.php +++ b/src/Controller/PollController.php @@ -5,7 +5,10 @@ namespace App\Controller; use App\Entity\Choice; use App\Entity\Owner; use App\Entity\Poll; +use FOS\RestBundle\Controller\Annotations\Delete; use FOS\RestBundle\Controller\Annotations\Get; +use FOS\RestBundle\Controller\Annotations\Post; +use FOS\RestBundle\Controller\Annotations\Put; use FOS\RestBundle\Controller\Annotations\Route; use JMS\Serializer\SerializerBuilder; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; diff --git a/src/Controller/VoteController.php b/src/Controller/VoteController.php index 8f20658..0605c0e 100644 --- a/src/Controller/VoteController.php +++ b/src/Controller/VoteController.php @@ -7,6 +7,8 @@ use App\Entity\Owner; use App\Entity\Poll; use App\Entity\StackOfVotes; use App\Entity\Vote; +use FOS\RestBundle\Controller\Annotations\Delete; +use FOS\RestBundle\Controller\Annotations\Post; use FOS\RestBundle\Controller\Annotations\Route; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; @@ -160,6 +162,5 @@ class VoteController extends AbstractController { 'message' => 'le token d\'autorisation est invalide, vous ne pouvez pas modifier ce sondage', ] ); } - } } From b3bdeb0d4a841681b96805de2f1eeff5662e9925 Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Thu, 30 Jan 2020 11:49:36 +0100 Subject: [PATCH 4/5] better fixtures --- src/DataFixtures/AppPollFixtures.php | 56 +++++++--------------------- 1 file changed, 14 insertions(+), 42 deletions(-) diff --git a/src/DataFixtures/AppPollFixtures.php b/src/DataFixtures/AppPollFixtures.php index 09d859b..6fcfef6 100644 --- a/src/DataFixtures/AppPollFixtures.php +++ b/src/DataFixtures/AppPollFixtures.php @@ -47,7 +47,7 @@ class AppPollFixtures extends Fixture { $choiceA = new Choice(); $choiceA->setName( 'citron' ); $choiceB = new Choice(); - $choiceA->setName( 'orange' ); + $choiceB->setName( 'orange' ); $poll ->addChoice( $choiceA ) @@ -129,47 +129,6 @@ class AppPollFixtures extends Fixture { // voting test with 2 people -// $stack1 = new StackOfVotes(); -// $vote0 = new Vote(); -// $vote0->setChoice( $poll->getChoices()[ 0 ] ); -// $stack1->setOwner( $voter ) -// ->addVote( $vote0 ); -// -// $stack2 = new StackOfVotes(); -// $vote1 = new Vote(); -// $vote1 -// ->setChoice( $poll->getChoices()[ 1 ] ); -// $stack1 -// ->setOwner( $owner ) -// ->addVote( $vote1 ); -// -// -// $voter->addStackOfVote( $stack1 ); -// $owner->addStackOfVote( $stack2 ); -// -// $poll = new Poll(); -// $poll->setTitle( 'accès restreint sondage de texte' ); -// $poll ; -// $poll->setPassword( md5( 'mot_de_passe_super' ) ); -// $poll->setModificationPolicy( 'everybody' ); -// $poll->setDescription( 'description du sondage a accès restreint. le mot de passe est mot_de_passe_super' ); -// $poll->setAdminKey( uniqid() ); -// $poll->setCustomUrl( 'boudoum_podom_podom' ); -// -// $poll ; -// $poll->setMailOnComment( true ); -// $poll->setMailOnVote( true ); -// $poll->setOwner( $owner ); -// $owner->addPoll( $poll ); -// -// $manager->persist( $vote1 ); -// $manager->persist( $vote0 ); -// $manager->persist( $stack1 ); -// $manager->persist( $stack2 ); -// $manager->persist( $poll ); -// $manager->persist( $owner ); -// $manager->persist( $voter ); - // poll with date type $poll = new Poll(); @@ -187,7 +146,9 @@ class AppPollFixtures extends Fixture { ->setOwner( $owner ) ->addChoice( $choice ) ->addChoice( $choice2 ) + ->addChoice( $choice3 ) ->setModificationPolicy( 'self' ); + $manager->persist( $poll ); // poll with cartoon choices $poll = new Poll(); @@ -205,6 +166,17 @@ class AppPollFixtures extends Fixture { "Digimon", ] ); + $someoneComment = new Comment(); + $someoneComment + ->setText( "allez boumbo!" ) + ->setOwner( $commenterMan ); + $poll->addComment( $someoneComment ); + $someoneComment2 = new Comment(); + $someoneComment2 + ->setText( "je suis pour la team rocket de digimon" ) + ->setOwner( $owner ); + $poll->addComment( $someoneComment2 ); + $manager->persist( $poll ); $stack = new StackOfVotes(); From 158a7954464a6d141d983fee8e7d81ac71e7cee0 Mon Sep 17 00:00:00 2001 From: Baptiste Lemoine Date: Thu, 30 Jan 2020 12:25:58 +0100 Subject: [PATCH 5/5] :zap: more stack fixtures --- src/DataFixtures/AppPollFixtures.php | 4 - src/DataFixtures/CommentFixtures.php | 76 +++++++ src/DataFixtures/VotesStacksFixtures.php | 148 +++++++++++++ src/Entity/StackOfVotes.php | 262 ++++++++++++----------- 4 files changed, 359 insertions(+), 131 deletions(-) create mode 100644 src/DataFixtures/CommentFixtures.php create mode 100644 src/DataFixtures/VotesStacksFixtures.php diff --git a/src/DataFixtures/AppPollFixtures.php b/src/DataFixtures/AppPollFixtures.php index 6fcfef6..73ffeeb 100644 --- a/src/DataFixtures/AppPollFixtures.php +++ b/src/DataFixtures/AppPollFixtures.php @@ -54,7 +54,6 @@ class AppPollFixtures extends Fixture { ->addChoice( $choiceB ); $manager->persist( $poll ); - $stack1 = new StackOfVotes(); $stack1 ->setPoll( $poll ) @@ -72,10 +71,7 @@ class AppPollFixtures extends Fixture { ->setStacksOfVotes( $stack1 ) ->setValue( "yes" ) ->setChoice( $choiceB ); - $stack1->setPseudo( 'chuck norris' ); - - $manager->persist( $stack1 ); // voter guy votes again with an other pseudo diff --git a/src/DataFixtures/CommentFixtures.php b/src/DataFixtures/CommentFixtures.php new file mode 100644 index 0000000..64d0055 --- /dev/null +++ b/src/DataFixtures/CommentFixtures.php @@ -0,0 +1,76 @@ +getRepository( Poll::class ); + + $commenterMan = new Owner(); + $commenterMan->setEmail( 'tktest_commentateur_other@tktest.com' ) + ->setPseudo( 'Bill Murray' ); + $commenterMan2 = new Owner(); + $commenterMan2->setEmail( 'wulfila@tktest.com' ) + ->setPseudo( 'Wulfila' ); + + // comment on "citron ou orange" + $pollCitronOrange = $emPoll->find( 1 ); + $comment = new Comment(); + $comment->setOwner( $commenterMan ) + ->setText( 'quelle indécision wololo! finalement citron. heu non orange. AAAAh!' ); + $pollCitronOrange->addComment( $comment ); + $manager->persist( $comment ); + $manager->persist( $pollCitronOrange ); + $manager->persist( $commenterMan ); + $manager->flush(); + + // comment on "démo sondage de texte avec deux commentaires" + $poll = $emPoll->find( 2 ); + $comment = new Comment(); + $comment->setOwner( $commenterMan2 ) + ->setText( 'il est écrit Squalala.' ); + $pollCitronOrange->addComment( $comment ); + $manager->persist( $comment ); + $manager->persist( $poll ); + $manager->persist( $commenterMan2 ); + $manager->flush(); + + $comment = new Comment(); + $comment->setOwner( $commenterMan ) + ->setText( "Zelda. Orange." ); + $pollCitronOrange->addComment( $comment ); + $manager->persist( $comment ); + $manager->persist( $poll ); + $manager->persist( $commenterMan ); + $manager->flush(); + + + // comment on "c'est pour aujourdhui ou pour demain" + $poll = $emPoll->find( 3 ); + $comment = new Comment(); + $comment->setOwner( $commenterMan ) + ->setText( "va pour demain" ); + $pollCitronOrange->addComment( $comment ); + $manager->persist( $comment ); + $manager->persist( $poll ); + $manager->persist( $commenterMan ); + $manager->flush(); + + // comment on "dessin animé préféré" + $poll = $emPoll->find( 4 ); + $comment = new Comment(); + $comment->setOwner( $commenterMan2 ) + ->setText( "Ceci est un commentaire de fixture créé avec le CipherBliss poweur." ); + $pollCitronOrange->addComment( $comment ); + $manager->persist( $comment ); + $manager->persist( $poll ); + $manager->persist( $commenterMan2 ); + $manager->flush(); + } +} diff --git a/src/DataFixtures/VotesStacksFixtures.php b/src/DataFixtures/VotesStacksFixtures.php new file mode 100644 index 0000000..2fb35d5 --- /dev/null +++ b/src/DataFixtures/VotesStacksFixtures.php @@ -0,0 +1,148 @@ +getRepository( Poll::class ); + + $people1 = new Owner(); + $people1->setEmail( 'tktest_nikolas_edison@tktest.com' ) + ->setPseudo( 'Nikolas Edison' ); + $people2 = new Owner(); + $people2->setEmail( 'wulfila@tktest.com' ) + ->setPseudo( 'Wulfila' ); + $people3 = new Owner(); + $people3->setEmail( 'billie_jean@tktest.com' ) + ->setPseudo( 'Billie Jean' ); + + // "citron ou orange" + $poll = $emPoll->find( 1 ); + $stack1 = new StackOfVotes(); + $stack1 + ->setPoll( $poll ) + ->setOwner( $people1 ); + $voteA = new Vote(); + $voteA + ->setPoll( $poll ) + ->setStacksOfVotes( $stack1 ) + ->setValue( "yes" ) + ->setChoice( $poll->getChoices()[ 0 ] ); + $voteB = new Vote(); + $voteB + ->setPoll( $poll ) + ->setStacksOfVotes( $stack1 ) + ->setValue( "maybe" ) + ->setChoice( $poll->getChoices()[ 1 ] ); + $poll->addStackOfVote( $stack1 ); + $manager->persist( $poll ); + $manager->persist( $people1 ); + $manager->persist( $stack1 ); + + $stack2 = new StackOfVotes(); + $stack2 + ->setPoll( $poll ) + ->setOwner( $people2 ); + $voteA = new Vote(); + $voteA + ->setPoll( $poll ) + ->setStacksOfVotes( $stack2 ) + ->setValue( "no" ) + ->setChoice( $poll->getChoices()[ 0 ] ); + $voteB = new Vote(); + $voteB + ->setPoll( $poll ) + ->setStacksOfVotes( $stack2 ) + ->setValue( "yes" ) + ->setChoice( $poll->getChoices()[ 1 ] ); + $poll->addStackOfVote( $stack2 ); + $manager->persist( $poll ); + $manager->persist( $stack2 ); + $manager->persist( $people2 ); + + // comment on "démo sondage de texte avec deux commentaires" + $poll = $emPoll->find( 2 ); + + + // comment on "c'est pour aujourdhui ou pour demain" + $poll = $emPoll->find( 3 ); + + + // comment on "dessin animé préféré" + $poll = $emPoll->find( 4 ); + + $stack1 = new StackOfVotes(); + $stack1 + ->setPoll( $poll ) + ->setOwner( $people1 ); + $voteA = new Vote(); + $voteA + ->setPoll( $poll ) + ->setStacksOfVotes( $stack1 ) + ->setValue( "maybe" ) + ->setChoice( $poll->getChoices()[ 2 ] ); + $voteB = new Vote(); + $voteB + ->setPoll( $poll ) + ->setStacksOfVotes( $stack1 ) + ->setValue( "maybe" ) + ->setChoice( $poll->getChoices()[ 4 ] ); + $poll->addStackOfVote( $stack1 ); + $manager->persist( $poll ); + $manager->persist( $people1 ); + $manager->persist( $stack1 ); + + + $stack2 = new StackOfVotes(); + $stack2 + ->setPoll( $poll ) + ->setOwner( $people2 ); + $voteA = new Vote(); + $voteA + ->setPoll( $poll ) + ->setStacksOfVotes( $stack2 ) + ->setValue( "maybe" ) + ->setChoice( $poll->getChoices()[ 3 ] ); + $voteB = new Vote(); + $voteB + ->setPoll( $poll ) + ->setStacksOfVotes( $stack2 ) + ->setValue( "yes" ) + ->setChoice( $poll->getChoices()[ 5 ] ); + $poll->addStackOfVote( $stack2 ); + $manager->persist( $poll ); + $manager->persist( $people2 ); + $manager->persist( $stack2 ); + + + $stack3 = new StackOfVotes(); + $stack3 + ->setPoll( $poll ) + ->setOwner( $people3 ); + $voteA = new Vote(); + $voteA + ->setPoll( $poll ) + ->setStacksOfVotes( $stack3 ) + ->setValue( "yes" ) + ->setChoice( $poll->getChoices()[ 1 ] ); + $voteB = new Vote(); + $voteB + ->setPoll( $poll ) + ->setStacksOfVotes( $stack3 ) + ->setValue( "yes" ) + ->setChoice( $poll->getChoices()[ 3 ] ); + $poll->addStackOfVote( $stack3 ); + $manager->persist( $poll ); + $manager->persist( $people3 ); + $manager->persist( $stack3 ); + + $manager->flush(); + } +} diff --git a/src/Entity/StackOfVotes.php b/src/Entity/StackOfVotes.php index 9bb40d6..56fe536 100644 --- a/src/Entity/StackOfVotes.php +++ b/src/Entity/StackOfVotes.php @@ -1,141 +1,149 @@ $this->getId(), - 'pseudo' => '', - 'creation_date' => '', - 'votes' => [], + public function display() { + $tab = [ + 'id' => $this->getId(), + 'pseudo' => '', + 'creation_date' => '', + 'votes' => [], + ]; + // prefill votes with all choices ids + foreach ( $this->getPoll()->getChoices() as $choice ) { + $tab[ 'votes' ][ $choice->getId() ] = [ + 'choice_id' => $choice->getId(), ]; - // prefill votes with all choices ids - foreach ( $this->getPoll()->getChoices() as $choice ) { - $tab[ 'votes' ][ $choice->getId() ] = [ - 'choice_id' => $choice->getId(), - ]; - } - - foreach ( $this->getVotes() as $vote ) { - $tab[ 'votes' ][ $vote->getChoice()->getId() ] = [ - 'id' => $this->getId(), - 'vote_id' => $vote->getId(), - 'value' => $vote->getValue(), - 'choice_id' => $vote->getChoice()->getId(), - 'text' => $vote->getChoice()->getName(), - ]; - $tab[ 'pseudo' ] = $this->getOwner()->getPseudo(); - $tab[ 'creation_date' ] = $vote->getCreationDate(); - } - - return $tab; } - public function __construct() { - $this->votes = new ArrayCollection(); + foreach ( $this->getVotes() as $vote ) { + $tab[ 'votes' ][ $vote->getChoice()->getId() ] = [ + 'id' => $this->getId(), + 'vote_id' => $vote->getId(), + 'value' => $vote->getValue(), + 'choice_id' => $vote->getChoice()->getId(), + 'text' => $vote->getChoice()->getName(), + ]; + $tab[ 'pseudo' ] = $this->getOwner()->getPseudo(); + $tab[ 'creation_date' ] = $vote->getCreationDate(); } - public function getId(): ?int { - return $this->id; - } - - /** - * @return Collection|poll[] - */ - public function getVotes(): Collection { - return $this->votes; - } - - public function addVote( Vote $vote ): self { - if ( ! $this->votes->contains( $vote ) ) { - $vote->setPoll( $this->getPoll() ); - - $this->votes[] = $vote; - $vote->setStacksOfVotes( $this ); - } - - return $this; - } - - public function removeVote( Vote $vote ): self { - if ( $this->votes->contains( $vote ) ) { - $this->votes->removeElement( $vote ); - // set the owning side to null (unless already changed) - if ( $vote->getStacksOfVotes() === $this ) { - $vote->setStacksOfVotes( null ); - } - } - - return $this; - } - - public function getPseudo(): ?string { - return $this->pseudo; - } - - public function setPseudo( ?string $pseudo ): self { - $this->pseudo = $pseudo; - - return $this; - } - - public function getOwner(): ?Owner { - return $this->owner; - } - - public function setOwner( ?Owner $owner ): self { - $this->owner = $owner; - - return $this; - } - - public function getPoll(): ?Poll { - return $this->poll; - } - - public function setPoll( ?Poll $poll ): self { - $this->poll = $poll; - - return $this; - } + return $tab; } + + /** + * @ORM\PrePersist + */ + public function prePersist() { + $this->setPseudo( $this->getOwner()->getPseudo() ); + } + + public function __construct() { + $this->votes = new ArrayCollection(); + } + + public function getId(): ?int { + return $this->id; + } + + /** + * @return Collection|poll[] + */ + public function getVotes(): Collection { + return $this->votes; + } + + public function addVote( Vote $vote ): self { + if ( ! $this->votes->contains( $vote ) ) { + $vote->setPoll( $this->getPoll() ); + + $this->votes[] = $vote; + $vote->setStacksOfVotes( $this ); + } + + return $this; + } + + public function removeVote( Vote $vote ): self { + if ( $this->votes->contains( $vote ) ) { + $this->votes->removeElement( $vote ); + // set the owning side to null (unless already changed) + if ( $vote->getStacksOfVotes() === $this ) { + $vote->setStacksOfVotes( null ); + } + } + + return $this; + } + + public function getPseudo(): ?string { + return $this->pseudo; + } + + public function setPseudo( ?string $pseudo ): self { + $this->pseudo = $pseudo; + + return $this; + } + + public function getOwner(): ?Owner { + return $this->owner; + } + + public function setOwner( ?Owner $owner ): self { + $this->owner = $owner; + + return $this; + } + + public function getPoll(): ?Poll { + return $this->poll; + } + + public function setPoll( ?Poll $poll ): self { + $this->poll = $poll; + + return $this; + } +}