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://activityschema.org/object/poll';
52 const POLL_RESPONSE_OBJECT = 'http://activityschema.org/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) {
209 $data = $pollElements->item(0);
210 foreach ($data->getElementsByTagNameNS(self::POLL_OBJECT, 'question') as $node) {
211 $question = $node->textContent;
213 foreach ($data->getElementsByTagNameNS(self::POLL_OBJECT, 'option') as $node) {
214 $opts[] = $node->textContent;
217 $notice = Poll::saveNew($profile, $question, $opts, $options);
218 common_log(LOG_DEBUG, "Saved Poll from ActivityStream data ok: notice id " . $notice->id);
220 } catch (Exception $e) {
221 common_log(LOG_DEBUG, "Poll save from ActivityStream data failed: " . $e->getMessage());
223 } else if ($responseElements->length) {
224 $data = $responseElements->item(0);
225 $pollUri = $data->getAttribute('poll');
226 $selection = intval($data->getAttribute('selection'));
229 // TRANS: Exception thrown trying to respond to a poll without a poll reference.
230 throw new Exception(_m('Invalid poll response: no poll reference.'));
232 $poll = Poll::staticGet('uri', $pollUri);
234 // TRANS: Exception thrown trying to respond to a non-existing poll.
235 throw new Exception(_m('Invalid poll response: poll is unknown.'));
238 $notice = Poll_response::saveNew($profile, $poll, $selection, $options);
239 common_log(LOG_DEBUG, "Saved Poll_response ok, notice id: " . $notice->id);
241 } catch (Exception $e) {
242 common_log(LOG_DEBUG, "Poll response save fail: " . $e->getMessage());
245 common_log(LOG_DEBUG, "YYY no poll data");
250 function activityObjectFromNotice($notice)
252 assert($this->isMyNotice($notice));
254 switch ($notice->object_type) {
255 case self::POLL_OBJECT:
256 return $this->activityObjectFromNoticePoll($notice);
257 case self::POLL_RESPONSE_OBJECT:
258 return $this->activityObjectFromNoticePollResponse($notice);
260 // TRANS: Exception thrown when performing an unexpected action on a poll.
261 // TRANS: %s is the unpexpected object type.
262 throw new Exception(sprintf(_m('Unexpected type for poll plugin: %s.'), $notice->object_type));
266 function activityObjectFromNoticePollResponse($notice)
268 $object = new ActivityObject();
269 $object->id = $notice->uri;
270 $object->type = self::POLL_RESPONSE_OBJECT;
271 $object->title = $notice->content;
272 $object->summary = $notice->content;
273 $object->link = $notice->bestUrl();
275 $response = Poll_response::getByNotice($notice);
277 $poll = $response->getPoll();
279 // Stash data to be formatted later by
280 // $this->activityObjectOutputAtom() or
281 // $this->activityObjectOutputJson()...
282 $object->pollSelection = intval($response->selection);
283 $object->pollUri = $poll->uri;
289 function activityObjectFromNoticePoll($notice)
291 $object = new ActivityObject();
292 $object->id = $notice->uri;
293 $object->type = self::POLL_OBJECT;
294 $object->title = $notice->content;
295 $object->summary = $notice->content;
296 $object->link = $notice->bestUrl();
298 $poll = Poll::getByNotice($notice);
300 // Stash data to be formatted later by
301 // $this->activityObjectOutputAtom() or
302 // $this->activityObjectOutputJson()...
303 $object->pollQuestion = $poll->question;
304 $object->pollOptions = $poll->getOptions();
311 * Called when generating Atom XML ActivityStreams output from an
312 * ActivityObject belonging to this plugin. Gives the plugin
313 * a chance to add custom output.
315 * Note that you can only add output of additional XML elements,
316 * not change existing stuff here.
318 * If output is already handled by the base Activity classes,
319 * you can leave this base implementation as a no-op.
321 * @param ActivityObject $obj
322 * @param XMLOutputter $out to add elements at end of object
324 function activityObjectOutputAtom(ActivityObject $obj, XMLOutputter $out)
326 if (isset($obj->pollQuestion)) {
328 * <poll:poll xmlns:poll="http://apinamespace.org/activitystreams/object/poll">
329 * <poll:question>Who wants a poll question?</poll:question>
330 * <poll:option>Option one</poll:option>
331 * <poll:option>Option two</poll:option>
332 * <poll:option>Option three</poll:option>
335 $data = array('xmlns:poll' => self::POLL_OBJECT);
336 $out->elementStart('poll:poll', $data);
337 $out->element('poll:question', array(), $obj->pollQuestion);
338 foreach ($obj->pollOptions as $opt) {
339 $out->element('poll:option', array(), $opt);
341 $out->elementEnd('poll:poll');
343 if (isset($obj->pollSelection)) {
345 * <poll:response xmlns:poll="http://apinamespace.org/activitystreams/object/poll">
346 * poll="http://..../poll/...."
349 $data = array('xmlns:poll' => self::POLL_OBJECT,
350 'poll' => $obj->pollUri,
351 'selection' => $obj->pollSelection);
352 $out->element('poll:response', $data, '');
357 * Called when generating JSON ActivityStreams output from an
358 * ActivityObject belonging to this plugin. Gives the plugin
359 * a chance to add custom output.
361 * Modify the array contents to your heart's content, and it'll
362 * all get serialized out as JSON.
364 * If output is already handled by the base Activity classes,
365 * you can leave this base implementation as a no-op.
367 * @param ActivityObject $obj
368 * @param array &$out JSON-targeted array which can be modified
370 public function activityObjectOutputJson(ActivityObject $obj, array &$out)
372 common_log(LOG_DEBUG, 'QQQ: ' . var_export($obj, true));
373 if (isset($obj->pollQuestion)) {
376 * "question": "Who wants a poll question?",
384 $data = array('question' => $obj->pollQuestion,
385 'options' => array());
386 foreach ($obj->pollOptions as $opt) {
387 $data['options'][] = $opt;
389 $out['poll'] = $data;
391 if (isset($obj->pollSelection)) {
394 * "poll": "http://..../poll/....",
398 $data = array('poll' => $obj->pollUri,
399 'selection' => $obj->pollSelection);
400 $out['pollResponse'] = $data;
406 * @fixme WARNING WARNING WARNING parent class closes the final div that we
407 * open here, but we probably shouldn't open it here. Check parent class
408 * and Bookmark plugin for if that's right.
410 function showNotice($notice, $out)
412 switch ($notice->object_type) {
413 case self::POLL_OBJECT:
414 return $this->showNoticePoll($notice, $out);
415 case self::POLL_RESPONSE_OBJECT:
416 return $this->showNoticePollResponse($notice, $out);
418 // TRANS: Exception thrown when performing an unexpected action on a poll.
419 // TRANS: %s is the unpexpected object type.
420 throw new Exception(sprintf(_m('Unexpected type for poll plugin: %s.'), $notice->object_type));
424 function showNoticePoll($notice, $out)
426 $user = common_current_user();
428 // @hack we want regular rendering, then just add stuff after that
429 $nli = new NoticeListItem($notice, $out);
432 $out->elementStart('div', array('class' => 'entry-content poll-content'));
433 $poll = Poll::getByNotice($notice);
436 $profile = $user->getProfile();
437 $response = $poll->getResponse($profile);
439 // User has already responded; show the results.
440 $form = new PollResultForm($poll, $out);
442 $form = new PollResponseForm($poll, $out);
447 $out->text(_('Poll data is missing'));
449 $out->elementEnd('div');
452 $out->elementStart('div', array('class' => 'entry-content'));
455 function showNoticePollResponse($notice, $out)
457 $user = common_current_user();
459 // @hack we want regular rendering, then just add stuff after that
460 $nli = new NoticeListItem($notice, $out);
464 $out->elementStart('div', array('class' => 'entry-content'));
467 function entryForm($out)
469 return new NewPollForm($out);
472 // @fixme is this from parent?
480 // TRANS: Application title.
481 return _m('APPTITLE','Poll');