]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/QnA/QnAPlugin.php
Make new answers work
[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',
124             array('action' => 'qnanewanswer')
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 = QnA_Question::saveNew(
205                 $actor,
206                 $questionObj->title,
207                 $questionObj->summary,
208                 $options
209             );
210             break;
211         case Answer::ObjectType:
212             $question = QnA_Question::staticGet('uri', $questionObj->id);
213             if (empty($question)) {
214                 // FIXME: save the question
215                 throw new Exception("Answer to unknown question.");
216             }
217             $notice = QnA_Answer::saveNew($actor, $question, $options);
218             break;
219         default:
220             throw new Exception("Unknown object type received by QnA Plugin");
221         }
222
223         return $notice;
224     }
225
226     /**
227      * Turn a Notice into an activity object
228      *
229      * @param Notice $notice
230      *
231      * @return ActivityObject
232      */
233
234     function activityObjectFromNotice($notice)
235     {
236         $question = null;
237
238         switch ($notice->object_type) {
239         case QnA_Question::OBJECT_TYPE:
240             $question = QnA_Question::fromNotice($notice);
241             break;
242         case QnA_Answer::OBJECT_TYPE:
243             $answer   = QnA_Answer::fromNotice($notice);
244             $question = $answer->getQuestion();
245             break;
246         }
247
248         if (empty($question)) {
249             throw new Exception("Unknown object type.");
250         }
251
252         $notice = $question->getNotice();
253
254         if (empty($notice)) {
255             throw new Exception("Unknown question notice.");
256         }
257
258         $obj = new ActivityObject();
259
260         $obj->id      = $question->uri;
261         $obj->type    = QnA_Question::OBJECT_TYPE;
262         $obj->title   = $question->title;
263         $obj->link    = $notice->bestUrl();
264
265         // XXX: probably need other stuff here
266
267         return $obj;
268     }
269
270     /**
271      * Change the verb on Answer notices
272      *
273      * @param Notice $notice
274      *
275      * @return ActivityObject
276      */
277
278     function onEndNoticeAsActivity($notice, &$act) {
279         switch ($notice->object_type) {
280         case Answer::NORMAL:
281         case Answer::ANONYMOUS:
282             $act->verb = $notice->object_type;
283             break;
284         }
285         return true;
286     }
287
288     /**
289      * Custom HTML output for our notices
290      *
291      * @param Notice $notice
292      * @param HTMLOutputter $out
293      */
294     function showNotice($notice, $out)
295     {
296         switch ($notice->object_type) {
297         case QnA_Question::OBJECT_TYPE:
298             return $this->showNoticeQuestion($notice, $out);
299         case QnA_Answer::OBJECT_TYPE:
300             return $this->showNoticeAnswer($notice, $out);
301         default:
302             // TRANS: Exception thrown when performing an unexpected action on a question.
303             // TRANS: %s is the unpexpected object type.
304             throw new Exception(
305                 sprintf(
306                     _m('Unexpected type for QnA plugin: %s.'),
307                     $notice->object_type
308                 )
309             );
310         }
311     }
312
313     function showNoticeQuestion($notice, $out)
314     {
315         $user = common_current_user();
316
317         // @hack we want regular rendering, then just add stuff after that
318         $nli = new NoticeListItem($notice, $out);
319         $nli->showNotice();
320
321         $out->elementStart('div', array('class' => 'entry-content question-content'));
322         $question = QnA_Question::getByNotice($notice);
323
324         if ($question) {
325             if ($user) {
326                 $profile = $user->getProfile();
327                 $answer = $question->getAnswer($profile);
328                 if ($answer) {
329                     // User has already answer; show the results.
330                     $form = new QnaansweredForm($answer, $out);
331                 } else {
332                     $form = new QnaanswerForm($question, $out);
333                 }
334                 $form->show();
335             }
336         } else {
337             $out->text(_m('Question data is missing'));
338         }
339         $out->elementEnd('div');
340
341         // @fixme
342         $out->elementStart('div', array('class' => 'entry-content'));
343     }
344
345     function showNoticeAnswer($notice, $out)
346     {
347         $user = common_current_user();
348
349         // @hack we want regular rendering, then just add stuff after that
350         $nli = new NoticeListItem($notice, $out);
351         $nli->showNotice();
352
353         // @fixme
354         $out->elementStart('div', array('class' => 'entry-content'));
355     }
356
357     /**
358      * Form for our app
359      *
360      * @param HTMLOutputter $out
361      * @return Widget
362      */
363
364     function entryForm($out)
365     {
366         return new QnaquestionForm($out);
367     }
368
369     /**
370      * When a notice is deleted, clean up related tables.
371      *
372      * @param Notice $notice
373      */
374
375     function deleteRelated($notice)
376     {
377         switch ($notice->object_type) {
378         case QnA_Question::OBJECT_TYPE:
379             common_log(LOG_DEBUG, "Deleting question from notice...");
380             $question = QnA_Question::fromNotice($notice);
381             $question->delete();
382             break;
383         case QnA_Answer::OBJECT_TYPE:
384             common_log(LOG_DEBUG, "Deleting answer from notice...");
385             $answer = QnA_Answer::fromNotice($notice);
386             common_log(LOG_DEBUG, "to delete: $answer->id");
387             $answer->delete();
388             break;
389         default:
390             common_log(LOG_DEBUG, "Not deleting related, wtf...");
391         }
392     }
393
394     function onEndShowScripts($action)
395     {
396         // XXX maybe some cool shiz here
397     }
398
399     function onEndShowStyles($action)
400     {
401         $action->cssLink($this->path('css/qna.css'));
402         return true;
403     }
404 }