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':
92 case 'QnareviseanswerForm':
94 include_once $dir . '/lib/' . strtolower($cls).'.php';
99 include_once $dir . '/classes/' . $cls.'.php';
108 * Map URLs to actions
110 * @param Net_URL_Mapper $m path-to-action mapper
112 * @return boolean hook value; true means continue processing, false means stop.
115 function onRouterInitialized($m)
117 $UUIDregex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
120 'main/qna/newquestion',
121 array('action' => 'qnanewquestion')
124 'main/qna/newanswer',
125 array('action' => 'qnanewanswer')
128 'main/qna/reviseanswer',
129 array('action' => 'qnareviseanswer')
133 array('action' => 'qnavote', 'type' => 'question'),
134 array('id' => $UUIDregex)
138 array('action' => 'qnashowquestion'),
139 array('id' => $UUIDregex)
143 array('action' => 'qnavote', 'type' => 'answer'),
144 array('id' => $UUIDregex)
148 array('action' => 'qnashowanswer'),
149 array('id' => $UUIDregex)
155 function onPluginVersion(&$versions)
159 'version' => STATUSNET_VERSION,
160 'author' => 'Zach Copley',
161 'homepage' => 'http://status.net/wiki/Plugin:QnA',
163 _m('Question and Answers micro-app.')
168 function appTitle() {
169 return _m('Question');
178 QnA_Question::OBJECT_TYPE,
179 QnA_Answer::OBJECT_TYPE
184 * Given a parsed ActivityStreams activity, save it into a notice
185 * and other data structures.
187 * @param Activity $activity
188 * @param Profile $actor
189 * @param array $options=array()
191 * @return Notice the resulting notice
193 function saveNoticeFromActivity($activity, $actor, $options=array())
195 if (count($activity->objects) != 1) {
196 throw new Exception('Too many activity objects.');
199 $questionObj = $activity->objects[0];
201 if ($questinoObj->type != QnA_Question::OBJECT_TYPE) {
202 throw new Exception('Wrong type for object.');
207 switch ($activity->verb) {
208 case ActivityVerb::POST:
209 $notice = QnA_Question::saveNew(
212 $questionObj->summary,
216 case Answer::ObjectType:
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, $options);
225 throw new Exception("Unknown object type received by QnA Plugin");
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 QnA_Question::OBJECT_TYPE:
245 $question = QnA_Question::fromNotice($notice);
247 case QnA_Answer::OBJECT_TYPE:
248 $answer = QnA_Answer::fromNotice($notice);
249 $question = $answer->getQuestion();
253 if (empty($question)) {
254 throw new Exception("Unknown object type.");
257 $notice = $question->getNotice();
259 if (empty($notice)) {
260 throw new Exception("Unknown question notice.");
263 $obj = new ActivityObject();
265 $obj->id = $question->uri;
266 $obj->type = QnA_Question::OBJECT_TYPE;
267 $obj->title = $question->title;
268 $obj->link = $notice->bestUrl();
270 // XXX: probably need other stuff here
276 * Change the verb on Answer notices
278 * @param Notice $notice
280 * @return ActivityObject
283 function onEndNoticeAsActivity($notice, &$act) {
284 switch ($notice->object_type) {
286 case Answer::ANONYMOUS:
287 $act->verb = $notice->object_type;
294 * Custom HTML output for our notices
296 * @param Notice $notice
297 * @param HTMLOutputter $out
299 function showNotice($notice, $out)
301 switch ($notice->object_type) {
302 case QnA_Question::OBJECT_TYPE:
303 return $this->showNoticeQuestion($notice, $out);
304 case QnA_Answer::OBJECT_TYPE:
305 return $this->showNoticeAnswer($notice, $out);
307 // TRANS: Exception thrown when performing an unexpected action on a question.
308 // TRANS: %s is the unpexpected object type.
311 _m('Unexpected type for QnA plugin: %s.'),
318 function showNoticeQuestion($notice, $out)
320 $user = common_current_user();
322 // @hack we want regular rendering, then just add stuff after that
323 $nli = new NoticeListItem($notice, $out);
326 $out->elementStart('div', array('class' => 'entry-content question-content'));
327 $question = QnA_Question::getByNotice($notice);
331 $profile = $user->getProfile();
332 $answer = $question->getAnswer($profile);
334 // User has already answer; show the results.
335 $form = new QnareviseanswerForm($answer, $out);
337 $form = new QnaanswerForm($question, $out);
342 $out->text(_m('Question data is missing'));
344 $out->elementEnd('div');
347 $out->elementStart('div', array('class' => 'entry-content'));
350 function showNoticeAnswer($notice, $out)
352 $user = common_current_user();
354 // @hack we want regular rendering, then just add stuff after that
355 $nli = new NoticeListItem($notice, $out);
359 $out->elementStart('div', array('class' => 'entry-content'));
365 * @param HTMLOutputter $out
369 function entryForm($out)
371 return new QnaquestionForm($out);
375 * When a notice is deleted, clean up related tables.
377 * @param Notice $notice
380 function deleteRelated($notice)
382 switch ($notice->object_type) {
383 case QnA_Question::OBJECT_TYPE:
384 common_log(LOG_DEBUG, "Deleting question from notice...");
385 $question = QnA_Question::fromNotice($notice);
388 case QnA_Answer::OBJECT_TYPE:
389 common_log(LOG_DEBUG, "Deleting answer from notice...");
390 $answer = QnA_Answer::fromNotice($notice);
391 common_log(LOG_DEBUG, "to delete: $answer->id");
395 common_log(LOG_DEBUG, "Not deleting related, wtf...");
399 function onEndShowScripts($action)
401 // XXX maybe some cool shiz here
404 function onEndShowStyles($action)
406 $action->cssLink($this->path('css/qna.css'));