]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/QnA/classes/QnA_Answer.php
Renamed QuestionAndAnswerPlugin to QnAPlugin
[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 ANSWER = '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;        // (int) boolean -> whether the question asker has marked this as the best answer
54     public $created;     // datetime
55
56     /**
57      * Get an instance by key
58      *
59      * This is a utility method to get a single instance with a given key value.
60      *
61      * @param string $k Key to use to lookup
62      * @param mixed  $v Value to lookup
63      *
64      * @return QnA_Answer object found, or null for no hits
65      *
66      */
67     function staticGet($k, $v=null)
68     {
69         return Memcached_DataObject::staticGet('QnA_Answer', $k, $v);
70     }
71
72     /**
73      * Get an instance by compound key
74      *
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
77      * the name.
78      *
79      * @param array $kv array of key-value mappings
80      *
81      * @return QA_Answer object found, or null for no hits
82      *
83      */
84     function pkeyGet($kv)
85     {
86         return Memcached_DataObject::pkeyGet('QnA_Answer', $kv);
87     }
88
89     /**
90      * The One True Thingy that must be defined and declared.
91      */
92     public static function schemaDef()
93     {
94         return array(
95             'description' => 'Record of answers to questions',
96             'fields' => array(
97                 'id' => array(
98                     'type'     => 'char', 
99                     'length'   => 36, 
100                     'not null' => true, 'description' => 'UUID of the response'),
101                     'uri'      => array(
102                         'type'        => 'varchar', 
103                         'length'      => 255, 
104                         'not null'    => true, 
105                         'description' => 'UUID to the answer notice'
106                     ),
107                     'question_id' => array(
108                         'type'     => 'char', 
109                         'length'   => 36, 
110                         'not null' => true, 
111                         'description' => 'UUID of question being responded to'
112                     ),
113                     'best'     => array('type' => 'int', 'size' => 'tiny'),
114                     'profile_id'  => array('type' => 'int'),
115                     'created'     => array('type' => 'datetime', 'not null' => true),
116             ),
117             'primary key' => array('id'),
118             'unique keys' => array(
119                 'question_uri_key' => array('uri'),
120                 'question_id_profile_id_key' => array('question_id', 'profile_id'),
121             ),
122             'indexes' => array(
123                 'profile_id_question_id_index' => array('profile_id', 'question_id'),
124             )
125         );
126     }
127
128     /**
129      * Get an answer based on a notice
130      *
131      * @param Notice $notice Notice to check for
132      *
133      * @return QnA_Answer found response or null
134      */
135     function getByNotice($notice)
136     {
137         return self::staticGet('uri', $notice->uri);
138     }
139
140     /**
141      * Get the notice that belongs to this answer
142      *
143      * @return Notice
144      */
145     function getNotice()
146     {
147         return Notice::staticGet('uri', $this->uri);
148     }
149
150     function bestUrl()
151     {
152         return $this->getNotice()->bestUrl();
153     }
154
155     /**
156      * Get the Question this is an answer to
157      *
158      * @return QnA_Question
159      */
160     function getQuestion()
161     {
162         return Question::staticGet('id', $this->question_id);
163     }
164
165     /**
166      * Save a new answer notice
167      *
168      * @param Profile  $profile
169      * @param Question $Question the question being answered
170      * @param array
171      *
172      * @return Notice saved notice
173      */
174     static function saveNew($profile, $question, $options=null)
175     {
176         if (empty($options)) {
177             $options = array();
178         }
179
180         $a = new Answer();
181         $a->id          = UUID::gen();
182         $a->profile_id  = $profile->id;
183         $a->question_id = $question->id;
184         $a->created     = common_sql_now();
185         $a->uri         = common_local_url(
186             'showanswer',
187             array('id' => $pr->id)
188         );
189         
190         common_log(LOG_DEBUG, "Saving answer: $pr->id $pr->uri");
191         $a->insert();
192
193         // TRANS: Notice content answering a question.
194         // TRANS: %s is the answer
195         $content  = sprintf(
196             _m('answered "%s"'),
197             $answer
198         );
199         $link = '<a href="' . htmlspecialchars($question->uri) . '">' . htmlspecialchars($answer) . '</a>';
200         // TRANS: Rendered version of the notice content answering a question.
201         // TRANS: %s a link to the question with question title as the link content.
202         $rendered = sprintf(_m('answered "%s"'), $link);
203
204         $tags    = array();
205         $replies = array();
206
207         $options = array_merge(
208             array(
209                 'urls' => array(),
210                 'rendered' => $rendered,
211                 'tags' => $tags,
212                 'replies' => $replies,
213                 'reply_to' => $question->getNotice()->id,
214                 'object_type' => QnA::ANSWER_OBJECT),
215                 $options
216             );
217
218         if (!array_key_exists('uri', $options)) {
219             $options['uri'] = $pr->uri;
220         }
221
222         $saved = Notice::saveNew(
223             $profile->id,
224             $content,
225             array_key_exists('source', $options) ?
226             $options['source'] : 'web',
227             $options
228         );
229
230         return $saved;
231     }
232 }