]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/QuestionAndAnswer/classes/Question.php
Most objects and forms are in place, now I just have to make it work.
[quix0rs-gnu-social.git] / plugins / QuestionAndAnswer / classes / Question.php
1 <?php
2 /**
3  * Data class to mark a notice as a question
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 a question
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
46 class Question extends Managed_DataObject
47 {
48     public $__table = 'question'; // table name
49     public $id;          // char(36) primary key not null -> UUID
50     public $uri;
51     public $profile_id;  // int -> profile.id
52     public $title;       // text
53     public $description; // text
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 (usually 'user_id' for this class)
62      * @param mixed  $v Value to lookup
63      *
64      * @return User_greeting_count object found, or null for no hits
65      *
66      */
67     function staticGet($k, $v=null)
68     {
69         return Memcached_DataObject::staticGet('Question', $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 Bookmark object found, or null for no hits
82      *
83      */
84     function pkeyGet($kv)
85     {
86         return Memcached_DataObject::pkeyGet('Question', $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' => 'Per-notice question data for QuestionAndAnswer plugin',
96             'fields' => array(
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),
103             ),
104             'primary key' => array('id'),
105             'unique keys' => array(
106                 'question_uri_key' => array('uri'),
107             ),
108         );
109     }
110
111     /**
112      * Get a question based on a notice
113      *
114      * @param Notice $notice Notice to check for
115      *
116      * @return Question found question or null
117      */
118     function getByNotice($notice)
119     {
120         return self::staticGet('uri', $notice->uri);
121     }
122
123     function getNotice()
124     {
125         return Notice::staticGet('uri', $this->uri);
126     }
127
128     function bestUrl()
129     {
130         return $this->getNotice()->bestUrl();
131     }
132
133     /**
134      * Get the answer from a particular user to this question, if any.
135      *
136      * @param Profile $profile
137      *
138      * @return Answer object or null
139      */
140     function getAnswer(Profile $profile)
141     {
142         $a = new Answer();
143         $a->question_id = $this->id;
144         $a->profile_id = $profile->id;
145         $a->find();
146         if ($a->fetch()) {
147             return $a;
148         } else {
149             return null;
150         }
151     }
152
153     function countAnswers()
154     {
155         $a = new Answer();
156         
157         $a->question_id = $this->id;
158         return $a-count();
159     }
160
161     /**
162      * Save a new question notice
163      *
164      * @param Profile $profile
165      * @param string  $question
166      * @param string  $title
167      * @param string  $description
168      * @param array   $option // and whatnot
169      *
170      * @return Notice saved notice
171      */
172     static function saveNew($profile, $question, $title, $description, $options = array())
173     {
174         $q = new Question();
175
176         $q->id          = UUID::gen();
177         $q->profile_id  = $profile->id;
178         $q->title       = $title;
179         $q->description = $description;
180
181         if (array_key_exists('created', $options)) {
182             $q->created = $options['created'];
183         } else {
184             $q->created = common_sql_now();
185         }
186
187         if (array_key_exists('uri', $options)) {
188             $q->uri = $options['uri'];
189         } else {
190             $q->uri = common_local_url(
191                 'showquestion',
192                 array('id' => $q->id)
193             );
194         }
195
196         common_log(LOG_DEBUG, "Saving question: $q->id $q->uri");
197         $q->insert();
198
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.
201         $content  = sprintf(
202             _m('question: %1$s %2$s'),
203             $title,
204             $q->uri
205         );
206         
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);
211
212         $tags    = array('question');
213         $replies = array();
214
215         $options = array_merge(
216             array(
217                 'urls'        => array(),
218                 'rendered'    => $rendered,
219                 'tags'        => $tags,
220                 'replies'     => $replies,
221                 'object_type' => QuestionAndAnswerPlugin::QUESTION_OBJECT
222             ),
223             $options
224         );
225
226         if (!array_key_exists('uri', $options)) {
227             $options['uri'] = $p->uri;
228         }
229
230         $saved = Notice::saveNew(
231             $profile->id,
232             $content,
233             array_key_exists('source', $options) ?
234             $options['source'] : 'web',
235             $options
236         );
237
238         return $saved;
239     }
240 }