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());
69 * Load related modules when needed
71 * @param string $cls Name of the class to be loaded
73 * @return boolean hook value; true means continue processing, false means stop.
75 function onAutoload($cls)
77 $dir = dirname(__FILE__);
81 case 'QnanewquestionAction':
82 case 'QnanewanswerAction':
83 case 'QnashowquestionAction':
84 case 'QnashowanswerAction':
85 case 'QnareviseanswerAction':
87 include_once $dir . '/actions/'
88 . strtolower(mb_substr($cls, 0, -6)) . '.php';
90 case 'QnaquestionForm':
91 case 'QnashowanswerForm':
92 case 'QnanewanswerForm':
93 case 'QnareviseanswerForm':
95 case 'AnswerNoticeListItem':
96 include_once $dir . '/lib/' . strtolower($cls).'.php';
101 include_once $dir . '/classes/' . $cls.'.php';
110 * Map URLs to actions
112 * @param Net_URL_Mapper $m path-to-action mapper
114 * @return boolean hook value; true means continue processing, false means stop.
117 function onRouterInitialized($m)
119 $UUIDregex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
122 'main/qna/newquestion',
123 array('action' => 'qnanewquestion')
126 'main/qna/newanswer',
127 array('action' => 'qnanewanswer')
130 'main/qna/reviseanswer',
131 array('action' => 'qnareviseanswer')
135 array('action' => 'qnavote', 'type' => 'question'),
136 array('id' => $UUIDregex)
140 array('action' => 'qnashowquestion'),
141 array('id' => $UUIDregex)
145 array('action' => 'qnavote', 'type' => 'answer'),
146 array('id' => $UUIDregex)
150 array('action' => 'qnashowanswer'),
151 array('id' => $UUIDregex)
157 function onPluginVersion(&$versions)
161 'version' => STATUSNET_VERSION,
162 'author' => 'Zach Copley',
163 'homepage' => 'http://status.net/wiki/Plugin:QnA',
165 _m('Question and Answers micro-app.')
170 function appTitle() {
171 return _m('Question');
180 QnA_Question::OBJECT_TYPE,
181 QnA_Answer::OBJECT_TYPE
186 * Given a parsed ActivityStreams activity, save it into a notice
187 * and other data structures.
189 * @param Activity $activity
190 * @param Profile $actor
191 * @param array $options=array()
193 * @return Notice the resulting notice
195 function saveNoticeFromActivity($activity, $actor, $options=array())
197 if (count($activity->objects) != 1) {
198 throw new Exception('Too many activity objects.');
201 $questionObj = $activity->objects[0];
203 if ($questinoObj->type != QnA_Question::OBJECT_TYPE) {
204 throw new Exception('Wrong type for object.');
209 switch ($activity->verb) {
210 case ActivityVerb::POST:
211 $notice = QnA_Question::saveNew(
214 $questionObj->summary,
218 case Answer::ObjectType:
219 $question = QnA_Question::staticGet('uri', $questionObj->id);
220 if (empty($question)) {
221 // FIXME: save the question
222 throw new Exception("Answer to unknown question.");
224 $notice = QnA_Answer::saveNew($actor, $question, $options);
227 throw new Exception("Unknown object type received by QnA Plugin");
234 * Turn a Notice into an activity object
236 * @param Notice $notice
238 * @return ActivityObject
241 function activityObjectFromNotice($notice)
245 switch ($notice->object_type) {
246 case QnA_Question::OBJECT_TYPE:
247 $question = QnA_Question::fromNotice($notice);
249 case QnA_Answer::OBJECT_TYPE:
250 $answer = QnA_Answer::fromNotice($notice);
251 $question = $answer->getQuestion();
255 if (empty($question)) {
256 throw new Exception("Unknown object type.");
259 $notice = $question->getNotice();
261 if (empty($notice)) {
262 throw new Exception("Unknown question notice.");
265 $obj = new ActivityObject();
267 $obj->id = $question->uri;
268 $obj->type = QnA_Question::OBJECT_TYPE;
269 $obj->title = $question->title;
270 $obj->link = $notice->bestUrl();
272 // XXX: probably need other stuff here
278 * Change the verb on Answer notices
280 * @param Notice $notice
282 * @return ActivityObject
285 function onEndNoticeAsActivity($notice, &$act) {
286 switch ($notice->object_type) {
288 case Answer::ANONYMOUS:
289 $act->verb = $notice->object_type;
296 * Output our CSS class for QnA notice list elements
298 * @param NoticeListItem $nli The item being shown
300 * @return boolean hook value
303 function onStartOpenNoticeListItemElement($nli)
305 $type = $nli->notice->object_type;
309 case QnA_Question::OBJECT_TYPE:
310 $id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
311 $nli->out->elementStart(
313 'class' => 'hentry notice question',
314 'id' => 'notice-' . $id
317 Event::handle('EndOpenNoticeListItemElement', array($nli));
320 case QnA_Answer::OBJECT_TYPE:
321 $id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
323 $cls = array('hentry', 'notice', 'answer');
325 $answer = QnA_Answer::staticGet('uri', $notice->uri);
327 if (!empty($answer) && !empty($answer->best)) {
331 $nli->out->elementStart(
334 'class' => implode(' ', $cls),
335 'id' => 'notice-' . $id
338 Event::handle('EndOpenNoticeListItemElement', array($nli));
349 * Custom HTML output for our notices
351 * @param Notice $notice
352 * @param HTMLOutputter $out
355 function showNotice($notice, $out)
357 switch ($notice->object_type) {
358 case QnA_Question::OBJECT_TYPE:
359 return $this->showNoticeQuestion($notice, $out);
360 case QnA_Answer::OBJECT_TYPE:
361 return $this->showNoticeAnswer($notice, $out);
363 // TRANS: Exception thrown when performing an unexpected action on a question.
364 // TRANS: %s is the unpexpected object type.
367 _m('Unexpected type for QnA plugin: %s.'),
374 function showNoticeQuestion($notice, $out)
376 $user = common_current_user();
378 // @hack we want regular rendering, then just add stuff after that
379 $nli = new NoticeListItem($notice, $out);
382 $out->elementStart('div', array('class' => 'entry-content question-desciption'));
384 $question = QnA_Question::getByNotice($notice);
386 if (!empty($question)) {
388 $short = $this->shorten($question->description, $notice);
391 // Don't prompt user for an answer if the question is closed or
392 // the current user posed the question in the first place
393 if (empty($question->closed)) {
395 $profile = $user->getProfile();
396 $answer = $question->getAnswer($profile);
398 $form = new QnanewanswerForm($question, $out);
403 $out->element('span', 'closed', _m('This question is closed.'));
406 $out->text(_m('Question data is missing.'));
408 $out->elementEnd('div');
411 $out->elementStart('div', array('class' => 'entry-content'));
414 function showNoticeAnswer($notice, $out)
416 $user = common_current_user();
418 $answer = QnA_Answer::getByNotice($notice);
419 $question = $answer->getQuestion();
421 $nli = new NoticeListItem($notice, $out);
424 $out->elementStart('div', array('class' => 'entry-content answer-content'));
426 if (!empty($answer)) {
427 $form = new QnashowanswerForm($out, $answer);
430 $out->text(_m('Answer data is missing.'));
433 $out->elementEnd('div');
436 $out->elementStart('div', array('class' => 'entry-content'));
439 static function shorten($content, $notice)
443 if (Notice::contentTooLong($content)) {
444 common_debug("content too long");
445 $max = Notice::maxContent();
446 $short = mb_substr($content, 0, $max - 1);
448 '<a href="%s" rel="more" title="%s">…</a>',
462 * @param HTMLOutputter $out
466 function entryForm($out)
468 return new QnaquestionForm($out);
472 * When a notice is deleted, clean up related tables.
474 * @param Notice $notice
477 function deleteRelated($notice)
479 switch ($notice->object_type) {
480 case QnA_Question::OBJECT_TYPE:
481 common_log(LOG_DEBUG, "Deleting question from notice...");
482 $question = QnA_Question::fromNotice($notice);
485 case QnA_Answer::OBJECT_TYPE:
486 common_log(LOG_DEBUG, "Deleting answer from notice...");
487 $answer = QnA_Answer::fromNotice($notice);
488 common_log(LOG_DEBUG, "to delete: $answer->id");
492 common_log(LOG_DEBUG, "Not deleting related, wtf...");
496 function onEndShowScripts($action)
498 // XXX maybe some cool shiz here
501 function onEndShowStyles($action)
503 $action->cssLink($this->path('css/qna.css'));