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