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/
46 class QnA_Question extends Managed_DataObject
48 const OBJECT_TYPE = 'http://activityschema.org/object/question';
50 public $__table = 'qna_question'; // table name
51 public $id; // char(36) primary key not null -> UUID
53 public $profile_id; // int -> profile.id
54 public $title; // text
55 public $description; // text
56 public $closed; // int (boolean) whether a question is closed
57 public $created; // datetime
60 * Get an instance by key
62 * This is a utility method to get a single instance with a given key value.
64 * @param string $k Key to use to lookup
65 * @param mixed $v Value to lookup
67 * @return QnA_Question object found, or null for no hits
70 function staticGet($k, $v=null)
72 return Memcached_DataObject::staticGet('QnA_Question', $k, $v);
76 * Get an instance by compound key
78 * This is a utility method to get a single instance with a given set of
79 * key-value pairs. Usually used for the primary key for a compound key; thus
82 * @param array $kv array of key-value mappings
84 * @return Bookmark object found, or null for no hits
89 return Memcached_DataObject::pkeyGet('QnA_Question', $kv);
93 * The One True Thingy that must be defined and declared.
95 public static function schemaDef()
98 'description' => 'Per-notice question data for QNA plugin',
104 'description' => 'UUID'
111 'profile_id' => array('type' => 'int'),
112 'title' => array('type' => 'text'),
113 'closed' => array('type' => 'int', 'size' => 'tiny'),
114 'description' => array('type' => 'text'),
116 'type' => 'datetime',
120 'primary key' => array('id'),
121 'unique keys' => array(
122 'question_uri_key' => array('uri'),
128 * Get a question based on a notice
130 * @param Notice $notice Notice to check for
132 * @return Question found question or null
134 function getByNotice($notice)
136 return self::staticGet('uri', $notice->uri);
141 return Notice::staticGet('uri', $this->uri);
146 return $this->getNotice()->bestUrl();
149 function getProfile()
151 $profile = Profile::staticGet('id', $this->profile_id);
152 if (empty($profile)) {
153 throw new Exception("No profile with ID {$this->profile_id}");
159 * Get the answer from a particular user to this question, if any.
161 * @param Profile $profile
163 * @return Answer object or null
165 function getAnswer(Profile $profile)
167 $a = new QnA_Answer();
168 $a->question_id = $this->id;
169 $a->profile_id = $profile->id;
178 function getAnswers()
180 $a = new QnA_Answer();
181 $a->question_id = $this->id;
190 function countAnswers()
192 $a = new QnA_Answer();
193 $a->question_id = $this->id;
197 static function fromNotice($notice)
199 return QnA_Question::staticGet('uri', $notice->uri);
204 return self::toHTML($this->getProfile(), $this);
209 return self::toString($this->getProfile(), $this);
212 static function toHTML($profile, $question)
214 $notice = $question->getNotice();
216 $fmt = '<span class="question_description">%s</span>';
220 htmlspecialchars($question->description)
226 static function toString($profile, $question, $answers)
228 return sprintf(htmlspecialchars($question->description));
232 * Save a new question notice
234 * @param Profile $profile
235 * @param string $question
236 * @param string $title
237 * @param string $description
238 * @param array $option // and whatnot
240 * @return Notice saved notice
242 static function saveNew($profile, $title, $description, $options = array())
244 $q = new QnA_Question();
246 $q->id = UUID::gen();
247 $q->profile_id = $profile->id;
249 $q->description = $description;
251 if (array_key_exists('created', $options)) {
252 $q->created = $options['created'];
254 $q->created = common_sql_now();
257 if (array_key_exists('uri', $options)) {
258 $q->uri = $options['uri'];
260 $q->uri = common_local_url(
262 array('id' => $q->id)
266 common_log(LOG_DEBUG, "Saving question: $q->id $q->uri");
269 // TRANS: Notice content creating a question.
270 // TRANS: %1$s is the title of the question, %2$s is a link to the question.
272 _m('question: %1$s %2$s'),
277 $link = '<a href="' . htmlspecialchars($q->uri) . '">' . htmlspecialchars($title) . '</a>';
278 // TRANS: Rendered version of the notice content creating a question.
279 // TRANS: %s a link to the question as link description.
280 $rendered = sprintf(_m('Question: %s'), $link);
282 $tags = array('question');
285 $options = array_merge(
288 'rendered' => $rendered,
290 'replies' => $replies,
291 'object_type' => self::OBJECT_TYPE
296 if (!array_key_exists('uri', $options)) {
297 $options['uri'] = $q->uri;
300 $saved = Notice::saveNew(
303 array_key_exists('source', $options) ?
304 $options['source'] : 'web',