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 // @fixme which domain should we use for these namespaces?
51 const QUESTION_OBJECT = 'http://activityschema.org/object/question';
52 const ANSWER_OBJECT = 'http://activityschema.org/object/answer';
55 * Set up our tables (question and answer)
60 * @return boolean hook value; true means continue processing, false means stop.
62 function onCheckSchema()
64 $schema = Schema::get();
66 $schema->ensureTable('qna_question', QnA_Question::schemaDef());
67 $schema->ensureTable('qna_answer', QnA_Answer::schemaDef());
68 $schema->ensureTable('qna_vote', QnA_Vote::schemaDef());
74 * Load related modules when needed
76 * @param string $cls Name of the class to be loaded
78 * @return boolean hook value; true means continue processing, false means stop.
80 function onAutoload($cls)
82 $dir = dirname(__FILE__);
86 case 'NewquestionAction':
87 case 'NewanswerAction':
88 case 'ShowquestionAction':
89 case 'ShowanswerAction':
91 include_once $dir . '/actions/'
92 . strtolower(mb_substr($cls, 0, -6)) . '.php';
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 $regexId = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
124 array('action' => 'newquestion')
127 'main/question/answer',
128 array('action' => 'newanswer')
132 array('action' => 'qnavote', 'type' => 'question'),
133 array('id' => $regexId)
137 array('action' => 'showquestion'),
138 array('id' => $regexId)
142 array('action' => 'qnavote', 'type' => 'answer'),
143 array('id' => $regexId)
147 array('action' => 'showanswer'),
148 array('id' => $regexId)
154 function onPluginVersion(&$versions)
158 'version' => STATUSNET_VERSION,
159 'author' => 'Zach Copley',
160 'homepage' => 'http://status.net/wiki/Plugin:QnA',
162 _m('Question and Answers micro-app.')
167 function appTitle() {
168 return _m('Question');
177 Question::OBJECT_TYPE,
183 * Given a parsed ActivityStreams activity, save it into a notice
184 * and other data structures.
186 * @param Activity $activity
187 * @param Profile $actor
188 * @param array $options=array()
190 * @return Notice the resulting notice
192 function saveNoticeFromActivity($activity, $actor, $options=array())
194 if (count($activity->objects) != 1) {
195 throw new Exception('Too many activity objects.');
198 $questionObj = $activity->objects[0];
200 if ($questinoObj->type != QnA_Question::OBJECT_TYPE) {
201 throw new Exception('Wrong type for object.');
206 switch ($activity->verb) {
207 case ActivityVerb::POST:
208 $notice = Question::saveNew(
212 // $questionObj->summary,
217 $question = QnA_Question::staticGet('uri', $questionObj->id);
218 if (empty($question)) {
219 // FIXME: save the question
220 throw new Exception("Answer to unknown question.");
222 $notice = QnA_Answer::saveNew($actor, $question, $activity->verb, $options);
225 throw new Exception("Unknown verb for question");
232 * Turn a Notice into an activity object
234 * @param Notice $notice
236 * @return ActivityObject
239 function activityObjectFromNotice($notice)
243 switch ($notice->object_type) {
244 case Question::OBJECT_TYPE:
245 $question = Qeustion::fromNotice($notice);
248 case Answer::ANONYMOUS:
249 $answer = Answer::fromNotice($notice);
250 $question = $answer->getQuestion();
254 if (empty($question)) {
255 throw new Exception("Unknown object type.");
258 $notice = $question->getNotice();
260 if (empty($notice)) {
261 throw new Exception("Unknown question notice.");
264 $obj = new ActivityObject();
266 $obj->id = $question->uri;
267 $obj->type = Question::OBJECT_TYPE;
268 $obj->title = $question->title;
269 $obj->link = $notice->bestUrl();
271 // XXX: probably need other stuff here
277 * Change the verb on Answer notices
279 * @param Notice $notice
281 * @return ActivityObject
284 function onEndNoticeAsActivity($notice, &$act) {
285 switch ($notice->object_type) {
287 case Answer::ANONYMOUS:
288 $act->verb = $notice->object_type;
295 * Custom HTML output for our notices
297 * @param Notice $notice
298 * @param HTMLOutputter $out
301 function showNotice($notice, $out)
303 switch ($notice->object_type) {
304 case Question::OBJECT_TYPE:
305 $this->showQuestionNotice($notice, $out);
308 case Answer::ANONYMOUS:
310 $this->showAnswerNotice($notice, $out);
314 $out->elementStart('div', array('class' => 'question'));
316 $profile = $notice->getProfile();
317 $avatar = $profile->getAvatar(AVATAR_MINI_SIZE);
320 array('src' => ($avatar) ?
321 $avatar->displayUrl() :
322 Avatar::defaultImage(AVATAR_MINI_SIZE),
323 'class' => 'avatar photo bookmark-avatar',
324 'width' => AVATAR_MINI_SIZE,
325 'height' => AVATAR_MINI_SIZE,
326 'alt' => $profile->getBestName()));
328 $out->raw(' '); // avoid for AJAX XML compatibility
330 $out->elementStart('span', 'vcard author'); // hack for belongsOnTimeline; JS needs to be able to find the author
332 array('class' => 'url',
333 'href' => $profile->profileurl,
334 'title' => $profile->getBestName()),
336 $out->elementEnd('span');
339 function showAnswerNotice($notice, $out)
341 $rsvp = Answer::fromNotice($notice);
343 $out->elementStart('div', 'answer');
344 $out->raw($answer->asHTML());
345 $out->elementEnd('div');
349 function showQuestionNotice($notice, $out)
351 $profile = $notice->getProfile();
352 $question = Question::fromNotice($notice);
354 assert(!empty($question));
355 assert(!empty($profile));
357 $out->elementStart('div', 'question-notice');
359 $out->elementStart('h3');
361 if (!empty($question->url)) {
363 array('href' => $question->url,
364 'class' => 'question-title'),
367 $out->text($question->title);
370 if (!empty($question->location)) {
371 $out->elementStart('div', 'question-location');
372 $out->element('strong', null, _('Location: '));
373 $out->element('span', 'location', $question->location);
374 $out->elementEnd('div');
377 if (!empty($question->description)) {
378 $out->elementStart('div', 'question-description');
379 $out->element('strong', null, _('Description: '));
380 $out->element('span', 'description', $question->description);
381 $out->elementEnd('div');
384 $answers = $question->getAnswers();
386 $out->elementStart('div', 'question-answers');
387 $out->element('strong', null, _('Answer: '));
388 $out->element('span', 'question-answer');
392 $out->elementEnd('div');
394 $user = common_current_user();
397 $question = $question->getAnswer($user->getProfile());
399 if (empty($answer)) {
400 $form = new AnswerForm($question, $out);
406 $out->elementEnd('div');
412 * @param HTMLOutputter $out
416 function entryForm($out)
418 return new QuestionForm($out);
422 * When a notice is deleted, clean up related tables.
424 * @param Notice $notice
427 function deleteRelated($notice)
429 switch ($notice->object_type) {
430 case Question::OBJECT_TYPE:
431 common_log(LOG_DEBUG, "Deleting question from notice...");
432 $question = Question::fromNotice($notice);
436 case Answer::ANONYMOUS:
437 common_log(LOG_DEBUG, "Deleting answer from notice...");
438 $answer = Answer::fromNotice($notice);
439 common_log(LOG_DEBUG, "to delete: $answer->id");
443 common_log(LOG_DEBUG, "Not deleting related, wtf...");
447 function onEndShowScripts($action)
449 // XXX maybe some cool shiz here
452 function onEndShowStyles($action)
454 $action->cssLink($this->path('css/questionandanswer.css'));