Poll.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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;
  19. use App\Core\DB\DB;
  20. use App\Core\Event;
  21. use App\Core\Form;
  22. use function App\Core\I18n\_m;
  23. use App\Core\Modules\NoteHandlerPlugin;
  24. use App\Core\Router\RouteLoader;
  25. use App\Entity\Note;
  26. use App\Entity\PollResponse;
  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. * @param RouteLoader $r
  53. *
  54. * @return bool hook value; true means continue processing, false means stop.
  55. */
  56. public function onAddRoute(RouteLoader $r): bool
  57. {
  58. $r->connect('newpoll', 'main/poll/new/{num<\\d+>?3}', [Controller\NewPoll::class, 'newpoll']);
  59. return Event::next;
  60. }
  61. /**
  62. * Populate twig vars
  63. *
  64. * @param array $vars
  65. *
  66. * @return bool hook value; true means continue processing, false means stop.
  67. */
  68. public function onStartTwigPopulateVars(array &$vars): bool
  69. {
  70. $vars['tabs'][] = ['title' => 'Poll',
  71. 'href' => 'newpoll',
  72. ];
  73. return Event::next;
  74. }
  75. /**
  76. * Output our dedicated stylesheet
  77. *
  78. * @param array $styles stylesheets path
  79. *
  80. * @return bool hook value; true means continue processing, false means stop.
  81. */
  82. public function onStartShowStyles(array &$styles): bool
  83. {
  84. $styles[] = 'poll/poll.css';
  85. return Event::next;
  86. }
  87. /**
  88. * Output our note content to the timeline
  89. *
  90. * @param Request $request
  91. * @param Note $note
  92. * @param array $otherContent content
  93. *
  94. * @throws InvalidFormException invalid forms
  95. * @throws RedirectException
  96. * @throws ServerException User already responded to poll
  97. * @throws \App\Util\Exception\NoLoggedInUser user not logged in
  98. *
  99. * @return bool hook value; true means continue processing, false means stop.
  100. */
  101. public function onShowNoteContent(Request $request, Note $note, array &$otherContent)
  102. {
  103. $responses = null;
  104. $formView = null;
  105. try {
  106. $poll = DB::findOneBy('poll', ['note_id' => $note->getId()]);
  107. } catch (NotFoundException $e) {
  108. return Event::next;
  109. }
  110. if (Common::isLoggedIn() && !PollResponse::exits($poll->getId(), Common::ensureLoggedIn()->getId())) {
  111. $opts = $poll->getOptionsArr();
  112. $options = [];
  113. for ($i = 1; $i <= count($opts); ++$i) {
  114. $options[$opts[$i - 1]] = $i;
  115. }
  116. $formOptions = [
  117. ['Options' . $poll->getId(), ChoiceType::class, [
  118. 'choices' => $options,
  119. 'expanded' => true,
  120. ]],
  121. ['note_id', HiddenType::class, ['data' => $note->getId()]],
  122. ['pollresponse', SubmitType::class, ['label' => _m('Vote')]],
  123. ];
  124. $form = Form::create($formOptions);
  125. $formView = $form->createView();
  126. $ret = self::noteActionHandle($request, $form, $note, 'pollresponse', /** TODO needs documentation */ function ($note, $data) {
  127. $user = Common::ensureLoggedIn();
  128. try {
  129. $poll = DB::findOneBy('poll', ['note_id' => $note->getId()]);
  130. } catch (NotFoundException $e) {
  131. return Event::next;
  132. }
  133. if (PollResponse::exits($poll->getId(), $user->getId())) {
  134. return Event::next;
  135. }
  136. $selection = array_values($data)[1];
  137. if (!$poll->isValidSelection($selection)) {
  138. throw new InvalidFormException();
  139. }
  140. if (PollResponse::exits($poll->getId(), $user->getId())) {
  141. throw new ServerException('User already responded to poll');
  142. }
  143. $pollResponse = PollResponse::create(['poll_id' => $poll->getId(), 'gsactor_id' => $user->getId(), 'selection' => $selection]);
  144. DB::persist($pollResponse);
  145. DB::flush();
  146. throw new RedirectException();
  147. });
  148. if ($ret != null) {
  149. return $ret;
  150. }
  151. } else {
  152. $responses = $poll->countResponses();
  153. }
  154. $otherContent[] = ['name' => 'Poll', 'vars' => ['question' => $poll->getQuestion(), 'responses' => $responses, 'form' => $formView]];
  155. return Event::next;
  156. }
  157. }