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