Poll.php 5.8 KB

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