]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/QnA/classes/QnA_Question.php
Merge branch '1.0.x' into qna
[quix0rs-gnu-social.git] / plugins / QnA / classes / QnA_Question.php
1 <?php
2 /**
3  * Data class to mark a notice as a question
4  *
5  * PHP version 5
6  *
7  * @category QnA
8  * @package  StatusNet
9  * @author   Zach Copley <zach@status.net>
10  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11  * @link     http://status.net/
12  *
13  * StatusNet - the distributed open-source microblogging tool
14  * Copyright (C) 2011, StatusNet, Inc.
15  *
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.
20  *
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.
25  *
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/>.
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  * For storing a question
36  *
37  * @category QnA
38  * @package  StatusNet
39  * @author   Zach Copley <zach@status.net>
40  * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
41  * @link     http://status.net/
42  *
43  * @see      DB_DataObject
44  */
45
46 class QnA_Question extends Managed_DataObject
47 {
48     
49     const QUESTION = 'http://activityschema.org/object/question';
50     
51     public $__table = 'qna_question'; // table name
52     public $id;          // char(36) primary key not null -> UUID
53     public $uri;
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
59
60     /**
61      * Get an instance by key
62      *
63      * This is a utility method to get a single instance with a given key value.
64      *
65      * @param string $k Key to use to lookup
66      * @param mixed  $v Value to lookup
67      *
68      * @return QnA_Question object found, or null for no hits
69      *
70      */
71     function staticGet($k, $v=null)
72     {
73         return Memcached_DataObject::staticGet('QnA_Question', $k, $v);
74     }
75
76     /**
77      * Get an instance by compound key
78      *
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
81      * the name.
82      *
83      * @param array $kv array of key-value mappings
84      *
85      * @return Bookmark object found, or null for no hits
86      *
87      */
88     function pkeyGet($kv)
89     {
90         return Memcached_DataObject::pkeyGet('QnA_Question', $kv);
91     }
92
93     /**
94      * The One True Thingy that must be defined and declared.
95      */
96     public static function schemaDef()
97     {
98         return array(
99             'description' => 'Per-notice question data for QNA plugin',
100             'fields' => array(
101                 'id' => array(
102                     'type'        => 'char', 
103                     'length'      => 36, 
104                     'not null'    => true, 
105                     'description' => 'UUID'
106                 ),
107                 'uri' => array(
108                     'type'     => 'varchar', 
109                     'length'   => 255, 
110                     'not null' => true
111                 ),
112                 'profile_id'  => array('type' => 'int'),
113                 'title'       => array('type' => 'text'),
114                 'closed'      => array('type' => 'int', size => 'tiny'),
115                 'description' => array('type' => 'text'),
116                 'created'     => array(
117                     'type' => 'datetime', 
118                     'not null' => true
119                 ),
120             ),
121             'primary key' => array('id'),
122             'unique keys' => array(
123                 'question_uri_key' => array('uri'),
124             ),
125         );
126     }
127
128     /**
129      * Get a question based on a notice
130      *
131      * @param Notice $notice Notice to check for
132      *
133      * @return Question found question or null
134      */
135     function getByNotice($notice)
136     {
137         return self::staticGet('uri', $notice->uri);
138     }
139
140     function getNotice()
141     {
142         return Notice::staticGet('uri', $this->uri);
143     }
144
145     function bestUrl()
146     {
147         return $this->getNotice()->bestUrl();
148     }
149
150     /**
151      * Get the answer from a particular user to this question, if any.
152      *
153      * @param Profile $profile
154      *
155      * @return Answer object or null
156      */
157     function getAnswer(Profile $profile)
158     {
159         $a = new QnA_Answer();
160         $a->question_id = $this->id;
161         $a->profile_id = $profile->id;
162         $a->find();
163         if ($a->fetch()) {
164             return $a;
165         } else {
166             return null;
167         }
168     }
169
170     function countAnswers()
171     {
172         $a              = new QnA_Answer();
173         $a->question_id = $this->id;
174         return $a-count();
175     }
176
177     /**
178      * Save a new question notice
179      *
180      * @param Profile $profile
181      * @param string  $question
182      * @param string  $title
183      * @param string  $description
184      * @param array   $option // and whatnot
185      *
186      * @return Notice saved notice
187      */
188     static function saveNew($profile, $question, $title, $description, $options = array())
189     {
190         $q = new QnA_Question();
191
192         $q->id          = UUID::gen();
193         $q->profile_id  = $profile->id;
194         $q->title       = $title;
195         $q->description = $description;
196
197         if (array_key_exists('created', $options)) {
198             $q->created = $options['created'];
199         } else {
200             $q->created = common_sql_now();
201         }
202
203         if (array_key_exists('uri', $options)) {
204             $q->uri = $options['uri'];
205         } else {
206             $q->uri = common_local_url(
207                 'showquestion',
208                 array('id' => $q->id)
209             );
210         }
211
212         common_log(LOG_DEBUG, "Saving question: $q->id $q->uri");
213         $q->insert();
214
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.
217         $content  = sprintf(
218             _m('question: %1$s %2$s'),
219             $title,
220             $q->uri
221         );
222         
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);
227
228         $tags    = array('question');
229         $replies = array();
230
231         $options = array_merge(
232             array(
233                 'urls'        => array(),
234                 'rendered'    => $rendered,
235                 'tags'        => $tags,
236                 'replies'     => $replies,
237                 'object_type' => QnAPlugin::QUESTION_OBJECT
238             ),
239             $options
240         );
241
242         if (!array_key_exists('uri', $options)) {
243             $options['uri'] = $p->uri;
244         }
245
246         $saved = Notice::saveNew(
247             $profile->id,
248             $content,
249             array_key_exists('source', $options) ?
250             $options['source'] : 'web',
251             $options
252         );
253
254         return $saved;
255     }
256 }