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 'QnaclosequestionAction':
85 case 'QnashowanswerAction':
86 case 'QnareviseanswerAction':
88 include_once $dir . '/actions/'
89 . strtolower(mb_substr($cls, 0, -6)) . '.php';
91 case 'QnanewquestionForm':
92 case 'QnashowquestionForm':
93 case 'QnanewanswerForm':
94 case 'QnashowanswerForm':
95 case 'QnareviseanswerForm':
97 include_once $dir . '/lib/' . strtolower($cls).'.php';
102 include_once $dir . '/classes/' . $cls.'.php';
111 * Map URLs to actions
113 * @param Net_URL_Mapper $m path-to-action mapper
115 * @return boolean hook value; true means continue processing, false means stop.
118 function onRouterInitialized($m)
120 $UUIDregex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
123 'main/qna/newquestion',
124 array('action' => 'qnanewquestion')
127 'answer/qna/closequestion',
128 array('action' => 'qnaclosequestion')
131 'main/qna/newanswer',
132 array('action' => 'qnanewanswer')
135 'main/qna/reviseanswer',
136 array('action' => 'qnareviseanswer')
140 array('action' => 'qnavote', 'type' => 'question'),
141 array('id' => $UUIDregex)
145 array('action' => 'qnashowquestion'),
146 array('id' => $UUIDregex)
150 array('action' => 'qnavote', 'type' => 'answer'),
151 array('id' => $UUIDregex)
155 array('action' => 'qnashowanswer'),
156 array('id' => $UUIDregex)
162 function onPluginVersion(&$versions)
166 'version' => STATUSNET_VERSION,
167 'author' => 'Zach Copley',
168 'homepage' => 'http://status.net/wiki/Plugin:QnA',
170 _m('Question and Answers micro-app.')
175 function appTitle() {
176 return _m('Question');
185 QnA_Question::OBJECT_TYPE,
186 QnA_Answer::OBJECT_TYPE
191 * Given a parsed ActivityStreams activity, save it into a notice
192 * and other data structures.
194 * @param Activity $activity
195 * @param Profile $actor
196 * @param array $options=array()
198 * @return Notice the resulting notice
200 function saveNoticeFromActivity($activity, $actor, $options=array())
202 if (count($activity->objects) != 1) {
203 throw new Exception('Too many activity objects.');
206 $questionObj = $activity->objects[0];
208 if ($questionObj->type != QnA_Question::OBJECT_TYPE) {
209 throw new Exception('Wrong type for object.');
214 switch ($activity->verb) {
215 case ActivityVerb::POST:
216 $notice = QnA_Question::saveNew(
219 $questionObj->summary,
223 case Answer::ObjectType:
224 $question = QnA_Question::staticGet('uri', $questionObj->id);
225 if (empty($question)) {
226 // FIXME: save the question
227 throw new Exception("Answer to unknown question.");
229 $notice = QnA_Answer::saveNew($actor, $question, $options);
232 throw new Exception("Unknown object type received by QnA Plugin");
239 * Turn a Notice into an activity object
241 * @param Notice $notice
243 * @return ActivityObject
246 function activityObjectFromNotice($notice)
250 switch ($notice->object_type) {
251 case QnA_Question::OBJECT_TYPE:
252 $question = QnA_Question::fromNotice($notice);
254 case QnA_Answer::OBJECT_TYPE:
255 $answer = QnA_Answer::fromNotice($notice);
256 $question = $answer->getQuestion();
260 if (empty($question)) {
261 throw new Exception("Unknown object type.");
264 $notice = $question->getNotice();
266 if (empty($notice)) {
267 throw new Exception("Unknown question notice.");
270 $obj = new ActivityObject();
272 $obj->id = $question->uri;
273 $obj->type = QnA_Question::OBJECT_TYPE;
274 $obj->title = $question->title;
275 $obj->link = $notice->bestUrl();
277 // XXX: probably need other stuff here
283 * Output our CSS class for QnA notice list elements
285 * @param NoticeListItem $nli The item being shown
287 * @return boolean hook value
290 function onStartOpenNoticeListItemElement($nli)
292 $type = $nli->notice->object_type;
296 case QnA_Question::OBJECT_TYPE:
297 $id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
298 $class = 'hentry notice question';
299 if ($nli->notice->scope != 0 && $nli->notice->scope != 1) {
300 $class .= ' limited-scope';
302 $nli->out->elementStart(
305 'id' => 'notice-' . $id
308 Event::handle('EndOpenNoticeListItemElement', array($nli));
311 case QnA_Answer::OBJECT_TYPE:
312 $id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
314 $cls = array('hentry', 'notice', 'answer');
316 $answer = QnA_Answer::staticGet('uri', $nli->notice->uri);
318 if (!empty($answer) && !empty($answer->best)) {
322 $nli->out->elementStart(
325 'class' => implode(' ', $cls),
326 'id' => 'notice-' . $id
329 Event::handle('EndOpenNoticeListItemElement', array($nli));
340 * Custom HTML output for our notices
342 * @param Notice $notice
343 * @param HTMLOutputter $out
346 function showNotice($notice, $out)
348 switch ($notice->object_type) {
349 case QnA_Question::OBJECT_TYPE:
350 return $this->showNoticeQuestion($notice, $out);
351 case QnA_Answer::OBJECT_TYPE:
352 return $this->showNoticeAnswer($notice, $out);
354 // TRANS: Exception thrown when performing an unexpected action on a question.
355 // TRANS: %s is the unpexpected object type.
358 _m('Unexpected type for QnA plugin: %s.'),
365 function showNoticeQuestion($notice, $out)
367 $user = common_current_user();
369 // @hack we want regular rendering, then just add stuff after that
370 $nli = new NoticeListItem($notice, $out);
373 $out->elementStart('div', array('class' => 'entry-content question-description'));
375 $question = QnA_Question::getByNotice($notice);
377 if (!empty($question)) {
379 $form = new QnashowquestionForm($out, $question);
382 $profile = $user->getProfile();
383 $answer = $question->getAnswer($profile);
384 if (empty($answer)) {
385 $form = new QnanewanswerForm($out, $question);
388 $form = new QnashowquestionForm($out, $question);
393 $out->text(_m('Question data is missing.'));
395 $out->elementEnd('div');
398 $out->elementStart('div', array('class' => 'entry-content'));
401 function showNoticeAnswer($notice, $out)
403 $user = common_current_user();
405 $answer = QnA_Answer::getByNotice($notice);
406 $question = $answer->getQuestion();
408 $nli = new NoticeListItem($notice, $out);
411 $out->elementStart('div', array('class' => 'entry-content answer-content'));
413 if (!empty($answer)) {
414 $form = new QnashowanswerForm($out, $answer);
417 $out->text(_m('Answer data is missing.'));
420 $out->elementEnd('div');
423 $out->elementStart('div', array('class' => 'entry-content'));
426 static function shorten($content, $notice)
430 if (Notice::contentTooLong($content)) {
431 common_debug("content too long");
432 $max = Notice::maxContent();
433 $short = mb_substr($content, 0, $max - 1);
435 '<a href="%s" rel="more" title="%s">…</a>',
449 * @param HTMLOutputter $out
453 function entryForm($out)
455 return new QnanewquestionForm($out);
459 * When a notice is deleted, clean up related tables.
461 * @param Notice $notice
464 function deleteRelated($notice)
466 switch ($notice->object_type) {
467 case QnA_Question::OBJECT_TYPE:
468 common_log(LOG_DEBUG, "Deleting question from notice...");
469 $question = QnA_Question::fromNotice($notice);
472 case QnA_Answer::OBJECT_TYPE:
473 common_log(LOG_DEBUG, "Deleting answer from notice...");
474 $answer = QnA_Answer::fromNotice($notice);
475 common_log(LOG_DEBUG, "to delete: $answer->id");
479 common_log(LOG_DEBUG, "Not deleting related, wtf...");
483 function onEndShowScripts($action)
485 $action->script($this->path('js/qna.js'));
489 function onEndShowStyles($action)
491 $action->cssLink($this->path('css/qna.css'));