Poll.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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;
  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' => 255, '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 Poll found poll or null
  79. */
  80. static function getByNotice($notice)
  81. {
  82. return self::getKV('uri', $notice->uri);
  83. }
  84. function getOptions()
  85. {
  86. return explode("\n", $this->options);
  87. }
  88. /**
  89. * Is this a valid selection index?
  90. *
  91. * @param numeric $selection (1-based)
  92. * @return boolean
  93. */
  94. 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. function getNotice()
  105. {
  106. return Notice::getKV('uri', $this->uri);
  107. }
  108. 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 Poll_response object or null
  117. */
  118. 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. 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. * @return Notice saved notice
  155. */
  156. static function saveNew($profile, $question, $opts, $options=null)
  157. {
  158. if (empty($options)) {
  159. $options = array();
  160. }
  161. $p = new Poll();
  162. $p->id = UUID::gen();
  163. $p->profile_id = $profile->id;
  164. $p->question = $question;
  165. $p->options = implode("\n", $opts);
  166. if (array_key_exists('created', $options)) {
  167. $p->created = $options['created'];
  168. } else {
  169. $p->created = common_sql_now();
  170. }
  171. if (array_key_exists('uri', $options)) {
  172. $p->uri = $options['uri'];
  173. } else {
  174. $p->uri = common_local_url('showpoll',
  175. array('id' => $p->id));
  176. }
  177. common_log(LOG_DEBUG, "Saving poll: $p->id $p->uri");
  178. $p->insert();
  179. // TRANS: Notice content creating a poll.
  180. // TRANS: %1$s is the poll question, %2$s is a link to the poll.
  181. $content = sprintf(_m('Poll: %1$s %2$s'),
  182. $question,
  183. $p->uri);
  184. $link = '<a href="' . htmlspecialchars($p->uri) . '">' . htmlspecialchars($question) . '</a>';
  185. // TRANS: Rendered version of the notice content creating a poll.
  186. // TRANS: %s is a link to the poll with the question as link description.
  187. $rendered = sprintf(_m('Poll: %s'), $link);
  188. $tags = array('poll');
  189. $replies = array();
  190. $options = array_merge(array('urls' => array(),
  191. 'rendered' => $rendered,
  192. 'tags' => $tags,
  193. 'replies' => $replies,
  194. 'object_type' => PollPlugin::POLL_OBJECT),
  195. $options);
  196. if (!array_key_exists('uri', $options)) {
  197. $options['uri'] = $p->uri;
  198. }
  199. $saved = Notice::saveNew($profile->id,
  200. $content,
  201. array_key_exists('source', $options) ?
  202. $options['source'] : 'web',
  203. $options);
  204. return $saved;
  205. }
  206. }