-
Notifications
You must be signed in to change notification settings - Fork 988
/
Survey_dynamic.php
302 lines (273 loc) · 7.98 KB
/
Survey_dynamic.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
* LimeSurvey
* Copyright (C) 2007 The LimeSurvey Project Team / Carsten Schmitz
* All rights reserved.
* License: GNU/GPL License v2 or later, see LICENSE.php
* LimeSurvey is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*
* $Id$
* Files Purpose: lots of common functions
*/
class Survey_dynamic extends LSActiveRecord
{
protected static $sid = 0;
/**
* Returns the static model of Settings table
*
* @static
* @access public
* @param int $surveyid
* @return Survey_dynamic
*/
public static function model($sid = NULL)
{
$refresh = false;
if (!is_null($sid)) {
self::sid($sid);
$refresh = true;
}
$model = parent::model(__CLASS__);
//We need to refresh if we changed sid
if ($refresh === true) $model->refreshMetaData();
return $model;
}
/**
* Sets the survey ID for the next model
*
* @static
* @access public
* @param int $sid
* @return void
*/
public static function sid($sid)
{
self::$sid = (int) $sid;
}
/**
* Returns the setting's table name to be used by the model
*
* @access public
* @return string
*/
public function tableName()
{
return '{{survey_' . self::$sid . '}}';
}
/**
* Returns the primary key of this table
*
* @access public
* @return string
*/
public function primaryKey()
{
return 'id';
}
/**
* Insert records from $data array
*
* @access public
* @param array $data
* @return boolean
*/
public function insertRecords($data)
{
$record = new self;
foreach ($data as $k => $v)
{
$search = array('`', "'");
$k = str_replace($search, '', $k);
$v = str_replace($search, '', $v);
$record->$k = $v;
}
try
{
$record->save();
return $record->id;
}
catch(Exception $e)
{
return false;
}
}
/**
* Deletes some records from survey's table
* according to specific condition
*
* @static
* @access public
* @param array $condition
* @return int
*/
public static function deleteSomeRecords($condition = FALSE)
{
$survey = new Survey_dynamic;
$criteria = new CDbCriteria;
if ($condition != FALSE)
{
foreach ($condition as $column => $value)
{
return $criteria->addCondition($column . "=`" . $value . "`");
}
}
return $survey->deleteAll($criteria);
}
/**
* Return criteria updated with the ones needed for including results from the timings table
*
* @param CDbCriteria|string $criteria
*
* @return CDbCriteria
*/
public function addTimingCriteria($condition)
{
$newCriteria = new CDbCriteria();
$criteria = $this->getCommandBuilder()->createCriteria($condition);
if ($criteria->select == '*')
{
$criteria->select = 't.*';
}
$newCriteria->join = "LEFT JOIN {{survey_" . self::$sid . "_timings}} survey_timings ON t.id = survey_timings.id";
$newCriteria->select = 'survey_timings.*'; // Otherwise we don't get records from the token table
$newCriteria->mergeWith($criteria);
return $newCriteria;
}
/**
* Return criteria updated with the ones needed for including results from the token table
*
* @param CDbCriteria|string $criteria
*
* @return CDbCriteria
*/
public function addTokenCriteria($condition)
{
$newCriteria = new CDbCriteria();
$criteria = $this->getCommandBuilder()->createCriteria($condition);
if ($criteria->select == '*')
{
$criteria->select = 't.*';
}
$newCriteria->join = "LEFT JOIN {{tokens_" . self::$sid . "}} tokens ON t.token = tokens.token";
$newCriteria->select = 'tokens.*'; // Otherwise we don't get records from the token table
$newCriteria->mergeWith($criteria);
return $newCriteria;
}
/**
* Return true if actual survey is completed
*
* @param $srid : actual save survey id
*
* @return boolean
*/
public function isCompleted($srid)
{
$sid = self::$sid;
$completed=false;
if(Yii::app()->db->schema->getTable($this->tableName())){
$data=Yii::app()->db->createCommand()
->select("submitdate")
->from($this->tableName())
->where('id=:id', array(':id'=>$srid))
->queryRow();
if($data && $data['submitdate'])
{
$completed=true;
}
}
return $completed;
}
/**
* Return true if actual respnse exist in database
*
* @param $srid : actual save survey id
*
* @return boolean
*/
public function exist($srid)
{
$sid = self::$sid;
$exist=false;
if(Yii::app()->db->schema->getTable($this->tableName())){
$data=Yii::app()->db->createCommand()
->select("id")
->from($this->tableName())
->where('id=:id', array(':id'=>$srid))
->queryRow();
if($data)
{
$exist=true;
}
}
return $exist;
}
/**
* Return next id if next response exist in database
*
* @param integer $srid : actual save survey id
* @param boolean $usefilterstate
*
* @return integer
*/
public function next($srid,$usefilterstate=false)
{
$sid = self::$sid;
$next=false;
if ($usefilterstate && incompleteAnsFilterState() == 'incomplete')
$wherefilterstate='submitdate IS NULL';
elseif ($usefilterstate && incompleteAnsFilterState() == 'complete')
$wherefilterstate='submitdate IS NOT NULL';
else
$wherefilterstate='1=1';
if(Yii::app()->db->schema->getTable($this->tableName())){
$data=Yii::app()->db->createCommand()
->select("id")
->from($this->tableName())
->where(array('and',$wherefilterstate,'id > :id'), array(':id'=>$srid))
->order('id ASC')
->queryRow();
if($data)
{
$next=$data['id'];
}
}
return $next;
}
/**
* Return previous id if previous response exist in database
*
* @param integer $srid : actual save survey id
* @param boolean $usefilterstate
*
* @return integer
*/
public function previous($srid,$usefilterstate=false)
{
$sid = self::$sid;
$previous=false;
if ($usefilterstate && incompleteAnsFilterState() == 'incomplete')
$wherefilterstate='submitdate IS NULL';
elseif ($usefilterstate && incompleteAnsFilterState() == 'complete')
$wherefilterstate='submitdate IS NOT NULL';
else
$wherefilterstate='1=1';
if(Yii::app()->db->schema->getTable($this->tableName())){
$data=Yii::app()->db->createCommand()
->select("id")
->from($this->tableName())
->where(array('and',$wherefilterstate,'id < :id'), array(':id'=>$srid))
->order('id DESC')
->queryRow();
if($data)
{
$previous=$data['id'];
}
}
return $previous;
}
}
?>