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 $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
59 * Get an instance by key
61 * This is a utility method to get a single instance with a given key value.
63 * @param string $k Key to use to lookup
64 * @param mixed $v Value to lookup
66 * @return QnA_Answer object found, or null for no hits
69 function staticGet($k, $v=null)
71 return Memcached_DataObject::staticGet('QnA_Answer', $k, $v);
75 * Get an instance by compound key
77 * This is a utility method to get a single instance with a given set of
78 * key-value pairs. Usually used for the primary key for a compound key; thus
81 * @param array $kv array of key-value mappings
83 * @return QA_Answer object found, or null for no hits
88 return Memcached_DataObject::pkeyGet('QnA_Answer', $kv);
92 * The One True Thingy that must be defined and declared.
94 public static function schemaDef()
97 'description' => 'Record of answers to questions',
102 'not null' => true, 'description' => 'UUID of the response'),
107 'description' => 'UUID to the answer notice'
109 'question_id' => array(
113 'description' => 'UUID of question being responded to'
115 'content' => array('type' => 'text'), // got a better name?
116 'best' => array('type' => 'int', 'size' => 'tiny'),
117 'revisions' => array('type' => 'int'),
118 'profile_id' => array('type' => 'int'),
119 'created' => array('type' => 'datetime', 'not null' => true),
121 'primary key' => array('id'),
122 'unique keys' => array(
123 'question_uri_key' => array('uri'),
124 'question_id_profile_id_key' => array('question_id', 'profile_id'),
127 'profile_id_question_id_index' => array('profile_id', 'question_id'),
133 * Get an answer based on a notice
135 * @param Notice $notice Notice to check for
137 * @return QnA_Answer found response or null
139 function getByNotice($notice)
141 $answer = self::staticGet('uri', $notice->uri);
142 if (empty($answer)) {
143 throw new Exception("No answer with URI {$this->notice->uri}");
149 * Get the notice that belongs to this answer
155 return Notice::staticGet('uri', $this->uri);
160 return $this->getNotice()->bestUrl();
164 * Get the Question this is an answer to
166 * @return QnA_Question
168 function getQuestion()
170 $question = QnA_Question::staticGet('id', $this->question_id);
171 if (empty($question)) {
172 throw new Exception("No question with ID {$this->question_id}");
177 function getProfile()
179 $profile = Profile::staticGet('id', $this->profile_id);
180 if (empty($profile)) {
181 throw new Exception("No profile with ID {$this->profile_id}");
190 $this->getQuestion(),
197 return self::toString(
199 $this->getQuestion(),
204 static function toHTML($profile, $question, $answer)
206 $notice = $question->getNotice();
208 $fmt = '<span class="answer_author"><a href="%1$s">answer</a> by <a href="%2$s">%3$s</a></span>';
209 $fmt .= '<span class="answer_content">%4$s</span>';
213 htmlspecialchars($notice->bestUrl()),
214 htmlspecialchars($profile->profileurl),
215 htmlspecialchars($profile->getBestName()),
216 htmlspecialchars($answer->content)
220 static function toString($profile, $question, $answer)
222 $notice = $question->getNotice();
225 '%1$s answered the question "%2$s": %3$s'
230 htmlspecialchars($profile->getBestName()),
231 htmlspecialchars($question->title),
232 htmlspecialchars($answer->content)
237 * Save a new answer notice
239 * @param Profile $profile
240 * @param Question $Question the question being answered
243 * @return Notice saved notice
245 static function saveNew($profile, $question, $text, $options = null)
247 if (empty($options)) {
251 $answer = new QnA_Answer();
252 $answer->id = UUID::gen();
253 $answer->profile_id = $profile->id;
254 $answer->question_id = $question->id;
255 $answer->revisions = 0;
257 $answer->content = $text;
258 $answer->created = common_sql_now();
259 $answer->uri = common_local_url(
261 array('id' => $answer->id)
264 common_log(LOG_DEBUG, "Saving answer: $answer->id, $answer->uri");
272 $link = '<a href="' . htmlspecialchars($answer->uri) . '">' . htmlspecialchars($question->title) . '</a>';
273 // TRANS: Rendered version of the notice content answering a question.
274 // TRANS: %s a link to the question with question title as the link content.
275 $rendered = sprintf(_m('answered "%s"'), $link);
280 $options = array_merge(
283 'content' => $content,
284 'rendered' => $rendered,
286 'replies' => $replies,
287 'reply_to' => $question->getNotice()->id,
288 'object_type' => self::OBJECT_TYPE
293 if (!array_key_exists('uri', $options)) {
294 $options['uri'] = $answer->uri;
297 $saved = Notice::saveNew(
300 array_key_exists('source', $options) ?
301 $options['source'] : 'web',