respondpoll.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Respond to a Poll
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category Poll
  24. * @package StatusNet
  25. * @author Brion Vibber <brion@status.net>
  26. * @copyright 2011 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Respond to a Poll
  37. *
  38. * @category Poll
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @copyright 2010 StatusNet, Inc.
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  43. * @link http://status.net/
  44. */
  45. class RespondPollAction extends Action
  46. {
  47. protected $user = null;
  48. protected $error = null;
  49. protected $complete = null;
  50. protected $poll = null;
  51. protected $selection = null;
  52. /**
  53. * Returns the title of the action
  54. *
  55. * @return string Action title
  56. * @throws Exception
  57. */
  58. public function title()
  59. {
  60. // TRANS: Page title for poll response.
  61. return _m('Poll response');
  62. }
  63. /**
  64. * For initializing members of the class.
  65. *
  66. * @param array $args misc. arguments
  67. *
  68. * @return boolean true
  69. * @throws ClientException
  70. * @throws Exception
  71. * @throws Exception
  72. */
  73. public function prepare(array $args = [])
  74. {
  75. parent::prepare($args);
  76. if ($this->boolean('ajax')) {
  77. GNUsocial::setApi(true);
  78. }
  79. $this->user = common_current_user();
  80. if (empty($this->user)) {
  81. // TRANS: Client exception thrown trying to respond to a poll while not logged in.
  82. throw new ClientException(
  83. _m('You must be logged in to respond to a poll.'),
  84. 403
  85. );
  86. }
  87. if ($this->isPost()) {
  88. $this->checkSessionToken();
  89. }
  90. $id = $this->trimmed('id');
  91. $this->poll = Poll::getKV('id', $id);
  92. if (empty($this->poll)) {
  93. // TRANS: Client exception thrown trying to respond to a non-existing poll.
  94. throw new ClientException(_m('Invalid or missing poll.'), 404);
  95. }
  96. $selection = intval($this->trimmed('pollselection'));
  97. if ($selection < 1 || $selection > count($this->poll->getOptions())) {
  98. // TRANS: Client exception thrown responding to a poll with an invalid answer.
  99. throw new ClientException(_m('Invalid poll selection.'));
  100. }
  101. $this->selection = $selection;
  102. return true;
  103. }
  104. /**
  105. * Handler method
  106. *
  107. * @return void
  108. * @throws ClientException
  109. * @throws ReflectionException
  110. * @throws ServerException
  111. */
  112. public function handle()
  113. {
  114. parent::handle();
  115. if ($this->isPost()) {
  116. $this->respondPoll();
  117. } else {
  118. $this->showPage();
  119. }
  120. return;
  121. }
  122. /**
  123. * Add a new Poll
  124. *
  125. * @return void
  126. * @throws ClientException
  127. * @throws ReflectionException
  128. * @throws ServerException
  129. */
  130. public function respondPoll()
  131. {
  132. try {
  133. Poll_response::saveNew(
  134. $this->user->getProfile(),
  135. $this->poll,
  136. $this->selection
  137. );
  138. } catch (ClientException $ce) {
  139. $this->error = $ce->getMessage();
  140. $this->showPage();
  141. return;
  142. }
  143. if ($this->boolean('ajax')) {
  144. $this->startHTML('text/xml;charset=utf-8');
  145. $this->elementStart('head');
  146. // TRANS: Page title after sending a poll response.
  147. $this->element('title', null, _m('Poll results'));
  148. $this->elementEnd('head');
  149. $this->elementStart('body');
  150. $form = new PollResultForm($this->poll, $this);
  151. $form->show();
  152. $this->elementEnd('body');
  153. $this->endHTML();
  154. } else {
  155. common_redirect($this->poll->getUrl(), 303);
  156. }
  157. }
  158. /**
  159. * Show the Poll form
  160. *
  161. * @return void
  162. */
  163. public function showContent()
  164. {
  165. if (!empty($this->error)) {
  166. $this->element('p', 'error', $this->error);
  167. }
  168. $form = new PollResponseForm($this->poll, $this);
  169. $form->show();
  170. return;
  171. }
  172. /**
  173. * Return true if read only.
  174. *
  175. * MAY override
  176. *
  177. * @param array $args other arguments
  178. *
  179. * @return boolean is read only action?
  180. */
  181. public function isReadOnly($args)
  182. {
  183. if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
  184. $_SERVER['REQUEST_METHOD'] == 'HEAD') {
  185. return true;
  186. } else {
  187. return false;
  188. }
  189. }
  190. }