3 * Data class to record responses to polls
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/
45 class Poll_response extends Managed_DataObject
47 public $__table = 'poll_response'; // table name
48 public $id; // char(36) primary key not null -> UUID
49 public $uri; // varchar(191) not 255 because utf8mb4 takes more space
50 public $poll_id; // char(36) -> poll.id UUID
51 public $profile_id; // int -> profile.id
52 public $selection; // int -> choice #
53 public $created; // datetime
56 * The One True Thingy that must be defined and declared.
58 public static function schemaDef()
61 'description' => 'Record of responses to polls',
63 'id' => array('type' => 'char', 'length' => 36, 'not null' => true, 'description' => 'UUID of the response'),
64 'uri' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'UUID to the response notice'),
65 'poll_id' => array('type' => 'char', 'length' => 36, 'not null' => true, 'description' => 'UUID of poll being responded to'),
66 'profile_id' => array('type' => 'int'),
67 'selection' => array('type' => 'int'),
68 'created' => array('type' => 'datetime', 'not null' => true),
70 'primary key' => array('id'),
71 'unique keys' => array(
72 'poll_uri_key' => array('uri'),
73 'poll_response_poll_id_profile_id_key' => array('poll_id', 'profile_id'),
76 'poll_response_profile_id_poll_id_index' => array('profile_id', 'poll_id'),
82 * Get a poll response based on a notice
84 * @param Notice $notice Notice to check for
86 * @return Poll_response found response or null
88 static function getByNotice($notice)
90 return self::getKV('uri', $notice->uri);
94 * Get the notice that belongs to this response...
100 return Notice::getKV('uri', $this->uri);
105 return $this->getNotice()->getUrl();
114 return Poll::getKV('id', $this->poll_id);
117 * Save a new poll notice
119 * @param Profile $profile
120 * @param Poll $poll the poll being responded to
121 * @param int $selection (1-based)
122 * @param array $opts (poll responses)
124 * @return Notice saved notice
126 static function saveNew($profile, $poll, $selection, $options=null)
128 if (empty($options)) {
132 if (!$poll->isValidSelection($selection)) {
133 // TRANS: Client exception thrown when responding to a poll with an invalid option.
134 throw new ClientException(_m('Invalid poll selection.'));
136 $opts = $poll->getOptions();
137 $answer = $opts[$selection - 1];
139 $pr = new Poll_response();
140 $pr->id = UUID::gen();
141 $pr->profile_id = $profile->id;
142 $pr->poll_id = $poll->id;
143 $pr->selection = $selection;
145 if (array_key_exists('created', $options)) {
146 $pr->created = $options['created'];
148 $pr->created = common_sql_now();
151 if (array_key_exists('uri', $options)) {
152 $pr->uri = $options['uri'];
154 $pr->uri = common_local_url('showpollresponse',
155 array('id' => $pr->id));
158 common_log(LOG_DEBUG, "Saving poll response: $pr->id $pr->uri");
161 // TRANS: Notice content voting for a poll.
162 // TRANS: %s is the chosen option in the poll.
163 $content = sprintf(_m('voted for "%s"'),
165 $link = '<a href="' . htmlspecialchars($poll->uri) . '">' . htmlspecialchars($answer) . '</a>';
166 // TRANS: Rendered version of the notice content voting for a poll.
167 // TRANS: %s a link to the poll with the chosen option as link description.
168 $rendered = sprintf(_m('voted for "%s"'), $link);
172 $options = array_merge(array('urls' => array(),
173 'rendered' => $rendered,
175 'reply_to' => $poll->getNotice()->id,
176 'object_type' => PollPlugin::POLL_RESPONSE_OBJECT),
179 if (!array_key_exists('uri', $options)) {
180 $options['uri'] = $pr->uri;
183 $saved = Notice::saveNew($profile->id,
185 array_key_exists('source', $options) ?
186 $options['source'] : 'web',