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. var $oldSaveNew = true;
  48. /**
  49. * Set up our tables (question and answer)
  50. *
  51. * @see Schema
  52. * @see ColumnDef
  53. *
  54. * @return boolean hook value; true means continue processing, false means stop.
  55. */
  56. function onCheckSchema()
  57. {
  58. $schema = Schema::get();
  59. $schema->ensureTable('qna_question', QnA_Question::schemaDef());
  60. $schema->ensureTable('qna_answer', QnA_Answer::schemaDef());
  61. $schema->ensureTable('qna_vote', QnA_Vote::schemaDef());
  62. return true;
  63. }
  64. public function newFormAction() {
  65. return 'qnanewquestion';
  66. }
  67. /**
  68. * Map URLs to actions
  69. *
  70. * @param URLMapper $m path-to-action mapper
  71. *
  72. * @return boolean hook value; true means continue processing, false means stop.
  73. */
  74. public function onRouterInitialized(URLMapper $m)
  75. {
  76. $UUIDregex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
  77. $m->connect(
  78. 'main/qna/newquestion',
  79. array('action' => 'qnanewquestion')
  80. );
  81. $m->connect(
  82. 'answer/qna/closequestion',
  83. array('action' => 'qnaclosequestion')
  84. );
  85. $m->connect(
  86. 'main/qna/newanswer',
  87. array('action' => 'qnanewanswer')
  88. );
  89. $m->connect(
  90. 'main/qna/reviseanswer',
  91. array('action' => 'qnareviseanswer')
  92. );
  93. $m->connect(
  94. 'question/vote/:id',
  95. array('action' => 'qnavote', 'type' => 'question'),
  96. array('id' => $UUIDregex)
  97. );
  98. $m->connect(
  99. 'question/:id',
  100. array('action' => 'qnashowquestion'),
  101. array('id' => $UUIDregex)
  102. );
  103. $m->connect(
  104. 'answer/vote/:id',
  105. array('action' => 'qnavote', 'type' => 'answer'),
  106. array('id' => $UUIDregex)
  107. );
  108. $m->connect(
  109. 'answer/:id',
  110. array('action' => 'qnashowanswer'),
  111. array('id' => $UUIDregex)
  112. );
  113. return true;
  114. }
  115. function onPluginVersion(&$versions)
  116. {
  117. $versions[] = array(
  118. 'name' => 'QnA',
  119. 'version' => GNUSOCIAL_VERSION,
  120. 'author' => 'Zach Copley',
  121. 'homepage' => 'http://status.net/wiki/Plugin:QnA',
  122. 'description' =>
  123. // TRANS: Plugin description.
  124. _m('Question and Answers micro-app.')
  125. );
  126. return true;
  127. }
  128. function appTitle() {
  129. // TRANS: Application title.
  130. return _m('TITLE','Question');
  131. }
  132. function tag() {
  133. return 'question';
  134. }
  135. function types() {
  136. return array(
  137. QnA_Question::OBJECT_TYPE,
  138. QnA_Answer::OBJECT_TYPE
  139. );
  140. }
  141. /**
  142. * Given a parsed ActivityStreams activity, save it into a notice
  143. * and other data structures.
  144. *
  145. * @param Activity $activity
  146. * @param Profile $actor
  147. * @param array $options=array()
  148. *
  149. * @return Notice the resulting notice
  150. */
  151. function saveNoticeFromActivity(Activity $activity, Profile $actor, array $options=array())
  152. {
  153. if (count($activity->objects) != 1) {
  154. // TRANS: Exception thrown when there are too many activity objects.
  155. throw new Exception(_m('Too many activity objects.'));
  156. }
  157. $questionObj = $activity->objects[0];
  158. if ($questionObj->type != QnA_Question::OBJECT_TYPE) {
  159. // TRANS: Exception thrown when an incorrect object type is encountered.
  160. throw new Exception(_m('Wrong type for object.'));
  161. }
  162. $notice = null;
  163. switch ($activity->verb) {
  164. case ActivityVerb::POST:
  165. $notice = QnA_Question::saveNew(
  166. $actor,
  167. $questionObj->title,
  168. $questionObj->summary,
  169. $options
  170. );
  171. break;
  172. case Answer::ObjectType:
  173. $question = QnA_Question::getKV('uri', $questionObj->id);
  174. if (empty($question)) {
  175. // FIXME: save the question
  176. // TRANS: Exception thrown when answering a non-existing question.
  177. throw new Exception(_m('Answer to unknown question.'));
  178. }
  179. $notice = QnA_Answer::saveNew($actor, $question, $options);
  180. break;
  181. default:
  182. // TRANS: Exception thrown when an object type is encountered that cannot be handled.
  183. throw new Exception(_m('Unknown object type.'));
  184. }
  185. return $notice;
  186. }
  187. /**
  188. * Turn a Notice into an activity object
  189. *
  190. * @param Notice $notice
  191. *
  192. * @return ActivityObject
  193. */
  194. function activityObjectFromNotice(Notice $notice)
  195. {
  196. $question = null;
  197. switch ($notice->object_type) {
  198. case QnA_Question::OBJECT_TYPE:
  199. $question = QnA_Question::fromNotice($notice);
  200. break;
  201. case QnA_Answer::OBJECT_TYPE:
  202. $answer = QnA_Answer::fromNotice($notice);
  203. $question = $answer->getQuestion();
  204. break;
  205. }
  206. if (empty($question)) {
  207. // TRANS: Exception thrown when an object type is encountered that cannot be handled.
  208. throw new Exception(_m('Unknown object type.'));
  209. }
  210. $notice = $question->getNotice();
  211. if (empty($notice)) {
  212. // TRANS: Exception thrown when requesting a non-existing question notice.
  213. throw new Exception(_m('Unknown question notice.'));
  214. }
  215. $obj = new ActivityObject();
  216. $obj->id = $question->uri;
  217. $obj->type = QnA_Question::OBJECT_TYPE;
  218. $obj->title = $question->title;
  219. $obj->link = $notice->getUrl();
  220. // XXX: probably need other stuff here
  221. return $obj;
  222. }
  223. /**
  224. * Output our CSS class for QnA notice list elements
  225. *
  226. * @param NoticeListItem $nli The item being shown
  227. *
  228. * @return boolean hook value
  229. */
  230. function onStartOpenNoticeListItemElement($nli)
  231. {
  232. $type = $nli->notice->object_type;
  233. switch($type)
  234. {
  235. case QnA_Question::OBJECT_TYPE:
  236. $id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
  237. $class = 'h-entry notice question';
  238. if ($nli->notice->scope != 0 && $nli->notice->scope != 1) {
  239. $class .= ' limited-scope';
  240. }
  241. $question = QnA_Question::getKV('uri', $nli->notice->uri);
  242. if (!empty($question->closed)) {
  243. $class .= ' closed';
  244. }
  245. $nli->out->elementStart(
  246. 'li', array(
  247. 'class' => $class,
  248. 'id' => 'notice-' . $id
  249. )
  250. );
  251. Event::handle('EndOpenNoticeListItemElement', array($nli));
  252. return false;
  253. break;
  254. case QnA_Answer::OBJECT_TYPE:
  255. $id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
  256. $cls = array('h-entry', 'notice', 'answer');
  257. $answer = QnA_Answer::getKV('uri', $nli->notice->uri);
  258. if (!empty($answer) && !empty($answer->best)) {
  259. $cls[] = 'best';
  260. }
  261. $nli->out->elementStart(
  262. 'li',
  263. array(
  264. 'class' => implode(' ', $cls),
  265. 'id' => 'notice-' . $id
  266. )
  267. );
  268. Event::handle('EndOpenNoticeListItemElement', array($nli));
  269. return false;
  270. break;
  271. default:
  272. return true;
  273. }
  274. return true;
  275. }
  276. /**
  277. * Output the HTML for this kind of object in a list
  278. *
  279. * @param NoticeListItem $nli The list item being shown.
  280. *
  281. * @return boolean hook value
  282. *
  283. * @todo FIXME: WARNING WARNING WARNING this closes a 'div' that is implicitly opened in BookmarkPlugin's showNotice implementation
  284. */
  285. function onStartShowNoticeItem($nli)
  286. {
  287. if (!$this->isMyNotice($nli->notice)) {
  288. return true;
  289. }
  290. $out = $nli->out;
  291. $notice = $nli->notice;
  292. $nli->showNotice($notice, $out);
  293. $nli->showNoticeLink();
  294. $nli->showNoticeSource();
  295. $nli->showNoticeLocation();
  296. $nli->showPermalink();
  297. $nli->showRepeat();
  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. }