4
Copyright (C) 2008 Nicolas Deschildre <ndeschildre@gmail.com>
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.
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.
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.
24
class ChoiceSolutionListModel extends ModelList
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.
40
var $_choice_relation_relative_number = -1;
41
var $_duplicate_of = -2;
43
var $_link_states = array(
50
var $_ordering = "solutionumber";
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?
59
var $_include_minimal_data = false;
60
var $_include_user_vote = true;
64
* Default constructor.
66
function ChoiceSolutionListModel()
76
//Get the choice solution list
77
$query = "SELECT qapoll_choice_solution.*, qapoll_choice_solution_link.solution_number, " .
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, ":"") .
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 " .
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 . " ":"") .
93
$this->_buildChoiceSolutionListQuery_where() .
94
$this->_buildChoiceSolutionListQuery_orderby();
96
$choicesolutions = it_query($query);
98
//Store the result in a array
99
$choicesolution_list = array();
100
while ($choicesolution = db_fetch_object($choicesolutions))
102
$choicesolution_list[] = $choicesolution;
106
return $choicesolution_list;
109
function _buildChoiceSolutionListQuery_where()
111
$where = "WHERE true ";
113
if($this->_states['deleted'] == false)
115
$where .= "AND qapoll_choice_solution.status != -2 ";
117
if($this->_states['new'] == false)
119
$where .= "AND qapoll_choice_solution.status != 1 ";
121
if($this->_states['duplicate'] == false)
123
$where .= "AND qapoll_choice_solution.status != -1 ";
126
if($this->_link_states['deleted'] == false)
128
$where .= "AND qapoll_choice_solution_link.status != -2 ";
130
if($this->_link_states['new'] == false)
132
$where .= "AND qapoll_choice_solution_link.status != 1 ";
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 . "' ";
148
function _buildChoiceSolutionListQuery_orderby()
151
switch($this->_ordering)
153
case "solutionumber":
154
$orderby = "qapoll_choice_solution_link.solution_number ";
157
case "selectedfirst":
158
$orderby = "qapoll_choice_solution_link.selected DESC, qapoll_choice_solution_link.solution_number ";
162
$orderby = "qapoll_choice_solution_link.solution_number ";
166
return ($orderby != null)?"ORDER BY " . $orderby:"";
170
* Set the filter parameters. Giving the GET array is usually fine.
171
* It will sanitize the necessary stuff.
173
function setFilterParameters($getarray)
175
//Save the array first.
176
$this->_filter_array = $getarray;
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'];
185
if($getarray['userid'] != null && is_numeric($getarray['userid']))
186
$this->_userid = $getarray['userid'];
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);
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);
203
if($getarray['ordering'] != null)
204
$this->_ordering = $getarray['ordering'];
209
* Set the data filter. This will be used to control the columns of data returned.
210
* Useful to reduce SQL processing time.
212
function setDataFilter($filter_array)
214
//Save the filter array
215
$this->_data_filter = $filter_array;
217
//Override all the options: set all to false.
218
if($filter_array['include_minimal_data'] != null && $filter_array['include_minimal_data'] == true)
220
$this->_include_minimal_data = true;
221
$this->_include_user_vote = false;
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'];
234
* Delete the fetched entries. Set status = -1 and update comment count.
236
function deleteEntries()
238
$entries_id = array();
239
$entries = $this->getData();
241
foreach($entries as $entry)
243
$choicesolution = new ChoiceSolutionModel();
244
$choicesolution->setId($entry->id);
245
$choicesolution->delete();
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.
259
function _callback_isOwner($model_id, $user_id)
261
//If there is no filter, the model is probably not initialized yet. => Design error.
263
if($this->_filter_array == null)
265
drupal_set_message("Probable design error while getting user permissions. " .
266
"Please see ChoiceSolutionListModel::_callback_isOwner", 'notice_msg');
270
//Check if user_id is the owner of model_id
271
$data = $this->getData();
272
foreach($data as $item)
274
if($item->id == $model_id)
275
return ($item->userid == $user_id);
4
Copyright (C) 2008 Nicolas Deschildre <ndeschildre@gmail.com>
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.
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.
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.
24
class ChoiceSolutionListModel extends ModelList
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.
40
var $_choice_relation_relative_number = -1;
41
var $_duplicate_of = -2;
43
var $_link_states = array(
50
var $_ordering = "solutionumber";
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?
59
var $_include_minimal_data = false;
60
var $_include_user_vote = true;
64
* Default constructor.
66
function ChoiceSolutionListModel()
76
//Get the choice solution list
77
$query = "SELECT qapoll_choice_solution.*, qapoll_choice_solution_link.solution_number, " .
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, ":"") .
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 " .
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 . " ":"") .
93
$this->_buildChoiceSolutionListQuery_where() .
94
$this->_buildChoiceSolutionListQuery_orderby();
96
$choicesolutions = it_query($query);
98
//Store the result in a array
99
$choicesolution_list = array();
100
while ($choicesolution = db_fetch_object($choicesolutions))
102
$choicesolution_list[] = $choicesolution;
106
return $choicesolution_list;
109
function _buildChoiceSolutionListQuery_where()
111
$where = "WHERE true ";
113
if($this->_states['deleted'] == false)
115
$where .= "AND qapoll_choice_solution.status != -2 ";
117
if($this->_states['new'] == false)
119
$where .= "AND qapoll_choice_solution.status != 1 ";
121
if($this->_states['duplicate'] == false)
123
$where .= "AND qapoll_choice_solution.status != -1 ";
126
if($this->_link_states['deleted'] == false)
128
$where .= "AND qapoll_choice_solution_link.status != -2 ";
130
if($this->_link_states['new'] == false)
132
$where .= "AND qapoll_choice_solution_link.status != 1 ";
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 . "' ";
148
function _buildChoiceSolutionListQuery_orderby()
151
switch($this->_ordering)
153
case "solutionumber":
154
$orderby = "qapoll_choice_solution_link.solution_number ";
157
case "selectedfirst":
158
$orderby = "qapoll_choice_solution_link.selected DESC, qapoll_choice_solution_link.solution_number ";
162
$orderby = "qapoll_choice_solution_link.solution_number ";
166
return ($orderby != null)?"ORDER BY " . $orderby:"";
170
* Set the filter parameters. Giving the GET array is usually fine.
171
* It will sanitize the necessary stuff.
173
function setFilterParameters($getarray)
175
//Save the array first.
176
$this->_filter_array = $getarray;
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'];
185
if($getarray['userid'] != null && is_numeric($getarray['userid']))
186
$this->_userid = $getarray['userid'];
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);
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);
203
if($getarray['ordering'] != null)
204
$this->_ordering = $getarray['ordering'];
209
* Set the data filter. This will be used to control the columns of data returned.
210
* Useful to reduce SQL processing time.
212
function setDataFilter($filter_array)
214
//Save the filter array
215
$this->_data_filter = $filter_array;
217
//Override all the options: set all to false.
218
if($filter_array['include_minimal_data'] != null && $filter_array['include_minimal_data'] == true)
220
$this->_include_minimal_data = true;
221
$this->_include_user_vote = false;
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'];
234
* Delete the fetched entries. Set status = -1 and update comment count.
236
function deleteEntries()
238
$entries_id = array();
239
$entries = $this->getData();
241
foreach($entries as $entry)
243
$choicesolution = new ChoiceSolutionModel();
244
$choicesolution->setId($entry->id);
245
$choicesolution->delete();
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.
259
function _callback_isOwner($model_id, $user_id)
261
//If there is no filter, the model is probably not initialized yet. => Design error.
263
if($this->_filter_array == null)
265
drupal_set_message("Probable design error while getting user permissions. " .
266
"Please see ChoiceSolutionListModel::_callback_isOwner", 'notice_msg');
270
//Check if user_id is the owner of model_id
271
$data = $this->getData();
272
foreach($data as $item)
274
if($item->id == $model_id)
275
return ($item->userid == $user_id);