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 var $oldSaveNew = true;
53 * Set up our tables (question and answer)
58 * @return boolean hook value; true means continue processing, false means stop.
60 function onCheckSchema()
62 $schema = Schema::get();
64 $schema->ensureTable('qna_question', QnA_Question::schemaDef());
65 $schema->ensureTable('qna_answer', QnA_Answer::schemaDef());
66 $schema->ensureTable('qna_vote', QnA_Vote::schemaDef());
71 public function newFormAction() {
72 return 'qnanewquestion';
78 * @param URLMapper $m path-to-action mapper
80 * @return boolean hook value; true means continue processing, false means stop.
83 public function onRouterInitialized(URLMapper $m)
85 $UUIDregex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
88 'main/qna/newquestion',
89 array('action' => 'qnanewquestion')
92 'answer/qna/closequestion',
93 array('action' => 'qnaclosequestion')
97 array('action' => 'qnanewanswer')
100 'main/qna/reviseanswer',
101 array('action' => 'qnareviseanswer')
105 array('action' => 'qnavote', 'type' => 'question'),
106 array('id' => $UUIDregex)
110 array('action' => 'qnashowquestion'),
111 array('id' => $UUIDregex)
115 array('action' => 'qnavote', 'type' => 'answer'),
116 array('id' => $UUIDregex)
120 array('action' => 'qnashowanswer'),
121 array('id' => $UUIDregex)
127 function onPluginVersion(array &$versions)
131 'version' => GNUSOCIAL_VERSION,
132 'author' => 'Zach Copley',
133 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/QnA',
135 // TRANS: Plugin description.
136 _m('Question and Answers micro-app.')
141 function appTitle() {
142 // TRANS: Application title.
143 return _m('TITLE','Question');
152 QnA_Question::OBJECT_TYPE,
153 QnA_Answer::OBJECT_TYPE
158 * Given a parsed ActivityStreams activity, save it into a notice
159 * and other data structures.
161 * @param Activity $activity
162 * @param Profile $actor
163 * @param array $options=array()
165 * @return Notice the resulting notice
167 function saveNoticeFromActivity(Activity $activity, Profile $actor, array $options=array())
169 if (count($activity->objects) != 1) {
170 // TRANS: Exception thrown when there are too many activity objects.
171 throw new Exception(_m('Too many activity objects.'));
174 $questionObj = $activity->objects[0];
176 if ($questionObj->type != QnA_Question::OBJECT_TYPE) {
177 // TRANS: Exception thrown when an incorrect object type is encountered.
178 throw new Exception(_m('Wrong type for object.'));
183 switch ($activity->verb) {
184 case ActivityVerb::POST:
185 $notice = QnA_Question::saveNew(
188 $questionObj->summary,
192 case Answer::ObjectType:
193 $question = QnA_Question::getKV('uri', $questionObj->id);
194 if (empty($question)) {
195 // FIXME: save the question
196 // TRANS: Exception thrown when answering a non-existing question.
197 throw new Exception(_m('Answer to unknown question.'));
199 $notice = QnA_Answer::saveNew($actor, $question, $options);
202 // TRANS: Exception thrown when an object type is encountered that cannot be handled.
203 throw new Exception(_m('Unknown object type.'));
210 * Turn a Notice into an activity object
212 * @param Notice $notice
214 * @return ActivityObject
217 function activityObjectFromNotice(Notice $notice)
221 switch ($notice->object_type) {
222 case QnA_Question::OBJECT_TYPE:
223 $question = QnA_Question::fromNotice($notice);
225 case QnA_Answer::OBJECT_TYPE:
226 $answer = QnA_Answer::fromNotice($notice);
227 $question = $answer->getQuestion();
231 if (empty($question)) {
232 // TRANS: Exception thrown when an object type is encountered that cannot be handled.
233 throw new Exception(_m('Unknown object type.'));
236 $notice = $question->getNotice();
238 if (empty($notice)) {
239 // TRANS: Exception thrown when requesting a non-existing question notice.
240 throw new Exception(_m('Unknown question notice.'));
243 $obj = new ActivityObject();
245 $obj->id = $question->uri;
246 $obj->type = QnA_Question::OBJECT_TYPE;
247 $obj->title = $question->title;
248 $obj->link = $notice->getUrl();
250 // XXX: probably need other stuff here
256 * Output our CSS class for QnA notice list elements
258 * @param NoticeListItem $nli The item being shown
260 * @return boolean hook value
263 function onStartOpenNoticeListItemElement(NoticeListItem $nli)
265 $type = $nli->notice->object_type;
269 case QnA_Question::OBJECT_TYPE:
270 $id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
271 $class = 'h-entry notice question';
272 if ($nli->notice->scope != 0 && $nli->notice->scope != 1) {
273 $class .= ' limited-scope';
276 $question = QnA_Question::getKV('uri', $nli->notice->uri);
278 if (!empty($question->closed)) {
282 $nli->out->elementStart(
285 'id' => 'notice-' . $id
288 Event::handle('EndOpenNoticeListItemElement', array($nli));
291 case QnA_Answer::OBJECT_TYPE:
292 $id = (empty($nli->repeat)) ? $nli->notice->id : $nli->repeat->id;
294 $cls = array('h-entry', 'notice', 'answer');
296 $answer = QnA_Answer::getKV('uri', $nli->notice->uri);
298 if (!empty($answer) && !empty($answer->best)) {
302 $nli->out->elementStart(
305 'class' => implode(' ', $cls),
306 'id' => 'notice-' . $id
309 Event::handle('EndOpenNoticeListItemElement', array($nli));
320 * Output the HTML for this kind of object in a list
322 * @param NoticeListItem $nli The list item being shown.
324 * @return boolean hook value
326 * @todo FIXME: WARNING WARNING WARNING this closes a 'div' that is implicitly opened in BookmarkPlugin's showNotice implementation
328 function onStartShowNoticeItem(NoticeListItem $nli)
330 if (!$this->isMyNotice($nli->notice)) {
335 $notice = $nli->notice;
337 $nli->showNotice($notice, $out);
339 $nli->showNoticeLink();
340 $nli->showNoticeSource();
341 $nli->showNoticeLocation();
342 $nli->showPermalink();
344 $nli->showNoticeOptions();
346 if ($notice->object_type == QnA_Question::OBJECT_TYPE) {
347 $user = common_current_user();
348 $question = QnA_Question::getByNotice($notice);
350 if (!empty($user) and !empty($question)) {
351 $profile = $user->getProfile();
352 $answer = $question->getAnswer($profile);
354 // Output a placeholder input -- clicking on it will
355 // bring up a real answer form
357 // NOTE: this whole ul is just a placeholder
358 if (empty($question->closed) && empty($answer)) {
359 $out->elementStart('ul', 'notices qna-dummy');
360 $out->elementStart('li', 'qna-dummy-placeholder');
364 'class' => 'placeholder',
365 // TRANS: Placeholder value for a possible answer to a question
366 // TRANS: by the logged in user.
367 'value' => _m('Your answer...')
370 $out->elementEnd('li');
371 $out->elementEnd('ul');
379 function adaptNoticeListItem($nli) {
380 return new QnAListItem($nli);
383 static function shorten($content, $notice)
387 if (Notice::contentTooLong($content)) {
388 common_debug("content too long");
389 $max = Notice::maxContent();
390 // TRANS: Link description for link to full notice text if it is longer than
391 // TRANS: what will be dispplayed.
393 $short = mb_substr($content, 0, $max - 1);
394 $short .= sprintf('<a href="%1$s" rel="more" title="%2$s">%3$s</a>',
396 // TRANS: Title for link that is an ellipsis in English.
409 * @param HTMLOutputter $out
412 function entryForm($out)
414 return new QnanewquestionForm($out);
418 * When a notice is deleted, clean up related tables.
420 * @param Notice $notice
422 function deleteRelated(Notice $notice)
424 switch ($notice->object_type) {
425 case QnA_Question::OBJECT_TYPE:
426 common_log(LOG_DEBUG, "Deleting question from notice...");
427 $question = QnA_Question::fromNotice($notice);
430 case QnA_Answer::OBJECT_TYPE:
431 common_log(LOG_DEBUG, "Deleting answer from notice...");
432 $answer = QnA_Answer::fromNotice($notice);
433 common_log(LOG_DEBUG, "to delete: $answer->id");
437 common_log(LOG_DEBUG, "Not deleting related, wtf...");
441 function onEndShowScripts($action)
443 $action->script($this->path('js/qna.js'));
447 function onEndShowStyles($action)
449 $action->cssLink($this->path('css/qna.css'));