QnAPlugin.php 13 KB

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