3 * Data class to save answers to questions
9 * @author Zach Copley <zach@status.net>
10 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11 * @link http://status.net/
13 * StatusNet - the distributed open-source microblogging tool
14 * Copyright (C) 2011, StatusNet, Inc.
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.
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.
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/>.
30 if (!defined('STATUSNET')) {
39 * @author Zach Copley <zach@status.net>
40 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
41 * @link http://status.net/
45 class QnA_Answer extends Managed_DataObject
47 const OBJECT_TYPE = 'http://activityschema.org/object/answer';
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
60 * The One True Thingy that must be defined and declared.
62 public static function schemaDef()
65 'description' => 'Record of answers to questions',
70 'not null' => true, 'description' => 'UUID of the response',
76 'description' => 'UUID to the answer notice',
78 'question_id' => array(
82 'description' => 'UUID of question being responded to',
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),
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'),
96 'profile_id_question_id_index' => array('profile_id', 'question_id'),
102 * Get an answer based on a notice
104 * @param Notice $notice Notice to check for
106 * @return QnA_Answer found response or null
108 static function getByNotice($notice)
110 $answer = self::getKV('uri', $notice->uri);
111 if (empty($answer)) {
112 throw new Exception("No answer with URI {$notice->uri}");
118 * Get the notice that belongs to this answer
124 return Notice::getKV('uri', $this->uri);
127 static function fromNotice($notice)
129 return QnA_Answer::getKV('uri', $notice->uri);
134 return $this->getNotice()->getUrl();
138 * Get the Question this is an answer to
140 * @return QnA_Question
142 function getQuestion()
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));
153 function getProfile()
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));
168 $this->getQuestion(),
175 return self::toString(
177 $this->getQuestion(),
182 static function toHTML($profile, $question, $answer)
184 $notice = $question->getNotice();
186 $out = new XMLStringer();
188 $cls = array('qna_answer');
189 if (!empty($answer->best)) {
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');
198 if (!empty($answer->revisions)) {
199 $out->elementstart('span', 'answer-revisions');
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)
207 $out->elementEnd('span');
210 $out->elementEnd('p');
212 return $out->getString();
215 static function toString($profile, $question, $answer)
217 // @todo FIXME: unused variable?
218 $notice = $question->getNotice();
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)
232 * Save a new answer notice
234 * @param Profile $profile
235 * @param Question $Question the question being answered
238 * @return Notice saved notice
240 static function saveNew($profile, $question, $text, $options = null)
242 if (empty($options)) {
246 $answer = new QnA_Answer();
247 $answer->id = UUID::gen();
248 $answer->profile_id = $profile->id;
249 $answer->question_id = $question->id;
250 $answer->revisions = 0;
252 $answer->content = $text;
253 $answer->created = common_sql_now();
254 $answer->uri = common_local_url(
256 array('id' => $answer->id)
259 common_log(LOG_DEBUG, "Saving answer: $answer->id, $answer->uri");
263 // TRANS: Text for a question that was answered.
264 // TRANS: %s is the question title.
269 $link = '<a href="' . htmlspecialchars($answer->uri) . '">' . htmlspecialchars($question->title) . '</a>';
270 // TRANS: Rendered version of the notice content answering a question.
271 // TRANS: %s a link to the question with question title as the link content.
272 $rendered = sprintf(_m('answered "%s"'), $link);
277 $options = array_merge(
280 'content' => $content,
281 'rendered' => $rendered,
283 'replies' => $replies,
284 'reply_to' => $question->getNotice()->id,
285 'object_type' => self::OBJECT_TYPE
290 if (!array_key_exists('uri', $options)) {
291 $options['uri'] = $answer->uri;
294 $saved = Notice::saveNew(
297 array_key_exists('source', $options) ?
298 $options['source'] : 'web',