3 * Data class to mark notices as bookmarks
9 * @author Brion Vibber <brion@status.net>
10 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11 * @link http://status.net/
13 * StatusNet - the distributed open-source microblogging tool
14 * Copyright (C) 2011, StatusNet, Inc.
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.
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.
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/>.
30 if (!defined('STATUSNET')) {
35 * For storing the poll options and such
37 * @category PollPlugin
39 * @author Brion Vibber <brion@status.net>
40 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
41 * @link http://status.net/
46 class Poll extends Managed_DataObject
48 public $__table = 'poll'; // table name
49 public $id; // char(36) primary key not null -> UUID
51 public $profile_id; // int -> profile.id
52 public $question; // text
53 public $options; // text; newline(?)-delimited
54 public $created; // datetime
57 * Get an instance by key
59 * This is a utility method to get a single instance with a given key value.
61 * @param string $k Key to use to lookup (usually 'user_id' for this class)
62 * @param mixed $v Value to lookup
64 * @return User_greeting_count object found, or null for no hits
67 function staticGet($k, $v=null)
69 return Memcached_DataObject::staticGet('Poll', $k, $v);
73 * Get an instance by compound key
75 * This is a utility method to get a single instance with a given set of
76 * key-value pairs. Usually used for the primary key for a compound key; thus
79 * @param array $kv array of key-value mappings
81 * @return Bookmark object found, or null for no hits
86 return Memcached_DataObject::pkeyGet('Poll', $kv);
90 * The One True Thingy that must be defined and declared.
92 public static function schemaDef()
95 'description' => 'Per-notice poll data for Poll plugin',
97 'id' => array('type' => 'char', 'length' => 36, 'not null' => true, 'description' => 'UUID'),
98 'uri' => array('type' => 'varchar', 'length' => 255, 'not null' => true),
99 'profile_id' => array('type' => 'int'),
100 'question' => array('type' => 'text'),
101 'options' => array('type' => 'text'),
102 'created' => array('type' => 'datetime', 'not null' => true),
104 'primary key' => array('id'),
105 'unique keys' => array(
106 'poll_uri_key' => array('uri'),
112 * Get a bookmark based on a notice
114 * @param Notice $notice Notice to check for
116 * @return Poll found poll or null
118 function getByNotice($notice)
120 return self::staticGet('uri', $notice->uri);
123 function getOptions()
125 return explode("\n", $this->options);
129 * Is this a valid selection index?
131 * @param numeric $selection (1-based)
134 function isValidSelection($selection)
136 if ($selection != intval($selection)) {
139 if ($selection < 1 || $selection > count($this->getOptions())) {
147 return Notice::staticGet('uri', $this->uri);
152 return $this->getNotice()->bestUrl();
156 * Get the response of a particular user to this poll, if any.
158 * @param Profile $profile
159 * @return Poll_response object or null
161 function getResponse(Profile $profile)
163 $pr = new Poll_response();
164 $pr->poll_id = $this->id;
165 $pr->profile_id = $profile->id;
174 function countResponses()
176 $pr = new Poll_response();
177 $pr->poll_id = $this->id;
178 $pr->groupBy('selection');
179 $pr->selectAdd('count(profile_id) as votes');
183 while ($pr->fetch()) {
184 // Votes list 1-based
185 // Array stores 0-based
186 $raw[$pr->selection - 1] = $pr->votes;
190 foreach (array_keys($this->getOptions()) as $key) {
191 if (isset($raw[$key])) {
192 $counts[$key] = $raw[$key];
201 * Save a new poll notice
203 * @param Profile $profile
204 * @param string $question
205 * @param array $opts (poll responses)
207 * @return Notice saved notice
209 static function saveNew($profile, $question, $opts, $options=null)
211 if (empty($options)) {
217 $p->id = UUID::gen();
218 $p->profile_id = $profile->id;
219 $p->question = $question;
220 $p->options = implode("\n", $opts);
222 if (array_key_exists('created', $options)) {
223 $p->created = $options['created'];
225 $p->created = common_sql_now();
228 if (array_key_exists('uri', $options)) {
229 $p->uri = $options['uri'];
231 $p->uri = common_local_url('showpoll',
232 array('id' => $p->id));
235 common_log(LOG_DEBUG, "Saving poll: $p->id $p->uri");
238 // TRANS: Notice content creating a poll.
239 // TRANS: %1$s is the poll question, %2$s is a link to the poll.
240 $content = sprintf(_m('Poll: %1$s %2$s'),
243 $link = '<a href="' . htmlspecialchars($p->uri) . '">' . htmlspecialchars($question) . '</a>';
244 // TRANS: Rendered version of the notice content creating a poll.
245 // TRANS: %s a link to the poll with the question as link description.
246 $rendered = sprintf(_m('Poll: %s'), $link);
248 $tags = array('poll');
251 $options = array_merge(array('urls' => array(),
252 'rendered' => $rendered,
254 'replies' => $replies,
255 'object_type' => PollPlugin::POLL_OBJECT),
258 if (!array_key_exists('uri', $options)) {
259 $options['uri'] = $p->uri;
262 $saved = Notice::saveNew($profile->id,
264 array_key_exists('source', $options) ?
265 $options['source'] : 'web',