]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/QnA/QnAPlugin.php
QnA - save answer revisions and show # of revisions
[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 'QnareviseanswerAction':
86         case 'QnavoteAction':
87             include_once $dir . '/actions/'
88                 . strtolower(mb_substr($cls, 0, -6)) . '.php';
89             return false;
90         case 'QnaquestionForm':
91         case 'QnaanswerForm':
92         case 'QnareviseanswerForm':
93         case 'QnavoteForm':
94             include_once $dir . '/lib/' . strtolower($cls).'.php';
95             break;
96         case 'QnA_Question':
97         case 'QnA_Answer':
98         case 'QnA_Vote':
99             include_once $dir . '/classes/' . $cls.'.php';
100             return false;
101             break;
102         default:
103             return true;
104         }
105     }
106
107     /**
108      * Map URLs to actions
109      *
110      * @param Net_URL_Mapper $m path-to-action mapper
111      *
112      * @return boolean hook value; true means continue processing, false means stop.
113      */
114
115     function onRouterInitialized($m)
116     {
117         $UUIDregex = '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}';
118
119         $m->connect(
120             'main/qna/newquestion',
121             array('action' => 'qnanewquestion')
122         );
123         $m->connect(
124             'main/qna/newanswer',
125             array('action' => 'qnanewanswer')
126         );
127         $m->connect(
128             'main/qna/reviseanswer',
129             array('action' => 'qnareviseanswer')
130         );
131         $m->connect(
132             'question/vote/:id',
133             array('action' => 'qnavote', 'type' => 'question'),
134             array('id' => $UUIDregex)
135         );
136         $m->connect(
137             'question/:id',
138             array('action' => 'qnashowquestion'),
139             array('id' => $UUIDregex)
140         );
141         $m->connect(
142             'answer/vote/:id',
143             array('action' => 'qnavote', 'type' => 'answer'),
144             array('id' => $UUIDregex)
145         );
146         $m->connect(
147             'answer/:id',
148             array('action' => 'qnashowanswer'),
149             array('id' => $UUIDregex)
150         );
151
152         return true;
153     }
154
155     function onPluginVersion(&$versions)
156     {
157         $versions[] = array(
158             'name'        => 'QnA',
159             'version'     => STATUSNET_VERSION,
160             'author'      => 'Zach Copley',
161             'homepage'    => 'http://status.net/wiki/Plugin:QnA',
162             'description' =>
163              _m('Question and Answers micro-app.')
164         );
165         return true;
166     }
167
168     function appTitle() {
169         return _m('Question');
170     }
171
172     function tag() {
173         return 'question';
174     }
175
176     function types() {
177         return array(
178             QnA_Question::OBJECT_TYPE,
179             QnA_Answer::OBJECT_TYPE
180         );
181     }
182
183     /**
184      * Given a parsed ActivityStreams activity, save it into a notice
185      * and other data structures.
186      *
187      * @param Activity $activity
188      * @param Profile $actor
189      * @param array $options=array()
190      *
191      * @return Notice the resulting notice
192      */
193     function saveNoticeFromActivity($activity, $actor, $options=array())
194     {
195         if (count($activity->objects) != 1) {
196             throw new Exception('Too many activity objects.');
197         }
198
199         $questionObj = $activity->objects[0];
200
201         if ($questinoObj->type != QnA_Question::OBJECT_TYPE) {
202             throw new Exception('Wrong type for object.');
203         }
204
205         $notice = null;
206
207         switch ($activity->verb) {
208         case ActivityVerb::POST:
209             $notice = QnA_Question::saveNew(
210                 $actor,
211                 $questionObj->title,
212                 $questionObj->summary,
213                 $options
214             );
215             break;
216         case Answer::ObjectType:
217             $question = QnA_Question::staticGet('uri', $questionObj->id);
218             if (empty($question)) {
219                 // FIXME: save the question
220                 throw new Exception("Answer to unknown question.");
221             }
222             $notice = QnA_Answer::saveNew($actor, $question, $options);
223             break;
224         default:
225             throw new Exception("Unknown object type received by QnA Plugin");
226         }
227
228         return $notice;
229     }
230
231     /**
232      * Turn a Notice into an activity object
233      *
234      * @param Notice $notice
235      *
236      * @return ActivityObject
237      */
238
239     function activityObjectFromNotice($notice)
240     {
241         $question = null;
242
243         switch ($notice->object_type) {
244         case QnA_Question::OBJECT_TYPE:
245             $question = QnA_Question::fromNotice($notice);
246             break;
247         case QnA_Answer::OBJECT_TYPE:
248             $answer   = QnA_Answer::fromNotice($notice);
249             $question = $answer->getQuestion();
250             break;
251         }
252
253         if (empty($question)) {
254             throw new Exception("Unknown object type.");
255         }
256
257         $notice = $question->getNotice();
258
259         if (empty($notice)) {
260             throw new Exception("Unknown question notice.");
261         }
262
263         $obj = new ActivityObject();
264
265         $obj->id      = $question->uri;
266         $obj->type    = QnA_Question::OBJECT_TYPE;
267         $obj->title   = $question->title;
268         $obj->link    = $notice->bestUrl();
269
270         // XXX: probably need other stuff here
271
272         return $obj;
273     }
274
275     /**
276      * Change the verb on Answer notices
277      *
278      * @param Notice $notice
279      *
280      * @return ActivityObject
281      */
282
283     function onEndNoticeAsActivity($notice, &$act) {
284         switch ($notice->object_type) {
285         case Answer::NORMAL:
286         case Answer::ANONYMOUS:
287             $act->verb = $notice->object_type;
288             break;
289         }
290         return true;
291     }
292
293     /**
294      * Custom HTML output for our notices
295      *
296      * @param Notice $notice
297      * @param HTMLOutputter $out
298      */
299     function showNotice($notice, $out)
300     {
301         switch ($notice->object_type) {
302         case QnA_Question::OBJECT_TYPE:
303             return $this->showNoticeQuestion($notice, $out);
304         case QnA_Answer::OBJECT_TYPE:
305             return $this->showNoticeAnswer($notice, $out);
306         default:
307             // TRANS: Exception thrown when performing an unexpected action on a question.
308             // TRANS: %s is the unpexpected object type.
309             throw new Exception(
310                 sprintf(
311                     _m('Unexpected type for QnA plugin: %s.'),
312                     $notice->object_type
313                 )
314             );
315         }
316     }
317
318     function showNoticeQuestion($notice, $out)
319     {
320         $user = common_current_user();
321
322         // @hack we want regular rendering, then just add stuff after that
323         $nli = new NoticeListItem($notice, $out);
324         $nli->showNotice();
325
326         $out->elementStart('div', array('class' => 'entry-content question-content'));
327         $question = QnA_Question::getByNotice($notice);
328
329         if ($question) {
330             if ($user) {
331                 $profile = $user->getProfile();
332                 $answer = $question->getAnswer($profile);
333                 if ($answer) {
334                     // User has already answer; show the results.
335                     $form = new QnareviseanswerForm($answer, $out);
336                 } else {
337                     $form = new QnaanswerForm($question, $out);
338                 }
339                 $form->show();
340             }
341         } else {
342             $out->text(_m('Question data is missing'));
343         }
344         $out->elementEnd('div');
345
346         // @fixme
347         $out->elementStart('div', array('class' => 'entry-content'));
348     }
349
350     function showNoticeAnswer($notice, $out)
351     {
352         $user = common_current_user();
353
354         // @hack we want regular rendering, then just add stuff after that
355         $nli = new NoticeListItem($notice, $out);
356         $nli->showNotice();
357
358         // @fixme
359         $out->elementStart('div', array('class' => 'entry-content'));
360     }
361
362     /**
363      * Form for our app
364      *
365      * @param HTMLOutputter $out
366      * @return Widget
367      */
368
369     function entryForm($out)
370     {
371         return new QnaquestionForm($out);
372     }
373
374     /**
375      * When a notice is deleted, clean up related tables.
376      *
377      * @param Notice $notice
378      */
379
380     function deleteRelated($notice)
381     {
382         switch ($notice->object_type) {
383         case QnA_Question::OBJECT_TYPE:
384             common_log(LOG_DEBUG, "Deleting question from notice...");
385             $question = QnA_Question::fromNotice($notice);
386             $question->delete();
387             break;
388         case QnA_Answer::OBJECT_TYPE:
389             common_log(LOG_DEBUG, "Deleting answer from notice...");
390             $answer = QnA_Answer::fromNotice($notice);
391             common_log(LOG_DEBUG, "to delete: $answer->id");
392             $answer->delete();
393             break;
394         default:
395             common_log(LOG_DEBUG, "Not deleting related, wtf...");
396         }
397     }
398
399     function onEndShowScripts($action)
400     {
401         // XXX maybe some cool shiz here
402     }
403
404     function onEndShowStyles($action)
405     {
406         $action->cssLink($this->path('css/qna.css'));
407         return true;
408     }
409 }