]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/QnA/classes/QnA_Question.php
Merge branch 'testing' into 1.0.x
[quix0rs-gnu-social.git] / plugins / QnA / classes / QnA_Question.php
1 <?php
2 /**
3  * Data class to mark a notice as a question
4  *
5  * PHP version 5
6  *
7  * @category QnA
8  * @package  StatusNet
9  * @author   Zach Copley <zach@status.net>
10  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11  * @link     http://status.net/
12  *
13  * StatusNet - the distributed open-source microblogging tool
14  * Copyright (C) 2011, StatusNet, Inc.
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Affero General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
24  * GNU Affero General Public License for more details.
25  *
26  * You should have received a copy of the GNU Affero General Public License
27  * along with this program. If not, see <http://www.gnu.org/licenses/>.
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  * For storing a question
36  *
37  * @category QnA
38  * @package  StatusNet
39  * @author   Zach Copley <zach@status.net>
40  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
41  * @link     http://status.net/
42  *
43  * @see      DB_DataObject
44  */
45 class QnA_Question extends Managed_DataObject
46 {
47     const OBJECT_TYPE = 'http://activityschema.org/object/question';
48
49     public $__table = 'qna_question'; // table name
50     public $id;          // char(36) primary key not null -> UUID
51     public $uri;
52     public $profile_id;  // int -> profile.id
53     public $title;       // text
54     public $description; // text
55     public $closed;      // int (boolean) whether a question is closed
56     public $created;     // datetime
57
58     /**
59      * Get an instance by key
60      *
61      * This is a utility method to get a single instance with a given key value.
62      *
63      * @param string $k Key to use to lookup
64      * @param mixed  $v Value to lookup
65      *
66      * @return QnA_Question object found, or null for no hits
67      *
68      */
69     function staticGet($k, $v=null)
70     {
71         return Memcached_DataObject::staticGet('QnA_Question', $k, $v);
72     }
73
74     /**
75      * Get an instance by compound key
76      *
77      * This is a utility method to get a single instance with a given set of
78      * key-value pairs. Usually used for the primary key for a compound key; thus
79      * the name.
80      *
81      * @param array $kv array of key-value mappings
82      *
83      * @return Bookmark object found, or null for no hits
84      *
85      */
86     function pkeyGet($kv)
87     {
88         return Memcached_DataObject::pkeyGet('QnA_Question', $kv);
89     }
90
91     /**
92      * The One True Thingy that must be defined and declared.
93      */
94     public static function schemaDef()
95     {
96         return array(
97             'description' => 'Per-notice question data for QNA plugin',
98             'fields' => array(
99                 'id' => array(
100                     'type'        => 'char',
101                     'length'      => 36,
102                     'not null'    => true,
103                     'description' => 'UUID'
104                 ),
105                 'uri' => array(
106                     'type'     => 'varchar',
107                     'length'   => 255,
108                     'not null' => true
109                 ),
110                 'profile_id'  => array('type' => 'int'),
111                 'title'       => array('type' => 'text'),
112                 'closed'      => array('type' => 'int', 'size' => 'tiny'),
113                 'description' => array('type' => 'text'),
114                 'created'     => array(
115                     'type'     => 'datetime',
116                     'not null' => true
117                 ),
118             ),
119             'primary key' => array('id'),
120             'unique keys' => array(
121                 'question_uri_key' => array('uri'),
122             ),
123         );
124     }
125
126     /**
127      * Get a question based on a notice
128      *
129      * @param Notice $notice Notice to check for
130      *
131      * @return Question found question or null
132      */
133     function getByNotice($notice)
134     {
135         return self::staticGet('uri', $notice->uri);
136     }
137
138     function getNotice()
139     {
140         return Notice::staticGet('uri', $this->uri);
141     }
142
143     function bestUrl()
144     {
145         return $this->getNotice()->bestUrl();
146     }
147
148     function getProfile()
149     {
150         $profile = Profile::staticGet('id', $this->profile_id);
151         if (empty($profile)) {
152             // TRANS: Exception trown when getting a profile for a non-existing ID.
153             // TRANS: %s is the provided profile ID.
154             throw new Exception(sprintf(_m('No profile with ID %s'),$this->profile_id));
155         }
156         return $profile;
157     }
158
159     /**
160      * Get the answer from a particular user to this question, if any.
161      *
162      * @param Profile $profile
163      *
164      * @return Answer object or null
165      */
166     function getAnswer(Profile $profile)
167     {
168         $a = new QnA_Answer();
169         $a->question_id = $this->id;
170         $a->profile_id = $profile->id;
171         $a->find();
172         if ($a->fetch()) {
173             return $a;
174         } else {
175             return null;
176         }
177     }
178
179     function getAnswers()
180     {
181         $a = new QnA_Answer();
182         $a->question_id = $this->id;
183         $cnt = $a->find();
184         if (!empty($cnt)) {
185             return $a;
186         } else {
187             return null;
188         }
189     }
190
191     function countAnswers()
192     {
193         $a = new QnA_Answer();
194
195         $a->question_id = $this->id;
196
197         return $a->count();
198     }
199
200     static function fromNotice($notice)
201     {
202         return QnA_Question::staticGet('uri', $notice->uri);
203     }
204
205     function asHTML()
206     {
207         return self::toHTML($this->getProfile(), $this);
208     }
209
210     function asString()
211     {
212         return self::toString($this->getProfile(), $this);
213     }
214
215     static function toHTML($profile, $question)
216     {
217         $notice = $question->getNotice();
218
219         $out = new XMLStringer();
220
221         $cls = array('qna_question');
222
223         if (!empty($question->closed)) {
224             $cls[] = 'closed';
225         }
226
227         $out->elementStart('p', array('class' => implode(' ', $cls)));
228
229         if (!empty($question->description)) {
230             $out->elementStart('span', 'question-description');
231             $out->raw(common_render_text($question->description));
232             $out->elementEnd('span');
233         }
234
235         $cnt = $question->countAnswers();
236
237         if (!empty($cnt)) {
238             $out->elementStart('span', 'answer-count');
239             // TRANS: Number of given answers to a question.
240             // TRANS: %s is the number of given answers.
241             $out->text(sprintf(_m('%s answer','%s answers',$cnt), $cnt));
242             $out->elementEnd('span');
243         }
244
245         if (!empty($question->closed)) {
246             $out->elementStart('span', 'question-closed');
247             // TRANS: Notification that a question cannot be answered anymore because it is closed.
248             $out->text(_m('This question is closed.'));
249             $out->elementEnd('span');
250         }
251
252         $out->elementEnd('p');
253
254         return $out->getString();
255     }
256
257     static function toString($profile, $question, $answers)
258     {
259         return sprintf(htmlspecialchars($question->description));
260     }
261
262     /**
263      * Save a new question notice
264      *
265      * @param Profile $profile
266      * @param string  $question
267      * @param string  $title
268      * @param string  $description
269      * @param array   $option // and whatnot
270      *
271      * @return Notice saved notice
272      */
273     static function saveNew($profile, $title, $description, $options = array())
274     {
275         $q = new QnA_Question();
276
277         $q->id          = UUID::gen();
278         $q->profile_id  = $profile->id;
279         $q->title       = $title;
280         $q->description = $description;
281
282         if (array_key_exists('created', $options)) {
283             $q->created = $options['created'];
284         } else {
285             $q->created = common_sql_now();
286         }
287
288         if (array_key_exists('uri', $options)) {
289             $q->uri = $options['uri'];
290         } else {
291             $q->uri = common_local_url(
292                 'qnashowquestion',
293                 array('id' => $q->id)
294             );
295         }
296
297         common_log(LOG_DEBUG, "Saving question: $q->id $q->uri");
298         $q->insert();
299
300         if (Notice::contentTooLong($q->title . ' ' . $q->uri)) {
301             $max       = Notice::maxContent();
302             $uriLen    = mb_strlen($q->uri);
303             $targetLen = $max - ($uriLen + 15);
304             $title = mb_substr($q->title, 0, $targetLen) . '…';
305         }
306
307         $content = $title . ' ' . $q->uri;
308
309         $link = '<a href="' . htmlspecialchars($q->uri) . '">' . htmlspecialchars($q->title) . '</a>';
310         // TRANS: Rendered version of the notice content creating a question.
311         // TRANS: %s a link to the question as link description.
312         $rendered = sprintf(_m('Question: %s'), $link);
313
314         $tags    = array('question');
315         $replies = array();
316
317         $options = array_merge(
318             array(
319                 'urls'        => array(),
320                 'rendered'    => $rendered,
321                 'tags'        => $tags,
322                 'replies'     => $replies,
323                 'object_type' => self::OBJECT_TYPE
324             ),
325             $options
326         );
327
328         if (!array_key_exists('uri', $options)) {
329             $options['uri'] = $q->uri;
330         }
331
332         $saved = Notice::saveNew(
333             $profile->id,
334             $content,
335             array_key_exists('source', $options) ?
336             $options['source'] : 'web',
337             $options
338         );
339
340         return $saved;
341     }
342 }