Poll.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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;
  20. use App\Core\DB;
  21. use App\Core\Event;
  22. use App\Core\Form;
  23. use function App\Core\I18n\_m;
  24. use App\Core\Modules\NoteHandlerPlugin;
  25. use App\Core\Router;
  26. use App\Entity\Note;
  27. use App\Util\Common;
  28. use App\Util\Exception\InvalidFormException;
  29. use App\Util\Exception\NotFoundException;
  30. use App\Util\Exception\RedirectException;
  31. use App\Util\Exception\ServerException;
  32. use EventResult;
  33. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  34. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  35. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  36. use Symfony\Component\HttpFoundation\Request;
  37. /**
  38. * Poll plugin main class
  39. *
  40. * @package GNUsocial
  41. * @category Poll
  42. *
  43. * @author Daniel Brandao <up201705812@fe.up.pt>
  44. * @author Hugo Sales <hugo@hsal.es>
  45. * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
  46. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  47. */
  48. class Poll extends NoteHandlerPlugin
  49. {
  50. /**
  51. * Map URLs to actions
  52. *
  53. * @return bool hook value; true means continue processing, false means stop
  54. */
  55. public function onAddRoute(Router $r): EventResult
  56. {
  57. $r->connect('newpoll', 'main/poll/new/{num<\\d+>?3}', [Controller\NewPoll::class, 'newpoll']);
  58. return Event::next;
  59. }
  60. /**
  61. * Populate twig vars
  62. *
  63. * @return bool hook value; true means continue processing, false means stop.
  64. *
  65. * public function onStartTwigPopulateVars(array &$vars): \EventResult
  66. * {
  67. * $vars['tabs'][] = ['title' => 'Poll',
  68. * 'href' => 'newpoll',
  69. * ];
  70. * return Event::next;
  71. * }*/
  72. /**
  73. * Output our dedicated stylesheet
  74. *
  75. * @param array $styles stylesheets path
  76. *
  77. * @return bool hook value; true means continue processing, false means stop
  78. */
  79. public function onStartShowStyles(array &$styles): EventResult
  80. {
  81. $styles[] = 'poll/poll.css';
  82. return Event::next;
  83. }
  84. /**
  85. * Output our note content to the feed
  86. *
  87. * @param array $otherContent content
  88. *
  89. * @throws \App\Util\Exception\NoLoggedInUser user not logged in
  90. * @throws InvalidFormException invalid forms
  91. * @throws RedirectException
  92. * @throws ServerException User already responded to poll
  93. *
  94. * @return bool hook value; true means continue processing, false means stop
  95. */
  96. public function onShowNoteContent(Request $request, Note $note, array &$otherContent): EventResult
  97. {
  98. $responses = null;
  99. $formView = null;
  100. try {
  101. $poll = DB::findOneBy('poll', ['note_id' => $note->getId()]);
  102. } catch (NotFoundException $e) {
  103. return Event::next;
  104. }
  105. if (Common::isLoggedIn() && !Entity\PollResponse::exits($poll->getId(), Common::ensureLoggedIn()->getId())) {
  106. $opts = $poll->getOptionsArr();
  107. $options = [];
  108. for ($i = 1; $i <= \count($opts); ++$i) {
  109. $options[$opts[$i - 1]] = $i;
  110. }
  111. $formOptions = [
  112. ['Options' . $poll->getId(), ChoiceType::class, [
  113. 'choices' => $options,
  114. 'expanded' => true,
  115. ]],
  116. ['note_id', HiddenType::class, ['data' => $note->getId()]],
  117. ['pollresponse', SubmitType::class, ['label' => _m('Vote')]],
  118. ];
  119. $form = Form::create($formOptions);
  120. $formView = $form->createView();
  121. $ret = self::noteActionHandle($request, $form, $note, 'pollresponse', /** TODO needs documentation */ function ($note, $data) {
  122. $user = Common::ensureLoggedIn();
  123. try {
  124. $poll = DB::findOneBy('poll', ['note_id' => $note->getId()]);
  125. } catch (NotFoundException $e) {
  126. return Event::next;
  127. }
  128. if (Entity\PollResponse::exits($poll->getId(), $user->getId())) {
  129. return Event::next;
  130. }
  131. $selection = array_values($data)[1];
  132. if (!$poll->isValidSelection($selection)) {
  133. throw new InvalidFormException();
  134. }
  135. if (Entity\PollResponse::exits($poll->getId(), $user->getId())) {
  136. throw new ServerException('User already responded to poll');
  137. }
  138. $pollResponse = Entity\PollResponse::create(['poll_id' => $poll->getId(), 'actor_id' => $user->getId(), 'selection' => $selection]);
  139. DB::persist($pollResponse);
  140. DB::flush();
  141. throw new RedirectException();
  142. });
  143. if ($ret != null) {
  144. return $ret;
  145. }
  146. } else {
  147. $responses = $poll->countResponses();
  148. }
  149. $otherContent[] = ['name' => 'Poll', 'vars' => ['question' => $poll->getQuestion(), 'responses' => $responses, 'form' => $formView]];
  150. return Event::next;
  151. }
  152. }