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
50 public $uri; // varchar(191) not 255 because utf8mb4 takes more space
51 public $profile_id; // int -> profile.id
52 public $question; // text
53 public $options; // text; newline(?)-delimited
54 public $created; // datetime
57 * The One True Thingy that must be defined and declared.
59 public static function schemaDef()
62 'description' => 'Per-notice poll data for Poll plugin',
64 'id' => array('type' => 'char', 'length' => 36, 'not null' => true, 'description' => 'UUID'),
65 'uri' => array('type' => 'varchar', 'length' => 191, 'not null' => true),
66 'profile_id' => array('type' => 'int'),
67 'question' => array('type' => 'text'),
68 'options' => array('type' => 'text'),
69 'created' => array('type' => 'datetime', 'not null' => true),
71 'primary key' => array('id'),
72 'unique keys' => array(
73 'poll_uri_key' => array('uri'),
79 * Get a bookmark based on a notice
81 * @param Notice $notice Notice to check for
83 * @return Poll found poll or null
85 static function getByNotice($notice)
87 return self::getKV('uri', $notice->uri);
92 return explode("\n", $this->options);
96 * Is this a valid selection index?
98 * @param numeric $selection (1-based)
101 function isValidSelection($selection)
103 if ($selection != intval($selection)) {
106 if ($selection < 1 || $selection > count($this->getOptions())) {
114 return Notice::getKV('uri', $this->uri);
119 return $this->getNotice()->getUrl();
123 * Get the response of a particular user to this poll, if any.
125 * @param Profile $profile
126 * @return Poll_response object or null
128 function getResponse(Profile $profile)
130 $pr = Poll_response::pkeyGet(array('poll_id' => $this->id,
131 'profile_id' => $profile->id));
135 function countResponses()
137 $pr = new Poll_response();
138 $pr->poll_id = $this->id;
139 $pr->groupBy('selection');
140 $pr->selectAdd('count(profile_id) as votes');
144 while ($pr->fetch()) {
145 // Votes list 1-based
146 // Array stores 0-based
147 $raw[$pr->selection - 1] = $pr->votes;
151 foreach (array_keys($this->getOptions()) as $key) {
152 if (isset($raw[$key])) {
153 $counts[$key] = $raw[$key];
162 * Save a new poll notice
164 * @param Profile $profile
165 * @param string $question
166 * @param array $opts (poll responses)
168 * @return Notice saved notice
170 static function saveNew($profile, $question, $opts, $options=null)
172 if (empty($options)) {
178 $p->id = UUID::gen();
179 $p->profile_id = $profile->id;
180 $p->question = $question;
181 $p->options = implode("\n", $opts);
183 if (array_key_exists('created', $options)) {
184 $p->created = $options['created'];
186 $p->created = common_sql_now();
189 if (array_key_exists('uri', $options)) {
190 $p->uri = $options['uri'];
192 $p->uri = common_local_url('showpoll',
193 array('id' => $p->id));
196 common_log(LOG_DEBUG, "Saving poll: $p->id $p->uri");
199 // TRANS: Notice content creating a poll.
200 // TRANS: %1$s is the poll question, %2$s is a link to the poll.
201 $content = sprintf(_m('Poll: %1$s %2$s'),
204 $link = '<a href="' . htmlspecialchars($p->uri) . '">' . htmlspecialchars($question) . '</a>';
205 // TRANS: Rendered version of the notice content creating a poll.
206 // TRANS: %s is a link to the poll with the question as link description.
207 $rendered = sprintf(_m('Poll: %s'), $link);
209 $tags = array('poll');
212 $options = array_merge(array('urls' => array(),
213 'rendered' => $rendered,
215 'replies' => $replies,
216 'object_type' => PollPlugin::POLL_OBJECT),
219 if (!array_key_exists('uri', $options)) {
220 $options['uri'] = $p->uri;
223 $saved = Notice::saveNew($profile->id,
225 array_key_exists('source', $options) ?
226 $options['source'] : 'web',