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 ANSWER = '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; // (int) boolean -> whether the question asker has marked this as the best answer
54 public $created; // datetime
57 * Get an instance by key
59 * This is a utility method to get a single instance with a given key value.
61 * @param string $k Key to use to lookup
62 * @param mixed $v Value to lookup
64 * @return QnA_Answer object found, or null for no hits
67 function staticGet($k, $v=null)
69 return Memcached_DataObject::staticGet('QnA_Answer', $k, $v);
73 * Get an instance by compound key
75 * This is a utility method to get a single instance with a given set of
76 * key-value pairs. Usually used for the primary key for a compound key; thus
79 * @param array $kv array of key-value mappings
81 * @return QA_Answer object found, or null for no hits
86 return Memcached_DataObject::pkeyGet('QnA_Answer', $kv);
90 * The One True Thingy that must be defined and declared.
92 public static function schemaDef()
95 'description' => 'Record of answers to questions',
100 'not null' => true, 'description' => 'UUID of the response'),
105 'description' => 'UUID to the answer notice'
107 'question_id' => array(
111 'description' => 'UUID of question being responded to'
113 'best' => array('type' => 'int', 'size' => 'tiny'),
114 'profile_id' => array('type' => 'int'),
115 'created' => array('type' => 'datetime', 'not null' => true),
117 'primary key' => array('id'),
118 'unique keys' => array(
119 'question_uri_key' => array('uri'),
120 'question_id_profile_id_key' => array('question_id', 'profile_id'),
123 'profile_id_question_id_index' => array('profile_id', 'question_id'),
129 * Get an answer based on a notice
131 * @param Notice $notice Notice to check for
133 * @return QnA_Answer found response or null
135 function getByNotice($notice)
137 return self::staticGet('uri', $notice->uri);
141 * Get the notice that belongs to this answer
147 return Notice::staticGet('uri', $this->uri);
152 return $this->getNotice()->bestUrl();
156 * Get the Question this is an answer to
158 * @return QnA_Question
160 function getQuestion()
162 return Question::staticGet('id', $this->question_id);
166 * Save a new answer notice
168 * @param Profile $profile
169 * @param Question $Question the question being answered
172 * @return Notice saved notice
174 static function saveNew($profile, $question, $options=null)
176 if (empty($options)) {
181 $a->id = UUID::gen();
182 $a->profile_id = $profile->id;
183 $a->question_id = $question->id;
184 $a->created = common_sql_now();
185 $a->uri = common_local_url(
187 array('id' => $pr->id)
190 common_log(LOG_DEBUG, "Saving answer: $pr->id $pr->uri");
193 // TRANS: Notice content answering a question.
194 // TRANS: %s is the answer
199 $link = '<a href="' . htmlspecialchars($question->uri) . '">' . htmlspecialchars($answer) . '</a>';
200 // TRANS: Rendered version of the notice content answering a question.
201 // TRANS: %s a link to the question with question title as the link content.
202 $rendered = sprintf(_m('answered "%s"'), $link);
207 $options = array_merge(
210 'rendered' => $rendered,
212 'replies' => $replies,
213 'reply_to' => $question->getNotice()->id,
214 'object_type' => QnA::ANSWER_OBJECT),
218 if (!array_key_exists('uri', $options)) {
219 $options['uri'] = $pr->uri;
222 $saved = Notice::saveNew(
225 array_key_exists('source', $options) ?
226 $options['source'] : 'web',