]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/QnA/QnAPlugin.php
* Move stuff around again
[quix0rs-gnu-social.git] / plugins / QnA / QnAPlugin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * Microapp plugin for Questions and Answers
7  *
8  * PHP version 5
9  *
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.
14  *
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.
19  *
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/>.
22  *
23  * @category  QnA
24  * @package   StatusNet
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/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * Question and Answer plugin
39  *
40  * @category  Plugin
41  * @package   StatusNet
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/
46  */
47 class QnAPlugin extends MicroAppPlugin
48 {
49     /**
50      * Set up our tables (question and answer)
51      *
52      * @see Schema
53      * @see ColumnDef
54      *
55      * @return boolean hook value; true means continue processing, false means stop.
56      */
57     function onCheckSchema()
58     {
59         $schema = Schema::get();
60
61         $schema->ensureTable('qna_question', QnA_Question::schemaDef());
62         $schema->ensureTable('qna_answer', QnA_Answer::schemaDef());
63         $schema->ensureTable('qna_vote', QnA_Vote::schemaDef());
64
65         return true;
66     }
67
68     /**
69      * Load related modules when needed
70      *
71      * @param string $cls Name of the class to be loaded
72      *
73      * @return boolean hook value; true means continue processing, false means stop.
74      */
75     function onAutoload($cls)
76     {
77         $dir = dirname(__FILE__);
78
79         switch ($cls)
80         {
81         case 'QnanewquestionAction':
82         case 'QnanewanswerAction':
83         case 'QnashowquestionAction':
84         case 'QnashowanswerAction':
85         case 'QnavoteAction':
86             include_once $dir . '/actions/'
87                 . strtolower(mb_substr($cls, 0, -6)) . '.php';
88             return false;
89         case 'QnaquestionForm':
90         case 'QnaanswerForm':
91         case 'QnavoteForm';
92             include_once $dir . '/lib/' . strtolower($cls).'.php';
93             break;
94         case 'QnA_Question':
95         case 'QnA_Answer':
96         case 'QnA_Vote':
97             include_once $dir . '/classes/' . $cls.'.php';
98             return false;
99             break;
100         default:
101             return true;
102         }
103     }
104
105     /**
106      * Map URLs to actions
107      *
108      * @param Net_URL_Mapper $m path-to-action mapper
109      *
110      * @return boolean hook value; true means continue processing, false means stop.
111      */
112
113     function onRouterInitialized($m)
114     {
115         $UUIDregex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
116
117         $m->connect(
118             'main/qna/newquestion',
119             array('action' => 'qnanewquestion')
120         );
121         $m->connect(
122             'main/qna/newanswer/:id',
123             array('action' => 'qnanewanswer'),
124             array('id' => $UUIDregex)
125         );
126         $m->connect(
127             'question/vote/:id',
128             array('action' => 'qnavote', 'type' => 'question'),
129             array('id' => $UUIDregex)
130         );
131         $m->connect(
132             'question/:id',
133             array('action' => 'qnashowquestion'),
134             array('id' => $UUIDregex)
135         );
136         $m->connect(
137             'answer/vote/:id',
138             array('action' => 'qnavote', 'type' => 'answer'),
139             array('id' => $UUIDregex)
140         );
141         $m->connect(
142             'answer/:id',
143             array('action' => 'qnashowanswer'),
144             array('id' => $UUIDregex)
145         );
146
147         return true;
148     }
149
150     function onPluginVersion(&$versions)
151     {
152         $versions[] = array(
153             'name'        => 'QnA',
154             'version'     => STATUSNET_VERSION,
155             'author'      => 'Zach Copley',
156             'homepage'    => 'http://status.net/wiki/Plugin:QnA',
157             'description' =>
158              _m('Question and Answers micro-app.')
159         );
160         return true;
161     }
162
163     function appTitle() {
164         return _m('Question');
165     }
166
167     function tag() {
168         return 'question';
169     }
170
171     function types() {
172         return array(
173             QnA_Question::OBJECT_TYPE,
174             QnA_Answer::OBJECT_TYPE
175         );
176     }
177
178     /**
179      * Given a parsed ActivityStreams activity, save it into a notice
180      * and other data structures.
181      *
182      * @param Activity $activity
183      * @param Profile $actor
184      * @param array $options=array()
185      *
186      * @return Notice the resulting notice
187      */
188     function saveNoticeFromActivity($activity, $actor, $options=array())
189     {
190         if (count($activity->objects) != 1) {
191             throw new Exception('Too many activity objects.');
192         }
193
194         $questionObj = $activity->objects[0];
195
196         if ($questinoObj->type != QnA_Question::OBJECT_TYPE) {
197             throw new Exception('Wrong type for object.');
198         }
199
200         $notice = null;
201
202         switch ($activity->verb) {
203         case ActivityVerb::POST:
204             $notice = Question::saveNew(
205                 $actor,
206                 $questionObj->title
207                // null,
208                // $questionObj->summary,
209                // $options
210             );
211             break;
212         case Answer::NORMAL:
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.");
217             }
218             $notice = QnA_Answer::saveNew($actor, $question, $activity->verb, $options);
219             break;
220         default:
221             throw new Exception("Unknown verb for question");
222         }
223
224         return $notice;
225     }
226
227     /**
228      * Turn a Notice into an activity object
229      *
230      * @param Notice $notice
231      *
232      * @return ActivityObject
233      */
234
235     function activityObjectFromNotice($notice)
236     {
237         $question = null;
238
239         switch ($notice->object_type) {
240         case QnA_Question::OBJECT_TYPE:
241             $question = QnA_Question::fromNotice($notice);
242             break;
243         case QnA_Answer::OBJECT_TYPE:
244             $answer   = QnA_Answer::fromNotice($notice);
245             $question = $answer->getQuestion();
246             break;
247         }
248
249         if (empty($question)) {
250             throw new Exception("Unknown object type.");
251         }
252
253         $notice = $question->getNotice();
254
255         if (empty($notice)) {
256             throw new Exception("Unknown question notice.");
257         }
258
259         $obj = new ActivityObject();
260
261         $obj->id      = $question->uri;
262         $obj->type    = QnA_Question::OBJECT_TYPE;
263         $obj->title   = $question->title;
264         $obj->link    = $notice->bestUrl();
265
266         // XXX: probably need other stuff here
267
268         return $obj;
269     }
270
271     /**
272      * Change the verb on Answer notices
273      *
274      * @param Notice $notice
275      *
276      * @return ActivityObject
277      */
278
279     function onEndNoticeAsActivity($notice, &$act) {
280         switch ($notice->object_type) {
281         case Answer::NORMAL:
282         case Answer::ANONYMOUS:
283             $act->verb = $notice->object_type;
284             break;
285         }
286         return true;
287     }
288
289     /**
290      * Custom HTML output for our notices
291      *
292      * @param Notice $notice
293      * @param HTMLOutputter $out
294      */
295
296     function showNotice($notice, $out)
297     {
298         switch ($notice->object_type) {
299         case QnA_Question::OBJECT_TYPE:
300             $this->showQuestionNotice($notice, $out);
301             break;
302         case QnA_Answer::OBJECT_TYPE:
303             $this->showAnswerNotice($notice, $out);
304             break;
305         }
306
307         // bad craziness
308         $out->elementStart('div', array('class' => 'question'));
309
310         $profile = $notice->getProfile();
311         $avatar = $profile->getAvatar(AVATAR_MINI_SIZE);
312
313         $out->element(
314             'img',
315             array(
316                 'src'    => ($avatar)
317                     ? $avatar->displayUrl()
318                     : Avatar::defaultImage(AVATAR_MINI_SIZE),
319                 'class'  => 'avatar photo question-avatar',
320                 'width'  => AVATAR_MINI_SIZE,
321                 'height' => AVATAR_MINI_SIZE,
322                 'alt'    => $profile->getBestName()
323             )
324         );
325
326         $out->raw('&#160;'); // avoid &nbsp; for AJAX XML compatibility
327
328         // hack for belongsOnTimeline; JS needs to be able to find the author
329         $out->elementStart('span', 'vcard author');
330         $out->element(
331             'a',
332             array(
333                 'class' => 'url',
334                 'href'  => $profile->profileurl,
335                 'title' => $profile->getBestName()
336             ),
337             $profile->nickname
338         );
339
340         $out->elementEnd('span');
341     }
342
343     function showAnswerNotice($notice, $out)
344     {
345         $answer = QnA_Answer::fromNotice($notice);
346
347         assert(!empty($answer));
348
349         $out->elementStart('div', 'answer');
350         $out->raw($answer->asHTML());
351         $out->elementEnd('div');
352     }
353
354     function showQuestionNotice($notice, $out)
355     {
356         $profile  = $notice->getProfile();
357         $question = QnA_Question::fromNotice($notice);
358
359         assert(!empty($question));
360         assert(!empty($profile));
361
362         $out->elementStart('div', 'question-notice');
363
364         $out->elementStart('h3');
365
366         if (!empty($question->url)) {
367             $out->element(
368                 'a',
369                 array(
370                     'href'  => $question->url,
371                     'class' => 'question-title'
372                 ),
373                 $question->title
374             );
375         } else {
376             $out->text($question->title);
377         }
378
379         if (!empty($question->location)) {
380             $out->elementStart('div', 'question-location');
381             $out->element('strong', null, _('Location: '));
382             $out->element('span', 'location', $question->location);
383             $out->elementEnd('div');
384         }
385
386         if (!empty($question->description)) {
387             $out->elementStart('div', 'question-description');
388             $out->element('strong', null, _('Description: '));
389             $out->element('span', 'description', $question->description);
390             $out->elementEnd('div');
391         }
392
393         //$answers = $question->getAnswers();
394
395         $out->elementStart('div', 'question-answers');
396         $out->element('strong', null, _('Answer: '));
397         $out->element('span', 'question-answer');
398
399         $out->elementEnd('div');
400
401         $user = common_current_user();
402
403         if (!empty($user)) {
404
405             $answer = $question->getAnswer($user->getProfile());
406
407             if (empty($answer)) {
408                 $form = new QnaanswerForm($question, $out);
409                 $form->show();
410             }
411
412
413         }
414
415         $out->elementEnd('div');
416     }
417
418     /**
419      * Form for our app
420      *
421      * @param HTMLOutputter $out
422      * @return Widget
423      */
424
425     function entryForm($out)
426     {
427         return new QnaquestionForm($out);
428     }
429
430     /**
431      * When a notice is deleted, clean up related tables.
432      *
433      * @param Notice $notice
434      */
435
436     function deleteRelated($notice)
437     {
438         switch ($notice->object_type) {
439         case QnA_Question::OBJECT_TYPE:
440             common_log(LOG_DEBUG, "Deleting question from notice...");
441             $question = QnA_Question::fromNotice($notice);
442             $question->delete();
443             break;
444         case QnA_Answer::OBJECT_TYPE:
445             common_log(LOG_DEBUG, "Deleting answer from notice...");
446             $answer = QnA_Answer::fromNotice($notice);
447             common_log(LOG_DEBUG, "to delete: $answer->id");
448             $answer->delete();
449             break;
450         default:
451             common_log(LOG_DEBUG, "Not deleting related, wtf...");
452         }
453     }
454
455     function onEndShowScripts($action)
456     {
457         // XXX maybe some cool shiz here
458     }
459
460     function onEndShowStyles($action)
461     {
462         $action->cssLink($this->path('css/qna.css'));
463         return true;
464     }
465 }