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 compound key
61 * This is a utility method to get a single instance with a given set of
62 * key-value pairs. Usually used for the primary key for a compound key; thus
65 * @param array $kv array of key-value mappings
67 * @return Bookmark object found, or null for no hits
72 return Memcached_DataObject::pkeyGet('QnA_Question', $kv);
76 * The One True Thingy that must be defined and declared.
78 public static function schemaDef()
81 'description' => 'Per-notice question data for QNA plugin',
87 'description' => 'UUID'
94 'profile_id' => array('type' => 'int'),
95 'title' => array('type' => 'text'),
96 'closed' => array('type' => 'int', 'size' => 'tiny'),
97 'description' => array('type' => 'text'),
103 'primary key' => array('id'),
104 'unique keys' => array(
105 'question_uri_key' => array('uri'),
111 * Get a question based on a notice
113 * @param Notice $notice Notice to check for
115 * @return Question found question or null
117 static function getByNotice($notice)
119 return self::staticGet('uri', $notice->uri);
124 return Notice::staticGet('uri', $this->uri);
129 return $this->getNotice()->bestUrl();
132 function getProfile()
134 $profile = Profile::staticGet('id', $this->profile_id);
135 if (empty($profile)) {
136 // TRANS: Exception trown when getting a profile for a non-existing ID.
137 // TRANS: %s is the provided profile ID.
138 throw new Exception(sprintf(_m('No profile with ID %s'),$this->profile_id));
144 * Get the answer from a particular user to this question, if any.
146 * @param Profile $profile
148 * @return Answer object or null
150 function getAnswer(Profile $profile)
152 $a = new QnA_Answer();
153 $a->question_id = $this->id;
154 $a->profile_id = $profile->id;
163 function getAnswers()
165 $a = new QnA_Answer();
166 $a->question_id = $this->id;
175 function countAnswers()
177 $a = new QnA_Answer();
179 $a->question_id = $this->id;
184 static function fromNotice($notice)
186 return QnA_Question::staticGet('uri', $notice->uri);
191 return self::toHTML($this->getProfile(), $this);
196 return self::toString($this->getProfile(), $this);
199 static function toHTML($profile, $question)
201 $notice = $question->getNotice();
203 $out = new XMLStringer();
205 $cls = array('qna_question');
207 if (!empty($question->closed)) {
211 $out->elementStart('p', array('class' => implode(' ', $cls)));
213 if (!empty($question->description)) {
214 $out->elementStart('span', 'question-description');
215 $out->raw(common_render_text($question->description));
216 $out->elementEnd('span');
219 $cnt = $question->countAnswers();
222 $out->elementStart('span', 'answer-count');
223 // TRANS: Number of given answers to a question.
224 // TRANS: %s is the number of given answers.
225 $out->text(sprintf(_m('%s answer','%s answers',$cnt), $cnt));
226 $out->elementEnd('span');
229 if (!empty($question->closed)) {
230 $out->elementStart('span', 'question-closed');
231 // TRANS: Notification that a question cannot be answered anymore because it is closed.
232 $out->text(_m('This question is closed.'));
233 $out->elementEnd('span');
236 $out->elementEnd('p');
238 return $out->getString();
241 static function toString($profile, $question, $answers)
243 return sprintf(htmlspecialchars($question->description));
247 * Save a new question notice
249 * @param Profile $profile
250 * @param string $question
251 * @param string $title
252 * @param string $description
253 * @param array $option // and whatnot
255 * @return Notice saved notice
257 static function saveNew($profile, $title, $description, $options = array())
259 $q = new QnA_Question();
261 $q->id = UUID::gen();
262 $q->profile_id = $profile->id;
264 $q->description = $description;
266 if (array_key_exists('created', $options)) {
267 $q->created = $options['created'];
269 $q->created = common_sql_now();
272 if (array_key_exists('uri', $options)) {
273 $q->uri = $options['uri'];
275 $q->uri = common_local_url(
277 array('id' => $q->id)
281 common_log(LOG_DEBUG, "Saving question: $q->id $q->uri");
284 if (Notice::contentTooLong($q->title . ' ' . $q->uri)) {
285 $max = Notice::maxContent();
286 $uriLen = mb_strlen($q->uri);
287 $targetLen = $max - ($uriLen + 15);
288 $title = mb_substr($q->title, 0, $targetLen) . '…';
291 $content = $title . ' ' . $q->uri;
293 $link = '<a href="' . htmlspecialchars($q->uri) . '">' . htmlspecialchars($q->title) . '</a>';
294 // TRANS: Rendered version of the notice content creating a question.
295 // TRANS: %s a link to the question as link description.
296 $rendered = sprintf(_m('Question: %s'), $link);
298 $tags = array('question');
301 $options = array_merge(
304 'rendered' => $rendered,
306 'replies' => $replies,
307 'object_type' => self::OBJECT_TYPE
312 if (!array_key_exists('uri', $options)) {
313 $options['uri'] = $q->uri;
316 $saved = Notice::saveNew(
319 array_key_exists('source', $options) ?
320 $options['source'] : 'web',