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
49 const QUESTION = 'http://activityschema.org/object/question';
51 public $__table = 'qna_question'; // table name
52 public $id; // char(36) primary key not null -> UUID
54 public $profile_id; // int -> profile.id
55 public $title; // text
56 public $description; // text
57 public $closed; // int (boolean) whether a question is closed
58 public $created; // datetime
61 * Get an instance by key
63 * This is a utility method to get a single instance with a given key value.
65 * @param string $k Key to use to lookup
66 * @param mixed $v Value to lookup
68 * @return QnA_Question object found, or null for no hits
71 function staticGet($k, $v=null)
73 return Memcached_DataObject::staticGet('QnA_Question', $k, $v);
77 * Get an instance by compound key
79 * This is a utility method to get a single instance with a given set of
80 * key-value pairs. Usually used for the primary key for a compound key; thus
83 * @param array $kv array of key-value mappings
85 * @return Bookmark object found, or null for no hits
90 return Memcached_DataObject::pkeyGet('QnA_Question', $kv);
94 * The One True Thingy that must be defined and declared.
96 public static function schemaDef()
99 'description' => 'Per-notice question data for QNA plugin',
105 'description' => 'UUID'
112 'profile_id' => array('type' => 'int'),
113 'title' => array('type' => 'text'),
114 'closed' => array('type' => 'int', size => 'tiny'),
115 'description' => array('type' => 'text'),
117 'type' => 'datetime',
121 'primary key' => array('id'),
122 'unique keys' => array(
123 'question_uri_key' => array('uri'),
129 * Get a question based on a notice
131 * @param Notice $notice Notice to check for
133 * @return Question found question or null
135 function getByNotice($notice)
137 return self::staticGet('uri', $notice->uri);
142 return Notice::staticGet('uri', $this->uri);
147 return $this->getNotice()->bestUrl();
151 * Get the answer from a particular user to this question, if any.
153 * @param Profile $profile
155 * @return Answer object or null
157 function getAnswer(Profile $profile)
159 $a = new QnA_Answer();
160 $a->question_id = $this->id;
161 $a->profile_id = $profile->id;
170 function countAnswers()
172 $a = new QnA_Answer();
173 $a->question_id = $this->id;
178 * Save a new question notice
180 * @param Profile $profile
181 * @param string $question
182 * @param string $title
183 * @param string $description
184 * @param array $option // and whatnot
186 * @return Notice saved notice
188 static function saveNew($profile, $question, $title, $description, $options = array())
190 $q = new QnA_Question();
192 $q->id = UUID::gen();
193 $q->profile_id = $profile->id;
195 $q->description = $description;
197 if (array_key_exists('created', $options)) {
198 $q->created = $options['created'];
200 $q->created = common_sql_now();
203 if (array_key_exists('uri', $options)) {
204 $q->uri = $options['uri'];
206 $q->uri = common_local_url(
208 array('id' => $q->id)
212 common_log(LOG_DEBUG, "Saving question: $q->id $q->uri");
215 // TRANS: Notice content creating a question.
216 // TRANS: %1$s is the title of the question, %2$s is a link to the question.
218 _m('question: %1$s %2$s'),
223 $link = '<a href="' . htmlspecialchars($q->uri) . '">' . htmlspecialchars($title) . '</a>';
224 // TRANS: Rendered version of the notice content creating a question.
225 // TRANS: %s a link to the question as link description.
226 $rendered = sprintf(_m('Question: %s'), $link);
228 $tags = array('question');
231 $options = array_merge(
234 'rendered' => $rendered,
236 'replies' => $replies,
237 'object_type' => QnAPlugin::QUESTION_OBJECT
242 if (!array_key_exists('uri', $options)) {
243 $options['uri'] = $p->uri;
246 $saved = Notice::saveNew(
249 array_key_exists('source', $options) ?
250 $options['source'] : 'web',