]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Poll/Poll.php
AS output/input for poll responses
[quix0rs-gnu-social.git] / plugins / Poll / Poll.php
1 <?php
2 /**
3  * Data class to mark notices as bookmarks
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 extends Managed_DataObject
47 {
48     public $__table = 'poll'; // table name
49     public $id;          // char(36) primary key not null -> UUID
50     public $uri;
51     public $profile_id;  // int -> profile.id
52     public $question;    // text
53     public $options;     // text; newline(?)-delimited
54     public $created;     // datetime
55
56     /**
57      * Get an instance by key
58      *
59      * This is a utility method to get a single instance with a given key value.
60      *
61      * @param string $k Key to use to lookup (usually 'user_id' for this class)
62      * @param mixed  $v Value to lookup
63      *
64      * @return User_greeting_count object found, or null for no hits
65      *
66      */
67
68     function staticGet($k, $v=null)
69     {
70         return Memcached_DataObject::staticGet('Poll', $k, $v);
71     }
72
73     /**
74      * Get an instance by compound key
75      *
76      * This is a utility method to get a single instance with a given set of
77      * key-value pairs. Usually used for the primary key for a compound key; thus
78      * the name.
79      *
80      * @param array $kv array of key-value mappings
81      *
82      * @return Bookmark object found, or null for no hits
83      *
84      */
85
86     function pkeyGet($kv)
87     {
88         return Memcached_DataObject::pkeyGet('Poll', $kv);
89     }
90
91     /**
92      * The One True Thingy that must be defined and declared.
93      */
94     public static function schemaDef()
95     {
96         return array(
97             'description' => 'Per-notice poll data for Poll plugin',
98             'fields' => array(
99                 'id' => array('type' => 'char', 'length' => 36, 'not null' => true, 'description' => 'UUID'),
100                 'uri' => array('type' => 'varchar', 'length' => 255, 'not null' => true),
101                 'profile_id' => array('type' => 'int'),
102                 'question' => array('type' => 'text'),
103                 'options' => array('type' => 'text'),
104                 'created' => array('type' => 'datetime', 'not null' => true),
105             ),
106             'primary key' => array('id'),
107             'unique keys' => array(
108                 'poll_uri_key' => array('uri'),
109             ),
110         );
111     }
112
113     /**
114      * Get a bookmark based on a notice
115      *
116      * @param Notice $notice Notice to check for
117      *
118      * @return Poll found poll or null
119      */
120
121     function getByNotice($notice)
122     {
123         return self::staticGet('uri', $notice->uri);
124     }
125
126     function getOptions()
127     {
128         return explode("\n", $this->options);
129     }
130
131     /**
132      * Is this a valid selection index?
133      *
134      * @param numeric $selection (1-based)
135      * @return boolean
136      */
137     function isValidSelection($selection)
138     {
139         if ($selection != intval($selection)) {
140             return false;
141         }
142         if ($selection < 1 || $selection > count($this->getOptions())) {
143             return false;
144         }
145         return true;
146     }
147
148     function getNotice()
149     {
150         return Notice::staticGet('uri', $this->uri);
151     }
152
153     function bestUrl()
154     {
155         return $this->getNotice()->bestUrl();
156     }
157
158     /**
159      * Get the response of a particular user to this poll, if any.
160      *
161      * @param Profile $profile
162      * @return Poll_response object or null
163      */
164     function getResponse(Profile $profile)
165     {
166         $pr = new Poll_response();
167         $pr->poll_id = $this->id;
168         $pr->profile_id = $profile->id;
169         $pr->find();
170         if ($pr->fetch()) {
171             return $pr;
172         } else {
173             return null;
174         }
175     }
176
177     function countResponses()
178     {
179         $pr = new Poll_response();
180         $pr->poll_id = $this->id;
181         $pr->groupBy('selection');
182         $pr->selectAdd('count(profile_id) as votes');
183         $pr->find();
184
185         $raw = array();
186         while ($pr->fetch()) {
187             // Votes list 1-based
188             // Array stores 0-based
189             $raw[$pr->selection - 1] = $pr->votes;
190         }
191
192         $counts = array();
193         foreach (array_keys($this->getOptions()) as $key) {
194             if (isset($raw[$key])) {
195                 $counts[$key] = $raw[$key];
196             } else {
197                 $counts[$key] = 0;
198             }
199         }
200         return $counts;
201     }
202
203     /**
204      * Save a new poll notice
205      *
206      * @param Profile $profile
207      * @param string  $question
208      * @param array   $opts (poll responses)
209      *
210      * @return Notice saved notice
211      */
212
213     static function saveNew($profile, $question, $opts, $options=null)
214     {
215         if (empty($options)) {
216             $options = array();
217         }
218
219         $p = new Poll();
220
221         $p->id          = UUID::gen();
222         $p->profile_id  = $profile->id;
223         $p->question    = $question;
224         $p->options     = implode("\n", $opts);
225
226         if (array_key_exists('created', $options)) {
227             $p->created = $options['created'];
228         } else {
229             $p->created = common_sql_now();
230         }
231
232         if (array_key_exists('uri', $options)) {
233             $p->uri = $options['uri'];
234         } else {
235             $p->uri = common_local_url('showpoll',
236                                         array('id' => $p->id));
237         }
238
239         common_log(LOG_DEBUG, "Saving poll: $p->id $p->uri");
240         $p->insert();
241
242         $content  = sprintf(_m('Poll: %s %s'),
243                             $question,
244                             $p->uri);
245         $rendered = sprintf(_m('Poll: <a href="%s">%s</a>'),
246                             htmlspecialchars($p->uri),
247                             htmlspecialchars($question));
248
249         $tags    = array('poll');
250         $replies = array();
251
252         $options = array_merge(array('urls' => array(),
253                                      'rendered' => $rendered,
254                                      'tags' => $tags,
255                                      'replies' => $replies,
256                                      'object_type' => PollPlugin::POLL_OBJECT),
257                                $options);
258
259         if (!array_key_exists('uri', $options)) {
260             $options['uri'] = $p->uri;
261         }
262
263         $saved = Notice::saveNew($profile->id,
264                                  $content,
265                                  array_key_exists('source', $options) ?
266                                  $options['source'] : 'web',
267                                  $options);
268
269         return $saved;
270     }
271 }