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/
47 class PollPlugin extends MicroAppPlugin
49 const VERSION = '0.1';
51 // @fixme which domain should we use for these namespaces?
52 const POLL_OBJECT = 'http://apinamespace.org/activitystreams/object/poll';
53 const POLL_RESPONSE_OBJECT = 'http://apinamespace.org/activitystreams/object/poll-response';
56 * Database schema setup
61 * @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());
73 * Show the CSS necessary for this plugin
75 * @param Action $action the action being run
77 * @return boolean hook value
80 function onEndShowStyles($action)
82 $action->cssLink($this->path('poll.css'));
87 * Load related modules when needed
89 * @param string $cls Name of the class to be loaded
91 * @return boolean hook value; true means continue processing, false means stop.
94 function onAutoload($cls)
96 $dir = dirname(__FILE__);
100 case 'ShowpollAction':
101 case 'NewpollAction':
102 case 'RespondpollAction':
103 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
106 case 'Poll_response':
107 include_once $dir.'/'.$cls.'.php';
110 case 'PollResponseForm':
111 case 'PollResultForm':
112 include_once $dir.'/'.strtolower($cls).'.php';
120 * Map URLs to actions
122 * @param Net_URL_Mapper $m path-to-action mapper
124 * @return boolean hook value; true means continue processing, false means stop.
127 function onRouterInitialized($m)
129 $m->connect('main/poll/new',
130 array('action' => 'newpoll'));
132 $m->connect('main/poll/:id',
133 array('action' => 'showpoll'),
134 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
136 $m->connect('main/poll/response/:id',
137 array('action' => 'showpollresponse'),
138 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
140 $m->connect('main/poll/:id/respond',
141 array('action' => 'respondpoll'),
142 array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
148 * Plugin version data
150 * @param array &$versions array of version data
155 function onPluginVersion(&$versions)
157 $versions[] = array('name' => 'Poll',
158 'version' => self::VERSION,
159 'author' => 'Brion Vibber',
160 'homepage' => 'http://status.net/wiki/Plugin:Poll',
162 _m('Simple extension for supporting basic polls.'));
168 return array(self::POLL_OBJECT, self::POLL_RESPONSE_OBJECT);
172 * When a notice is deleted, delete the related Poll
174 * @param Notice $notice Notice being deleted
176 * @return boolean hook value
179 function deleteRelated($notice)
181 $p = Poll::getByNotice($notice);
191 * Save a poll from an activity
193 * @param Profile $profile Profile to use as author
194 * @param Activity $activity Activity to save
195 * @param array $options Options to pass to bookmark-saving code
197 * @return Notice resulting notice
200 function saveNoticeFromActivity($activity, $profile, $options=array())
203 common_log(LOG_DEBUG, "XXX activity: " . var_export($activity, true));
204 common_log(LOG_DEBUG, "XXX profile: " . var_export($profile, true));
205 common_log(LOG_DEBUG, "XXX options: " . var_export($options, true));
207 // Ok for now, we can grab stuff from the XML entry directly.
208 // This won't work when reading from JSON source
209 if ($activity->entry) {
210 $elements = $activity->entry->getElementsByTagNameNS(self::POLL_OBJECT, 'data');
211 if ($elements->length) {
212 $data = $elements->item(0);
213 $question = $data->getAttribute('question');
215 foreach ($data->attributes as $node) {
216 $name = $node->nodeName;
217 if (substr($name, 0, 6) == 'option') {
218 $n = intval(substr($name, 6));
220 $opts[$n - 1] = $node->nodeValue;
224 common_log(LOG_DEBUG, "YYY question: $question");
225 common_log(LOG_DEBUG, "YYY opts: " . var_export($opts, true));
227 $notice = Poll::saveNew($profile, $question, $opts, $options);
228 common_log(LOG_DEBUG, "YYY ok: " . $notice->id);
230 } catch (Exception $e) {
231 common_log(LOG_DEBUG, "YYY fail: " . $e->getMessage());
234 common_log(LOG_DEBUG, "YYY no poll data");
239 function activityObjectFromNotice($notice)
241 assert($this->isMyNotice($notice));
243 $object = new ActivityObject();
244 $object->id = $notice->uri;
245 $object->type = self::POLL_OBJECT;
246 $object->title = 'Poll title';
247 $object->summary = 'Poll summary';
248 $object->link = $notice->bestUrl();
250 $poll = Poll::getByNotice($notice);
252 * Adding the poll-specific data. There's no standard in AS for polls,
253 * so we're making stuff up.
255 * For the moment, using a kind of icky-looking schema that happens to
256 * work with out code for generating both Atom and JSON forms, though
259 * <poll:data xmlns:poll="http://apinamespace.org/activitystreams/object/poll"
260 * question="Who wants a poll question?"
261 * option1="Option one"
262 * option2="Option two"
263 * option3="Option three"></poll:data>
266 * "xmlns:poll": http://apinamespace.org/activitystreams/object/poll
267 * "question": "Who wants a poll question?"
268 * "option1": "Option one"
269 * "option2": "Option two"
270 * "option3": "Option three"
274 // @fixme there's no way to specify an XML node tree here, like <poll><option/><option/></poll>
275 // @fixme there's no way to specify a JSON array or multi-level tree unless you break the XML attribs
276 // @fixme XML node contents don't get shown in JSON
277 $data = array('xmlns:poll' => self::POLL_OBJECT,
278 'question' => $poll->question);
279 foreach ($poll->getOptions() as $i => $opt) {
280 $data['option' . ($i + 1)] = $opt;
282 $object->extra[] = array('poll:data', $data, '');
287 * @fixme WARNING WARNING WARNING parent class closes the final div that we
288 * open here, but we probably shouldn't open it here. Check parent class
289 * and Bookmark plugin for if that's right.
291 function showNotice($notice, $out)
293 switch ($notice->object_type) {
294 case self::POLL_OBJECT:
295 return $this->showNoticePoll($notice, $out);
296 case self::POLL_RESPONSE_OBJECT:
297 return $this->showNoticePollResponse($notice, $out);
299 throw new Exception('Unexpected type for poll plugin: ' . $notice->object_type);
303 function showNoticePoll($notice, $out)
305 $user = common_current_user();
307 // @hack we want regular rendering, then just add stuff after that
308 $nli = new NoticeListItem($notice, $out);
311 $out->elementStart('div', array('class' => 'entry-content poll-content'));
312 $poll = Poll::getByNotice($notice);
315 $profile = $user->getProfile();
316 $response = $poll->getResponse($profile);
318 // User has already responded; show the results.
319 $form = new PollResultForm($poll, $out);
321 $form = new PollResponseForm($poll, $out);
326 $out->text('Poll data is missing');
328 $out->elementEnd('div');
331 $out->elementStart('div', array('class' => 'entry-content'));
334 function showNoticePollResponse($notice, $out)
336 $user = common_current_user();
338 // @hack we want regular rendering, then just add stuff after that
339 $nli = new NoticeListItem($notice, $out);
343 $out->elementStart('div', array('class' => 'entry-content'));
346 function entryForm($out)
348 return new NewPollForm($out);
351 // @fixme is this from parent?