Poll_response.php 6.2 KB

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