QnAPlugin.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Microapp plugin for Questions and Answers
  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 QnA
  24. * @package StatusNet
  25. * @author Zach Copley <zach@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. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Question and Answer plugin
  37. *
  38. * @category Plugin
  39. * @package StatusNet
  40. * @author Zach Copley <zach@status.net>
  41. * @copyright 2011 StatusNet, Inc.
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  43. * @link http://status.net/
  44. */
  45. class QnAPlugin extends MicroAppPlugin
  46. {
  47. const PLUGIN_VERSION = '2.0.0';
  48. var $oldSaveNew = true;
  49. /**
  50. * Set up our tables (question and answer)
  51. *
  52. * @see Schema
  53. * @see ColumnDef
  54. *
  55. * @return boolean hook value; true means continue processing, false means stop.
  56. */
  57. function onCheckSchema()
  58. {
  59. $schema = Schema::get();
  60. $schema->ensureTable('qna_question', QnA_Question::schemaDef());
  61. $schema->ensureTable('qna_answer', QnA_Answer::schemaDef());
  62. $schema->ensureTable('qna_vote', QnA_Vote::schemaDef());
  63. return true;
  64. }
  65. public function newFormAction() {
  66. return 'qnanewquestion';
  67. }
  68. /**
  69. * Map URLs to actions
  70. *
  71. * @param URLMapper $m path-to-action mapper
  72. *
  73. * @return boolean hook value; true means continue processing, false means stop.
  74. */
  75. public function onRouterInitialized(URLMapper $m)
  76. {
  77. $UUIDregex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
  78. $m->connect(
  79. 'main/qna/newquestion',
  80. array('action' => 'qnanewquestion')
  81. );
  82. $m->connect(
  83. 'answer/qna/closequestion',
  84. array('action' => 'qnaclosequestion')
  85. );
  86. $m->connect(
  87. 'main/qna/newanswer',
  88. array('action' => 'qnanewanswer')
  89. );
  90. $m->connect(
  91. 'main/qna/reviseanswer',
  92. array('action' => 'qnareviseanswer')
  93. );
  94. $m->connect(
  95. 'question/vote/:id',
  96. array('action' => 'qnavote', 'type' => 'question'),
  97. array('id' => $UUIDregex)
  98. );
  99. $m->connect(
  100. 'question/:id',
  101. array('action' => 'qnashowquestion'),
  102. array('id' => $UUIDregex)
  103. );
  104. $m->connect(
  105. 'answer/vote/:id',
  106. array('action' => 'qnavote', 'type' => 'answer'),
  107. array('id' => $UUIDregex)
  108. );
  109. $m->connect(
  110. 'answer/:id',
  111. array('action' => 'qnashowanswer'),
  112. array('id' => $UUIDregex)
  113. );
  114. return true;
  115. }
  116. function onPluginVersion(array &$versions)
  117. {
  118. $versions[] = array(
  119. 'name' => 'QnA',
  120. 'version' => self::PLUGIN_VERSION,
  121. 'author' => 'Zach Copley',
  122. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/QnA',
  123. 'description' =>
  124. // TRANS: Plugin description.
  125. _m('Question and Answers micro-app.')
  126. );
  127. return true;
  128. }
  129. function appTitle() {
  130. // TRANS: Application title.
  131. return _m('TITLE','Question');
  132. }
  133. function tag() {
  134. return 'question';
  135. }
  136. function types() {
  137. return array(
  138. QnA_Question::OBJECT_TYPE,
  139. QnA_Answer::OBJECT_TYPE
  140. );
  141. }
  142. /**
  143. * Given a parsed ActivityStreams activity, save it into a notice
  144. * and other data structures.
  145. *
  146. * @param Activity $activity
  147. * @param Profile $actor
  148. * @param array $options=array()
  149. *
  150. * @return Notice the resulting notice
  151. */
  152. function saveNoticeFromActivity(Activity $activity, Profile $actor, array $options=array())
  153. {
  154. if (count($activity->objects) != 1) {
  155. // TRANS: Exception thrown when there are too many activity objects.
  156. throw new Exception(_m('Too many activity objects.'));
  157. }
  158. $questionObj = $activity->objects[0];
  159. if ($questionObj->type != QnA_Question::OBJECT_TYPE) {
  160. // TRANS: Exception thrown when an incorrect object type is encountered.
  161. throw new Exception(_m('Wrong type for object.'));
  162. }
  163. $notice = null;
  164. switch ($activity->verb) {
  165. case ActivityVerb::POST:
  166. $notice = QnA_Question::saveNew(
  167. $actor,
  168. $questionObj->title,
  169. $questionObj->summary,
  170. $options
  171. );
  172. break;
  173. case Answer::ObjectType:
  174. $question = QnA_Question::getKV('uri', $questionObj->id);
  175. if (empty($question)) {
  176. // FIXME: save the question
  177. // TRANS: Exception thrown when answering a non-existing question.
  178. throw new Exception(_m('Answer to unknown question.'));
  179. }
  180. $notice = QnA_Answer::saveNew($actor, $question, $options);
  181. break;
  182. default:
  183. // TRANS: Exception thrown when an object type is encountered that cannot be handled.
  184. throw new Exception(_m('Unknown object type.'));
  185. }
  186. return $notice;
  187. }
  188. /**
  189. * Turn a Notice into an activity object
  190. *
  191. * @param Notice $notice
  192. *
  193. * @return ActivityObject
  194. */
  195. function activityObjectFromNotice(Notice $notice)
  196. {
  197. $question = null;
  198. switch ($notice->object_type) {
  199. case QnA_Question::OBJECT_TYPE:
  200. $question = QnA_Question::fromNotice($notice);
  201. break;
  202. case QnA_Answer::OBJECT_TYPE:
  203. $answer = QnA_Answer::fromNotice($notice);
  204. $question = $answer->getQuestion();
  205. break;
  206. }
  207. if (empty($question)) {
  208. // TRANS: Exception thrown when an object type is encountered that cannot be handled.
  209. throw new Exception(_m('Unknown object type.'));
  210. }
  211. $notice = $question->getNotice();
  212. if (empty($notice)) {
  213. // TRANS: Exception thrown when requesting a non-existing question notice.
  214. throw new Exception(_m('Unknown question notice.'));
  215. }
  216. $obj = new ActivityObject();
  217. $obj->id = $question->uri;
  218. $obj->type = QnA_Question::OBJECT_TYPE;
  219. $obj->title = $question->title;
  220. $obj->link = $notice->getUrl();
  221. // XXX: probably need other stuff here
  222. return $obj;
  223. }
  224. /**
  225. * Output our CSS class for QnA notice list elements
  226. *
  227. * @param NoticeListItem $nli The item being shown
  228. *
  229. * @return boolean hook value
  230. */
  231. function onStartOpenNoticeListItemElement(NoticeListItem $nli)
  232. {
  233. $type = $nli->notice->object_type;
  234. switch($type)
  235. {
  236. case QnA_Question::OBJECT_TYPE:
  237. $id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
  238. $class = 'h-entry notice question';
  239. if ($nli->notice->scope != 0 && $nli->notice->scope != 1) {
  240. $class .= ' limited-scope';
  241. }
  242. $question = QnA_Question::getKV('uri', $nli->notice->uri);
  243. if (!empty($question->closed)) {
  244. $class .= ' closed';
  245. }
  246. $nli->out->elementStart(
  247. 'li', array(
  248. 'class' => $class,
  249. 'id' => 'notice-' . $id
  250. )
  251. );
  252. Event::handle('EndOpenNoticeListItemElement', array($nli));
  253. return false;
  254. break;
  255. case QnA_Answer::OBJECT_TYPE:
  256. $id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
  257. $cls = array('h-entry', 'notice', 'answer');
  258. $answer = QnA_Answer::getKV('uri', $nli->notice->uri);
  259. if (!empty($answer) && !empty($answer->best)) {
  260. $cls[] = 'best';
  261. }
  262. $nli->out->elementStart(
  263. 'li',
  264. array(
  265. 'class' => implode(' ', $cls),
  266. 'id' => 'notice-' . $id
  267. )
  268. );
  269. Event::handle('EndOpenNoticeListItemElement', array($nli));
  270. return false;
  271. break;
  272. default:
  273. return true;
  274. }
  275. return true;
  276. }
  277. /**
  278. * Output the HTML for this kind of object in a list
  279. *
  280. * @param NoticeListItem $nli The list item being shown.
  281. *
  282. * @return boolean hook value
  283. *
  284. * @todo FIXME: WARNING WARNING WARNING this closes a 'div' that is implicitly opened in BookmarkPlugin's showNotice implementation
  285. */
  286. function onStartShowNoticeItem(NoticeListItem $nli)
  287. {
  288. if (!$this->isMyNotice($nli->notice)) {
  289. return true;
  290. }
  291. $out = $nli->out;
  292. $notice = $nli->notice;
  293. $nli->showNotice($notice, $out);
  294. $nli->showNoticeLink();
  295. $nli->showNoticeSource();
  296. $nli->showNoticeLocation();
  297. $nli->showPermalink();
  298. $nli->showNoticeOptions();
  299. if ($notice->object_type == QnA_Question::OBJECT_TYPE) {
  300. $user = common_current_user();
  301. $question = QnA_Question::getByNotice($notice);
  302. if (!empty($user) and !empty($question)) {
  303. $profile = $user->getProfile();
  304. $answer = $question->getAnswer($profile);
  305. // Output a placeholder input -- clicking on it will
  306. // bring up a real answer form
  307. // NOTE: this whole ul is just a placeholder
  308. if (empty($question->closed) && empty($answer)) {
  309. $out->elementStart('ul', 'notices qna-dummy');
  310. $out->elementStart('li', 'qna-dummy-placeholder');
  311. $out->element(
  312. 'input',
  313. array(
  314. 'class' => 'placeholder',
  315. // TRANS: Placeholder value for a possible answer to a question
  316. // TRANS: by the logged in user.
  317. 'value' => _m('Your answer...')
  318. )
  319. );
  320. $out->elementEnd('li');
  321. $out->elementEnd('ul');
  322. }
  323. }
  324. }
  325. return false;
  326. }
  327. function adaptNoticeListItem($nli) {
  328. return new QnAListItem($nli);
  329. }
  330. static function shorten($content, $notice)
  331. {
  332. $short = null;
  333. if (Notice::contentTooLong($content)) {
  334. common_debug("content too long");
  335. $max = Notice::maxContent();
  336. // TRANS: Link description for link to full notice text if it is longer than
  337. // TRANS: what will be dispplayed.
  338. $ellipsis = _m('…');
  339. $short = mb_substr($content, 0, $max - 1);
  340. $short .= sprintf('<a href="%1$s" rel="more" title="%2$s">%3$s</a>',
  341. $notice->getUrl(),
  342. // TRANS: Title for link that is an ellipsis in English.
  343. _m('more...'),
  344. $ellipsis);
  345. } else {
  346. $short = $content;
  347. }
  348. return $short;
  349. }
  350. /**
  351. * Form for our app
  352. *
  353. * @param HTMLOutputter $out
  354. * @return Widget
  355. */
  356. function entryForm($out)
  357. {
  358. return new QnanewquestionForm($out);
  359. }
  360. /**
  361. * When a notice is deleted, clean up related tables.
  362. *
  363. * @param Notice $notice
  364. */
  365. function deleteRelated(Notice $notice)
  366. {
  367. switch ($notice->object_type) {
  368. case QnA_Question::OBJECT_TYPE:
  369. common_log(LOG_DEBUG, "Deleting question from notice...");
  370. $question = QnA_Question::fromNotice($notice);
  371. $question->delete();
  372. break;
  373. case QnA_Answer::OBJECT_TYPE:
  374. common_log(LOG_DEBUG, "Deleting answer from notice...");
  375. $answer = QnA_Answer::fromNotice($notice);
  376. common_log(LOG_DEBUG, "to delete: $answer->id");
  377. $answer->delete();
  378. break;
  379. default:
  380. common_log(LOG_DEBUG, "Not deleting related, wtf...");
  381. }
  382. }
  383. function onEndShowScripts($action)
  384. {
  385. $action->script($this->path('js/qna.js'));
  386. return true;
  387. }
  388. function onEndShowStyles($action)
  389. {
  390. $action->cssLink($this->path('css/qna.css'));
  391. return true;
  392. }
  393. }