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