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