PollPlugin.php 16 KB

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