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