3 * Data class to mark a notice as a question
7 * @category QuestionAndAnswer
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
37 * @category QuestionAndAnswer
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 Question extends Managed_DataObject
48 public $__table = 'question'; // table name
49 public $id; // char(36) primary key not null -> UUID
51 public $profile_id; // int -> profile.id
52 public $title; // text
53 public $description; // text
54 public $created; // datetime
57 * Get an instance by key
59 * This is a utility method to get a single instance with a given key value.
61 * @param string $k Key to use to lookup (usually 'user_id' for this class)
62 * @param mixed $v Value to lookup
64 * @return User_greeting_count object found, or null for no hits
67 function staticGet($k, $v=null)
69 return Memcached_DataObject::staticGet('Question', $k, $v);
73 * Get an instance by compound key
75 * This is a utility method to get a single instance with a given set of
76 * key-value pairs. Usually used for the primary key for a compound key; thus
79 * @param array $kv array of key-value mappings
81 * @return Bookmark object found, or null for no hits
86 return Memcached_DataObject::pkeyGet('Question', $kv);
90 * The One True Thingy that must be defined and declared.
92 public static function schemaDef()
95 'description' => 'Per-notice question data for QuestionAndAnswer plugin',
97 'id' => array('type' => 'char', 'length' => 36, 'not null' => true, 'description' => 'UUID'),
98 'uri' => array('type' => 'varchar', 'length' => 255, 'not null' => true),
99 'profile_id' => array('type' => 'int'),
100 'title' => array('type' => 'text'),
101 'description' => array('type' => 'text'),
102 'created' => array('type' => 'datetime', 'not null' => true),
104 'primary key' => array('id'),
105 'unique keys' => array(
106 'question_uri_key' => array('uri'),
112 * Get a question based on a notice
114 * @param Notice $notice Notice to check for
116 * @return Question found question or null
118 function getByNotice($notice)
120 return self::staticGet('uri', $notice->uri);
125 return Notice::staticGet('uri', $this->uri);
130 return $this->getNotice()->bestUrl();
134 * Get the answer from a particular user to this question, if any.
136 * @param Profile $profile
138 * @return Answer object or null
140 function getAnswer(Profile $profile)
143 $a->question_id = $this->id;
144 $a->profile_id = $profile->id;
153 function countAnswers()
157 $a->question_id = $this->id;
162 * Save a new question notice
164 * @param Profile $profile
165 * @param string $question
166 * @param string $title
167 * @param string $description
168 * @param array $option // and whatnot
170 * @return Notice saved notice
172 static function saveNew($profile, $question, $title, $description, $options = array())
176 $q->id = UUID::gen();
177 $q->profile_id = $profile->id;
179 $q->description = $description;
181 if (array_key_exists('created', $options)) {
182 $q->created = $options['created'];
184 $q->created = common_sql_now();
187 if (array_key_exists('uri', $options)) {
188 $q->uri = $options['uri'];
190 $q->uri = common_local_url(
192 array('id' => $q->id)
196 common_log(LOG_DEBUG, "Saving question: $q->id $q->uri");
199 // TRANS: Notice content creating a question.
200 // TRANS: %1$s is the title of the question, %2$s is a link to the question.
202 _m('question: %1$s %2$s'),
207 $link = '<a href="' . htmlspecialchars($q->uri) . '">' . htmlspecialchars($title) . '</a>';
208 // TRANS: Rendered version of the notice content creating a question.
209 // TRANS: %s a link to the question as link description.
210 $rendered = sprintf(_m('Question: %s'), $link);
212 $tags = array('question');
215 $options = array_merge(
218 'rendered' => $rendered,
220 'replies' => $replies,
221 'object_type' => QuestionAndAnswerPlugin::QUESTION_OBJECT
226 if (!array_key_exists('uri', $options)) {
227 $options['uri'] = $p->uri;
230 $saved = Notice::saveNew(
233 array_key_exists('source', $options) ?
234 $options['source'] : 'web',