~aleritty/ideatorrent-mysql/trunk

« back to all changes in this revision

Viewing changes to modules/ideatorrent/models/choicesolutionlist.php

  • Committer: aleritty
  • Date: 2009-05-27 20:52:15 UTC
  • Revision ID: aleritty@toshi-20090527205215-3id27b3l2kosikwe
passato alla versione speditami

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
 
3
 
/*
4
 
Copyright (C) 2008 Nicolas Deschildre <ndeschildre@gmail.com>
5
 
 
6
 
This program is free software; you can redistribute it and/or
7
 
modify it under the terms of the GNU General Public License
8
 
as published by the Free Software Foundation; either version 2
9
 
of the License, or (at your option) any later version.
10
 
 
11
 
This program is distributed in the hope that it will be useful,
12
 
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
GNU General Public License for more details.
15
 
 
16
 
You should have received a copy of the GNU General Public License
17
 
along with this program; if not, write to the Free Software
18
 
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19
 
*/
20
 
 
21
 
 
22
 
 
23
 
 
24
 
class ChoiceSolutionListModel extends ModelList
25
 
{
26
 
 
27
 
 
28
 
        /**
29
 
         * Filter parameters.
30
 
         * choice_id: Let's get the choice solution related to a given choice id.
31
 
         * choice_relation_relative_number : Let's filter by the choice relative number.
32
 
         * duplicate_of : Let's show the solutions which are the duplicates of the given number.
33
 
         *      Since the dup are not shown by default, it's best to set state_duplicate to true :)
34
 
         * userid : Filter by the author of the solution
35
 
         * link_states: Filter by the state of the links.
36
 
         * states: Filter by the state of the solution.
37
 
         * ordering : The ordering to use.
38
 
         */
39
 
        var $_choice_id = -1;
40
 
        var $_choice_relation_relative_number = -1;
41
 
        var $_duplicate_of = -2;
42
 
        var $_userid = -1;
43
 
        var $_link_states = array(
44
 
                "deleted" => false,
45
 
                "new" => true);
46
 
        var $_states = array(
47
 
                "deleted" => false,
48
 
                "duplicate" => false,
49
 
                "new" => true);
50
 
        var $_ordering = "solutionumber";
51
 
 
52
 
        /**
53
 
         * Data filters. They are used to restrict the columns of the data returned.
54
 
         * This can be very usefull when we only need something specific, and we
55
 
         * don't want to waste processing time.
56
 
         * include_minimal_data: Override all the below options by setting them to false.
57
 
         * include_user_bookmark: shall we include the user vote?
58
 
         */
59
 
        var $_include_minimal_data = false;
60
 
        var $_include_user_vote = true;
61
 
 
62
 
 
63
 
        /**
64
 
         * Default constructor.
65
 
         */
66
 
        function ChoiceSolutionListModel()
67
 
        {
68
 
        }
69
 
 
70
 
        /**
71
 
         * Get the data.
72
 
         */
73
 
        function _loadData()
74
 
        {
75
 
 
76
 
                //Get the choice solution list
77
 
                $query = "SELECT qapoll_choice_solution.*, qapoll_choice_solution_link.solution_number, " .
78
 
 
79
 
                        //Get the user vote only if logged.
80
 
                        (($GLOBALS['user']->uid != null && $this->_include_user_vote == true)?"COALESCE(qapoll_vote.value, -2) as myvote, ":"") .
81
 
 
82
 
                        "qapoll_choice_solution_link.choiceid, qapoll_choice_solution_link.advertize, " .
83
 
                        "qapoll_choice_solution_link.status, qapoll_choice_solution_link.selected, users.name as username " .
84
 
                        "FROM qapoll_choice_solution " . 
85
 
                        "JOIN qapoll_choice_solution_link ON qapoll_choice_solution.id = qapoll_choice_solution_link.choicesolutionid " .
86
 
                        "LEFT JOIN users ON users.uid = qapoll_choice_solution.userid " .
87
 
 
88
 
                        //Get the user vote only if logged.
89
 
                        (($GLOBALS['user']->uid != null && $this->_include_user_vote == true)?
90
 
                                "LEFT JOIN qapoll_vote ON qapoll_vote.choicesolutionid = qapoll_choice_solution.id AND qapoll_vote.userid = " .
91
 
                                $GLOBALS['user']->uid . " ":"") .
92
 
 
93
 
                        $this->_buildChoiceSolutionListQuery_where() .
94
 
                        $this->_buildChoiceSolutionListQuery_orderby();
95
 
 
96
 
                $choicesolutions = it_query($query);
97
 
 
98
 
                //Store the result in a array
99
 
                $choicesolution_list = array();
100
 
                while ($choicesolution = db_fetch_object($choicesolutions))
101
 
                {
102
 
                        $choicesolution_list[] = $choicesolution;
103
 
 
104
 
                }
105
 
 
106
 
                return $choicesolution_list;
107
 
        }
108
 
 
109
 
        function _buildChoiceSolutionListQuery_where()
110
 
        {
111
 
                $where = "WHERE true ";
112
 
 
113
 
                if($this->_states['deleted'] == false)
114
 
                {
115
 
                        $where .= "AND qapoll_choice_solution.status != -2 ";
116
 
                }
117
 
                if($this->_states['new'] == false)
118
 
                {
119
 
                        $where .= "AND qapoll_choice_solution.status != 1 ";
120
 
                }
121
 
                if($this->_states['duplicate'] == false)
122
 
                {
123
 
                        $where .= "AND qapoll_choice_solution.status != -1 ";
124
 
                }
125
 
 
126
 
                if($this->_link_states['deleted'] == false)
127
 
                {
128
 
                        $where .= "AND qapoll_choice_solution_link.status != -2 ";
129
 
                }
130
 
                if($this->_link_states['new'] == false)
131
 
                {
132
 
                        $where .= "AND qapoll_choice_solution_link.status != 1 ";
133
 
                }
134
 
 
135
 
                if($this->_userid != -1)
136
 
                        $where .= "AND qapoll_choice_solution.userid = '" . $this->_userid . "' ";
137
 
                if($this->_choice_id != -1)
138
 
                        $where .= "AND qapoll_choice_solution_link.choiceid = '" . $this->_choice_id . "' ";
139
 
                if($this->_choice_relation_relative_number != -1)
140
 
                        $where .= "AND qapoll_choice_solution_link.solution_number = '" . $this->_choice_relation_relative_number . "' ";
141
 
                if($this->_duplicate_of != -2)
142
 
                        $where .= "AND qapoll_choice_solution.duplicate_choice_solution_id = '" . $this->_duplicate_of . "' ";
143
 
 
144
 
 
145
 
                return $where;
146
 
        }
147
 
 
148
 
        function _buildChoiceSolutionListQuery_orderby()
149
 
        {
150
 
 
151
 
                switch($this->_ordering)
152
 
                {
153
 
                        case "solutionumber":
154
 
                                $orderby = "qapoll_choice_solution_link.solution_number ";
155
 
                        break;
156
 
 
157
 
                        case "selectedfirst":
158
 
                                $orderby = "qapoll_choice_solution_link.selected DESC, qapoll_choice_solution_link.solution_number ";
159
 
                        break;
160
 
 
161
 
                        default:
162
 
                                $orderby = "qapoll_choice_solution_link.solution_number ";
163
 
                        break;
164
 
                }
165
 
 
166
 
                return ($orderby != null)?"ORDER BY " . $orderby:"";
167
 
        }
168
 
 
169
 
        /**
170
 
         * Set the filter parameters. Giving the GET array is usually fine.
171
 
         * It will sanitize the necessary stuff.
172
 
         */
173
 
        function setFilterParameters($getarray)
174
 
        {
175
 
                //Save the array first.
176
 
                $this->_filter_array = $getarray;
177
 
 
178
 
                if($getarray['choice_id'] != null && is_numeric($getarray['choice_id']))
179
 
                        $this->_choice_id = $getarray['choice_id'];
180
 
                if($getarray['choice_relation_relative_number'] != null && is_numeric($getarray['choice_relation_relative_number']))
181
 
                        $this->_choice_relation_relative_number = $getarray['choice_relation_relative_number'];
182
 
                if($getarray['duplicate_of'] != null && is_numeric($getarray['duplicate_of']))
183
 
                        $this->_duplicate_of = $getarray['duplicate_of'];
184
 
 
185
 
                if($getarray['userid'] != null && is_numeric($getarray['userid']))
186
 
                        $this->_userid = $getarray['userid'];
187
 
 
188
 
 
189
 
                //Save the states options
190
 
                if($getarray['state_new'] != null && is_numeric($getarray['state_new']))
191
 
                        $this->_states['new'] = ($getarray['state_new'] != 0);
192
 
                if($getarray['state_deleted'] != null && is_numeric($getarray['state_deleted']))
193
 
                        $this->_states['deleted'] = ($getarray['state_deleted'] != 0);
194
 
                if($getarray['state_duplicate'] != null && is_numeric($getarray['state_duplicate']))
195
 
                        $this->_states['duplicate'] = ($getarray['state_duplicate'] != 0);
196
 
 
197
 
                //Save the link states options
198
 
                if($getarray['state_link_new'] != null && is_numeric($getarray['state_link_new']))
199
 
                        $this->_link_states['new'] = ($getarray['state_link_new'] != 0);
200
 
                if($getarray['state_link_deleted'] != null && is_numeric($getarray['state_link_deleted']))
201
 
                        $this->_link_states['deleted'] = ($getarray['state_link_deleted'] != 0);
202
 
 
203
 
                if($getarray['ordering'] != null)
204
 
                        $this->_ordering = $getarray['ordering'];
205
 
 
206
 
        }
207
 
 
208
 
        /**
209
 
         * Set the data filter. This will be used to control the columns of data returned.
210
 
         * Useful to reduce SQL processing time.
211
 
         */
212
 
        function setDataFilter($filter_array)
213
 
        {
214
 
                //Save the filter array
215
 
                $this->_data_filter = $filter_array;
216
 
 
217
 
                //Override all the options: set all to false.
218
 
                if($filter_array['include_minimal_data'] != null && $filter_array['include_minimal_data'] == true)
219
 
                {
220
 
                        $this->_include_minimal_data = true;
221
 
                        $this->_include_user_vote = false;
222
 
                }
223
 
                else
224
 
                {
225
 
                        if($filter_array['include_user_vote'] != null && is_numeric($filter_array['include_user_vote']))
226
 
                                $this->_include_user_vote = $filter_array['include_user_vote'];
227
 
 
228
 
                }
229
 
 
230
 
        }
231
 
 
232
 
 
233
 
        /**
234
 
         * Delete the fetched entries. Set status = -1 and update comment count.
235
 
         */
236
 
        function deleteEntries()
237
 
        {
238
 
                $entries_id = array();
239
 
                $entries = $this->getData();
240
 
 
241
 
                foreach($entries as $entry)
242
 
                {
243
 
                        $choicesolution = new ChoiceSolutionModel();
244
 
                        $choicesolution->setId($entry->id);
245
 
                        $choicesolution->delete();
246
 
                }
247
 
 
248
 
                it_query($query);
249
 
        }
250
 
 
251
 
 
252
 
 
253
 
        /**
254
 
         * This function is a callback. DO NOT CALL DIRECTLY!
255
 
         * It is used by UserModel::_hasPermission to determine if the user user_id is
256
 
         * the owner of the $model_id object.
257
 
         * If $model_id is not in this instance, it return false.
258
 
         */
259
 
        function _callback_isOwner($model_id, $user_id)
260
 
        {
261
 
                //If there is no filter, the model is probably not initialized yet. => Design error.
262
 
                //Return false, and 
263
 
                if($this->_filter_array == null)
264
 
                {
265
 
                        drupal_set_message("Probable design error while getting user permissions. " . 
266
 
                                "Please see ChoiceSolutionListModel::_callback_isOwner", 'notice_msg');
267
 
                        return false;
268
 
                }
269
 
 
270
 
                //Check if user_id is the owner of model_id
271
 
                $data = $this->getData();
272
 
                foreach($data as $item)
273
 
                {
274
 
                        if($item->id == $model_id)
275
 
                                return ($item->userid == $user_id);
276
 
                }
277
 
 
278
 
                return false;
279
 
        }
280
 
 
281
 
}
282
 
 
283
 
