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 * The One True Thingy that must be defined and declared.
61 public static function schemaDef()
64 'description' => 'Per-notice question data for QNA plugin',
70 'description' => 'UUID'
77 'profile_id' => array('type' => 'int'),
78 'title' => array('type' => 'text'),
79 'closed' => array('type' => 'int', 'size' => 'tiny'),
80 'description' => array('type' => 'text'),
86 'primary key' => array('id'),
87 'unique keys' => array(
88 'question_uri_key' => array('uri'),
94 * Get a question based on a notice
96 * @param Notice $notice Notice to check for
98 * @return Question found question or null
100 static function getByNotice(Notice $notice)
102 return self::getKV('uri', $notice->uri);
107 return Notice::getKV('uri', $this->uri);
112 return $this->getNotice()->getUrl();
115 function getProfile()
117 $profile = Profile::getKV('id', $this->profile_id);
118 if (empty($profile)) {
119 // TRANS: Exception trown when getting a profile for a non-existing ID.
120 // TRANS: %s is the provided profile ID.
121 throw new Exception(sprintf(_m('No profile with ID %s'),$this->profile_id));
127 * Get the answer from a particular user to this question, if any.
129 * @param Profile $profile
131 * @return Answer object or null
133 function getAnswer(Profile $profile)
135 $a = new QnA_Answer();
136 $a->question_id = $this->id;
137 $a->profile_id = $profile->id;
146 function getAnswers()
148 $a = new QnA_Answer();
149 $a->question_id = $this->id;
158 function countAnswers()
160 $a = new QnA_Answer();
162 $a->question_id = $this->id;
167 static function fromNotice(Notice $notice)
169 return QnA_Question::getKV('uri', $notice->uri);
174 return self::toHTML($this->getProfile(), $this);
179 return self::toString($this->getProfile(), $this);
182 static function toHTML(Profile $profile, $question)
184 $notice = $question->getNotice();
186 $out = new XMLStringer();
188 $cls = array('qna_question');
190 if (!empty($question->closed)) {
194 $out->elementStart('p', array('class' => implode(' ', $cls)));
196 if (!empty($question->description)) {
197 $out->elementStart('span', 'question-description');
198 $out->raw(common_render_text($question->description));
199 $out->elementEnd('span');
202 $cnt = $question->countAnswers();
205 $out->elementStart('span', 'answer-count');
206 // TRANS: Number of given answers to a question.
207 // TRANS: %s is the number of given answers.
208 $out->text(sprintf(_m('%s answer','%s answers',$cnt), $cnt));
209 $out->elementEnd('span');
212 if (!empty($question->closed)) {
213 $out->elementStart('span', 'question-closed');
214 // TRANS: Notification that a question cannot be answered anymore because it is closed.
215 $out->text(_m('This question is closed.'));
216 $out->elementEnd('span');
219 $out->elementEnd('p');
221 return $out->getString();
224 static function toString($profile, $question, $answers)
226 return sprintf(htmlspecialchars($question->description));
230 * Save a new question notice
232 * @param Profile $profile
233 * @param string $question
234 * @param string $title
235 * @param string $description
236 * @param array $option // and whatnot
238 * @return Notice saved notice
240 static function saveNew(Profile $profile, $title, $description, array $options = array())
242 $q = new QnA_Question();
244 $q->id = UUID::gen();
245 $q->profile_id = $profile->id;
247 $q->description = $description;
249 if (array_key_exists('created', $options)) {
250 $q->created = $options['created'];
252 $q->created = common_sql_now();
255 if (array_key_exists('uri', $options)) {
256 $q->uri = $options['uri'];
258 $q->uri = common_local_url(
260 array('id' => $q->id)
264 common_debug("Saving question: $q->id $q->uri");
267 if (Notice::contentTooLong($q->title . ' ' . $q->uri)) {
268 $max = Notice::maxContent();
269 $uriLen = mb_strlen($q->uri);
270 $targetLen = $max - ($uriLen + 15);
271 $title = mb_substr($q->title, 0, $targetLen) . '…';
274 $content = $title . ' ' . $q->uri;
276 $link = '<a href="' . htmlspecialchars($q->uri) . '">' . htmlspecialchars($q->title) . '</a>';
277 // TRANS: Rendered version of the notice content creating a question.
278 // TRANS: %s a link to the question as link description.
279 $rendered = sprintf(_m('Question: %s'), $link);
281 $tags = array('question');
284 $options = array_merge(
287 'rendered' => $rendered,
289 'replies' => $replies,
290 'object_type' => self::OBJECT_TYPE
295 if (!array_key_exists('uri', $options)) {
296 $options['uri'] = $q->uri;
299 $saved = Notice::saveNew(
302 array_key_exists('source', $options) ?
303 $options['source'] : 'web',