Poll.php 5.7 KB

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