]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/QnA/classes/QnA_Answer.php
Merge remote-tracking branch 'statusnet/master'
[quix0rs-gnu-social.git] / plugins / QnA / classes / QnA_Answer.php
1 <?php
2 /**
3  * Data class to save answers to questions
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 answers
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_Answer extends Managed_DataObject
46 {
47     const  OBJECT_TYPE = 'http://activityschema.org/object/answer';
48
49     public $__table = 'qna_answer'; // table name
50     public $id;          // char(36) primary key not null -> UUID
51     public $question_id; // char(36) -> question.id UUID
52     public $profile_id;  // int -> question.id
53     public $best;        // (boolean) int -> whether the question asker has marked this as the best answer
54     public $revisions;   // int -> count of revisions to this answer
55     public $content;     // text -> response text
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_Answer object found, or null for no hits
67      *
68      */
69     function staticGet($k, $v=null)
70     {
71         return Memcached_DataObject::staticGet('QnA_Answer', $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 QA_Answer object found, or null for no hits
84      *
85      */
86     function pkeyGet($kv)
87     {
88         return Memcached_DataObject::pkeyGet('QnA_Answer', $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' => 'Record of answers to questions',
98             'fields' => array(
99                 'id' => array(
100                     'type'     => 'char',
101                     'length'   => 36,
102                     'not null' => true, 'description' => 'UUID of the response'),
103                     'uri'      => array(
104                         'type'        => 'varchar',
105                         'length'      => 255,
106                         'not null'    => true,
107                         'description' => 'UUID to the answer notice'
108                     ),
109                     'question_id' => array(
110                         'type'        => 'char',
111                         'length'      => 36,
112                         'not null'    => true,
113                         'description' => 'UUID of question being responded to'
114                     ),
115                     'content'    => array('type' => 'text'), // got a better name?
116                     'best'       => array('type' => 'int', 'size' => 'tiny'),
117                     'revisions'  => array('type' => 'int'),
118                     'profile_id' => array('type' => 'int'),
119                     'created'    => array('type' => 'datetime', 'not null' => true),
120             ),
121             'primary key' => array('id'),
122             'unique keys' => array(
123                 'question_uri_key' => array('uri'),
124                 'question_id_profile_id_key' => array('question_id', 'profile_id'),
125             ),
126             'indexes' => array(
127                 'profile_id_question_id_index' => array('profile_id', 'question_id'),
128             )
129         );
130     }
131
132     /**
133      * Get an answer based on a notice
134      *
135      * @param Notice $notice Notice to check for
136      *
137      * @return QnA_Answer found response or null
138      */
139     function getByNotice($notice)
140     {
141         $answer = self::staticGet('uri', $notice->uri);
142         if (empty($answer)) {
143             throw new Exception("No answer with URI {$notice->uri}");
144         }
145         return $answer;
146     }
147
148     /**
149      * Get the notice that belongs to this answer
150      *
151      * @return Notice
152      */
153     function getNotice()
154     {
155         return Notice::staticGet('uri', $this->uri);
156     }
157
158     static function fromNotice($notice)
159     {
160         return QnA_Answer::staticGet('uri', $notice->uri);
161     }
162
163     function bestUrl()
164     {
165         return $this->getNotice()->bestUrl();
166     }
167
168     /**
169      * Get the Question this is an answer to
170      *
171      * @return QnA_Question
172      */
173     function getQuestion()
174     {
175         $question = QnA_Question::staticGet('id', $this->question_id);
176         if (empty($question)) {
177             // TRANS: Exception thown when getting a question with a non-existing ID.
178             // TRANS: %s is the non-existing question ID.
179             throw new Exception(sprintf(_m('No question with ID %s'),$this->question_id));
180         }
181         return $question;
182     }
183
184     function getProfile()
185     {
186         $profile = Profile::staticGet('id', $this->profile_id);
187         if (empty($profile)) {
188             // TRANS: Exception thown when getting a profile with a non-existing ID.
189             // TRANS: %s is the non-existing profile ID.
190             throw new Exception(sprintf(_m('No profile with ID %s'),$this->profile_id));
191         }
192         return $profile;
193     }
194
195     function asHTML()
196     {
197         return self::toHTML(
198             $this->getProfile(),
199             $this->getQuestion(),
200             $this
201         );
202     }
203
204     function asString()
205     {
206         return self::toString(
207             $this->getProfile(),
208             $this->getQuestion(),
209             $this
210         );
211     }
212
213     static function toHTML($profile, $question, $answer)
214     {
215         $notice = $question->getNotice();
216
217         $out = new XMLStringer();
218
219         $cls = array('qna_answer');
220         if (!empty($answer->best)) {
221             $cls[] = 'best';
222         }
223
224         $out->elementStart('p', array('class' => implode(' ', $cls)));
225         $out->elementStart('span', 'answer-content');
226         $out->raw(common_render_text($answer->content));
227         $out->elementEnd('span');
228
229         if (!empty($answer->revisions)) {
230             $out->elementstart('span', 'answer-revisions');
231             $out->text(
232                 htmlspecialchars(
233                     // Notification of how often an answer was revised.
234                     // TRANS: %s is the number of answer revisions.
235                     sprintf(_m('%s revision','%s revisions',$answer->revisions), $answer->revisions)
236                 )
237             );
238             $out->elementEnd('span');
239         }
240
241         $out->elementEnd('p');
242
243         return $out->getString();
244     }
245
246     static function toString($profile, $question, $answer)
247     {
248         // @todo FIXME: unused variable?
249         $notice = $question->getNotice();
250
251         return sprintf(
252             // TRANS: Text for a question that was answered.
253             // TRANS: %1$s is the user that answered, %2$s is the question title,
254             // TRANS: %2$s is the answer content.
255             _m('%1$s answered the question "%2$s": %3$s'),
256             htmlspecialchars($profile->getBestName()),
257             htmlspecialchars($question->title),
258             htmlspecialchars($answer->content)
259         );
260     }
261
262     /**
263      * Save a new answer notice
264      *
265      * @param Profile  $profile
266      * @param Question $Question the question being answered
267      * @param array
268      *
269      * @return Notice saved notice
270      */
271     static function saveNew($profile, $question, $text, $options = null)
272     {
273         if (empty($options)) {
274             $options = array();
275         }
276
277         $answer              = new QnA_Answer();
278         $answer->id          = UUID::gen();
279         $answer->profile_id  = $profile->id;
280         $answer->question_id = $question->id;
281         $answer->revisions   = 0;
282         $answer->best        = 0;
283         $answer->content     = $text;
284         $answer->created     = common_sql_now();
285         $answer->uri         = common_local_url(
286             'qnashowanswer',
287             array('id' => $answer->id)
288         );
289
290         common_log(LOG_DEBUG, "Saving answer: $answer->id, $answer->uri");
291         $answer->insert();
292
293         $content  = sprintf(
294             // TRANS: Text for a question that was answered.
295             // TRANS: %s is the question title.
296             _m('answered "%s"'),
297             $question->title
298         );
299
300         $link = '<a href="' . htmlspecialchars($answer->uri) . '">' . htmlspecialchars($question->title) . '</a>';
301         // TRANS: Rendered version of the notice content answering a question.
302         // TRANS: %s a link to the question with question title as the link content.
303         $rendered = sprintf(_m('answered "%s"'), $link);
304
305         $tags    = array();
306         $replies = array();
307
308         $options = array_merge(
309             array(
310                 'urls'        => array(),
311                 'content'     => $content,
312                 'rendered'    => $rendered,
313                 'tags'        => $tags,
314                 'replies'     => $replies,
315                 'reply_to'    => $question->getNotice()->id,
316                 'object_type' => self::OBJECT_TYPE
317             ),
318             $options
319         );
320
321         if (!array_key_exists('uri', $options)) {
322             $options['uri'] = $answer->uri;
323         }
324
325         $saved = Notice::saveNew(
326             $profile->id,
327             $content,
328             array_key_exists('source', $options) ?
329             $options['source'] : 'web',
330             $options
331         );
332
333         return $saved;
334     }
335 }