]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/QuestionAndAnswer/classes/Answer.php
Most objects and forms are in place, now I just have to make it work.
[quix0rs-gnu-social.git] / plugins / QuestionAndAnswer / classes / Answer.php
1 <?php
2 /**
3  * Data class to save answers to questions
4  *
5  * PHP version 5
6  *
7  * @category QuestionAndAnswer
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 QuestionAndAnswer
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 Answer extends Managed_DataObject
46 {
47     public $__table = 'answer'; // table name
48     public $id;          // char(36) primary key not null -> UUID
49     public $question_id; // char(36) -> question.id UUID
50     public $profile_id;  // int -> question.id
51     public $votes;       // int -> total number of votes (up & down)
52     public $best;        // (int) boolean -> whether the question asker has marked this as the best answer
53     public $created;     // datetime
54
55     /**
56      * Get an instance by key
57      *
58      * This is a utility method to get a single instance with a given key value.
59      *
60      * @param string $k Key to use to lookup (usually 'user_id' for this class)
61      * @param mixed  $v Value to lookup
62      *
63      * @return User_greeting_count object found, or null for no hits
64      *
65      */
66     function staticGet($k, $v=null)
67     {
68         return Memcached_DataObject::staticGet('Answer', $k, $v);
69     }
70
71     /**
72      * Get an instance by compound key
73      *
74      * This is a utility method to get a single instance with a given set of
75      * key-value pairs. Usually used for the primary key for a compound key; thus
76      * the name.
77      *
78      * @param array $kv array of key-value mappings
79      *
80      * @return Bookmark object found, or null for no hits
81      *
82      */
83     function pkeyGet($kv)
84     {
85         return Memcached_DataObject::pkeyGet('Answer', $kv);
86     }
87
88     /**
89      * The One True Thingy that must be defined and declared.
90      */
91     public static function schemaDef()
92     {
93         return array(
94             'description' => 'Record of answers to questions',
95             'fields' => array(
96                 'id' => array('type' => 'char', 'length' => 36, 'not null' => true, 'description' => 'UUID of the response'),
97                 'uri' => array('type' => 'varchar', 'length' => 255, 'not null' => true, 'description' => 'UUID to the answer notice'),
98                 'question_id' => array('type' => 'char', 'length' => 36, 'not null' => true, 'description' => 'UUID of question being responded to'),
99                 'votes' => array('type' => 'int'),
100                 'best'  => array('type' => 'int'),
101                 'profile_id' => array('type' => 'int'),
102                 'created' => array('type' => 'datetime', 'not null' => true),
103             ),
104             'primary key' => array('id'),
105             'unique keys' => array(
106                 'question_uri_key' => array('uri'),
107                 'question_id_profile_id_key' => array('question_id', 'profile_id'),
108             ),
109             'indexes' => array(
110                 'profile_id_question_Id_index' => array('profile_id', 'question_id'),
111             )
112         );
113     }
114
115     /**
116      * Get an answer based on a notice
117      *
118      * @param Notice $notice Notice to check for
119      *
120      * @return Answer found response or null
121      */
122     function getByNotice($notice)
123     {
124         return self::staticGet('uri', $notice->uri);
125     }
126
127     /**
128      * Get the notice that belongs to this answer
129      *
130      * @return Notice
131      */
132     function getNotice()
133     {
134         return Notice::staticGet('uri', $this->uri);
135     }
136
137     function bestUrl()
138     {
139         return $this->getNotice()->bestUrl();
140     }
141
142     /**
143      * Get the Question this is an answer to
144      *
145      * @return Question
146      */
147     function getQuestion()
148     {
149         return Question::staticGet('id', $this->question_id);
150     }
151     /**
152      * Save a new answer notice
153      *
154      * @param Profile  $profile
155      * @param Question $Question the question being answered
156      * @param array
157      *
158      * @return Notice saved notice
159      */
160     static function saveNew($profile, $question, $options=null)
161     {
162         if (empty($options)) {
163             $options = array();
164         }
165
166         $a = new Answer();
167         $a->id          = UUID::gen();
168         $a->profile_id  = $profile->id;
169         $a->question_id = $question->id;
170         $a->created     = common_sql_now();
171         $a->uri         = common_local_url(
172             'showanswer',
173             array('id' => $pr->id)
174         );
175         
176         common_log(LOG_DEBUG, "Saving answer: $pr->id $pr->uri");
177         $a->insert();
178
179         // TRANS: Notice content answering a question.
180         // TRANS: %s is the answer
181         $content  = sprintf(
182             _m('answered "%s"'),
183             $answer
184         );
185         $link = '<a href="' . htmlspecialchars($question->uri) . '">' . htmlspecialchars($answer) . '</a>';
186         // TRANS: Rendered version of the notice content answering a question.
187         // TRANS: %s a link to the question with the chosen option as link description.
188         $rendered = sprintf(_m('answered "%s"'), $link);
189
190         $tags    = array();
191         $replies = array();
192
193         $options = array_merge(array('urls' => array(),
194                                      'rendered' => $rendered,
195                                      'tags' => $tags,
196                                      'replies' => $replies,
197                                      'reply_to' => $question->getNotice()->id,
198                                      'object_type' => QuestionAndAnswer::ANSWER_OBJECT),
199                                $options);
200
201         if (!array_key_exists('uri', $options)) {
202             $options['uri'] = $pr->uri;
203         }
204
205         $saved = Notice::saveNew($profile->id,
206                                  $content,
207                                  array_key_exists('source', $options) ?
208                                  $options['source'] : 'web',
209                                  $options);
210
211         return $saved;
212     }
213 }