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';
54 var $oldSaveNew = true;
57 * Database schema setup
62 * @return boolean hook value; true means continue processing, false means stop.
64 function onCheckSchema()
66 $schema = Schema::get();
67 $schema->ensureTable('poll', Poll::schemaDef());
68 $schema->ensureTable('poll_response', Poll_response::schemaDef());
69 $schema->ensureTable('user_poll_prefs', User_poll_prefs::schemaDef());
74 * Show the CSS necessary for this plugin
76 * @param Action $action the action being run
78 * @return boolean hook value
80 function onEndShowStyles($action)
82 $action->cssLink($this->path('css/poll.css'));
89 * @param URLMapper $m path-to-action mapper
91 * @return boolean hook value; true means continue processing, false means stop.
93 public function onRouterInitialized(URLMapper $m)
95 $m->connect('main/poll/new',
96 array('action' => 'newpoll'));
98 $m->connect('main/poll/:id',
99 array('action' => 'showpoll'),
100 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
102 $m->connect('main/poll/response/:id',
103 array('action' => 'showpollresponse'),
104 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
106 $m->connect('main/poll/:id/respond',
107 array('action' => 'respondpoll'),
108 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
110 $m->connect('settings/poll',
111 array('action' => 'pollsettings'));
117 * Plugin version data
119 * @param array &$versions array of version data
123 function onPluginVersion(array &$versions)
125 $versions[] = array('name' => 'Poll',
126 'version' => self::VERSION,
127 'author' => 'Brion Vibber',
128 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/Poll',
130 // TRANS: Plugin description.
131 _m('Simple extension for supporting basic polls.'));
137 return array(self::POLL_OBJECT, self::POLL_RESPONSE_OBJECT);
141 * When a notice is deleted, delete the related Poll
143 * @param Notice $notice Notice being deleted
145 * @return boolean hook value
147 function deleteRelated(Notice $notice)
149 $p = Poll::getByNotice($notice);
159 * Save a poll from an activity
161 * @param Profile $profile Profile to use as author
162 * @param Activity $activity Activity to save
163 * @param array $options Options to pass to bookmark-saving code
165 * @return Notice resulting notice
167 function saveNoticeFromActivity(Activity $activity, Profile $profile, array $options=array())
170 common_log(LOG_DEBUG, "XXX activity: " . var_export($activity, true));
171 common_log(LOG_DEBUG, "XXX profile: " . var_export($profile, true));
172 common_log(LOG_DEBUG, "XXX options: " . var_export($options, true));
174 // Ok for now, we can grab stuff from the XML entry directly.
175 // This won't work when reading from JSON source
176 if ($activity->entry) {
177 $pollElements = $activity->entry->getElementsByTagNameNS(self::POLL_OBJECT, 'poll');
178 $responseElements = $activity->entry->getElementsByTagNameNS(self::POLL_OBJECT, 'response');
179 if ($pollElements->length) {
183 $data = $pollElements->item(0);
184 foreach ($data->getElementsByTagNameNS(self::POLL_OBJECT, 'question') as $node) {
185 $question = $node->textContent;
187 foreach ($data->getElementsByTagNameNS(self::POLL_OBJECT, 'option') as $node) {
188 $opts[] = $node->textContent;
191 $notice = Poll::saveNew($profile, $question, $opts, $options);
192 common_log(LOG_DEBUG, "Saved Poll from ActivityStream data ok: notice id " . $notice->id);
194 } catch (Exception $e) {
195 common_log(LOG_DEBUG, "Poll save from ActivityStream data failed: " . $e->getMessage());
197 } else if ($responseElements->length) {
198 $data = $responseElements->item(0);
199 $pollUri = $data->getAttribute('poll');
200 $selection = intval($data->getAttribute('selection'));
203 // TRANS: Exception thrown trying to respond to a poll without a poll reference.
204 throw new Exception(_m('Invalid poll response: No poll reference.'));
206 $poll = Poll::getKV('uri', $pollUri);
208 // TRANS: Exception thrown trying to respond to a non-existing poll.
209 throw new Exception(_m('Invalid poll response: Poll is unknown.'));
212 $notice = Poll_response::saveNew($profile, $poll, $selection, $options);
213 common_log(LOG_DEBUG, "Saved Poll_response ok, notice id: " . $notice->id);
215 } catch (Exception $e) {
216 common_log(LOG_DEBUG, "Poll response save fail: " . $e->getMessage());
219 common_log(LOG_DEBUG, "YYY no poll data");
224 function activityObjectFromNotice(Notice $notice)
226 assert($this->isMyNotice($notice));
228 switch ($notice->object_type) {
229 case self::POLL_OBJECT:
230 return $this->activityObjectFromNoticePoll($notice);
231 case self::POLL_RESPONSE_OBJECT:
232 return $this->activityObjectFromNoticePollResponse($notice);
234 // TRANS: Exception thrown when performing an unexpected action on a poll.
235 // TRANS: %s is the unexpected object type.
236 throw new Exception(sprintf(_m('Unexpected type for poll plugin: %s.'), $notice->object_type));
240 function activityObjectFromNoticePollResponse(Notice $notice)
242 $object = new ActivityObject();
243 $object->id = $notice->uri;
244 $object->type = self::POLL_RESPONSE_OBJECT;
245 $object->title = $notice->content;
246 $object->summary = $notice->content;
247 $object->link = $notice->getUrl();
249 $response = Poll_response::getByNotice($notice);
251 $poll = $response->getPoll();
253 // Stash data to be formatted later by
254 // $this->activityObjectOutputAtom() or
255 // $this->activityObjectOutputJson()...
256 $object->pollSelection = intval($response->selection);
257 $object->pollUri = $poll->uri;
263 function activityObjectFromNoticePoll(Notice $notice)
265 $object = new ActivityObject();
266 $object->id = $notice->uri;
267 $object->type = self::POLL_OBJECT;
268 $object->title = $notice->content;
269 $object->summary = $notice->content;
270 $object->link = $notice->getUrl();
272 $poll = Poll::getByNotice($notice);
274 // Stash data to be formatted later by
275 // $this->activityObjectOutputAtom() or
276 // $this->activityObjectOutputJson()...
277 $object->pollQuestion = $poll->question;
278 $object->pollOptions = $poll->getOptions();
285 * Called when generating Atom XML ActivityStreams output from an
286 * ActivityObject belonging to this plugin. Gives the plugin
287 * a chance to add custom output.
289 * Note that you can only add output of additional XML elements,
290 * not change existing stuff here.
292 * If output is already handled by the base Activity classes,
293 * you can leave this base implementation as a no-op.
295 * @param ActivityObject $obj
296 * @param XMLOutputter $out to add elements at end of object
298 function activityObjectOutputAtom(ActivityObject $obj, XMLOutputter $out)
300 if (isset($obj->pollQuestion)) {
302 * <poll:poll xmlns:poll="http://apinamespace.org/activitystreams/object/poll">
303 * <poll:question>Who wants a poll question?</poll:question>
304 * <poll:option>Option one</poll:option>
305 * <poll:option>Option two</poll:option>
306 * <poll:option>Option three</poll:option>
309 $data = array('xmlns:poll' => self::POLL_OBJECT);
310 $out->elementStart('poll:poll', $data);
311 $out->element('poll:question', array(), $obj->pollQuestion);
312 foreach ($obj->pollOptions as $opt) {
313 $out->element('poll:option', array(), $opt);
315 $out->elementEnd('poll:poll');
317 if (isset($obj->pollSelection)) {
319 * <poll:response xmlns:poll="http://apinamespace.org/activitystreams/object/poll">
320 * poll="http://..../poll/...."
323 $data = array('xmlns:poll' => self::POLL_OBJECT,
324 'poll' => $obj->pollUri,
325 'selection' => $obj->pollSelection);
326 $out->element('poll:response', $data, '');
331 * Called when generating JSON ActivityStreams output from an
332 * ActivityObject belonging to this plugin. Gives the plugin
333 * a chance to add custom output.
335 * Modify the array contents to your heart's content, and it'll
336 * all get serialized out as JSON.
338 * If output is already handled by the base Activity classes,
339 * you can leave this base implementation as a no-op.
341 * @param ActivityObject $obj
342 * @param array &$out JSON-targeted array which can be modified
344 public function activityObjectOutputJson(ActivityObject $obj, array &$out)
346 common_log(LOG_DEBUG, 'QQQ: ' . var_export($obj, true));
347 if (isset($obj->pollQuestion)) {
350 * "question": "Who wants a poll question?",
358 $data = array('question' => $obj->pollQuestion,
359 'options' => array());
360 foreach ($obj->pollOptions as $opt) {
361 $data['options'][] = $opt;
363 $out['poll'] = $data;
365 if (isset($obj->pollSelection)) {
368 * "poll": "http://..../poll/....",
372 $data = array('poll' => $obj->pollUri,
373 'selection' => $obj->pollSelection);
374 $out['pollResponse'] = $data;
378 function entryForm($out)
380 return new NewPollForm($out);
383 // @fixme is this from parent?
391 // TRANS: Application title.
392 return _m('APPTITLE','Poll');
395 function onStartAddNoticeReply($nli, $parent, $child)
397 // Filter out any poll responses
398 if ($parent->object_type == self::POLL_OBJECT &&
399 $child->object_type == self::POLL_RESPONSE_OBJECT) {
405 // Hide poll responses for @chuck
407 function onEndNoticeWhoGets($notice, &$ni) {
408 if ($notice->object_type == self::POLL_RESPONSE_OBJECT) {
409 foreach ($ni as $id => $source) {
410 $user = User::getKV('id', $id);
412 $pollPrefs = User_poll_prefs::getKV('user_id', $user->id);
413 if (!empty($pollPrefs) && ($pollPrefs->hide_responses)) {
423 * Menu item for personal subscriptions/groups area
425 * @param Action $action action being executed
427 * @return boolean hook return
430 function onEndAccountSettingsNav($action)
432 $action_name = $action->trimmed('action');
434 $action->menuItem(common_local_url('pollsettings'),
435 // TRANS: Poll plugin menu item on user settings page.
437 // TRANS: Poll plugin tooltip for user settings menu item.
438 _m('Configure poll behavior'),
439 $action_name === 'pollsettings');
444 protected function showNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped=null)
446 if ($stored->object_type == self::POLL_RESPONSE_OBJECT) {
447 parent::showNoticeContent($stored, $out, $scoped);
451 // If the stored notice is a POLL_OBJECT
452 $poll = Poll::getByNotice($stored);
453 if ($poll instanceof Poll) {
454 if (!$scoped instanceof Profile || $poll->getResponse($scoped) instanceof Poll_response) {
455 // Either the user is not logged in or it has already responded; show the results.
456 $form = new PollResultForm($poll, $out);
458 $form = new PollResponseForm($poll, $out);
462 // TRANS: Error text displayed if no poll data could be found.
463 $out->text(_m('Poll data is missing'));