3 * Data class to save answers to questions
7 * @category QuestionAndAnswer
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')) {
37 * @category QuestionAndAnswer
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 Answer extends Managed_DataObject
47 public $__table = 'answer'; // table name
48 public $id; // char(36) primary key not null -> UUID
49 public $question_id; // char(36) -> question.id UUID
50 public $profile_id; // int -> question.id
51 public $votes; // int -> total number of votes (up & down)
52 public $best; // (int) boolean -> whether the question asker has marked this as the best answer
53 public $created; // datetime
56 * Get an instance by key
58 * This is a utility method to get a single instance with a given key value.
60 * @param string $k Key to use to lookup (usually 'user_id' for this class)
61 * @param mixed $v Value to lookup
63 * @return User_greeting_count object found, or null for no hits
66 function staticGet($k, $v=null)
68 return Memcached_DataObject::staticGet('Answer', $k, $v);
72 * Get an instance by compound key
74 * This is a utility method to get a single instance with a given set of
75 * key-value pairs. Usually used for the primary key for a compound key; thus
78 * @param array $kv array of key-value mappings
80 * @return Bookmark object found, or null for no hits
85 return Memcached_DataObject::pkeyGet('Answer', $kv);
89 * The One True Thingy that must be defined and declared.
91 public static function schemaDef()
94 'description' => 'Record of answers to questions',
96 'id' => array('type' => 'char', 'length' => 36, 'not null' => true, 'description' => 'UUID of the response'),
97 'uri' => array('type' => 'varchar', 'length' => 255, 'not null' => true, 'description' => 'UUID to the answer notice'),
98 'question_id' => array('type' => 'char', 'length' => 36, 'not null' => true, 'description' => 'UUID of question being responded to'),
99 'votes' => array('type' => 'int'),
100 'best' => array('type' => 'int'),
101 'profile_id' => array('type' => 'int'),
102 'created' => array('type' => 'datetime', 'not null' => true),
104 'primary key' => array('id'),
105 'unique keys' => array(
106 'question_uri_key' => array('uri'),
107 'question_id_profile_id_key' => array('question_id', 'profile_id'),
110 'profile_id_question_Id_index' => array('profile_id', 'question_id'),
116 * Get an answer based on a notice
118 * @param Notice $notice Notice to check for
120 * @return Answer found response or null
122 function getByNotice($notice)
124 return self::staticGet('uri', $notice->uri);
128 * Get the notice that belongs to this answer
134 return Notice::staticGet('uri', $this->uri);
139 return $this->getNotice()->bestUrl();
143 * Get the Question this is an answer to
147 function getQuestion()
149 return Question::staticGet('id', $this->question_id);
152 * Save a new answer notice
154 * @param Profile $profile
155 * @param Question $Question the question being answered
158 * @return Notice saved notice
160 static function saveNew($profile, $question, $options=null)
162 if (empty($options)) {
167 $a->id = UUID::gen();
168 $a->profile_id = $profile->id;
169 $a->question_id = $question->id;
170 $a->created = common_sql_now();
171 $a->uri = common_local_url(
173 array('id' => $pr->id)
176 common_log(LOG_DEBUG, "Saving answer: $pr->id $pr->uri");
179 // TRANS: Notice content answering a question.
180 // TRANS: %s is the answer
185 $link = '<a href="' . htmlspecialchars($question->uri) . '">' . htmlspecialchars($answer) . '</a>';
186 // TRANS: Rendered version of the notice content answering a question.
187 // TRANS: %s a link to the question with the chosen option as link description.
188 $rendered = sprintf(_m('answered "%s"'), $link);
193 $options = array_merge(array('urls' => array(),
194 'rendered' => $rendered,
196 'replies' => $replies,
197 'reply_to' => $question->getNotice()->id,
198 'object_type' => QuestionAndAnswer::ANSWER_OBJECT),
201 if (!array_key_exists('uri', $options)) {
202 $options['uri'] = $pr->uri;
205 $saved = Notice::saveNew($profile->id,
207 array_key_exists('source', $options) ?
208 $options['source'] : 'web',