AnswerPoll.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. // {{{ License
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. // }}}
  18. namespace Plugin\Poll\Controller;
  19. use App\Core\DB\DB;
  20. use App\Entity\Poll;
  21. use App\Entity\PollResponse;
  22. use App\Util\Common;
  23. use App\Util\Exception\InvalidFormException;
  24. use App\Util\Exception\NotFoundException;
  25. use App\Util\Exception\RedirectException;
  26. use App\Util\Exception\ServerException;
  27. use Plugin\Poll\Forms\PollResponseForm;
  28. use Symfony\Component\HttpFoundation\Request;
  29. /**
  30. * Respond to a Poll
  31. *
  32. * @package GNUsocial
  33. * @category PollPlugin
  34. *
  35. * @author Daniel Brandao <up201705812@fe.up.pt>
  36. * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
  37. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  38. */
  39. class AnswerPoll
  40. {
  41. /**
  42. * Handle poll response
  43. *
  44. * @param Request $request
  45. * @param string $id poll id
  46. *
  47. * @throws InvalidFormException invalid form
  48. * @throws NotFoundException poll does not exist
  49. * @throws RedirectException
  50. * @throws ServerException User already responded to poll
  51. * @throws \App\Util\Exception\NoLoggedInUser User is not logged in
  52. *
  53. * @return array template
  54. */
  55. public function answerPoll(Request $request, string $id)
  56. {
  57. $user = Common::ensureLoggedIn();
  58. $poll = Poll::getFromId((int) $id);
  59. if ($poll == null) {
  60. throw new NotFoundException('Poll does not exist');
  61. }
  62. $question = $poll->getQuestion();
  63. $opts = $poll->getOptionsArr();
  64. $form = PollResponseForm::make($poll, $poll->getNoteId());
  65. $form->handleRequest($request);
  66. if ($form->isSubmitted()) {
  67. if ($form->isValid()) {
  68. $data = $form->getData();
  69. $selection = array_values($data)[1];
  70. if (!$poll->isValidSelection($selection)) {
  71. throw new InvalidFormException();
  72. }
  73. if (PollResponse::exits($poll->getId(), $user->getId())) {
  74. throw new ServerException('User already responded to poll');
  75. }
  76. $pollResponse = PollResponse::create(['poll_id' => $poll->getId(), 'gsactor_id' => $user->getId(), 'selection' => $selection]);
  77. DB::persist($pollResponse);
  78. DB::flush();
  79. throw new RedirectException('showpoll', ['id' => $poll->getId()]);
  80. } else {
  81. throw new InvalidFormException();
  82. }
  83. }
  84. return ['_template' => 'Poll/respondpoll.html.twig', 'question' => $question, 'form' => $form->createView()];
  85. }
  86. }