Poll.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. /**
  3. * Data class to mark notices as bookmarks
  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 extends Managed_DataObject
  44. {
  45. public $__table = 'poll'; // 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 $profile_id; // int -> profile.id
  49. public $question; // text
  50. public $options; // text; newline(?)-delimited
  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' => 'Per-notice poll data for Poll plugin',
  59. 'fields' => array(
  60. 'id' => array('type' => 'char', 'length' => 36, 'not null' => true, 'description' => 'UUID'),
  61. 'uri' => array('type' => 'varchar', 'length' => 191, 'not null' => true),
  62. 'profile_id' => array('type' => 'int'),
  63. 'question' => array('type' => 'text'),
  64. 'options' => array('type' => 'text'),
  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. ),
  71. );
  72. }
  73. /**
  74. * Get a bookmark based on a notice
  75. *
  76. * @param Notice $notice Notice to check for
  77. *
  78. * @return get_called_class found poll or null
  79. */
  80. public static function getByNotice($notice)
  81. {
  82. return self::getKV('uri', $notice->uri);
  83. }
  84. public function getOptions()
  85. {
  86. return explode("\n", $this->options);
  87. }
  88. /**
  89. * Is this a valid selection index?
  90. *
  91. * @param int $selection (1-based)
  92. * @return boolean
  93. */
  94. public function isValidSelection($selection)
  95. {
  96. if ($selection != intval($selection)) {
  97. return false;
  98. }
  99. if ($selection < 1 || $selection > count($this->getOptions())) {
  100. return false;
  101. }
  102. return true;
  103. }
  104. public function getNotice()
  105. {
  106. return Notice::getKV('uri', $this->uri);
  107. }
  108. public function getUrl()
  109. {
  110. return $this->getNotice()->getUrl();
  111. }
  112. /**
  113. * Get the response of a particular user to this poll, if any.
  114. *
  115. * @param Profile $profile
  116. * @return get_called_class object or null
  117. */
  118. public function getResponse(Profile $profile)
  119. {
  120. $pr = Poll_response::pkeyGet(array('poll_id' => $this->id,
  121. 'profile_id' => $profile->id));
  122. return $pr;
  123. }
  124. public function countResponses()
  125. {
  126. $pr = new Poll_response();
  127. $pr->poll_id = $this->id;
  128. $pr->groupBy('selection');
  129. $pr->selectAdd('count(profile_id) as votes');
  130. $pr->find();
  131. $raw = array();
  132. while ($pr->fetch()) {
  133. // Votes list 1-based
  134. // Array stores 0-based
  135. $raw[$pr->selection - 1] = $pr->votes;
  136. }
  137. $counts = array();
  138. foreach (array_keys($this->getOptions()) as $key) {
  139. if (isset($raw[$key])) {
  140. $counts[$key] = $raw[$key];
  141. } else {
  142. $counts[$key] = 0;
  143. }
  144. }
  145. return $counts;
  146. }
  147. /**
  148. * Save a new poll notice
  149. *
  150. * @param Profile $profile
  151. * @param string $question
  152. * @param array $opts (poll responses)
  153. *
  154. * @param null $options
  155. * @return Notice saved notice
  156. * @throws ClientException
  157. */
  158. public static function saveNew($profile, $question, $opts, $options = null)
  159. {
  160. if (empty($options)) {
  161. $options = array();
  162. }
  163. $p = new Poll();
  164. $p->id = UUID::gen();
  165. $p->profile_id = $profile->id;
  166. $p->question = $question;
  167. $p->options = implode("\n", $opts);
  168. if (array_key_exists('created', $options)) {
  169. $p->created = $options['created'];
  170. } else {
  171. $p->created = common_sql_now();
  172. }
  173. if (array_key_exists('uri', $options)) {
  174. $p->uri = $options['uri'];
  175. } else {
  176. $p->uri = common_local_url(
  177. 'showpoll',
  178. array('id' => $p->id)
  179. );
  180. }
  181. common_log(LOG_DEBUG, "Saving poll: $p->id $p->uri");
  182. $p->insert();
  183. // TRANS: Notice content creating a poll.
  184. // TRANS: %1$s is the poll question, %2$s is a link to the poll.
  185. $content = sprintf(
  186. _m('Poll: %1$s %2$s'),
  187. $question,
  188. $p->uri
  189. );
  190. $link = '<a href="' . htmlspecialchars($p->uri) . '">' . htmlspecialchars($question) . '</a>';
  191. // TRANS: Rendered version of the notice content creating a poll.
  192. // TRANS: %s is a link to the poll with the question as link description.
  193. $rendered = sprintf(_m('Poll: %s'), $link);
  194. $tags = array('poll');
  195. $replies = array();
  196. $options = array_merge(
  197. array('urls' => array(),
  198. 'rendered' => $rendered,
  199. 'tags' => $tags,
  200. 'replies' => $replies,
  201. 'object_type' => PollPlugin::POLL_OBJECT),
  202. $options
  203. );
  204. if (!array_key_exists('uri', $options)) {
  205. $options['uri'] = $p->uri;
  206. }
  207. $saved = Notice::saveNew(
  208. $profile->id,
  209. $content,
  210. array_key_exists('source', $options) ?
  211. $options['source'] : 'web',
  212. $options
  213. );
  214. return $saved;
  215. }
  216. }