]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Poll/Poll.php
non-existent poco
[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     function staticGet($k, $v=null)
68     {
69         return Memcached_DataObject::staticGet('Poll', $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     function pkeyGet($kv)
85     {
86         return Memcached_DataObject::pkeyGet('Poll', $kv);
87     }
88
89     /**
90      * The One True Thingy that must be defined and declared.
91      */
92     public static function schemaDef()
93     {
94         return array(
95             'description' => 'Per-notice poll data for Poll plugin',
96             'fields' => array(
97                 'id' => array('type' => 'char', 'length' => 36, 'not null' => true, 'description' => 'UUID'),
98                 'uri' => array('type' => 'varchar', 'length' => 255, 'not null' => true),
99                 'profile_id' => array('type' => 'int'),
100                 'question' => array('type' => 'text'),
101                 'options' => array('type' => 'text'),
102                 'created' => array('type' => 'datetime', 'not null' => true),
103             ),
104             'primary key' => array('id'),
105             'unique keys' => array(
106                 'poll_uri_key' => array('uri'),
107             ),
108         );
109     }
110
111     /**
112      * Get a bookmark based on a notice
113      *
114      * @param Notice $notice Notice to check for
115      *
116      * @return Poll found poll or null
117      */
118     function getByNotice($notice)
119     {
120         return self::staticGet('uri', $notice->uri);
121     }
122
123     function getOptions()
124     {
125         return explode("\n", $this->options);
126     }
127
128     /**
129      * Is this a valid selection index?
130      *
131      * @param numeric $selection (1-based)
132      * @return boolean
133      */
134     function isValidSelection($selection)
135     {
136         if ($selection != intval($selection)) {
137             return false;
138         }
139         if ($selection < 1 || $selection > count($this->getOptions())) {
140             return false;
141         }
142         return true;
143     }
144
145     function getNotice()
146     {
147         return Notice::staticGet('uri', $this->uri);
148     }
149
150     function bestUrl()
151     {
152         return $this->getNotice()->bestUrl();
153     }
154
155     /**
156      * Get the response of a particular user to this poll, if any.
157      *
158      * @param Profile $profile
159      * @return Poll_response object or null
160      */
161     function getResponse(Profile $profile)
162     {
163         $pr = Poll_response::pkeyGet(array('poll_id' => $this->id,
164                                                                            'profile_id' => $profile->id));
165         return $pr;
166     }
167
168     function countResponses()
169     {
170         $pr = new Poll_response();
171         $pr->poll_id = $this->id;
172         $pr->groupBy('selection');
173         $pr->selectAdd('count(profile_id) as votes');
174         $pr->find();
175
176         $raw = array();
177         while ($pr->fetch()) {
178             // Votes list 1-based
179             // Array stores 0-based
180             $raw[$pr->selection - 1] = $pr->votes;
181         }
182
183         $counts = array();
184         foreach (array_keys($this->getOptions()) as $key) {
185             if (isset($raw[$key])) {
186                 $counts[$key] = $raw[$key];
187             } else {
188                 $counts[$key] = 0;
189             }
190         }
191         return $counts;
192     }
193
194     /**
195      * Save a new poll notice
196      *
197      * @param Profile $profile
198      * @param string  $question
199      * @param array   $opts (poll responses)
200      *
201      * @return Notice saved notice
202      */
203     static function saveNew($profile, $question, $opts, $options=null)
204     {
205         if (empty($options)) {
206             $options = array();
207         }
208
209         $p = new Poll();
210
211         $p->id          = UUID::gen();
212         $p->profile_id  = $profile->id;
213         $p->question    = $question;
214         $p->options     = implode("\n", $opts);
215
216         if (array_key_exists('created', $options)) {
217             $p->created = $options['created'];
218         } else {
219             $p->created = common_sql_now();
220         }
221
222         if (array_key_exists('uri', $options)) {
223             $p->uri = $options['uri'];
224         } else {
225             $p->uri = common_local_url('showpoll',
226                                         array('id' => $p->id));
227         }
228
229         common_log(LOG_DEBUG, "Saving poll: $p->id $p->uri");
230         $p->insert();
231
232         // TRANS: Notice content creating a poll.
233         // TRANS: %1$s is the poll question, %2$s is a link to the poll.
234         $content  = sprintf(_m('Poll: %1$s %2$s'),
235                             $question,
236                             $p->uri);
237         $link = '<a href="' . htmlspecialchars($p->uri) . '">' . htmlspecialchars($question) . '</a>';
238         // TRANS: Rendered version of the notice content creating a poll.
239         // TRANS: %s is a link to the poll with the question as link description.
240         $rendered = sprintf(_m('Poll: %s'), $link);
241
242         $tags    = array('poll');
243         $replies = array();
244
245         $options = array_merge(array('urls' => array(),
246                                      'rendered' => $rendered,
247                                      'tags' => $tags,
248                                      'replies' => $replies,
249                                      'object_type' => PollPlugin::POLL_OBJECT),
250                                $options);
251
252         if (!array_key_exists('uri', $options)) {
253             $options['uri'] = $p->uri;
254         }
255
256         $saved = Notice::saveNew($profile->id,
257                                  $content,
258                                  array_key_exists('source', $options) ?
259                                  $options['source'] : 'web',
260                                  $options);
261
262         return $saved;
263     }
264 }