PollPlugin.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. if (!defined('STATUSNET')) {
  31. exit(1);
  32. }
  33. /**
  34. * Poll plugin main class
  35. *
  36. * @category PollPlugin
  37. * @package StatusNet
  38. * @author Brion Vibber <brionv@status.net>
  39. * @author Evan Prodromou <evan@status.net>
  40. * @copyright 2011 StatusNet, Inc.
  41. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  42. * @link http://status.net/
  43. */
  44. class PollPlugin extends MicroAppPlugin
  45. {
  46. const PLUGIN_VERSION = '0.1.1';
  47. // @fixme which domain should we use for these namespaces?
  48. const POLL_OBJECT = 'http://activityschema.org/object/poll';
  49. const POLL_RESPONSE_OBJECT = 'http://activityschema.org/object/poll-response';
  50. public $oldSaveNew = true;
  51. /**
  52. * Database schema setup
  53. *
  54. * @return boolean hook value; true means continue processing, false means stop.
  55. * @see ColumnDef
  56. *
  57. * @see Schema
  58. */
  59. public function onCheckSchema()
  60. {
  61. $schema = Schema::get();
  62. $schema->ensureTable('poll', Poll::schemaDef());
  63. $schema->ensureTable('poll_response', Poll_response::schemaDef());
  64. $schema->ensureTable('user_poll_prefs', User_poll_prefs::schemaDef());
  65. return true;
  66. }
  67. /**
  68. * Show the CSS necessary for this plugin
  69. *
  70. * @param Action $action the action being run
  71. *
  72. * @return boolean hook value
  73. */
  74. public function onEndShowStyles($action)
  75. {
  76. $action->cssLink($this->path('css/poll.css'));
  77. return true;
  78. }
  79. /**
  80. * Map URLs to actions
  81. *
  82. * @param URLMapper $m path-to-action mapper
  83. *
  84. * @return boolean hook value; true means continue processing, false means stop.
  85. */
  86. public function onRouterInitialized(URLMapper $m)
  87. {
  88. $m->connect(
  89. 'main/poll/new',
  90. array('action' => 'newpoll')
  91. );
  92. $m->connect(
  93. 'main/poll/:id',
  94. array('action' => 'showpoll'),
  95. array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}')
  96. );
  97. $m->connect(
  98. 'main/poll/response/:id',
  99. array('action' => 'showpollresponse'),
  100. array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}')
  101. );
  102. $m->connect(
  103. 'main/poll/:id/respond',
  104. array('action' => 'respondpoll'),
  105. array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}')
  106. );
  107. $m->connect(
  108. 'settings/poll',
  109. array('action' => 'pollsettings')
  110. );
  111. return true;
  112. }
  113. /**
  114. * Plugin version data
  115. *
  116. * @param array &$versions array of version data
  117. *
  118. * @return bool true hook value
  119. * @throws Exception
  120. */
  121. public function onPluginVersion(array &$versions)
  122. {
  123. $versions[] = array('name' => 'Poll',
  124. 'version' => self::PLUGIN_VERSION,
  125. 'author' => 'Brion Vibber',
  126. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/Poll',
  127. 'rawdescription' =>
  128. // TRANS: Plugin description.
  129. _m('Simple extension for supporting basic polls.'));
  130. return true;
  131. }
  132. public function types()
  133. {
  134. return array(self::POLL_OBJECT, self::POLL_RESPONSE_OBJECT);
  135. }
  136. /**
  137. * When a notice is deleted, delete the related Poll
  138. *
  139. * @param Notice $notice Notice being deleted
  140. *
  141. * @return boolean hook value
  142. */
  143. public function deleteRelated(Notice $notice)
  144. {
  145. $p = Poll::getByNotice($notice);
  146. if (!empty($p)) {
  147. $p->delete();
  148. }
  149. return true;
  150. }
  151. /**
  152. * Save a poll from an activity
  153. *
  154. * @param Activity $activity Activity to save
  155. * @param Profile $profile Profile to use as author
  156. * @param array $options Options to pass to bookmark-saving code
  157. *
  158. * @return Notice resulting notice
  159. * @throws Exception if it failed
  160. */
  161. public function saveNoticeFromActivity(Activity $activity, Profile $profile, array $options = array())
  162. {
  163. // @fixme
  164. common_log(LOG_DEBUG, "XXX activity: " . var_export($activity, true));
  165. common_log(LOG_DEBUG, "XXX profile: " . var_export($profile, true));
  166. common_log(LOG_DEBUG, "XXX options: " . var_export($options, true));
  167. // Ok for now, we can grab stuff from the XML entry directly.
  168. // This won't work when reading from JSON source
  169. if ($activity->entry) {
  170. $pollElements = $activity->entry->getElementsByTagNameNS(self::POLL_OBJECT, 'poll');
  171. $responseElements = $activity->entry->getElementsByTagNameNS(self::POLL_OBJECT, 'response');
  172. if ($pollElements->length) {
  173. $question = '';
  174. $opts = [];
  175. $data = $pollElements->item(0);
  176. foreach ($data->getElementsByTagNameNS(self::POLL_OBJECT, 'question') as $node) {
  177. $question = $node->textContent;
  178. }
  179. foreach ($data->getElementsByTagNameNS(self::POLL_OBJECT, 'option') as $node) {
  180. $opts[] = $node->textContent;
  181. }
  182. try {
  183. $notice = Poll::saveNew($profile, $question, $opts, $options);
  184. common_log(LOG_DEBUG, "Saved Poll from ActivityStream data ok: notice id " . $notice->id);
  185. return $notice;
  186. } catch (Exception $e) {
  187. common_log(LOG_DEBUG, "Poll save from ActivityStream data failed: " . $e->getMessage());
  188. }
  189. } elseif ($responseElements->length) {
  190. $data = $responseElements->item(0);
  191. $pollUri = $data->getAttribute('poll');
  192. $selection = intval($data->getAttribute('selection'));
  193. if (!$pollUri) {
  194. // TRANS: Exception thrown trying to respond to a poll without a poll reference.
  195. throw new Exception(_m('Invalid poll response: No poll reference.'));
  196. }
  197. $poll = Poll::getKV('uri', $pollUri);
  198. if (!$poll) {
  199. // TRANS: Exception thrown trying to respond to a non-existing poll.
  200. throw new Exception(_m('Invalid poll response: Poll is unknown.'));
  201. }
  202. try {
  203. $notice = Poll_response::saveNew($profile, $poll, $selection, $options);
  204. common_log(LOG_DEBUG, "Saved Poll_response ok, notice id: " . $notice->id);
  205. return $notice;
  206. } catch (Exception $e) {
  207. common_log(LOG_DEBUG, "Poll response save fail: " . $e->getMessage());
  208. // TRANS: Exception thrown trying to respond to a non-existing poll.
  209. }
  210. } else {
  211. common_log(LOG_DEBUG, "YYY no poll data");
  212. }
  213. }
  214. // If it didn't return before
  215. throw new ServerException(_m('Failed to save Poll response.'));
  216. }
  217. public function activityObjectFromNotice(Notice $notice)
  218. {
  219. assert($this->isMyNotice($notice));
  220. switch ($notice->object_type) {
  221. case self::POLL_OBJECT:
  222. return $this->activityObjectFromNoticePoll($notice);
  223. case self::POLL_RESPONSE_OBJECT:
  224. return $this->activityObjectFromNoticePollResponse($notice);
  225. default:
  226. // TRANS: Exception thrown when performing an unexpected action on a poll.
  227. // TRANS: %s is the unexpected object type.
  228. throw new Exception(sprintf(_m('Unexpected type for poll plugin: %s.'), $notice->object_type));
  229. }
  230. }
  231. public function activityObjectFromNoticePollResponse(Notice $notice)
  232. {
  233. $object = new ActivityObject();
  234. $object->id = $notice->uri;
  235. $object->type = self::POLL_RESPONSE_OBJECT;
  236. $object->title = $notice->content;
  237. $object->summary = $notice->content;
  238. $object->link = $notice->getUrl();
  239. $response = Poll_response::getByNotice($notice);
  240. if ($response) {
  241. $poll = $response->getPoll();
  242. if ($poll) {
  243. // Stash data to be formatted later by
  244. // $this->activityObjectOutputAtom() or
  245. // $this->activityObjectOutputJson()...
  246. $object->pollSelection = intval($response->selection);
  247. $object->pollUri = $poll->uri;
  248. }
  249. }
  250. return $object;
  251. }
  252. public function activityObjectFromNoticePoll(Notice $notice)
  253. {
  254. $object = new ActivityObject();
  255. $object->id = $notice->uri;
  256. $object->type = self::POLL_OBJECT;
  257. $object->title = $notice->content;
  258. $object->summary = $notice->content;
  259. $object->link = $notice->getUrl();
  260. $poll = Poll::getByNotice($notice);
  261. if ($poll) {
  262. // Stash data to be formatted later by
  263. // $this->activityObjectOutputAtom() or
  264. // $this->activityObjectOutputJson()...
  265. $object->pollQuestion = $poll->question;
  266. $object->pollOptions = $poll->getOptions();
  267. }
  268. return $object;
  269. }
  270. /**
  271. * Called when generating Atom XML ActivityStreams output from an
  272. * ActivityObject belonging to this plugin. Gives the plugin
  273. * a chance to add custom output.
  274. *
  275. * Note that you can only add output of additional XML elements,
  276. * not change existing stuff here.
  277. *
  278. * If output is already handled by the base Activity classes,
  279. * you can leave this base implementation as a no-op.
  280. *
  281. * @param ActivityObject $obj
  282. * @param XMLOutputter $out to add elements at end of object
  283. */
  284. public function activityObjectOutputAtom(ActivityObject $obj, XMLOutputter $out)
  285. {
  286. if (isset($obj->pollQuestion)) {
  287. /**
  288. * <poll:poll xmlns:poll="http://apinamespace.org/activitystreams/object/poll">
  289. * <poll:question>Who wants a poll question?</poll:question>
  290. * <poll:option>Option one</poll:option>
  291. * <poll:option>Option two</poll:option>
  292. * <poll:option>Option three</poll:option>
  293. * </poll:poll>
  294. */
  295. $data = array('xmlns:poll' => self::POLL_OBJECT);
  296. $out->elementStart('poll:poll', $data);
  297. $out->element('poll:question', array(), $obj->pollQuestion);
  298. foreach ($obj->pollOptions as $opt) {
  299. $out->element('poll:option', array(), $opt);
  300. }
  301. $out->elementEnd('poll:poll');
  302. }
  303. if (isset($obj->pollSelection)) {
  304. /**
  305. * <poll:response xmlns:poll="http://apinamespace.org/activitystreams/object/poll">
  306. * poll="http://..../poll/...."
  307. * selection="3" />
  308. */
  309. $data = array('xmlns:poll' => self::POLL_OBJECT,
  310. 'poll' => $obj->pollUri,
  311. 'selection' => $obj->pollSelection);
  312. $out->element('poll:response', $data, '');
  313. }
  314. }
  315. /**
  316. * Called when generating JSON ActivityStreams output from an
  317. * ActivityObject belonging to this plugin. Gives the plugin
  318. * a chance to add custom output.
  319. *
  320. * Modify the array contents to your heart's content, and it'll
  321. * all get serialized out as JSON.
  322. *
  323. * If output is already handled by the base Activity classes,
  324. * you can leave this base implementation as a no-op.
  325. *
  326. * @param ActivityObject $obj
  327. * @param array &$out JSON-targeted array which can be modified
  328. */
  329. public function activityObjectOutputJson(ActivityObject $obj, array &$out)
  330. {
  331. common_log(LOG_DEBUG, 'QQQ: ' . var_export($obj, true));
  332. if (isset($obj->pollQuestion)) {
  333. /**
  334. * "poll": {
  335. * "question": "Who wants a poll question?",
  336. * "options": [
  337. * "Option 1",
  338. * "Option 2",
  339. * "Option 3"
  340. * ]
  341. * }
  342. */
  343. $data = array('question' => $obj->pollQuestion,
  344. 'options' => array());
  345. foreach ($obj->pollOptions as $opt) {
  346. $data['options'][] = $opt;
  347. }
  348. $out['poll'] = $data;
  349. }
  350. if (isset($obj->pollSelection)) {
  351. /**
  352. * "pollResponse": {
  353. * "poll": "http://..../poll/....",
  354. * "selection": 3
  355. * }
  356. */
  357. $data = array('poll' => $obj->pollUri,
  358. 'selection' => $obj->pollSelection);
  359. $out['pollResponse'] = $data;
  360. }
  361. }
  362. public function entryForm($out)
  363. {
  364. return new NewPollForm($out);
  365. }
  366. // @fixme is this from parent?
  367. public function tag()
  368. {
  369. return 'poll';
  370. }
  371. public function appTitle()
  372. {
  373. // TRANS: Application title.
  374. return _m('APPTITLE', 'Poll');
  375. }
  376. public function onStartAddNoticeReply($nli, $parent, $child)
  377. {
  378. // Filter out any poll responses
  379. if ($parent->object_type == self::POLL_OBJECT &&
  380. $child->object_type == self::POLL_RESPONSE_OBJECT) {
  381. return false;
  382. }
  383. return true;
  384. }
  385. // Hide poll responses for @chuck
  386. public function onEndNoticeWhoGets($notice, &$ni)
  387. {
  388. if ($notice->object_type == self::POLL_RESPONSE_OBJECT) {
  389. foreach ($ni as $id => $source) {
  390. $user = User::getKV('id', $id);
  391. if (!empty($user)) {
  392. $pollPrefs = User_poll_prefs::getKV('user_id', $user->id);
  393. if (!empty($pollPrefs) && ($pollPrefs->hide_responses)) {
  394. unset($ni[$id]);
  395. }
  396. }
  397. }
  398. }
  399. return true;
  400. }
  401. /**
  402. * Menu item for personal subscriptions/groups area
  403. *
  404. * @param Action $action action being executed
  405. *
  406. * @return boolean hook return
  407. */
  408. public function onEndAccountSettingsNav($action)
  409. {
  410. $action_name = $action->trimmed('action');
  411. $action->menuItem(
  412. common_local_url('pollsettings'),
  413. // TRANS: Poll plugin menu item on user settings page.
  414. _m('MENU', 'Polls'),
  415. // TRANS: Poll plugin tooltip for user settings menu item.
  416. _m('Configure poll behavior'),
  417. $action_name === 'pollsettings'
  418. );
  419. return true;
  420. }
  421. protected function showNoticeContent(Notice $stored, HTMLOutputter $out, Profile $scoped = null)
  422. {
  423. if ($stored->object_type == self::POLL_RESPONSE_OBJECT) {
  424. parent::showNoticeContent($stored, $out, $scoped);
  425. return;
  426. }
  427. // If the stored notice is a POLL_OBJECT
  428. $poll = Poll::getByNotice($stored);
  429. if ($poll instanceof Poll) {
  430. if (!$scoped instanceof Profile || $poll->getResponse($scoped) instanceof Poll_response) {
  431. // Either the user is not logged in or it has already responded; show the results.
  432. $form = new PollResultForm($poll, $out);
  433. } else {
  434. $form = new PollResponseForm($poll, $out);
  435. }
  436. $form->show();
  437. } else {
  438. // TRANS: Error text displayed if no poll data could be found.
  439. $out->text(_m('Poll data is missing'));
  440. }
  441. }
  442. }