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