3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2011, StatusNet, Inc.
6 * A plugin to enable social-bookmarking functionality
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.
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.
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/>.
23 * @category PollPlugin
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/
31 if (!defined('STATUSNET')) {
36 * Poll plugin main class
38 * @category PollPlugin
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/
46 class PollPlugin extends MicroAppPlugin
48 const VERSION = '0.1';
50 // @fixme which domain should we use for these namespaces?
51 const POLL_OBJECT = 'http://apinamespace.org/activitystreams/object/poll';
52 const POLL_RESPONSE_OBJECT = 'http://apinamespace.org/activitystreams/object/poll-response';
55 * Database schema setup
60 * @return boolean hook value; true means continue processing, false means stop.
62 function onCheckSchema()
64 $schema = Schema::get();
65 $schema->ensureTable('poll', Poll::schemaDef());
66 $schema->ensureTable('poll_response', Poll_response::schemaDef());
71 * Show the CSS necessary for this plugin
73 * @param Action $action the action being run
75 * @return boolean hook value
77 function onEndShowStyles($action)
79 $action->cssLink($this->path('poll.css'));
84 * Load related modules when needed
86 * @param string $cls Name of the class to be loaded
88 * @return boolean hook value; true means continue processing, false means stop.
90 function onAutoload($cls)
92 $dir = dirname(__FILE__);
96 case 'ShowpollAction':
98 case 'RespondpollAction':
99 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
102 case 'Poll_response':
103 include_once $dir.'/'.$cls.'.php';
106 case 'PollResponseForm':
107 case 'PollResultForm':
108 include_once $dir.'/'.strtolower($cls).'.php';
116 * Map URLs to actions
118 * @param Net_URL_Mapper $m path-to-action mapper
120 * @return boolean hook value; true means continue processing, false means stop.
122 function onRouterInitialized($m)
124 $m->connect('main/poll/new',
125 array('action' => 'newpoll'));
127 $m->connect('main/poll/:id',
128 array('action' => 'showpoll'),
129 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
131 $m->connect('main/poll/response/:id',
132 array('action' => 'showpollresponse'),
133 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
135 $m->connect('main/poll/:id/respond',
136 array('action' => 'respondpoll'),
137 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
143 * Plugin version data
145 * @param array &$versions array of version data
149 function onPluginVersion(&$versions)
151 $versions[] = array('name' => 'Poll',
152 'version' => self::VERSION,
153 'author' => 'Brion Vibber',
154 'homepage' => 'http://status.net/wiki/Plugin:Poll',
156 // TRANS: Plugin description.
157 _m('Simple extension for supporting basic polls.'));
163 return array(self::POLL_OBJECT, self::POLL_RESPONSE_OBJECT);
167 * When a notice is deleted, delete the related Poll
169 * @param Notice $notice Notice being deleted
171 * @return boolean hook value
173 function deleteRelated($notice)
175 $p = Poll::getByNotice($notice);
185 * Save a poll from an activity
187 * @param Profile $profile Profile to use as author
188 * @param Activity $activity Activity to save
189 * @param array $options Options to pass to bookmark-saving code
191 * @return Notice resulting notice
193 function saveNoticeFromActivity($activity, $profile, $options=array())
196 common_log(LOG_DEBUG, "XXX activity: " . var_export($activity, true));
197 common_log(LOG_DEBUG, "XXX profile: " . var_export($profile, true));
198 common_log(LOG_DEBUG, "XXX options: " . var_export($options, true));
200 // Ok for now, we can grab stuff from the XML entry directly.
201 // This won't work when reading from JSON source
202 if ($activity->entry) {
203 $pollElements = $activity->entry->getElementsByTagNameNS(self::POLL_OBJECT, 'poll');
204 $responseElements = $activity->entry->getElementsByTagNameNS(self::POLL_OBJECT, 'response');
205 if ($pollElements->length) {
206 $data = $pollElements->item(0);
207 $question = $data->getAttribute('question');
209 foreach ($data->attributes as $node) {
210 $name = $node->nodeName;
211 if (substr($name, 0, 6) == 'option') {
212 $n = intval(substr($name, 6));
214 $opts[$n - 1] = $node->nodeValue;
218 common_log(LOG_DEBUG, "YYY question: $question");
219 common_log(LOG_DEBUG, "YYY opts: " . var_export($opts, true));
221 $notice = Poll::saveNew($profile, $question, $opts, $options);
222 common_log(LOG_DEBUG, "YYY ok: " . $notice->id);
224 } catch (Exception $e) {
225 common_log(LOG_DEBUG, "YYY fail: " . $e->getMessage());
227 } else if ($responseElements->length) {
228 $data = $responseElements->item(0);
229 $pollUri = $data->getAttribute('poll');
230 $selection = intval($data->getAttribute('selection'));
233 // TRANS: Exception thrown trying to respond to a poll without a poll reference.
234 throw new Exception(_m('Invalid poll response: no poll reference.'));
236 $poll = Poll::staticGet('uri', $pollUri);
238 // TRANS: Exception thrown trying to respond to a non-existing poll.
239 throw new Exception(_m('Invalid poll response: poll is unknown.'));
242 $notice = Poll_response::saveNew($profile, $poll, $selection, $options);
243 common_log(LOG_DEBUG, "YYY response ok: " . $notice->id);
245 } catch (Exception $e) {
246 common_log(LOG_DEBUG, "YYY response fail: " . $e->getMessage());
249 common_log(LOG_DEBUG, "YYY no poll data");
254 function activityObjectFromNotice($notice)
256 assert($this->isMyNotice($notice));
258 switch ($notice->object_type) {
259 case self::POLL_OBJECT:
260 return $this->activityObjectFromNoticePoll($notice);
261 case self::POLL_RESPONSE_OBJECT:
262 return $this->activityObjectFromNoticePollResponse($notice);
264 // TRANS: Exception thrown when performing an unexpected action on a poll.
265 // TRANS: %s is the unpexpected object type.
266 throw new Exception(sprintf(_m('Unexpected type for poll plugin: %s.'), $notice->object_type));
270 function activityObjectFromNoticePollResponse($notice)
272 $object = new ActivityObject();
273 $object->id = $notice->uri;
274 $object->type = self::POLL_OBJECT;
275 $object->title = $notice->content;
276 $object->summary = $notice->content;
277 $object->link = $notice->bestUrl();
279 $response = Poll_response::getByNotice($notice);
281 common_log(LOG_DEBUG, "QQQ notice uri: $notice->uri");
283 $poll = $response->getPoll();
285 * For the moment, using a kind of icky-looking schema that happens to
286 * work with out code for generating both Atom and JSON forms, though
289 * <poll:response xmlns:poll="http://apinamespace.org/activitystreams/object/poll"
290 * poll="http://..../poll/...."
294 * "xmlns:poll": http://apinamespace.org/activitystreams/object/poll
295 * "uri": "http://..../poll/...."
300 // @fixme there's no way to specify an XML node tree here, like <poll><option/><option/></poll>
301 // @fixme there's no way to specify a JSON array or multi-level tree unless you break the XML attribs
302 // @fixme XML node contents don't get shown in JSON
303 $data = array('xmlns:poll' => self::POLL_OBJECT,
304 'poll' => $poll->uri,
305 'selection' => intval($response->selection));
306 $object->extra[] = array('poll:response', $data, '');
311 function activityObjectFromNoticePoll($notice)
313 $object = new ActivityObject();
314 $object->id = $notice->uri;
315 $object->type = self::POLL_RESPONSE_OBJECT;
316 $object->title = $notice->content;
317 $object->summary = $notice->content;
318 $object->link = $notice->bestUrl();
320 $poll = Poll::getByNotice($notice);
322 * Adding the poll-specific data. There's no standard in AS for polls,
323 * so we're making stuff up.
325 * For the moment, using a kind of icky-looking schema that happens to
326 * work with out code for generating both Atom and JSON forms, though
329 * <poll:data xmlns:poll="http://apinamespace.org/activitystreams/object/poll"
330 * question="Who wants a poll question?"
331 * option1="Option one"
332 * option2="Option two"
333 * option3="Option three"></poll:data>
336 * "xmlns:poll": http://apinamespace.org/activitystreams/object/poll
337 * "question": "Who wants a poll question?"
338 * "option1": "Option one"
339 * "option2": "Option two"
340 * "option3": "Option three"
344 // @fixme there's no way to specify an XML node tree here, like <poll><option/><option/></poll>
345 // @fixme there's no way to specify a JSON array or multi-level tree unless you break the XML attribs
346 // @fixme XML node contents don't get shown in JSON
347 $data = array('xmlns:poll' => self::POLL_OBJECT,
348 'question' => $poll->question);
349 foreach ($poll->getOptions() as $i => $opt) {
350 $data['option' . ($i + 1)] = $opt;
352 $object->extra[] = array('poll:poll', $data, '');
357 * @fixme WARNING WARNING WARNING parent class closes the final div that we
358 * open here, but we probably shouldn't open it here. Check parent class
359 * and Bookmark plugin for if that's right.
361 function showNotice($notice, $out)
363 switch ($notice->object_type) {
364 case self::POLL_OBJECT:
365 return $this->showNoticePoll($notice, $out);
366 case self::POLL_RESPONSE_OBJECT:
367 return $this->showNoticePollResponse($notice, $out);
369 // TRANS: Exception thrown when performing an unexpected action on a poll.
370 // TRANS: %s is the unpexpected object type.
371 throw new Exception(sprintf(_m('Unexpected type for poll plugin: %s.'), $notice->object_type));
375 function showNoticePoll($notice, $out)
377 $user = common_current_user();
379 // @hack we want regular rendering, then just add stuff after that
380 $nli = new NoticeListItem($notice, $out);
383 $out->elementStart('div', array('class' => 'entry-content poll-content'));
384 $poll = Poll::getByNotice($notice);
387 $profile = $user->getProfile();
388 $response = $poll->getResponse($profile);
390 // User has already responded; show the results.
391 $form = new PollResultForm($poll, $out);
393 $form = new PollResponseForm($poll, $out);
398 $out->text(_('Poll data is missing'));
400 $out->elementEnd('div');
403 $out->elementStart('div', array('class' => 'entry-content'));
406 function showNoticePollResponse($notice, $out)
408 $user = common_current_user();
410 // @hack we want regular rendering, then just add stuff after that
411 $nli = new NoticeListItem($notice, $out);
415 $out->elementStart('div', array('class' => 'entry-content'));
418 function entryForm($out)
420 return new NewPollForm($out);
423 // @fixme is this from parent?
431 // TRANS: Application title.
432 return _m('APPTITLE','Poll');