Poll_response.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 Poll_response found response or null
  83. */
  84. 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 Notice
  92. */
  93. function getNotice()
  94. {
  95. return Notice::getKV('uri', $this->uri);
  96. }
  97. function getUrl()
  98. {
  99. return $this->getNotice()->getUrl();
  100. }
  101. /**
  102. *
  103. * @return Poll
  104. */
  105. 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 array $opts (poll responses)
  116. *
  117. * @return Notice saved notice
  118. */
  119. static function saveNew($profile, $poll, $selection, $options=null)
  120. {
  121. if (empty($options)) {
  122. $options = array();
  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('showpollresponse',
  144. array('id' => $pr->id));
  145. }
  146. common_log(LOG_DEBUG, "Saving poll response: $pr->id $pr->uri");
  147. $pr->insert();
  148. // TRANS: Notice content voting for a poll.
  149. // TRANS: %s is the chosen option in the poll.
  150. $content = sprintf(_m('voted for "%s"'),
  151. $answer);
  152. $link = '<a href="' . htmlspecialchars($poll->uri) . '">' . htmlspecialchars($answer) . '</a>';
  153. // TRANS: Rendered version of the notice content voting for a poll.
  154. // TRANS: %s a link to the poll with the chosen option as link description.
  155. $rendered = sprintf(_m('voted for "%s"'), $link);
  156. $tags = array();
  157. $options = array_merge(array('urls' => array(),
  158. 'rendered' => $rendered,
  159. 'tags' => $tags,
  160. 'reply_to' => $poll->getNotice()->id,
  161. 'object_type' => PollPlugin::POLL_RESPONSE_OBJECT),
  162. $options);
  163. if (!array_key_exists('uri', $options)) {
  164. $options['uri'] = $pr->uri;
  165. }
  166. $saved = Notice::saveNew($profile->id,
  167. $content,
  168. array_key_exists('source', $options) ?
  169. $options['source'] : 'web',
  170. $options);
  171. return $saved;
  172. }
  173. }