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':
91 case 'QnaansweredForm':
93 include_once $dir . '/lib/' . strtolower($cls).'.php';
98 include_once $dir . '/classes/' . $cls.'.php';
107 * Map URLs to actions
109 * @param Net_URL_Mapper $m path-to-action mapper
111 * @return boolean hook value; true means continue processing, false means stop.
114 function onRouterInitialized($m)
116 $UUIDregex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
119 'main/qna/newquestion',
120 array('action' => 'qnanewquestion')
123 'main/qna/newanswer',
124 array('action' => 'qnanewanswer')
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 = QnA_Question::saveNew(
207 $questionObj->summary,
211 case Answer::ObjectType:
212 $question = QnA_Question::staticGet('uri', $questionObj->id);
213 if (empty($question)) {
214 // FIXME: save the question
215 throw new Exception("Answer to unknown question.");
217 $notice = QnA_Answer::saveNew($actor, $question, $options);
220 throw new Exception("Unknown object type received by QnA Plugin");
227 * Turn a Notice into an activity object
229 * @param Notice $notice
231 * @return ActivityObject
234 function activityObjectFromNotice($notice)
238 switch ($notice->object_type) {
239 case QnA_Question::OBJECT_TYPE:
240 $question = QnA_Question::fromNotice($notice);
242 case QnA_Answer::OBJECT_TYPE:
243 $answer = QnA_Answer::fromNotice($notice);
244 $question = $answer->getQuestion();
248 if (empty($question)) {
249 throw new Exception("Unknown object type.");
252 $notice = $question->getNotice();
254 if (empty($notice)) {
255 throw new Exception("Unknown question notice.");
258 $obj = new ActivityObject();
260 $obj->id = $question->uri;
261 $obj->type = QnA_Question::OBJECT_TYPE;
262 $obj->title = $question->title;
263 $obj->link = $notice->bestUrl();
265 // XXX: probably need other stuff here
271 * Change the verb on Answer notices
273 * @param Notice $notice
275 * @return ActivityObject
278 function onEndNoticeAsActivity($notice, &$act) {
279 switch ($notice->object_type) {
281 case Answer::ANONYMOUS:
282 $act->verb = $notice->object_type;
289 * Custom HTML output for our notices
291 * @param Notice $notice
292 * @param HTMLOutputter $out
294 function showNotice($notice, $out)
296 switch ($notice->object_type) {
297 case QnA_Question::OBJECT_TYPE:
298 return $this->showNoticeQuestion($notice, $out);
299 case QnA_Answer::OBJECT_TYPE:
300 return $this->showNoticeAnswer($notice, $out);
302 // TRANS: Exception thrown when performing an unexpected action on a question.
303 // TRANS: %s is the unpexpected object type.
306 _m('Unexpected type for QnA plugin: %s.'),
313 function showNoticeQuestion($notice, $out)
315 $user = common_current_user();
317 // @hack we want regular rendering, then just add stuff after that
318 $nli = new NoticeListItem($notice, $out);
321 $out->elementStart('div', array('class' => 'entry-content question-content'));
322 $question = QnA_Question::getByNotice($notice);
326 $profile = $user->getProfile();
327 $answer = $question->getAnswer($profile);
329 // User has already answer; show the results.
330 $form = new QnaansweredForm($answer, $out);
332 $form = new QnaanswerForm($question, $out);
337 $out->text(_m('Question data is missing'));
339 $out->elementEnd('div');
342 $out->elementStart('div', array('class' => 'entry-content'));
345 function showNoticeAnswer($notice, $out)
347 $user = common_current_user();
349 // @hack we want regular rendering, then just add stuff after that
350 $nli = new NoticeListItem($notice, $out);
354 $out->elementStart('div', array('class' => 'entry-content'));
360 * @param HTMLOutputter $out
364 function entryForm($out)
366 return new QnaquestionForm($out);
370 * When a notice is deleted, clean up related tables.
372 * @param Notice $notice
375 function deleteRelated($notice)
377 switch ($notice->object_type) {
378 case QnA_Question::OBJECT_TYPE:
379 common_log(LOG_DEBUG, "Deleting question from notice...");
380 $question = QnA_Question::fromNotice($notice);
383 case QnA_Answer::OBJECT_TYPE:
384 common_log(LOG_DEBUG, "Deleting answer from notice...");
385 $answer = QnA_Answer::fromNotice($notice);
386 common_log(LOG_DEBUG, "to delete: $answer->id");
390 common_log(LOG_DEBUG, "Not deleting related, wtf...");
394 function onEndShowScripts($action)
396 // XXX maybe some cool shiz here
399 function onEndShowStyles($action)
401 $action->cssLink($this->path('css/qna.css'));