]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Poll/PollPlugin.php
Merge branch '1.0.x' of gitorious.org:statusnet/mainline into 1.0.x
[quix0rs-gnu-social.git] / plugins / Poll / PollPlugin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * A plugin to enable social-bookmarking functionality
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  PollPlugin
24  * @package   StatusNet
25  * @author    Brion Vibber <brion@status.net>
26  * @copyright 2011 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 /**
36  * Poll plugin main class
37  *
38  * @category  PollPlugin
39  * @package   StatusNet
40  * @author    Brion Vibber <brionv@status.net>
41  * @author    Evan Prodromou <evan@status.net>
42  * @copyright 2011 StatusNet, Inc.
43  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
44  * @link      http://status.net/
45  */
46
47 class PollPlugin extends MicroAppPlugin
48 {
49     const VERSION         = '0.1';
50     const POLL_OBJECT     = 'http://apinamespace.org/activitystreams/object/poll';
51
52     /**
53      * Database schema setup
54      *
55      * @see Schema
56      * @see ColumnDef
57      *
58      * @return boolean hook value; true means continue processing, false means stop.
59      */
60
61     function onCheckSchema()
62     {
63         $schema = Schema::get();
64         $schema->ensureTable('poll', Poll::schemaDef());
65         $schema->ensureTable('poll_response', Poll_response::schemaDef());
66         return true;
67     }
68
69     /**
70      * Show the CSS necessary for this plugin
71      *
72      * @param Action $action the action being run
73      *
74      * @return boolean hook value
75      */
76
77     function onEndShowStyles($action)
78     {
79         $action->cssLink($this->path('poll.css'));
80         return true;
81     }
82
83     /**
84      * Load related modules when needed
85      *
86      * @param string $cls Name of the class to be loaded
87      *
88      * @return boolean hook value; true means continue processing, false means stop.
89      */
90
91     function onAutoload($cls)
92     {
93         $dir = dirname(__FILE__);
94
95         switch ($cls)
96         {
97         case 'ShowpollAction':
98         case 'NewpollAction':
99         case 'RespondpollAction':
100             include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
101             return false;
102         case 'Poll':
103         case 'Poll_response':
104             include_once $dir.'/'.$cls.'.php';
105             return false;
106         case 'NewPollForm':
107         case 'PollResponseForm':
108         case 'PollResultForm':
109             include_once $dir.'/'.strtolower($cls).'.php';
110             return false;
111         default:
112             return true;
113         }
114     }
115
116     /**
117      * Map URLs to actions
118      *
119      * @param Net_URL_Mapper $m path-to-action mapper
120      *
121      * @return boolean hook value; true means continue processing, false means stop.
122      */
123
124     function onRouterInitialized($m)
125     {
126         $m->connect('main/poll/new',
127                     array('action' => 'newpoll'),
128                     array('id' => '[0-9]+'));
129
130         $m->connect('main/poll/:id/respond',
131                     array('action' => 'respondpoll'),
132                     array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
133
134         return true;
135     }
136
137     /**
138      * Plugin version data
139      *
140      * @param array &$versions array of version data
141      *
142      * @return value
143      */
144
145     function onPluginVersion(&$versions)
146     {
147         $versions[] = array('name' => 'Poll',
148                             'version' => self::VERSION,
149                             'author' => 'Brion Vibber',
150                             'homepage' => 'http://status.net/wiki/Plugin:Poll',
151                             'rawdescription' =>
152                             _m('Simple extension for supporting basic polls.'));
153         return true;
154     }
155
156     function types()
157     {
158         return array(self::POLL_OBJECT);
159     }
160
161     /**
162      * When a notice is deleted, delete the related Poll
163      *
164      * @param Notice $notice Notice being deleted
165      *
166      * @return boolean hook value
167      */
168
169     function deleteRelated($notice)
170     {
171         $p = Poll::getByNotice($notice);
172
173         if (!empty($p)) {
174             $p->delete();
175         }
176
177         return true;
178     }
179
180     /**
181      * Save a poll from an activity
182      *
183      * @param Profile  $profile  Profile to use as author
184      * @param Activity $activity Activity to save
185      * @param array    $options  Options to pass to bookmark-saving code
186      *
187      * @return Notice resulting notice
188      */
189
190     function saveNoticeFromActivity($activity, $profile, $options=array())
191     {
192         // @fixme
193     }
194
195     function activityObjectFromNotice($notice)
196     {
197         assert($this->isMyNotice($notice));
198
199         $object = new ActivityObject();
200         $object->id      = $notice->uri;
201         $object->type    = self::POLL_OBJECT;
202         $object->title   = 'Poll title';
203         $object->summary = 'Poll summary';
204         $object->link    = $notice->bestUrl();
205
206         $poll = Poll::getByNotice($notice);
207         /**
208          * Adding the poll-specific data. There's no standard in AS for polls,
209          * so we're making stuff up.
210          *
211          * For the moment, using a kind of icky-looking schema that happens to
212          * work with out code for generating both Atom and JSON forms, though
213          * I don't like it:
214          *
215          * <poll:data xmlns:poll="http://apinamespace.org/activitystreams/object/poll"
216          *            question="Who wants a poll question?"
217          *            option1="Option one"
218          *            option2="Option two"
219          *            option3="Option three"></poll:data>
220          *
221          * "poll:data": {
222          *     "xmlns:poll": http://apinamespace.org/activitystreams/object/poll
223          *     "question": "Who wants a poll question?"
224          *     "option1": "Option one"
225          *     "option2": "Option two"
226          *     "option3": "Option three"
227          * }
228          *
229          */
230         // @fixme there's no way to specify an XML node tree here, like <poll><option/><option/></poll>
231         // @fixme there's no way to specify a JSON array or multi-level tree unless you break the XML attribs
232         // @fixme XML node contents don't get shown in JSON
233         $data = array('xmlns:poll' => self::POLL_OBJECT,
234                       'question'   => $poll->question);
235         foreach ($poll->getOptions() as $i => $opt) {
236             $data['option' . ($i + 1)] = $opt;
237         }
238         $object->extra[] = array('poll:data', $data, '');
239         return $object;
240     }
241
242     /**
243      * @fixme WARNING WARNING WARNING parent class closes the final div that we
244      * open here, but we probably shouldn't open it here. Check parent class
245      * and Bookmark plugin for if that's right.
246      */
247     function showNotice($notice, $out)
248     {
249         $user = common_current_user();
250
251         // @hack we want regular rendering, then just add stuff after that
252         $nli = new NoticeListItem($notice, $out);
253         $nli->showNotice();
254
255         $out->elementStart('div', array('class' => 'entry-content poll-content'));
256         $poll = Poll::getByNotice($notice);
257         if ($poll) {
258             if ($user) {
259                 $profile = $user->getProfile();
260                 $response = $poll->getResponse($profile);
261                 if ($response) {
262                     // User has already responded; show the results.
263                     $form = new PollResultForm($poll, $out);
264                 } else {
265                     $form = new PollResponseForm($poll, $out);
266                 }
267                 $form->show();
268             }
269         } else {
270             $out->text('Poll data is missing');
271         }
272         $out->elementEnd('div');
273
274         // @fixme
275         $out->elementStart('div', array('class' => 'entry-content'));
276     }
277
278     function entryForm($out)
279     {
280         return new NewPollForm($out);
281     }
282
283     // @fixme is this from parent?
284     function tag()
285     {
286         return 'poll';
287     }
288
289     function appTitle()
290     {
291         return _m('Poll');
292     }
293 }