3 * Data class to mark a notice as a question
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')) {
35 * For storing a question
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_Question extends Managed_DataObject
47 const OBJECT_TYPE = 'http://activityschema.org/object/question';
49 public $__table = 'qna_question'; // table name
50 public $id; // char(36) primary key not null -> UUID
52 public $profile_id; // int -> profile.id
53 public $title; // text
54 public $description; // text
55 public $closed; // int (boolean) whether a question is closed
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_Question object found, or null for no hits
69 function staticGet($k, $v=null)
71 return Memcached_DataObject::staticGet('QnA_Question', $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 Bookmark object found, or null for no hits
88 return Memcached_DataObject::pkeyGet('QnA_Question', $kv);
92 * The One True Thingy that must be defined and declared.
94 public static function schemaDef()
97 'description' => 'Per-notice question data for QNA plugin',
103 'description' => 'UUID'
110 'profile_id' => array('type' => 'int'),
111 'title' => array('type' => 'text'),
112 'closed' => array('type' => 'int', 'size' => 'tiny'),
113 'description' => array('type' => 'text'),
115 'type' => 'datetime',
119 'primary key' => array('id'),
120 'unique keys' => array(
121 'question_uri_key' => array('uri'),
127 * Get a question based on a notice
129 * @param Notice $notice Notice to check for
131 * @return Question found question or null
133 function getByNotice($notice)
135 return self::staticGet('uri', $notice->uri);
140 return Notice::staticGet('uri', $this->uri);
145 return $this->getNotice()->bestUrl();
148 function getProfile()
150 $profile = Profile::staticGet('id', $this->profile_id);
151 if (empty($profile)) {
152 // TRANS: Exception trown when getting a profile for a non-existing ID.
153 // TRANS: %s is the provided profile ID.
154 throw new Exception(sprintf(_m('No profile with ID %s'),$this->profile_id));
160 * Get the answer from a particular user to this question, if any.
162 * @param Profile $profile
164 * @return Answer object or null
166 function getAnswer(Profile $profile)
168 $a = new QnA_Answer();
169 $a->question_id = $this->id;
170 $a->profile_id = $profile->id;
179 function getAnswers()
181 $a = new QnA_Answer();
182 $a->question_id = $this->id;
191 function countAnswers()
193 $a = new QnA_Answer();
195 $a->question_id = $this->id;
200 static function fromNotice($notice)
202 return QnA_Question::staticGet('uri', $notice->uri);
207 return self::toHTML($this->getProfile(), $this);
212 return self::toString($this->getProfile(), $this);
215 static function toHTML($profile, $question)
217 $notice = $question->getNotice();
219 $out = new XMLStringer();
221 $cls = array('qna_question');
223 if (!empty($question->closed)) {
227 $out->elementStart('p', array('class' => implode(' ', $cls)));
229 if (!empty($question->description)) {
230 $out->elementStart('span', 'question-description');
231 $out->raw(common_render_text($question->description));
232 $out->elementEnd('span');
235 $cnt = $question->countAnswers();
238 $out->elementStart('span', 'answer-count');
239 // TRANS: Number of given answers to a question.
240 // TRANS: %s is the number of given answers.
241 $out->text(sprintf(_m('%s answer','%s answers',$cnt), $cnt));
242 $out->elementEnd('span');
245 if (!empty($question->closed)) {
246 $out->elementStart('span', 'question-closed');
247 // TRANS: Notification that a question cannot be answered anymore because it is closed.
248 $out->text(_m('This question is closed.'));
249 $out->elementEnd('span');
252 $out->elementEnd('p');
254 return $out->getString();
257 static function toString($profile, $question, $answers)
259 return sprintf(htmlspecialchars($question->description));
263 * Save a new question notice
265 * @param Profile $profile
266 * @param string $question
267 * @param string $title
268 * @param string $description
269 * @param array $option // and whatnot
271 * @return Notice saved notice
273 static function saveNew($profile, $title, $description, $options = array())
275 $q = new QnA_Question();
277 $q->id = UUID::gen();
278 $q->profile_id = $profile->id;
280 $q->description = $description;
282 if (array_key_exists('created', $options)) {
283 $q->created = $options['created'];
285 $q->created = common_sql_now();
288 if (array_key_exists('uri', $options)) {
289 $q->uri = $options['uri'];
291 $q->uri = common_local_url(
293 array('id' => $q->id)
297 common_log(LOG_DEBUG, "Saving question: $q->id $q->uri");
300 if (Notice::contentTooLong($q->title . ' ' . $q->uri)) {
301 $max = Notice::maxContent();
302 $uriLen = mb_strlen($q->uri);
303 $targetLen = $max - ($uriLen + 15);
304 $title = mb_substr($q->title, 0, $targetLen) . '…';
307 $content = $title . ' ' . $q->uri;
309 $link = '<a href="' . htmlspecialchars($q->uri) . '">' . htmlspecialchars($q->title) . '</a>';
310 // TRANS: Rendered version of the notice content creating a question.
311 // TRANS: %s a link to the question as link description.
312 $rendered = sprintf(_m('Question: %s'), $link);
314 $tags = array('question');
317 $options = array_merge(
320 'rendered' => $rendered,
322 'replies' => $replies,
323 'object_type' => self::OBJECT_TYPE
328 if (!array_key_exists('uri', $options)) {
329 $options['uri'] = $q->uri;
332 $saved = Notice::saveNew(
335 array_key_exists('source', $options) ?
336 $options['source'] : 'web',