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 * Set up our tables (question and answer)
55 * @return boolean hook value; true means continue processing, false means stop.
57 function onCheckSchema()
59 $schema = Schema::get();
61 $schema->ensureTable('qna_question', QnA_Question::schemaDef());
62 $schema->ensureTable('qna_answer', QnA_Answer::schemaDef());
63 $schema->ensureTable('qna_vote', QnA_Vote::schemaDef());
68 public function newFormAction() {
69 return 'qnanewquestion';
75 * @param Net_URL_Mapper $m path-to-action mapper
77 * @return boolean hook value; true means continue processing, false means stop.
80 function onRouterInitialized($m)
82 $UUIDregex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
85 'main/qna/newquestion',
86 array('action' => 'qnanewquestion')
89 'answer/qna/closequestion',
90 array('action' => 'qnaclosequestion')
94 array('action' => 'qnanewanswer')
97 'main/qna/reviseanswer',
98 array('action' => 'qnareviseanswer')
102 array('action' => 'qnavote', 'type' => 'question'),
103 array('id' => $UUIDregex)
107 array('action' => 'qnashowquestion'),
108 array('id' => $UUIDregex)
112 array('action' => 'qnavote', 'type' => 'answer'),
113 array('id' => $UUIDregex)
117 array('action' => 'qnashowanswer'),
118 array('id' => $UUIDregex)
124 function onPluginVersion(&$versions)
128 'version' => GNUSOCIAL_VERSION,
129 'author' => 'Zach Copley',
130 'homepage' => 'http://status.net/wiki/Plugin:QnA',
132 // TRANS: Plugin description.
133 _m('Question and Answers micro-app.')
138 function appTitle() {
139 // TRANS: Application title.
140 return _m('TITLE','Question');
149 QnA_Question::OBJECT_TYPE,
150 QnA_Answer::OBJECT_TYPE
155 * Given a parsed ActivityStreams activity, save it into a notice
156 * and other data structures.
158 * @param Activity $activity
159 * @param Profile $actor
160 * @param array $options=array()
162 * @return Notice the resulting notice
164 function saveNoticeFromActivity($activity, $actor, $options=array())
166 if (count($activity->objects) != 1) {
167 // TRANS: Exception thrown when there are too many activity objects.
168 throw new Exception(_m('Too many activity objects.'));
171 $questionObj = $activity->objects[0];
173 if ($questionObj->type != QnA_Question::OBJECT_TYPE) {
174 // TRANS: Exception thrown when an incorrect object type is encountered.
175 throw new Exception(_m('Wrong type for object.'));
180 switch ($activity->verb) {
181 case ActivityVerb::POST:
182 $notice = QnA_Question::saveNew(
185 $questionObj->summary,
189 case Answer::ObjectType:
190 $question = QnA_Question::getKV('uri', $questionObj->id);
191 if (empty($question)) {
192 // FIXME: save the question
193 // TRANS: Exception thrown when answering a non-existing question.
194 throw new Exception(_m('Answer to unknown question.'));
196 $notice = QnA_Answer::saveNew($actor, $question, $options);
199 // TRANS: Exception thrown when an object type is encountered that cannot be handled.
200 throw new Exception(_m('Unknown object type.'));
207 * Turn a Notice into an activity object
209 * @param Notice $notice
211 * @return ActivityObject
214 function activityObjectFromNotice($notice)
218 switch ($notice->object_type) {
219 case QnA_Question::OBJECT_TYPE:
220 $question = QnA_Question::fromNotice($notice);
222 case QnA_Answer::OBJECT_TYPE:
223 $answer = QnA_Answer::fromNotice($notice);
224 $question = $answer->getQuestion();
228 if (empty($question)) {
229 // TRANS: Exception thrown when an object type is encountered that cannot be handled.
230 throw new Exception(_m('Unknown object type.'));
233 $notice = $question->getNotice();
235 if (empty($notice)) {
236 // TRANS: Exception thrown when requesting a non-existing question notice.
237 throw new Exception(_m('Unknown question notice.'));
240 $obj = new ActivityObject();
242 $obj->id = $question->uri;
243 $obj->type = QnA_Question::OBJECT_TYPE;
244 $obj->title = $question->title;
245 $obj->link = $notice->getUrl();
247 // XXX: probably need other stuff here
253 * Output our CSS class for QnA notice list elements
255 * @param NoticeListItem $nli The item being shown
257 * @return boolean hook value
260 function onStartOpenNoticeListItemElement($nli)
262 $type = $nli->notice->object_type;
266 case QnA_Question::OBJECT_TYPE:
267 $id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
268 $class = 'hentry notice question';
269 if ($nli->notice->scope != 0 && $nli->notice->scope != 1) {
270 $class .= ' limited-scope';
273 $question = QnA_Question::getKV('uri', $nli->notice->uri);
275 if (!empty($question->closed)) {
279 $nli->out->elementStart(
282 'id' => 'notice-' . $id
285 Event::handle('EndOpenNoticeListItemElement', array($nli));
288 case QnA_Answer::OBJECT_TYPE:
289 $id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
291 $cls = array('hentry', 'notice', 'answer');
293 $answer = QnA_Answer::getKV('uri', $nli->notice->uri);
295 if (!empty($answer) && !empty($answer->best)) {
299 $nli->out->elementStart(
302 'class' => implode(' ', $cls),
303 'id' => 'notice-' . $id
306 Event::handle('EndOpenNoticeListItemElement', array($nli));
317 * Custom HTML output for our notices
319 * @param Notice $notice
320 * @param HTMLOutputter $out
322 function showNotice(Notice $notice, $out)
324 switch ($notice->object_type) {
325 case QnA_Question::OBJECT_TYPE:
326 return $this->showNoticeQuestion($notice, $out);
327 case QnA_Answer::OBJECT_TYPE:
328 return $this->showNoticeAnswer($notice, $out);
331 // TRANS: Exception thrown when performing an unexpected action on a question.
332 // TRANS: %s is the unpexpected object type.
333 sprintf(_m('Unexpected type for QnA plugin: %s.'),
340 function showNoticeQuestion(Notice $notice, $out)
342 $user = common_current_user();
344 // @hack we want regular rendering, then just add stuff after that
345 $nli = new NoticeListItem($notice, $out);
348 $out->elementStart('div', array('class' => 'entry-content question-description'));
350 $question = QnA_Question::getByNotice($notice);
352 if (!empty($question)) {
354 $form = new QnashowquestionForm($out, $question);
358 // TRANS: Error message displayed when question data is not present.
359 $out->text(_m('Question data is missing.'));
361 $out->elementEnd('div');
364 $out->elementStart('div', array('class' => 'entry-content'));
368 * Output the HTML for this kind of object in a list
370 * @param NoticeListItem $nli The list item being shown.
372 * @return boolean hook value
374 * @todo FIXME: WARNING WARNING WARNING this closes a 'div' that is implicitly opened in BookmarkPlugin's showNotice implementation
376 function onStartShowNoticeItem($nli)
378 if (!$this->isMyNotice($nli->notice)) {
383 $notice = $nli->notice;
385 $this->showNotice($notice, $out);
387 $nli->showNoticeLink();
388 $nli->showNoticeSource();
389 $nli->showNoticeLocation();
393 $out->elementEnd('div');
395 $nli->showNoticeOptions();
397 if ($notice->object_type == QnA_Question::OBJECT_TYPE) {
398 $user = common_current_user();
399 $question = QnA_Question::getByNotice($notice);
401 if (!empty($user) and !empty($question)) {
402 $profile = $user->getProfile();
403 $answer = $question->getAnswer($profile);
405 // Output a placeholder input -- clicking on it will
406 // bring up a real answer form
408 // NOTE: this whole ul is just a placeholder
409 if (empty($question->closed) && empty($answer)) {
410 $out->elementStart('ul', 'notices qna-dummy');
411 $out->elementStart('li', 'qna-dummy-placeholder');
415 'class' => 'placeholder',
416 // TRANS: Placeholder value for a possible answer to a question
417 // TRANS: by the logged in user.
418 'value' => _m('Your answer...')
421 $out->elementEnd('li');
422 $out->elementEnd('ul');
430 function showNoticeAnswer(Notice $notice, $out)
432 $user = common_current_user();
434 $answer = QnA_Answer::getByNotice($notice);
435 $question = $answer->getQuestion();
437 $nli = new NoticeListItem($notice, $out);
440 $out->elementStart('div', array('class' => 'entry-content answer-content'));
442 if (!empty($answer)) {
443 $form = new QnashowanswerForm($out, $answer);
446 // TRANS: Error message displayed when answer data is not present.
447 $out->text(_m('Answer data is missing.'));
450 $out->elementEnd('div');
453 $out->elementStart('div', array('class' => 'entry-content'));
456 static function shorten($content, $notice)
460 if (Notice::contentTooLong($content)) {
461 common_debug("content too long");
462 $max = Notice::maxContent();
463 // TRANS: Link description for link to full notice text if it is longer than
464 // TRANS: what will be dispplayed.
466 $short = mb_substr($content, 0, $max - 1);
467 $short .= sprintf('<a href="%1$s" rel="more" title="%2$s">%3$s</a>',
469 // TRANS: Title for link that is an ellipsis in English.
482 * @param HTMLOutputter $out
485 function entryForm($out)
487 return new QnanewquestionForm($out);
491 * When a notice is deleted, clean up related tables.
493 * @param Notice $notice
495 function deleteRelated($notice)
497 switch ($notice->object_type) {
498 case QnA_Question::OBJECT_TYPE:
499 common_log(LOG_DEBUG, "Deleting question from notice...");
500 $question = QnA_Question::fromNotice($notice);
503 case QnA_Answer::OBJECT_TYPE:
504 common_log(LOG_DEBUG, "Deleting answer from notice...");
505 $answer = QnA_Answer::fromNotice($notice);
506 common_log(LOG_DEBUG, "to delete: $answer->id");
510 common_log(LOG_DEBUG, "Not deleting related, wtf...");
514 function onEndShowScripts($action)
516 $action->script($this->path('js/qna.js'));
520 function onEndShowStyles($action)
522 $action->cssLink($this->path('css/qna.css'));