]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/QnA/classes/QnA_Answer.php
Merge branch '1.0.x' into qna
[quix0rs-gnu-social.git] / plugins / QnA / classes / QnA_Answer.php
1 <?php
2 /**
3  * Data class to save answers to questions
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 answers
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 class QnA_Answer extends Managed_DataObject
46 {
47     const  OBJECT_TYPE = 'http://activityschema.org/object/answer';
48
49     public $__table = 'qna_answer'; // table name
50     public $id;          // char(36) primary key not null -> UUID
51     public $question_id; // char(36) -> question.id UUID
52     public $profile_id;  // int -> question.id
53     public $best;        // (boolean) int -> whether the question asker has marked this as the best answer
54     public $revisions;   // int -> count of revisions to this answer
55     public $text;        // text -> response text
56     public $created;     // datetime
57
58     /**
59      * Get an instance by key
60      *
61      * This is a utility method to get a single instance with a given key value.
62      *
63      * @param string $k Key to use to lookup
64      * @param mixed  $v Value to lookup
65      *
66      * @return QnA_Answer object found, or null for no hits
67      *
68      */
69     function staticGet($k, $v=null)
70     {
71         return Memcached_DataObject::staticGet('QnA_Answer', $k, $v);
72     }
73
74     /**
75      * Get an instance by compound key
76      *
77      * This is a utility method to get a single instance with a given set of
78      * key-value pairs. Usually used for the primary key for a compound key; thus
79      * the name.
80      *
81      * @param array $kv array of key-value mappings
82      *
83      * @return QA_Answer object found, or null for no hits
84      *
85      */
86     function pkeyGet($kv)
87     {
88         return Memcached_DataObject::pkeyGet('QnA_Answer', $kv);
89     }
90
91     /**
92      * The One True Thingy that must be defined and declared.
93      */
94     public static function schemaDef()
95     {
96         return array(
97             'description' => 'Record of answers to questions',
98             'fields' => array(
99                 'id' => array(
100                     'type'     => 'char',
101                     'length'   => 36,
102                     'not null' => true, 'description' => 'UUID of the response'),
103                     'uri'      => array(
104                         'type'        => 'varchar',
105                         'length'      => 255,
106                         'not null'    => true,
107                         'description' => 'UUID to the answer notice'
108                     ),
109                     'question_id' => array(
110                         'type'        => 'char',
111                         'length'      => 36,
112                         'not null'    => true,
113                         'description' => 'UUID of question being responded to'
114                     ),
115                     'best'       => array('type' => 'int', 'size' => 'tiny'),
116                     'revisions'  => array('type' => 'int'),
117                     'profile_id' => array('type' => 'int'),
118                     'created'    => array('type' => 'datetime', 'not null' => true),
119             ),
120             'primary key' => array('id'),
121             'unique keys' => array(
122                 'question_uri_key' => array('uri'),
123                 'question_id_profile_id_key' => array('question_id', 'profile_id'),
124             ),
125             'indexes' => array(
126                 'profile_id_question_id_index' => array('profile_id', 'question_id'),
127             )
128         );
129     }
130
131     /**
132      * Get an answer based on a notice
133      *
134      * @param Notice $notice Notice to check for
135      *
136      * @return QnA_Answer found response or null
137      */
138     function getByNotice($notice)
139     {
140         $answer = self::staticGet('uri', $notice->uri);
141         if (empty($answer)) {
142             throw new Exception("No answer with URI {$this->notice->uri}");
143         }
144         return $answer;
145     }
146
147     /**
148      * Get the notice that belongs to this answer
149      *
150      * @return Notice
151      */
152     function getNotice()
153     {
154         return Notice::staticGet('uri', $this->uri);
155     }
156
157     function bestUrl()
158     {
159         return $this->getNotice()->bestUrl();
160     }
161
162     /**
163      * Get the Question this is an answer to
164      *
165      * @return QnA_Question
166      */
167     function getQuestion()
168     {
169         $question = self::staticGet('id', $this->question_id);
170         if (empty($question)) {
171             throw new Exception("No question with ID {$this->question_id}");
172         }
173         return question;
174     }
175     
176     function getProfile()
177     {
178         $profile = Profile::staticGet('id', $this->profile_id);
179         if (empty($profile)) {
180             throw new Exception("No profile with ID {$this->profile_id}");
181         }
182         return $profile;
183     }
184
185     function asHTML()
186     {
187         return self::toHTML(
188             $this->getProfile(),
189             $this->getQuestion()
190         );
191     }
192
193     function asString()
194     {
195         return self::toString(
196             $this->getProfile(),
197             $this->getQuestion()
198         );
199     }
200
201     static function toHTML($profile, $event, $response)
202     {
203         $fmt = null;
204
205         $notice = $event->getNotice();
206
207         switch ($response) {
208         case 'Y':
209             $fmt = _("<span class='automatic event-rsvp'><a href='%1s'>%2s</a> is attending <a href='%3s'>%4s</a>.</span>");
210             break;
211         case 'N':
212             $fmt = _("<span class='automatic event-rsvp'><a href='%1s'>%2s</a> is not attending <a href='%3s'>%4s</a>.</span>");
213             break;
214         case '?':
215             $fmt = _("<span class='automatic event-rsvp'><a href='%1s'>%2s</a> might attend <a href='%3s'>%4s</a>.</span>");
216             break;
217         default:
218             throw new Exception("Unknown response code {$response}");
219             break;
220         }
221
222         return sprintf($fmt,
223                        htmlspecialchars($profile->profileurl),
224                        htmlspecialchars($profile->getBestName()),
225                        htmlspecialchars($notice->bestUrl()),
226                        htmlspecialchars($event->title));
227     }
228
229     static function toString($profile, $event, $response)
230     {
231         $fmt = null;
232
233         $notice = $event->getNotice();
234
235         switch ($response) {
236         case 'Y':
237             $fmt = _("%1s is attending %2s.");
238             break;
239         case 'N':
240             $fmt = _("%1s is not attending %2s.");
241             break;
242         case '?':
243             $fmt = _("%1s might attend %2s.>");
244             break;
245         default:
246             throw new Exception("Unknown response code {$response}");
247             break;
248         }
249
250         return sprintf($fmt,
251                        $profile->getBestName(),
252                        $event->title);
253     }
254
255
256     /**
257      * Save a new answer notice
258      *
259      * @param Profile  $profile
260      * @param Question $Question the question being answered
261      * @param array
262      *
263      * @return Notice saved notice
264      */
265     static function saveNew($profile, $question, $text, $options = null)
266     {
267         if (empty($options)) {
268             $options = array();
269         }
270
271         $answer              = new QnA_Answer();
272         $answer->id          = UUID::gen();
273         $answer->profile_id  = $profile->id;
274         $answer->question_id = $question->id;
275         $answer->revisions   = 0;
276         $answer->best        = 0;
277         $answer->text        = $text;
278         $answer->created     = common_sql_now();
279         $answer->uri         = common_local_url(
280             'qnashowanswer',
281             array('id' => $answer->id)
282         );
283
284         common_log(LOG_DEBUG, "Saving answer: $answer->id, $answer->uri");
285         $answer->insert();
286
287         $content  = sprintf(
288             _m('answered "%s"'),
289             $question->title
290         );
291
292         $link = '<a href="' . htmlspecialchars($answer->uri) . '">' . htmlspecialchars($question->title) . '</a>';
293         // TRANS: Rendered version of the notice content answering a question.
294         // TRANS: %s a link to the question with question title as the link content.
295         $rendered = sprintf(_m('answered "%s"'), $link);
296
297         $tags    = array();
298         $replies = array();
299
300         $options = array_merge(
301             array(
302                 'urls'        => array(),
303                 'content'     => $content,
304                 'rendered'    => $rendered,
305                 'tags'        => $tags,
306                 'replies'     => $replies,
307                 'reply_to'    => $question->getNotice()->id,
308                 'object_type' => self::OBJECT_TYPE
309             ),
310             $options
311         );
312
313         if (!array_key_exists('uri', $options)) {
314             $options['uri'] = $answer->uri;
315         }
316
317         $saved = Notice::saveNew(
318             $profile->id,
319             $content,
320             array_key_exists('source', $options) ?
321             $options['source'] : 'web',
322             $options
323         );
324
325         return $saved;
326     }
327 }