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