AnswerPoll.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace Plugin\Poll\Controller;
  20. use App\Core\DB\DB;
  21. use App\Core\Form;
  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\Entity\Poll;
  28. use Plugin\Poll\Entity\PollResponse;
  29. use Plugin\Poll\Forms\PollResponseForm;
  30. use Symfony\Component\HttpFoundation\Request;
  31. /**
  32. * Respond to a Poll
  33. *
  34. * @package GNUsocial
  35. * @category PollPlugin
  36. *
  37. * @author Daniel Brandao <up201705812@fe.up.pt>
  38. * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
  39. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  40. */
  41. class AnswerPoll
  42. {
  43. /**
  44. * Handle poll response
  45. *
  46. * @param string $id poll id
  47. *
  48. * @throws \App\Util\Exception\NoLoggedInUser User is not logged in
  49. * @throws InvalidFormException invalid form
  50. * @throws NotFoundException poll does not exist
  51. * @throws RedirectException
  52. * @throws ServerException User already responded to poll
  53. *
  54. * @return array template
  55. */
  56. public function answerPoll(Request $request, string $id): array
  57. {
  58. $user = Common::ensureLoggedIn();
  59. $poll = Poll::getByPk((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(), 'actor_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. }