]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/QnA/classes/QnA_Question.php
Merge remote-tracking branch 'gitorious/1.0.x' 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
46 class QnA_Question extends Managed_DataObject
47 {
48     const OBJECT_TYPE = 'http://activityschema.org/object/question';
49
50     public $__table = 'qna_question'; // table name
51     public $id;          // char(36) primary key not null -> UUID
52     public $uri;
53     public $profile_id;  // int -> profile.id
54     public $title;       // text
55     public $description; // text
56     public $closed;      // int (boolean) whether a question is closed
57     public $created;     // datetime
58
59     /**
60      * Get an instance by key
61      *
62      * This is a utility method to get a single instance with a given key value.
63      *
64      * @param string $k Key to use to lookup
65      * @param mixed  $v Value to lookup
66      *
67      * @return QnA_Question object found, or null for no hits
68      *
69      */
70     function staticGet($k, $v=null)
71     {
72         return Memcached_DataObject::staticGet('QnA_Question', $k, $v);
73     }
74
75     /**
76      * Get an instance by compound key
77      *
78      * This is a utility method to get a single instance with a given set of
79      * key-value pairs. Usually used for the primary key for a compound key; thus
80      * the name.
81      *
82      * @param array $kv array of key-value mappings
83      *
84      * @return Bookmark object found, or null for no hits
85      *
86      */
87     function pkeyGet($kv)
88     {
89         return Memcached_DataObject::pkeyGet('QnA_Question', $kv);
90     }
91
92     /**
93      * The One True Thingy that must be defined and declared.
94      */
95     public static function schemaDef()
96     {
97         return array(
98             'description' => 'Per-notice question data for QNA plugin',
99             'fields' => array(
100                 'id' => array(
101                     'type'        => 'char',
102                     'length'      => 36,
103                     'not null'    => true,
104                     'description' => 'UUID'
105                 ),
106                 'uri' => array(
107                     'type'     => 'varchar',
108                     'length'   => 255,
109                     'not null' => true
110                 ),
111                 'profile_id'  => array('type' => 'int'),
112                 'title'       => array('type' => 'text'),
113                 'closed'      => array('type' => 'int', 'size' => 'tiny'),
114                 'description' => array('type' => 'text'),
115                 'created'     => array(
116                     'type'     => 'datetime',
117                     'not null' => true
118                 ),
119             ),
120             'primary key' => array('id'),
121             'unique keys' => array(
122                 'question_uri_key' => array('uri'),
123             ),
124         );
125     }
126
127     /**
128      * Get a question based on a notice
129      *
130      * @param Notice $notice Notice to check for
131      *
132      * @return Question found question or null
133      */
134     function getByNotice($notice)
135     {
136         return self::staticGet('uri', $notice->uri);
137     }
138
139     function getNotice()
140     {
141         return Notice::staticGet('uri', $this->uri);
142     }
143
144     function bestUrl()
145     {
146         return $this->getNotice()->bestUrl();
147     }
148
149     function getProfile()
150     {
151         $profile = Profile::staticGet('id', $this->profile_id);
152         if (empty($profile)) {
153             throw new Exception("No profile with ID {$this->profile_id}");
154         }
155         return $profile;
156     }
157
158     /**
159      * Get the answer from a particular user to this question, if any.
160      *
161      * @param Profile $profile
162      *
163      * @return Answer object or null
164      */
165     function getAnswer(Profile $profile)
166     {
167         $a = new QnA_Answer();
168         $a->question_id = $this->id;
169         $a->profile_id = $profile->id;
170         $a->find();
171         if ($a->fetch()) {
172             return $a;
173         } else {
174             return null;
175         }
176     }
177
178     function getAnswers()
179     {
180         $a = new QnA_Answer();
181         $a->question_id = $this->id;
182         $cnt = $a->find();
183         if (!empty($cnt)) {
184             return $a;
185         } else {
186             return null;
187         }
188     }
189
190     function countAnswers()
191     {
192         $a = new QnA_Answer();
193
194         $a->question_id = $this->id;
195
196         return $a->count();
197     }
198
199     static function fromNotice($notice)
200     {
201         return QnA_Question::staticGet('uri', $notice->uri);
202     }
203
204     function asHTML()
205     {
206         return self::toHTML($this->getProfile(), $this);
207     }
208
209     function asString()
210     {
211         return self::toString($this->getProfile(), $this);
212     }
213
214     static function toHTML($profile, $question)
215     {
216         $notice = $question->getNotice();
217
218         $out = new XMLStringer();
219
220         $cls = array('qna_question');
221
222         if (!empty($question->closed)) {
223             $cls[] = 'closed';
224         }
225
226         $out->elementStart('p', array('class' => implode(' ', $cls)));
227
228         if (!empty($question->description)) {
229             $out->elementStart('span', 'question-description');
230             $out->raw(QnAPlugin::shorten($question->description, $notice));
231             $out->elementEnd('span');
232         }
233
234         $cnt = $question->countAnswers();
235
236         if (!empty($cnt)) {
237             $out->elementStart('span', 'answer-count');
238             $out->text(sprintf(_m('%s answers'), $cnt));
239             $out->elementEnd('span');
240         }
241
242         if (!empty($question->closed)) {
243             $out->elementStart('span', 'question-closed');
244             $out->text(_m('This question is closed.'));
245             $out->elementEnd('span');
246         }
247
248         $out->elementEnd('p');
249
250         return $out->getString();
251     }
252
253     static function toString($profile, $question, $answers)
254     {
255         return sprintf(htmlspecialchars($question->description));
256     }
257
258     /**
259      * Save a new question notice
260      *
261      * @param Profile $profile
262      * @param string  $question
263      * @param string  $title
264      * @param string  $description
265      * @param array   $option // and whatnot
266      *
267      * @return Notice saved notice
268      */
269     static function saveNew($profile, $title, $description, $options = array())
270     {
271         $q = new QnA_Question();
272
273         $q->id          = UUID::gen();
274         $q->profile_id  = $profile->id;
275         $q->title       = $title;
276         $q->description = $description;
277
278         if (array_key_exists('created', $options)) {
279             $q->created = $options['created'];
280         } else {
281             $q->created = common_sql_now();
282         }
283
284         if (array_key_exists('uri', $options)) {
285             $q->uri = $options['uri'];
286         } else {
287             $q->uri = common_local_url(
288                 'qnashowquestion',
289                 array('id' => $q->id)
290             );
291         }
292
293         common_log(LOG_DEBUG, "Saving question: $q->id $q->uri");
294         $q->insert();
295
296         if (Notice::contentTooLong($q->title . ' ' . $q->uri)) {
297             $max       = Notice::maxContent();
298             $uriLen    = mb_strlen($q->uri);
299             $targetLen = $max - ($uriLen + 15);
300             $title = mb_substr($q->title, 0, $targetLen) . '…';
301
302         }
303
304         $content = $title . ' ' . $q->uri;
305
306         $link = '<a href="' . htmlspecialchars($q->uri) . '">' . htmlspecialchars($q->title) . '</a>';
307         // TRANS: Rendered version of the notice content creating a question.
308         // TRANS: %s a link to the question as link description.
309         $rendered = sprintf(_m('Question: %s'), $link);
310
311         $tags    = array('question');
312         $replies = array();
313
314         $options = array_merge(
315             array(
316                 'urls'        => array(),
317                 'rendered'    => $rendered,
318                 'tags'        => $tags,
319                 'replies'     => $replies,
320                 'object_type' => self::OBJECT_TYPE
321             ),
322             $options
323         );
324
325         if (!array_key_exists('uri', $options)) {
326             $options['uri'] = $q->uri;
327         }
328
329         $saved = Notice::saveNew(
330             $profile->id,
331             $content,
332             array_key_exists('source', $options) ?
333             $options['source'] : 'web',
334             $options
335         );
336
337         return $saved;
338     }
339 }