Poll_response.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Data class to record responses to polls
  18. *
  19. * @category PollPlugin
  20. * @package GNUsocial
  21. * @author Brion Vibber <brion@status.net>
  22. * @copyright 2011 StatusNet, Inc.
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * For storing the poll options and such
  28. *
  29. * @copyright 2011 StatusNet, Inc.
  30. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  31. *
  32. * @see DB_DataObject
  33. */
  34. class Poll_response extends Managed_DataObject
  35. {
  36. public $__table = 'poll_response'; // table name
  37. public $id; // char(36) primary key not null -> UUID
  38. public $uri; // varchar(191) not 255 because utf8mb4 takes more space
  39. public $poll_id; // char(36) -> poll.id UUID
  40. public $profile_id; // int -> profile.id
  41. public $selection; // int -> choice #
  42. public $created; // datetime
  43. /**
  44. * The One True Thingy that must be defined and declared.
  45. */
  46. public static function schemaDef()
  47. {
  48. return array(
  49. 'description' => 'Record of responses to polls',
  50. 'fields' => array(
  51. 'id' => array('type' => 'char', 'length' => 36, 'not null' => true, 'description' => 'UUID of the response'),
  52. 'uri' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'UUID to the response notice'),
  53. 'poll_id' => array('type' => 'char', 'length' => 36, 'not null' => true, 'description' => 'UUID of poll being responded to'),
  54. 'profile_id' => array('type' => 'int'),
  55. 'selection' => array('type' => 'int'),
  56. 'created' => array('type' => 'datetime', 'not null' => true),
  57. ),
  58. 'primary key' => array('id'),
  59. 'unique keys' => array(
  60. 'poll_response_uri_key' => array('uri'),
  61. 'poll_response_poll_id_profile_id_key' => array('poll_id', 'profile_id'),
  62. ),
  63. 'indexes' => array(
  64. 'poll_response_profile_id_poll_id_idx' => array('profile_id', 'poll_id'),
  65. )
  66. );
  67. }
  68. /**
  69. * Get a poll response based on a notice
  70. *
  71. * @param Notice $notice Notice to check for
  72. *
  73. * @return get_called_class found response or null
  74. */
  75. public static function getByNotice($notice)
  76. {
  77. return self::getKV('uri', $notice->uri);
  78. }
  79. /**
  80. * Get the notice that belongs to this response...
  81. *
  82. * @return get_called_class
  83. */
  84. public function getNotice()
  85. {
  86. return Notice::getKV('uri', $this->uri);
  87. }
  88. public function getUrl()
  89. {
  90. return $this->getNotice()->getUrl();
  91. }
  92. /**
  93. *
  94. * @return get_called_class
  95. */
  96. public function getPoll()
  97. {
  98. return Poll::getKV('id', $this->poll_id);
  99. }
  100. /**
  101. * Save a new poll notice
  102. *
  103. * @param Profile $profile
  104. * @param Poll $poll the poll being responded to
  105. * @param int $selection (1-based)
  106. * @param null $options
  107. * @return Notice saved notice
  108. * @throws ClientException
  109. */
  110. public static function saveNew($profile, $poll, $selection, $options = null)
  111. {
  112. if (empty($options)) {
  113. $options = [];
  114. }
  115. if (!$poll->isValidSelection($selection)) {
  116. // TRANS: Client exception thrown when responding to a poll with an invalid option.
  117. throw new ClientException(_m('Invalid poll selection.'));
  118. }
  119. $opts = $poll->getOptions();
  120. $answer = $opts[$selection - 1];
  121. $pr = new Poll_response();
  122. $pr->id = UUID::gen();
  123. $pr->profile_id = $profile->id;
  124. $pr->poll_id = $poll->id;
  125. $pr->selection = $selection;
  126. if (array_key_exists('created', $options)) {
  127. $pr->created = $options['created'];
  128. } else {
  129. $pr->created = common_sql_now();
  130. }
  131. if (array_key_exists('uri', $options)) {
  132. $pr->uri = $options['uri'];
  133. } else {
  134. $pr->uri = common_local_url(
  135. 'showpollresponse',
  136. array('id' => $pr->id)
  137. );
  138. }
  139. common_log(LOG_DEBUG, "Saving poll response: $pr->id $pr->uri");
  140. $pr->insert();
  141. // TRANS: Notice content voting for a poll.
  142. // TRANS: %s is the chosen option in the poll.
  143. $content = sprintf(
  144. _m('voted for "%s"'),
  145. $answer
  146. );
  147. $link = '<a href="' . htmlspecialchars($poll->uri) . '">' . htmlspecialchars($answer) . '</a>';
  148. // TRANS: Rendered version of the notice content voting for a poll.
  149. // TRANS: %s a link to the poll with the chosen option as link description.
  150. $rendered = sprintf(_m('voted for "%s"'), $link);
  151. $tags = array();
  152. $options = array_merge(
  153. array('urls' => array(),
  154. 'rendered' => $rendered,
  155. 'tags' => $tags,
  156. 'reply_to' => $poll->getNotice()->id,
  157. 'object_type' => PollPlugin::POLL_RESPONSE_OBJECT),
  158. $options
  159. );
  160. if (!array_key_exists('uri', $options)) {
  161. $options['uri'] = $pr->uri;
  162. }
  163. $saved = Notice::saveNew(
  164. $profile->id,
  165. $content,
  166. array_key_exists('source', $options) ?
  167. $options['source'] : 'web',
  168. $options
  169. );
  170. return $saved;
  171. }
  172. }