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());
71 * @param Net_URL_Mapper $m path-to-action mapper
73 * @return boolean hook value; true means continue processing, false means stop.
76 function onRouterInitialized($m)
78 $UUIDregex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
81 'main/qna/newquestion',
82 array('action' => 'qnanewquestion')
85 'answer/qna/closequestion',
86 array('action' => 'qnaclosequestion')
90 array('action' => 'qnanewanswer')
93 'main/qna/reviseanswer',
94 array('action' => 'qnareviseanswer')
98 array('action' => 'qnavote', 'type' => 'question'),
99 array('id' => $UUIDregex)
103 array('action' => 'qnashowquestion'),
104 array('id' => $UUIDregex)
108 array('action' => 'qnavote', 'type' => 'answer'),
109 array('id' => $UUIDregex)
113 array('action' => 'qnashowanswer'),
114 array('id' => $UUIDregex)
120 function onPluginVersion(&$versions)
124 'version' => GNUSOCIAL_VERSION,
125 'author' => 'Zach Copley',
126 'homepage' => 'http://status.net/wiki/Plugin:QnA',
128 // TRANS: Plugin description.
129 _m('Question and Answers micro-app.')
134 function appTitle() {
135 // TRANS: Application title.
136 return _m('TITLE','Question');
145 QnA_Question::OBJECT_TYPE,
146 QnA_Answer::OBJECT_TYPE
151 * Given a parsed ActivityStreams activity, save it into a notice
152 * and other data structures.
154 * @param Activity $activity
155 * @param Profile $actor
156 * @param array $options=array()
158 * @return Notice the resulting notice
160 function saveNoticeFromActivity($activity, $actor, $options=array())
162 if (count($activity->objects) != 1) {
163 // TRANS: Exception thrown when there are too many activity objects.
164 throw new Exception(_m('Too many activity objects.'));
167 $questionObj = $activity->objects[0];
169 if ($questionObj->type != QnA_Question::OBJECT_TYPE) {
170 // TRANS: Exception thrown when an incorrect object type is encountered.
171 throw new Exception(_m('Wrong type for object.'));
176 switch ($activity->verb) {
177 case ActivityVerb::POST:
178 $notice = QnA_Question::saveNew(
181 $questionObj->summary,
185 case Answer::ObjectType:
186 $question = QnA_Question::getKV('uri', $questionObj->id);
187 if (empty($question)) {
188 // FIXME: save the question
189 // TRANS: Exception thrown when answering a non-existing question.
190 throw new Exception(_m('Answer to unknown question.'));
192 $notice = QnA_Answer::saveNew($actor, $question, $options);
195 // TRANS: Exception thrown when an object type is encountered that cannot be handled.
196 throw new Exception(_m('Unknown object type.'));
203 * Turn a Notice into an activity object
205 * @param Notice $notice
207 * @return ActivityObject
210 function activityObjectFromNotice($notice)
214 switch ($notice->object_type) {
215 case QnA_Question::OBJECT_TYPE:
216 $question = QnA_Question::fromNotice($notice);
218 case QnA_Answer::OBJECT_TYPE:
219 $answer = QnA_Answer::fromNotice($notice);
220 $question = $answer->getQuestion();
224 if (empty($question)) {
225 // TRANS: Exception thrown when an object type is encountered that cannot be handled.
226 throw new Exception(_m('Unknown object type.'));
229 $notice = $question->getNotice();
231 if (empty($notice)) {
232 // TRANS: Exception thrown when requesting a non-existing question notice.
233 throw new Exception(_m('Unknown question notice.'));
236 $obj = new ActivityObject();
238 $obj->id = $question->uri;
239 $obj->type = QnA_Question::OBJECT_TYPE;
240 $obj->title = $question->title;
241 $obj->link = $notice->bestUrl();
243 // XXX: probably need other stuff here
249 * Output our CSS class for QnA notice list elements
251 * @param NoticeListItem $nli The item being shown
253 * @return boolean hook value
256 function onStartOpenNoticeListItemElement($nli)
258 $type = $nli->notice->object_type;
262 case QnA_Question::OBJECT_TYPE:
263 $id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
264 $class = 'hentry notice question';
265 if ($nli->notice->scope != 0 && $nli->notice->scope != 1) {
266 $class .= ' limited-scope';
269 $question = QnA_Question::getKV('uri', $nli->notice->uri);
271 if (!empty($question->closed)) {
275 $nli->out->elementStart(
278 'id' => 'notice-' . $id
281 Event::handle('EndOpenNoticeListItemElement', array($nli));
284 case QnA_Answer::OBJECT_TYPE:
285 $id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
287 $cls = array('hentry', 'notice', 'answer');
289 $answer = QnA_Answer::getKV('uri', $nli->notice->uri);
291 if (!empty($answer) && !empty($answer->best)) {
295 $nli->out->elementStart(
298 'class' => implode(' ', $cls),
299 'id' => 'notice-' . $id
302 Event::handle('EndOpenNoticeListItemElement', array($nli));
313 * Custom HTML output for our notices
315 * @param Notice $notice
316 * @param HTMLOutputter $out
318 function showNotice($notice, $out)
320 switch ($notice->object_type) {
321 case QnA_Question::OBJECT_TYPE:
322 return $this->showNoticeQuestion($notice, $out);
323 case QnA_Answer::OBJECT_TYPE:
324 return $this->showNoticeAnswer($notice, $out);
327 // TRANS: Exception thrown when performing an unexpected action on a question.
328 // TRANS: %s is the unpexpected object type.
329 sprintf(_m('Unexpected type for QnA plugin: %s.'),
336 function showNoticeQuestion($notice, $out)
338 $user = common_current_user();
340 // @hack we want regular rendering, then just add stuff after that
341 $nli = new NoticeListItem($notice, $out);
344 $out->elementStart('div', array('class' => 'entry-content question-description'));
346 $question = QnA_Question::getByNotice($notice);
348 if (!empty($question)) {
350 $form = new QnashowquestionForm($out, $question);
354 // TRANS: Error message displayed when question data is not present.
355 $out->text(_m('Question data is missing.'));
357 $out->elementEnd('div');
360 $out->elementStart('div', array('class' => 'entry-content'));
364 * Output the HTML for this kind of object in a list
366 * @param NoticeListItem $nli The list item being shown.
368 * @return boolean hook value
370 * @todo FIXME: WARNING WARNING WARNING this closes a 'div' that is implicitly opened in BookmarkPlugin's showNotice implementation
372 function onStartShowNoticeItem($nli)
374 if (!$this->isMyNotice($nli->notice)) {
379 $notice = $nli->notice;
381 $this->showNotice($notice, $out);
383 $nli->showNoticeLink();
384 $nli->showNoticeSource();
385 $nli->showNoticeLocation();
389 $out->elementEnd('div');
391 $nli->showNoticeOptions();
393 if ($notice->object_type == QnA_Question::OBJECT_TYPE) {
394 $user = common_current_user();
395 $question = QnA_Question::getByNotice($notice);
398 $profile = $user->getProfile();
399 $answer = $question->getAnswer($profile);
401 // Output a placeholder input -- clicking on it will
402 // bring up a real answer form
404 // NOTE: this whole ul is just a placeholder
405 if (empty($question->closed) && empty($answer)) {
406 $out->elementStart('ul', 'notices qna-dummy');
407 $out->elementStart('li', 'qna-dummy-placeholder');
411 'class' => 'placeholder',
412 // TRANS: Placeholder value for a possible answer to a question
413 // TRANS: by the logged in user.
414 'value' => _m('Your answer...')
417 $out->elementEnd('li');
418 $out->elementEnd('ul');
426 function showNoticeAnswer($notice, $out)
428 $user = common_current_user();
430 $answer = QnA_Answer::getByNotice($notice);
431 $question = $answer->getQuestion();
433 $nli = new NoticeListItem($notice, $out);
436 $out->elementStart('div', array('class' => 'entry-content answer-content'));
438 if (!empty($answer)) {
439 $form = new QnashowanswerForm($out, $answer);
442 // TRANS: Error message displayed when answer data is not present.
443 $out->text(_m('Answer data is missing.'));
446 $out->elementEnd('div');
449 $out->elementStart('div', array('class' => 'entry-content'));
452 static function shorten($content, $notice)
456 if (Notice::contentTooLong($content)) {
457 common_debug("content too long");
458 $max = Notice::maxContent();
459 // TRANS: Link description for link to full notice text if it is longer than
460 // TRANS: what will be dispplayed.
462 $short = mb_substr($content, 0, $max - 1);
463 $short .= sprintf('<a href="%1$s" rel="more" title="%2$s">%3$s</a>',
465 // TRANS: Title for link that is an ellipsis in English.
478 * @param HTMLOutputter $out
481 function entryForm($out)
483 return new QnanewquestionForm($out);
487 * When a notice is deleted, clean up related tables.
489 * @param Notice $notice
491 function deleteRelated($notice)
493 switch ($notice->object_type) {
494 case QnA_Question::OBJECT_TYPE:
495 common_log(LOG_DEBUG, "Deleting question from notice...");
496 $question = QnA_Question::fromNotice($notice);
499 case QnA_Answer::OBJECT_TYPE:
500 common_log(LOG_DEBUG, "Deleting answer from notice...");
501 $answer = QnA_Answer::fromNotice($notice);
502 common_log(LOG_DEBUG, "to delete: $answer->id");
506 common_log(LOG_DEBUG, "Not deleting related, wtf...");
510 function onEndShowScripts($action)
512 $action->script($this->path('js/qna.js'));
516 function onEndShowStyles($action)
518 $action->cssLink($this->path('css/qna.css'));