]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Poll/Poll_response.php
i18n/L10n updates
[quix0rs-gnu-social.git] / plugins / Poll / Poll_response.php
1 <?php
2 /**
3  * Data class to record responses to polls
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
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  * For storing the poll options and such
36  *
37  * @category PollPlugin
38  * @package  StatusNet
39  * @author   Brion Vibber <brion@status.net>
40  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
41  * @link     http://status.net/
42  *
43  * @see      DB_DataObject
44  */
45 class Poll_response extends Managed_DataObject
46 {
47     public $__table = 'poll_response'; // table name
48     public $id;          // char(36) primary key not null -> UUID
49     public $poll_id;     // char(36) -> poll.id UUID
50     public $profile_id;  // int -> profile.id
51     public $selection;   // int -> choice #
52     public $created;     // datetime
53
54     /**
55      * Get an instance by key
56      *
57      * This is a utility method to get a single instance with a given key value.
58      *
59      * @param string $k Key to use to lookup (usually 'user_id' for this class)
60      * @param mixed  $v Value to lookup
61      *
62      * @return User_greeting_count object found, or null for no hits
63      *
64      */
65     function staticGet($k, $v=null)
66     {
67         return Memcached_DataObject::staticGet('Poll_response', $k, $v);
68     }
69
70     /**
71      * Get an instance by compound key
72      *
73      * This is a utility method to get a single instance with a given set of
74      * key-value pairs. Usually used for the primary key for a compound key; thus
75      * the name.
76      *
77      * @param array $kv array of key-value mappings
78      *
79      * @return Bookmark object found, or null for no hits
80      *
81      */
82     function pkeyGet($kv)
83     {
84         return Memcached_DataObject::pkeyGet('Poll_response', $kv);
85     }
86
87     /**
88      * The One True Thingy that must be defined and declared.
89      */
90     public static function schemaDef()
91     {
92         return array(
93             'description' => 'Record of responses to polls',
94             'fields' => array(
95                 'id' => array('type' => 'char', 'length' => 36, 'not null' => true, 'description' => 'UUID of the response'),
96                 'uri' => array('type' => 'varchar', 'length' => 255, 'not null' => true, 'description' => 'UUID to the response notice'),
97                 'poll_id' => array('type' => 'char', 'length' => 36, 'not null' => true, 'description' => 'UUID of poll being responded to'),
98                 'profile_id' => array('type' => 'int'),
99                 'selection' => array('type' => 'int'),
100                 'created' => array('type' => 'datetime', 'not null' => true),
101             ),
102             'primary key' => array('id'),
103             'unique keys' => array(
104                 'poll_uri_key' => array('uri'),
105                 'poll_response_poll_id_profile_id_key' => array('poll_id', 'profile_id'),
106             ),
107             'indexes' => array(
108                 'poll_response_profile_id_poll_id_index' => array('profile_id', 'poll_id'),
109             )
110         );
111     }
112
113     /**
114      * Get a poll response based on a notice
115      *
116      * @param Notice $notice Notice to check for
117      *
118      * @return Poll_response found response or null
119      */
120     function getByNotice($notice)
121     {
122         return self::staticGet('uri', $notice->uri);
123     }
124
125     /**
126      * Get the notice that belongs to this response...
127      *
128      * @return Notice
129      */
130     function getNotice()
131     {
132         return Notice::staticGet('uri', $this->uri);
133     }
134
135     function bestUrl()
136     {
137         return $this->getNotice()->bestUrl();
138     }
139
140     /**
141      *
142      * @return Poll
143      */
144     function getPoll()
145     {
146         return Poll::staticGet('id', $this->poll_id);
147     }
148     /**
149      * Save a new poll notice
150      *
151      * @param Profile $profile
152      * @param Poll    $poll the poll being responded to
153      * @param int     $selection (1-based)
154      * @param array   $opts (poll responses)
155      *
156      * @return Notice saved notice
157      */
158     static function saveNew($profile, $poll, $selection, $options=null)
159     {
160         if (empty($options)) {
161             $options = array();
162         }
163
164         if (!$poll->isValidSelection($selection)) {
165             // TRANS: Client exception thrown when responding to a poll with an invalid option.
166             throw new ClientException(_m('Invalid poll selection.'));
167         }
168         $opts = $poll->getOptions();
169         $answer = $opts[$selection - 1];
170
171         $pr = new Poll_response();
172         $pr->id          = UUID::gen();
173         $pr->profile_id  = $profile->id;
174         $pr->poll_id     = $poll->id;
175         $pr->selection   = $selection;
176
177         if (array_key_exists('created', $options)) {
178             $pr->created = $options['created'];
179         } else {
180             $pr->created = common_sql_now();
181         }
182
183         if (array_key_exists('uri', $options)) {
184             $pr->uri = $options['uri'];
185         } else {
186             $pr->uri = common_local_url('showpollresponse',
187                                         array('id' => $pr->id));
188         }
189
190         common_log(LOG_DEBUG, "Saving poll response: $pr->id $pr->uri");
191         $pr->insert();
192
193         // TRANS: Notice content voting for a poll.
194         // TRANS: %s is the chosen option in the poll.
195         $content  = sprintf(_m('voted for "%s"'),
196                             $answer);
197         $link = '<a href="' . htmlspecialchars($poll->uri) . '">' . htmlspecialchars($answer) . '</a>';
198         // TRANS: Rendered version of the notice content voting for a poll.
199         // TRANS: %s a link to the poll with the chosen option as link description.
200         $rendered = sprintf(_m('voted for "%s"'), $link);
201
202         $tags    = array();
203         $replies = array();
204
205         $options = array_merge(array('urls' => array(),
206                                      'rendered' => $rendered,
207                                      'tags' => $tags,
208                                      'replies' => $replies,
209                                      'reply_to' => $poll->getNotice()->id,
210                                      'object_type' => PollPlugin::POLL_RESPONSE_OBJECT),
211                                $options);
212
213         if (!array_key_exists('uri', $options)) {
214             $options['uri'] = $pr->uri;
215         }
216
217         $saved = Notice::saveNew($profile->id,
218                                  $content,
219                                  array_key_exists('source', $options) ?
220                                  $options['source'] : 'web',
221                                  $options);
222
223         return $saved;
224     }
225 }