/
Quota.php
217 lines (199 loc) · 7.32 KB
/
Quota.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
<?php
/*
* LimeSurvey
* Copyright (C) 2013 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.
*
* Files Purpose: lots of common functions
*/
/**
* Class Quota
*
* @property integer $id ID (primary key)
* @property integer $sid Survey ID
* @property string $name Quota name (max 255 chars)
* @property integer $qlimit Quota limit
* @property integer $active Whether quota is active (0/1)
* @property integer $action
* @property integer $autoload_url Whether URL is automatically redirected if quota is triggered (0/1)
*
* @property QuotaLanguageSetting[] $languagesettings Indexed by language code
* @property QuotaLanguageSetting $mainLanguagesetting
* @property QuotaLanguageSetting $currentLanguageSetting
* @property Survey $survey
* @property QuotaMember[] $quotaMembers
*
* @property integer $completeCount Count of completed interviews for this quota
*/
class Quota extends LSActiveRecord
{
public const ACTION_TERMINATE = 1;
public const ACTION_CONFIRM_TERMINATE = 2;
/* Default attributes */
public $active = 1;
public $action = self::ACTION_TERMINATE;
/**
* Returns the static model of Settings table
*
* @param string $className
* @return CActiveRecord
*/
public static function model($className = __CLASS__)
{
return parent::model($className);
}
/** @inheritdoc */
public function tableName()
{
return '{{quota}}';
}
/** @inheritdoc */
public function primaryKey()
{
return 'id';
}
/** @inheritdoc */
public function relations()
{
return array(
'survey' => array(self::BELONGS_TO, 'Survey', 'sid'),
'languagesettings' => array(self::HAS_MANY, 'QuotaLanguageSetting', 'quotals_quota_id', 'index' => 'quotals_language'),
'quotaMembers' => array(self::HAS_MANY, 'QuotaMember', 'quota_id'),
);
}
/** @inheritdoc */
public function rules()
{
return array(
array('name,qlimit,action', 'required'),
array('name', 'LSYii_Validators'), // Maybe more restrictive
array('qlimit', 'numerical', 'integerOnly' => true, 'min' => '0', 'max' => 2147483647, 'allowEmpty' => true),
array('action', 'numerical', 'integerOnly' => true, 'min' => '1', 'max' => '2', 'allowEmpty' => true), // Default is null ?
array('active', 'numerical', 'integerOnly' => true, 'min' => '0', 'max' => '1', 'allowEmpty' => true),
array('autoload_url', 'numerical', 'integerOnly' => true, 'min' => '0', 'max' => '1', 'allowEmpty' => true),
array('name', 'length', 'min' => 0, 'max' => 255),
);
}
public function attributeLabels()
{
return array(
'name' => gT("Quota name"),
'active' => gT("Active"),
'qlimit' => gT("Limit"),
'autoload_url' => gT("Autoload URL"),
'action' => gT("Quota action"),
);
}
/**
* @param $data
* @return bool|int
* @deprecated at 2018-01-29 use $model->attributes = $data && $model->save()
*/
public function insertRecords($data)
{
$quota = new self();
foreach ($data as $k => $v) {
$quota->$k = $v;
}
try {
$quota->save();
return $quota->id;
} catch (Exception $e) {
return false;
}
}
/**
* @param mixed|bool $condition
* @param bool $recursive
*/
public function deleteQuota($condition = false, $recursive = true)
{
if ($recursive == true) {
$oResult = Quota::model()->findAllByAttributes($condition);
foreach ($oResult as $aRow) {
QuotaLanguageSetting::model()->deleteAllByAttributes(array('quotals_quota_id' => $aRow['id']));
QuotaMember::model()->deleteAllByAttributes(array('quota_id' => $aRow['id']));
}
}
Quota::model()->deleteAllByAttributes($condition);
}
/**
* @return QuotaLanguageSetting
*/
public function getMainLanguagesetting()
{
return $this->languagesettings[$this->survey->language];
}
public function getCompleteCount()
{
if (!tableExists("survey_{$this->sid}")) {
return null;
}
/* Must control if column name exist (@todo : move this to QuotaMember::model(), even with deactivated survey*/
$aExistingColumnName = SurveyDynamic::model($this->sid)->getTableSchema()->getColumnNames();
if (count($this->quotaMembers) > 0) {
// Keep a list of fields for easy reference
$aQuotaColumns = array();
foreach ($this->quotaMembers as $member) {
if (!in_array($member->memberInfo['fieldname'], $aExistingColumnName)) {
\Yii::log(
sprintf(
"Invalid quota member %s",
$member->memberInfo['fieldname']
),
'warning',
'application.model.Quota'
);
return null;
}
$aQuotaColumns[$member->memberInfo['fieldname']][] = $member->memberInfo['value'];
}
$oCriteria = new CDbCriteria();
$oCriteria->condition = new CDbExpression("submitdate IS NOT NULL");
foreach ($aQuotaColumns as $sColumn => $aValue) {
if (count($aValue) == 1) {
$oCriteria->compare(Yii::app()->db->quoteColumnName($sColumn), $aValue); // NO need params : compare bind
} else {
$oCriteria->addInCondition(Yii::app()->db->quoteColumnName($sColumn), $aValue); // NO need params : addInCondition bind
}
}
$return = SurveyDynamic::model($this->sid)->count($oCriteria);
return $return;
} else {
return 0;
}
}
public function getViewArray()
{
$languageSettings = $this->currentLanguageSetting;
$members = array();
foreach ($this->quotaMembers as $quotaMember) {
$members[] = $quotaMember->memberInfo;
}
$attributes = $this->attributes;
return array_merge(array(), $languageSettings->attributes, array('members' => $members), $attributes);
}
/**
* Get the QuotaLanguageSetting for current language
* @return QuotaLanguageSetting
*/
public function getCurrentLanguageSetting()
{
$oQuotaLanguageSettings = QuotaLanguageSetting::model()
->findByAttributes(array(
'quotals_quota_id' => $this->id,
'quotals_language' => Yii::app()->getLanguage(),
));
if ($oQuotaLanguageSettings) {
return $oQuotaLanguageSettings;
}
/* If not exist or found, return the one from survey base languague */
return $this->getMainLanguagesetting();
}
}