?>
 
1
<?php
 
2
 
 
3
/*
 
4
Copyright (C) 2008 Nicolas Deschildre <ndeschildre@gmail.com>
 
5
 
 
6
This program is free software; you can redistribute it and/or
 
7
modify it under the terms of the GNU General Public License
 
8
as published by the Free Software Foundation; either version 2
 
9
of the License, or (at your option) any later version.
 
10
 
 
11
This program is distributed in the hope that it will be useful,
 
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
GNU General Public License for more details.
 
15
 
 
16
You should have received a copy of the GNU General Public License
 
17
along with this program; if not, write to the Free Software
 
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
19
*/
 
20
 
 
21
 
 
22
 
 
23
 
 
24
class ChoiceSolutionListModel extends ModelList
 
25
{
 
26
 
 
27
 
 
28
        /**
 
29
         * Filter parameters.
 
30
         * choice_id: Let's get the choice solution related to a given choice id.
 
31
         * choice_relation_relative_number : Let's filter by the choice relative number.
 
32
         * duplicate_of : Let's show the solutions which are the duplicates of the given number.
 
33
         *      Since the dup are not shown by default, it's best to set state_duplicate to true :)
 
34
         * userid : Filter by the author of the solution
 
35
         * link_states: Filter by the state of the links.
 
36
         * states: Filter by the state of the solution.
 
37
         * ordering : The ordering to use.
 
38
         */
 
39
        var $_choice_id = -1;
 
40
        var $_choice_relation_relative_number = -1;
 
41
        var $_duplicate_of = -2;
 
42
        var $_userid = -1;
 
43
        var $_link_states = array(
 
44
                "deleted" => false,
 
45
                "new" => true);
 
46
        var $_states = array(
 
47
                "deleted" => false,
 
48
                "duplicate" => false,
 
49
                "new" => true);
 
50
        var $_ordering = "solutionumber";
 
51
 
 
52
        /**
 
53
         * Data filters. They are used to restrict the columns of the data returned.
 
54
         * This can be very usefull when we only need something specific, and we
 
55
         * don't want to waste processing time.
 
56
         * include_minimal_data: Override all the below options by setting them to false.
 
57
         * include_user_bookmark: shall we include the user vote?
 
58
         */
 
59
        var $_include_minimal_data = false;
 
60
        var $_include_user_vote = true;
 
61
 
 
62
 
 
63
        /**
 
64
         * Default constructor.
 
65
         */
 
66
        function ChoiceSolutionListModel()
 
67
        {
 
68
        }
 
69
 
 
70
        /**
 
71
         * Get the data.
 
72
         */
 
73
        function _loadData()
 
74
        {
 
75
 
 
76
                //Get the choice solution list
 
77
                $query = "SELECT qapoll_choice_solution.*, qapoll_choice_solution_link.solution_number, " .
 
78
 
 
79
                        //Get the user vote only if logged.
 
80
                        (($GLOBALS['user']->uid != null && $this->_include_user_vote == true)?"COALESCE(qapoll_vote.value, -2) as myvote, ":"") .
 
81
 
 
82
                        "qapoll_choice_solution_link.choiceid, qapoll_choice_solution_link.advertize, " .
 
83
                        "qapoll_choice_solution_link.status, qapoll_choice_solution_link.selected, users.name as username " .
 
84
                        "FROM qapoll_choice_solution " . 
 
85
                        "JOIN qapoll_choice_solution_link ON qapoll_choice_solution.id = qapoll_choice_solution_link.choicesolutionid " .
 
86
                        "LEFT JOIN users ON users.uid = qapoll_choice_solution.userid " .
 
87
 
 
88
                        //Get the user vote only if logged.
 
89
                        (($GLOBALS['user']->uid != null && $this->_include_user_vote == true)?
 
90
                                "LEFT JOIN qapoll_vote ON qapoll_vote.choicesolutionid = qapoll_choice_solution.id AND qapoll_vote.userid = " .
 
91
                                $GLOBALS['user']->uid . " ":"") .
 
92
 
 
93
                        $this->_buildChoiceSolutionListQuery_where() .
 
94
                        $this->_buildChoiceSolutionListQuery_orderby();
 
95
 
 
96
                $choicesolutions = it_query($query);
 
97
 
 
98
                //Store the result in a array
 
99
                $choicesolution_list = array();
 
100
                while ($choicesolution = db_fetch_object($choicesolutions))
 
101
                {
 
102
                        $choicesolution_list[] = $choicesolution;
 
103
 
 
104
                }
 
105
 
 
106
                return $choicesolution_list;
 
107
        }
 
108
 
 
109
        function _buildChoiceSolutionListQuery_where()
 
110
        {
 
111
                $where = "WHERE true ";
 
112
 
 
113
                if($this->_states['deleted'] == false)
 
114
                {
 
115
                        $where .= "AND qapoll_choice_solution.status != -2 ";
 
116
                }
 
117
                if($this->_states['new'] == false)
 
118
                {
 
119
                        $where .= "AND qapoll_choice_solution.status != 1 ";
 
120
                }
 
121
                if($this->_states['duplicate'] == false)
 
122
                {
 
123
                        $where .= "AND qapoll_choice_solution.status != -1 ";
 
124
                }
 
125
 
 
126
                if($this->_link_states['deleted'] == false)
 
127
                {
 
128
                        $where .= "AND qapoll_choice_solution_link.status != -2 ";
 
129
                }
 
130
                if($this->_link_states['new'] == false)
 
131
                {
 
132
                        $where .= "AND qapoll_choice_solution_link.status != 1 ";
 
133
                }
 
134
 
 
135
                if($this->_userid != -1)
 
136
                        $where .= "AND qapoll_choice_solution.userid = '" . $this->_userid . "' ";
 
137
                if($this->_choice_id != -1)
 
138
                        $where .= "AND qapoll_choice_solution_link.choiceid = '" . $this->_choice_id . "' ";
 
139
                if($this->_choice_relation_relative_number != -1)
 
140
                        $where .= "AND qapoll_choice_solution_link.solution_number = '" . $this->_choice_relation_relative_number . "' ";
 
141
                if($this->_duplicate_of != -2)
 
142
                        $where .= "AND qapoll_choice_solution.duplicate_choice_solution_id = '" . $this->_duplicate_of . "' ";
 
143
 
 
144
 
 
145
                return $where;
 
146
        }
 
147
 
 
148
        function _buildChoiceSolutionListQuery_orderby()
 
149
        {
 
150
 
 
151
                switch($this->_ordering)
 
152
                {
 
153
                        case "solutionumber":
 
154
                                $orderby = "qapoll_choice_solution_link.solution_number ";
 
155
                        break;
 
156
 
 
157
                        case "selectedfirst":
 
158
                                $orderby = "qapoll_choice_solution_link.selected DESC, qapoll_choice_solution_link.solution_number ";
 
159
                        break;
 
160
 
 
161
                        default:
 
162
                                $orderby = "qapoll_choice_solution_link.solution_number ";
 
163
                        break;
 
164
                }
 
165
 
 
166
                return ($orderby != null)?"ORDER BY " . $orderby:"";
 
167
        }
 
168
 
 
169
        /**
 
170
         * Set the filter parameters. Giving the GET array is usually fine.
 
171
         * It will sanitize the necessary stuff.
 
172
         */
 
173
        function setFilterParameters($getarray)
 
174
        {
 
175
                //Save the array first.
 
176
                $this->_filter_array = $getarray;
 
177
 
 
178
                if($getarray['choice_id'] != null && is_numeric($getarray['choice_id']))
 
179
                        $this->_choice_id = $getarray['choice_id'];
 
180
                if($getarray['choice_relation_relative_number'] != null && is_numeric($getarray['choice_relation_relative_number']))
 
181
                        $this->_choice_relation_relative_number = $getarray['choice_relation_relative_number'];
 
182
                if($getarray['duplicate_of'] != null && is_numeric($getarray['duplicate_of']))
 
183
                        $this->_duplicate_of = $getarray['duplicate_of'];
 
184
 
 
185
                if($getarray['userid'] != null && is_numeric($getarray['userid']))
 
186
                        $this->_userid = $getarray['userid'];
 
187
 
 
188
 
 
189
                //Save the states options
 
190
                if($getarray['state_new'] != null && is_numeric($getarray['state_new']))
 
191
                        $this->_states['new'] = ($getarray['state_new'] != 0);
 
192
                if($getarray['state_deleted'] != null && is_numeric($getarray['state_deleted']))
 
193
                        $this->_states['deleted'] = ($getarray['state_deleted'] != 0);
 
194
                if($getarray['state_duplicate'] != null && is_numeric($getarray['state_duplicate']))
 
195
                        $this->_states['duplicate'] = ($getarray['state_duplicate'] != 0);
 
196
 
 
197
                //Save the link states options
 
198
                if($getarray['state_link_new'] != null && is_numeric($getarray['state_link_new']))
 
199
                        $this->_link_states['new'] = ($getarray['state_link_new'] != 0);
 
200
                if($getarray['state_link_deleted'] != null && is_numeric($getarray['state_link_deleted']))
 
201
                        $this->_link_states['deleted'] = ($getarray['state_link_deleted'] != 0);
 
202
 
 
203
                if($getarray['ordering'] != null)
 
204
                        $this->_ordering = $getarray['ordering'];
 
205
 
 
206
        }
 
207
 
 
208
        /**
 
209
         * Set the data filter. This will be used to control the columns of data returned.
 
210
         * Useful to reduce SQL processing time.
 
211
         */
 
212
        function setDataFilter($filter_array)
 
213
        {
 
214
                //Save the filter array
 
215
                $this->_data_filter = $filter_array;
 
216
 
 
217
                //Override all the options: set all to false.
 
218
                if($filter_array['include_minimal_data'] != null && $filter_array['include_minimal_data'] == true)
 
219
                {
 
220
                        $this->_include_minimal_data = true;
 
221
                        $this->_include_user_vote = false;
 
222
                }
 
223
                else
 
224
                {
 
225
                        if($filter_array['include_user_vote'] != null && is_numeric($filter_array['include_user_vote']))
 
226
                                $this->_include_user_vote = $filter_array['include_user_vote'];
 
227
 
 
228
                }
 
229
 
 
230
        }
 
231
 
 
232
 
 
233
        /**
 
234
         * Delete the fetched entries. Set status = -1 and update comment count.
 
235
         */
 
236
        function deleteEntries()
 
237
        {
 
238
                $entries_id = array();
 
239
                $entries = $this->getData();
 
240
 
 
241
                foreach($entries as $entry)
 
242
                {
 
243
                        $choicesolution = new ChoiceSolutionModel();
 
244
                        $choicesolution->setId($entry->id);
 
245
                        $choicesolution->delete();
 
246
                }
 
247
 
 
248
                it_query($query);
 
249
        }
 
250
 
 
251
 
 
252
 
 
253
        /**
 
254
         * This function is a callback. DO NOT CALL DIRECTLY!
 
255
         * It is used by UserModel::_hasPermission to determine if the user user_id is
 
256
         * the owner of the $model_id object.
 
257
         * If $model_id is not in this instance, it return false.
 
258
         */
 
259
        function _callback_isOwner($model_id, $user_id)
 
260
        {
 
261
                //If there is no filter, the model is probably not initialized yet. => Design error.
 
262
                //Return false, and 
 
263
                if($this->_filter_array == null)
 
264
                {
 
265
                        drupal_set_message("Probable design error while getting user permissions. " . 
 
266
                                "Please see ChoiceSolutionListModel::_callback_isOwner", 'notice_msg');
 
267
                        return false;
 
268
                }
 
269
 
 
270
                //Check if user_id is the owner of model_id
 
271
                $data = $this->getData();
 
272
                foreach($data as $item)
 
273
                {
 
274
                        if($item->id == $model_id)
 
275
                                return ($item->userid == $user_id);
 
276
                }
 
277
 
 
278
                return false;
 
279
        }
 
280
 
 
281
}
 
282
 
 
283
?>