AnswerPoll.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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\Core\Form;
  21. use App\Util\Common;
  22. use App\Util\Exception\InvalidFormException;
  23. use App\Util\Exception\NotFoundException;
  24. use App\Util\Exception\RedirectException;
  25. use App\Util\Exception\ServerException;
  26. use Plugin\Poll\Entity\Poll;
  27. use Plugin\Poll\Entity\PollResponse;
  28. use Plugin\Poll\Forms\PollResponseForm;
  29. use Symfony\Component\HttpFoundation\Request;
  30. /**
  31. * Respond to a Poll
  32. *
  33. * @package GNUsocial
  34. * @category PollPlugin
  35. *
  36. * @author Daniel Brandao <up201705812@fe.up.pt>
  37. * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
  38. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  39. */
  40. class AnswerPoll
  41. {
  42. /**
  43. * Handle poll response
  44. *
  45. * @param Request $request
  46. * @param string $id poll id
  47. *
  48. * @throws InvalidFormException invalid form
  49. * @throws NotFoundException poll does not exist
  50. * @throws RedirectException
  51. * @throws ServerException User already responded to poll
  52. * @throws \App\Util\Exception\NoLoggedInUser User is not logged in
  53. *
  54. * @return array template
  55. */
  56. public function answerPoll(Request $request, string $id)
  57. {
  58. $user = Common::ensureLoggedIn();
  59. $poll = Poll::getWithPk((int) $id);
  60. if ($poll == null) {
  61. throw new NotFoundException('Poll does not exist');
  62. }
  63. $question = $poll->getQuestion();
  64. $opts = $poll->getOptionsArr();
  65. $form = Form::create([]); //PollResponseForm::make($poll, $poll->getNoteId());
  66. $form->handleRequest($request);
  67. if ($form->isSubmitted()) {
  68. if ($form->isValid()) {
  69. $data = $form->getData();
  70. $selection = array_values($data)[1];
  71. if (!$poll->isValidSelection($selection)) {
  72. throw new InvalidFormException();
  73. }
  74. if (PollResponse::exits($poll->getId(), $user->getId())) {
  75. throw new ServerException('User already responded to poll');
  76. }
  77. $pollResponse = PollResponse::create(['poll_id' => $poll->getId(), 'gsactor_id' => $user->getId(), 'selection' => $selection]);
  78. DB::persist($pollResponse);
  79. DB::flush();
  80. throw new RedirectException('showpoll', ['id' => $poll->getId()]);
  81. } else {
  82. throw new InvalidFormException();
  83. }
  84. }
  85. return ['_template' => 'Poll/respondpoll.html.twig', 'question' => $question, 'form' => $form->createView()];
  86. }
  87. }