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/:id',
124 array('action' => 'qnanewanswer'),
125 array('id' => $UUIDregex)
129 array('action' => 'qnavote', 'type' => 'question'),
130 array('id' => $UUIDregex)
134 array('action' => 'qnashowquestion'),
135 array('id' => $UUIDregex)
139 array('action' => 'qnavote', 'type' => 'answer'),
140 array('id' => $UUIDregex)
144 array('action' => 'qnashowanswer'),
145 array('id' => $UUIDregex)
151 function onPluginVersion(&$versions)
155 'version' => STATUSNET_VERSION,
156 'author' => 'Zach Copley',
157 'homepage' => 'http://status.net/wiki/Plugin:QnA',
159 _m('Question and Answers micro-app.')
164 function appTitle() {
165 return _m('Question');
174 QnA_Question::OBJECT_TYPE,
175 QnA_Answer::OBJECT_TYPE
180 * Given a parsed ActivityStreams activity, save it into a notice
181 * and other data structures.
183 * @param Activity $activity
184 * @param Profile $actor
185 * @param array $options=array()
187 * @return Notice the resulting notice
189 function saveNoticeFromActivity($activity, $actor, $options=array())
191 if (count($activity->objects) != 1) {
192 throw new Exception('Too many activity objects.');
195 $questionObj = $activity->objects[0];
197 if ($questinoObj->type != QnA_Question::OBJECT_TYPE) {
198 throw new Exception('Wrong type for object.');
203 switch ($activity->verb) {
204 case ActivityVerb::POST:
205 $notice = QnA_Question::saveNew(
208 $questionObj->summary,
212 case Answer::ObjectType:
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, $options);
221 throw new Exception("Unknown object type received by QnA Plugin");
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
295 function showNotice($notice, $out)
297 switch ($notice->object_type) {
298 case QnA_Question::OBJECT_TYPE:
299 return $this->showNoticeQuestion($notice, $out);
300 case QnA_Answer::OBJECT_TYPE:
301 return $this->showNoticeAnswer($notice, $out);
303 // TRANS: Exception thrown when performing an unexpected action on a question.
304 // TRANS: %s is the unpexpected object type.
307 _m('Unexpected type for QnA plugin: %s.'),
314 function showNoticeQuestion($notice, $out)
316 $user = common_current_user();
318 // @hack we want regular rendering, then just add stuff after that
319 $nli = new NoticeListItem($notice, $out);
322 $out->elementStart('div', array('class' => 'entry-content question-content'));
323 $question = QnA_Question::getByNotice($notice);
327 $profile = $user->getProfile();
328 $answer = $question->getAnswer($profile);
330 // User has already answer; show the results.
331 $form = new QnaansweredForm($answer, $out);
333 $form = new QnaanswerForm($question, $out);
338 $out->text(_m('Question data is missing'));
340 $out->elementEnd('div');
343 $out->elementStart('div', array('class' => 'entry-content'));
346 function showNoticeAnswer($notice, $out)
348 $user = common_current_user();
350 // @hack we want regular rendering, then just add stuff after that
351 $nli = new NoticeListItem($notice, $out);
355 $out->elementStart('div', array('class' => 'entry-content'));
361 * @param HTMLOutputter $out
365 function entryForm($out)
367 return new QnaquestionForm($out);
371 * When a notice is deleted, clean up related tables.
373 * @param Notice $notice
376 function deleteRelated($notice)
378 switch ($notice->object_type) {
379 case QnA_Question::OBJECT_TYPE:
380 common_log(LOG_DEBUG, "Deleting question from notice...");
381 $question = QnA_Question::fromNotice($notice);
384 case QnA_Answer::OBJECT_TYPE:
385 common_log(LOG_DEBUG, "Deleting answer from notice...");
386 $answer = QnA_Answer::fromNotice($notice);
387 common_log(LOG_DEBUG, "to delete: $answer->id");
391 common_log(LOG_DEBUG, "Not deleting related, wtf...");
395 function onEndShowScripts($action)
397 // XXX maybe some cool shiz here
400 function onEndShowStyles($action)
402 $action->cssLink($this->path('css/qna.css'));