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':
86 include_once $dir . '/actions/'
87 . strtolower(mb_substr($cls, 0, -6)) . '.php';
89 case 'QnaquestionForm':
92 include_once $dir . '/lib/' . strtolower($cls).'.php';
97 include_once $dir . '/classes/' . $cls.'.php';
106 * Map URLs to actions
108 * @param Net_URL_Mapper $m path-to-action mapper
110 * @return boolean hook value; true means continue processing, false means stop.
113 function onRouterInitialized($m)
115 $UUIDregex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
118 'main/qna/newquestion',
119 array('action' => 'qnanewquestion')
122 'main/qna/newanswer/:id',
123 array('action' => 'qnanewanswer'),
124 array('id' => $UUIDregex)
128 array('action' => 'qnavote', 'type' => 'question'),
129 array('id' => $UUIDregex)
133 array('action' => 'qnashowquestion'),
134 array('id' => $UUIDregex)
138 array('action' => 'qnavote', 'type' => 'answer'),
139 array('id' => $UUIDregex)
143 array('action' => 'qnashowanswer'),
144 array('id' => $UUIDregex)
150 function onPluginVersion(&$versions)
154 'version' => STATUSNET_VERSION,
155 'author' => 'Zach Copley',
156 'homepage' => 'http://status.net/wiki/Plugin:QnA',
158 _m('Question and Answers micro-app.')
163 function appTitle() {
164 return _m('Question');
173 QnA_Question::OBJECT_TYPE,
174 QnA_Answer::OBJECT_TYPE
179 * Given a parsed ActivityStreams activity, save it into a notice
180 * and other data structures.
182 * @param Activity $activity
183 * @param Profile $actor
184 * @param array $options=array()
186 * @return Notice the resulting notice
188 function saveNoticeFromActivity($activity, $actor, $options=array())
190 if (count($activity->objects) != 1) {
191 throw new Exception('Too many activity objects.');
194 $questionObj = $activity->objects[0];
196 if ($questinoObj->type != QnA_Question::OBJECT_TYPE) {
197 throw new Exception('Wrong type for object.');
202 switch ($activity->verb) {
203 case ActivityVerb::POST:
204 $notice = Question::saveNew(
208 // $questionObj->summary,
213 $question = QnA_Question::staticGet('uri', $questionObj->id);
214 if (empty($question)) {
215 // FIXME: save the question
216 throw new Exception("Answer to unknown question.");
218 $notice = QnA_Answer::saveNew($actor, $question, $activity->verb, $options);
221 throw new Exception("Unknown verb for question");
228 * Turn a Notice into an activity object
230 * @param Notice $notice
232 * @return ActivityObject
235 function activityObjectFromNotice($notice)
239 switch ($notice->object_type) {
240 case QnA_Question::OBJECT_TYPE:
241 $question = QnA_Question::fromNotice($notice);
243 case QnA_Answer::OBJECT_TYPE:
244 $answer = QnA_Answer::fromNotice($notice);
245 $question = $answer->getQuestion();
249 if (empty($question)) {
250 throw new Exception("Unknown object type.");
253 $notice = $question->getNotice();
255 if (empty($notice)) {
256 throw new Exception("Unknown question notice.");
259 $obj = new ActivityObject();
261 $obj->id = $question->uri;
262 $obj->type = QnA_Question::OBJECT_TYPE;
263 $obj->title = $question->title;
264 $obj->link = $notice->bestUrl();
266 // XXX: probably need other stuff here
272 * Change the verb on Answer notices
274 * @param Notice $notice
276 * @return ActivityObject
279 function onEndNoticeAsActivity($notice, &$act) {
280 switch ($notice->object_type) {
282 case Answer::ANONYMOUS:
283 $act->verb = $notice->object_type;
290 * Custom HTML output for our notices
292 * @param Notice $notice
293 * @param HTMLOutputter $out
296 function showNotice($notice, $out)
298 switch ($notice->object_type) {
299 case QnA_Question::OBJECT_TYPE:
300 $this->showQuestionNotice($notice, $out);
302 case QnA_Answer::OBJECT_TYPE:
303 $this->showAnswerNotice($notice, $out);
308 $out->elementStart('div', array('class' => 'question'));
310 $profile = $notice->getProfile();
311 $avatar = $profile->getAvatar(AVATAR_MINI_SIZE);
317 ? $avatar->displayUrl()
318 : Avatar::defaultImage(AVATAR_MINI_SIZE),
319 'class' => 'avatar photo question-avatar',
320 'width' => AVATAR_MINI_SIZE,
321 'height' => AVATAR_MINI_SIZE,
322 'alt' => $profile->getBestName()
326 $out->raw(' '); // avoid for AJAX XML compatibility
328 // hack for belongsOnTimeline; JS needs to be able to find the author
329 $out->elementStart('span', 'vcard author');
334 'href' => $profile->profileurl,
335 'title' => $profile->getBestName()
340 $out->elementEnd('span');
343 function showAnswerNotice($notice, $out)
345 $answer = QnA_Answer::fromNotice($notice);
347 assert(!empty($answer));
349 $out->elementStart('div', 'answer');
350 $out->raw($answer->asHTML());
351 $out->elementEnd('div');
354 function showQuestionNotice($notice, $out)
356 $profile = $notice->getProfile();
357 $question = QnA_Question::fromNotice($notice);
359 assert(!empty($question));
360 assert(!empty($profile));
362 $out->elementStart('div', 'question-notice');
364 $out->elementStart('h3');
366 if (!empty($question->url)) {
370 'href' => $question->url,
371 'class' => 'question-title'
376 $out->text($question->title);
379 if (!empty($question->location)) {
380 $out->elementStart('div', 'question-location');
381 $out->element('strong', null, _('Location: '));
382 $out->element('span', 'location', $question->location);
383 $out->elementEnd('div');
386 if (!empty($question->description)) {
387 $out->elementStart('div', 'question-description');
388 $out->element('strong', null, _('Description: '));
389 $out->element('span', 'description', $question->description);
390 $out->elementEnd('div');
393 //$answers = $question->getAnswers();
395 $out->elementStart('div', 'question-answers');
396 $out->element('strong', null, _('Answer: '));
397 $out->element('span', 'question-answer');
399 $out->elementEnd('div');
401 $user = common_current_user();
405 $answer = $question->getAnswer($user->getProfile());
407 if (empty($answer)) {
408 $form = new QnaanswerForm($question, $out);
415 $out->elementEnd('div');
421 * @param HTMLOutputter $out
425 function entryForm($out)
427 return new QnaquestionForm($out);
431 * When a notice is deleted, clean up related tables.
433 * @param Notice $notice
436 function deleteRelated($notice)
438 switch ($notice->object_type) {
439 case QnA_Question::OBJECT_TYPE:
440 common_log(LOG_DEBUG, "Deleting question from notice...");
441 $question = QnA_Question::fromNotice($notice);
444 case QnA_Answer::OBJECT_TYPE:
445 common_log(LOG_DEBUG, "Deleting answer from notice...");
446 $answer = QnA_Answer::fromNotice($notice);
447 common_log(LOG_DEBUG, "to delete: $answer->id");
451 common_log(LOG_DEBUG, "Not deleting related, wtf...");
455 function onEndShowScripts($action)
457 // XXX maybe some cool shiz here
460 function onEndShowStyles($action)
462 $action->cssLink($this->path('css/qna.css'));