3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2011, StatusNet, Inc.
6 * Microapp plugin for Questions and Answers
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.
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.
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/>.
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/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
38 * Question and Answer plugin
42 * @author Zach Copley <zach@status.net>
43 * @copyright 2011 StatusNet, Inc.
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45 * @link http://status.net/
47 class QnAPlugin extends MicroAppPlugin
50 var $oldSaveNew = true;
53 * Set up our tables (question and answer)
58 * @return boolean hook value; true means continue processing, false means stop.
60 function onCheckSchema()
62 $schema = Schema::get();
64 $schema->ensureTable('qna_question', QnA_Question::schemaDef());
65 $schema->ensureTable('qna_answer', QnA_Answer::schemaDef());
66 $schema->ensureTable('qna_vote', QnA_Vote::schemaDef());
71 public function newFormAction() {
72 return 'qnanewquestion';
78 * @param Net_URL_Mapper $m path-to-action mapper
80 * @return boolean hook value; true means continue processing, false means stop.
83 function onRouterInitialized($m)
85 $UUIDregex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
88 'main/qna/newquestion',
89 array('action' => 'qnanewquestion')
92 'answer/qna/closequestion',
93 array('action' => 'qnaclosequestion')
97 array('action' => 'qnanewanswer')
100 'main/qna/reviseanswer',
101 array('action' => 'qnareviseanswer')
105 array('action' => 'qnavote', 'type' => 'question'),
106 array('id' => $UUIDregex)
110 array('action' => 'qnashowquestion'),
111 array('id' => $UUIDregex)
115 array('action' => 'qnavote', 'type' => 'answer'),
116 array('id' => $UUIDregex)
120 array('action' => 'qnashowanswer'),
121 array('id' => $UUIDregex)
127 function onPluginVersion(&$versions)
131 'version' => GNUSOCIAL_VERSION,
132 'author' => 'Zach Copley',
133 'homepage' => 'http://status.net/wiki/Plugin:QnA',
135 // TRANS: Plugin description.
136 _m('Question and Answers micro-app.')
141 function appTitle() {
142 // TRANS: Application title.
143 return _m('TITLE','Question');
152 QnA_Question::OBJECT_TYPE,
153 QnA_Answer::OBJECT_TYPE
158 * Given a parsed ActivityStreams activity, save it into a notice
159 * and other data structures.
161 * @param Activity $activity
162 * @param Profile $actor
163 * @param array $options=array()
165 * @return Notice the resulting notice
167 function saveNoticeFromActivity(Activity $activity, Profile $actor, array $options=array())
169 if (count($activity->objects) != 1) {
170 // TRANS: Exception thrown when there are too many activity objects.
171 throw new Exception(_m('Too many activity objects.'));
174 $questionObj = $activity->objects[0];
176 if ($questionObj->type != QnA_Question::OBJECT_TYPE) {
177 // TRANS: Exception thrown when an incorrect object type is encountered.
178 throw new Exception(_m('Wrong type for object.'));
183 switch ($activity->verb) {
184 case ActivityVerb::POST:
185 $notice = QnA_Question::saveNew(
188 $questionObj->summary,
192 case Answer::ObjectType:
193 $question = QnA_Question::getKV('uri', $questionObj->id);
194 if (empty($question)) {
195 // FIXME: save the question
196 // TRANS: Exception thrown when answering a non-existing question.
197 throw new Exception(_m('Answer to unknown question.'));
199 $notice = QnA_Answer::saveNew($actor, $question, $options);
202 // TRANS: Exception thrown when an object type is encountered that cannot be handled.
203 throw new Exception(_m('Unknown object type.'));
210 * Turn a Notice into an activity object
212 * @param Notice $notice
214 * @return ActivityObject
217 function activityObjectFromNotice(Notice $notice)
221 switch ($notice->object_type) {
222 case QnA_Question::OBJECT_TYPE:
223 $question = QnA_Question::fromNotice($notice);
225 case QnA_Answer::OBJECT_TYPE:
226 $answer = QnA_Answer::fromNotice($notice);
227 $question = $answer->getQuestion();
231 if (empty($question)) {
232 // TRANS: Exception thrown when an object type is encountered that cannot be handled.
233 throw new Exception(_m('Unknown object type.'));
236 $notice = $question->getNotice();
238 if (empty($notice)) {
239 // TRANS: Exception thrown when requesting a non-existing question notice.
240 throw new Exception(_m('Unknown question notice.'));
243 $obj = new ActivityObject();
245 $obj->id = $question->uri;
246 $obj->type = QnA_Question::OBJECT_TYPE;
247 $obj->title = $question->title;
248 $obj->link = $notice->getUrl();
250 // XXX: probably need other stuff here
256 * Output our CSS class for QnA notice list elements
258 * @param NoticeListItem $nli The item being shown
260 * @return boolean hook value
263 function onStartOpenNoticeListItemElement($nli)
265 $type = $nli->notice->object_type;
269 case QnA_Question::OBJECT_TYPE:
270 $id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
271 $class = 'h-entry notice question';
272 if ($nli->notice->scope != 0 && $nli->notice->scope != 1) {
273 $class .= ' limited-scope';
276 $question = QnA_Question::getKV('uri', $nli->notice->uri);
278 if (!empty($question->closed)) {
282 $nli->out->elementStart(
285 'id' => 'notice-' . $id
288 Event::handle('EndOpenNoticeListItemElement', array($nli));
291 case QnA_Answer::OBJECT_TYPE:
292 $id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
294 $cls = array('h-entry', 'notice', 'answer');
296 $answer = QnA_Answer::getKV('uri', $nli->notice->uri);
298 if (!empty($answer) && !empty($answer->best)) {
302 $nli->out->elementStart(
305 'class' => implode(' ', $cls),
306 'id' => 'notice-' . $id
309 Event::handle('EndOpenNoticeListItemElement', array($nli));
320 * Custom HTML output for our notices
322 * @param Notice $notice
323 * @param HTMLOutputter $out
325 function showNoticeContent(Notice $notice, $out)
327 switch ($notice->object_type) {
328 case QnA_Question::OBJECT_TYPE:
329 return $this->showNoticeQuestion($notice, $out);
330 case QnA_Answer::OBJECT_TYPE:
331 return $this->showNoticeAnswer($notice, $out);
334 // TRANS: Exception thrown when performing an unexpected action on a question.
335 // TRANS: %s is the unpexpected object type.
336 sprintf(_m('Unexpected type for QnA plugin: %s.'),
343 function showNoticeQuestion(Notice $notice, $out)
345 $user = common_current_user();
347 // @hack we want regular rendering, then just add stuff after that
348 $nli = new NoticeListItem($notice, $out);
351 $out->elementStart('div', array('class' => 'e-content question-description'));
353 $question = QnA_Question::getByNotice($notice);
355 if (!empty($question)) {
357 $form = new QnashowquestionForm($out, $question);
361 // TRANS: Error message displayed when question data is not present.
362 $out->text(_m('Question data is missing.'));
364 $out->elementEnd('div');
367 $out->elementStart('div', array('class' => 'e-content'));
371 * Output the HTML for this kind of object in a list
373 * @param NoticeListItem $nli The list item being shown.
375 * @return boolean hook value
377 * @todo FIXME: WARNING WARNING WARNING this closes a 'div' that is implicitly opened in BookmarkPlugin's showNotice implementation
379 function onStartShowNoticeItem($nli)
381 if (!$this->isMyNotice($nli->notice)) {
386 $notice = $nli->notice;
388 $this->showNotice($notice, $out);
390 $nli->showNoticeLink();
391 $nli->showNoticeSource();
392 $nli->showNoticeLocation();
396 $out->elementEnd('div');
398 $nli->showNoticeOptions();
400 if ($notice->object_type == QnA_Question::OBJECT_TYPE) {
401 $user = common_current_user();
402 $question = QnA_Question::getByNotice($notice);
404 if (!empty($user) and !empty($question)) {
405 $profile = $user->getProfile();
406 $answer = $question->getAnswer($profile);
408 // Output a placeholder input -- clicking on it will
409 // bring up a real answer form
411 // NOTE: this whole ul is just a placeholder
412 if (empty($question->closed) && empty($answer)) {
413 $out->elementStart('ul', 'notices qna-dummy');
414 $out->elementStart('li', 'qna-dummy-placeholder');
418 'class' => 'placeholder',
419 // TRANS: Placeholder value for a possible answer to a question
420 // TRANS: by the logged in user.
421 'value' => _m('Your answer...')
424 $out->elementEnd('li');
425 $out->elementEnd('ul');
433 function showNoticeAnswer(Notice $notice, $out)
435 $user = common_current_user();
437 $answer = QnA_Answer::getByNotice($notice);
438 $question = $answer->getQuestion();
440 $nli = new NoticeListItem($notice, $out);
443 $out->elementStart('div', array('class' => 'e-content answer-content'));
445 if (!empty($answer)) {
446 $form = new QnashowanswerForm($out, $answer);
449 // TRANS: Error message displayed when answer data is not present.
450 $out->text(_m('Answer data is missing.'));
453 $out->elementEnd('div');
456 $out->elementStart('div', array('class' => 'e-content'));
459 static function shorten($content, $notice)
463 if (Notice::contentTooLong($content)) {
464 common_debug("content too long");
465 $max = Notice::maxContent();
466 // TRANS: Link description for link to full notice text if it is longer than
467 // TRANS: what will be dispplayed.
469 $short = mb_substr($content, 0, $max - 1);
470 $short .= sprintf('<a href="%1$s" rel="more" title="%2$s">%3$s</a>',
472 // TRANS: Title for link that is an ellipsis in English.
485 * @param HTMLOutputter $out
488 function entryForm($out)
490 return new QnanewquestionForm($out);
494 * When a notice is deleted, clean up related tables.
496 * @param Notice $notice
498 function deleteRelated(Notice $notice)
500 switch ($notice->object_type) {
501 case QnA_Question::OBJECT_TYPE:
502 common_log(LOG_DEBUG, "Deleting question from notice...");
503 $question = QnA_Question::fromNotice($notice);
506 case QnA_Answer::OBJECT_TYPE:
507 common_log(LOG_DEBUG, "Deleting answer from notice...");
508 $answer = QnA_Answer::fromNotice($notice);
509 common_log(LOG_DEBUG, "to delete: $answer->id");
513 common_log(LOG_DEBUG, "Not deleting related, wtf...");
517 function onEndShowScripts($action)
519 $action->script($this->path('js/qna.js'));
523 function onEndShowStyles($action)
525 $action->cssLink($this->path('css/qna.css'));