]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/QnA/QnAPlugin.php
Work on QnA notice display -- in progress
[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 'QnaansweredForm':
92         case 'QnavoteForm':
93             include_once $dir . '/lib/' . strtolower($cls).'.php';
94             break;
95         case 'QnA_Question':
96         case 'QnA_Answer':
97         case 'QnA_Vote':
98             include_once $dir . '/classes/' . $cls.'.php';
99             return false;
100             break;
101         default:
102             return true;
103         }
104     }
105
106     /**
107      * Map URLs to actions
108      *
109      * @param Net_URL_Mapper $m path-to-action mapper
110      *
111      * @return boolean hook value; true means continue processing, false means stop.
112      */
113
114     function onRouterInitialized($m)
115     {
116         $UUIDregex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
117
118         $m->connect(
119             'main/qna/newquestion',
120             array('action' => 'qnanewquestion')
121         );
122         $m->connect(
123             'main/qna/newanswer/:id',
124             array('action' => 'qnanewanswer'),
125             array('id' => $UUIDregex)
126         );
127         $m->connect(
128             'question/vote/:id',
129             array('action' => 'qnavote', 'type' => 'question'),
130             array('id' => $UUIDregex)
131         );
132         $m->connect(
133             'question/:id',
134             array('action' => 'qnashowquestion'),
135             array('id' => $UUIDregex)
136         );
137         $m->connect(
138             'answer/vote/:id',
139             array('action' => 'qnavote', 'type' => 'answer'),
140             array('id' => $UUIDregex)
141         );
142         $m->connect(
143             'answer/:id',
144             array('action' => 'qnashowanswer'),
145             array('id' => $UUIDregex)
146         );
147
148         return true;
149     }
150
151     function onPluginVersion(&$versions)
152     {
153         $versions[] = array(
154             'name'        => 'QnA',
155             'version'     => STATUSNET_VERSION,
156             'author'      => 'Zach Copley',
157             'homepage'    => 'http://status.net/wiki/Plugin:QnA',
158             'description' =>
159              _m('Question and Answers micro-app.')
160         );
161         return true;
162     }
163
164     function appTitle() {
165         return _m('Question');
166     }
167
168     function tag() {
169         return 'question';
170     }
171
172     function types() {
173         return array(
174             QnA_Question::OBJECT_TYPE,
175             QnA_Answer::OBJECT_TYPE
176         );
177     }
178
179     /**
180      * Given a parsed ActivityStreams activity, save it into a notice
181      * and other data structures.
182      *
183      * @param Activity $activity
184      * @param Profile $actor
185      * @param array $options=array()
186      *
187      * @return Notice the resulting notice
188      */
189     function saveNoticeFromActivity($activity, $actor, $options=array())
190     {
191         if (count($activity->objects) != 1) {
192             throw new Exception('Too many activity objects.');
193         }
194
195         $questionObj = $activity->objects[0];
196
197         if ($questinoObj->type != QnA_Question::OBJECT_TYPE) {
198             throw new Exception('Wrong type for object.');
199         }
200
201         $notice = null;
202
203         switch ($activity->verb) {
204         case ActivityVerb::POST:
205             $notice = QnA_Question::saveNew(
206                 $actor,
207                 $questionObj->title,
208                 $questionObj->summary,
209                 $options
210             );
211             break;
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.");
217             }
218             $notice = QnA_Answer::saveNew($actor, $question, $options);
219             break;
220         default:
221             throw new Exception("Unknown object type received by QnA Plugin");
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     function showNotice($notice, $out)
296     {
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);
302         default:
303             // TRANS: Exception thrown when performing an unexpected action on a question.
304             // TRANS: %s is the unpexpected object type.
305             throw new Exception(
306                 sprintf(
307                     _m('Unexpected type for QnA plugin: %s.'), 
308                     $notice->object_type
309                 )
310             );
311         }
312     }
313     
314     function showNoticeQuestion($notice, $out)
315     {
316         $user = common_current_user();
317
318         // @hack we want regular rendering, then just add stuff after that
319         $nli = new NoticeListItem($notice, $out);
320         $nli->showNotice();
321
322         $out->elementStart('div', array('class' => 'entry-content question-content'));
323         $question = QnA_Question::getByNotice($notice);
324         
325         if ($question) {
326             if ($user) {
327                 $profile = $user->getProfile();
328                 $answer = $question->getAnswer($profile);
329                 if ($answer) {
330                     // User has already answer; show the results.
331                     $form = new QnaansweredForm($answer, $out);
332                 } else {
333                     $form = new QnaanswerForm($question, $out);
334                 }
335                 $form->show();
336             }
337         } else {
338             $out->text(_m('Question data is missing'));
339         }
340         $out->elementEnd('div');
341
342         // @fixme
343         $out->elementStart('div', array('class' => 'entry-content'));
344     }
345
346     function showNoticeAnswer($notice, $out)
347     {
348         $user = common_current_user();
349
350         // @hack we want regular rendering, then just add stuff after that
351         $nli = new NoticeListItem($notice, $out);
352         $nli->showNotice();
353
354         // @fixme
355         $out->elementStart('div', array('class' => 'entry-content'));
356     }
357
358     /**
359      * Form for our app
360      *
361      * @param HTMLOutputter $out
362      * @return Widget
363      */
364
365     function entryForm($out)
366     {
367         return new QnaquestionForm($out);
368     }
369
370     /**
371      * When a notice is deleted, clean up related tables.
372      *
373      * @param Notice $notice
374      */
375
376     function deleteRelated($notice)
377     {
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);
382             $question->delete();
383             break;
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");
388             $answer->delete();
389             break;
390         default:
391             common_log(LOG_DEBUG, "Not deleting related, wtf...");
392         }
393     }
394
395     function onEndShowScripts($action)
396     {
397         // XXX maybe some cool shiz here
398     }
399
400     function onEndShowStyles($action)
401     {
402         $action->cssLink($this->path('css/qna.css'));
403         return true;
404     }
405 }