/
QuestionAttribute.php
309 lines (281 loc) · 12.5 KB
/
QuestionAttribute.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
303
304
305
306
307
308
309
<?php if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}
/*
* 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 QuestionAttribute
*
* @property integer $qaid ID Primary key
* @property integer $qid Question ID
* @property string $attribute attribute name (max 50 chars)
* @property string $value Attribute value
* @property string $language Language code eg:'en'
*
* @property Question $question
* @property Survey $survey
*
*/
class QuestionAttribute extends LSActiveRecord
{
/**
* @inheritdoc
* @return QuestionAttribute
*/
public static function model($class = __CLASS__)
{
/** @var self $model */
$model = parent::model($class);
return $model;
}
/** @inheritdoc */
public function tableName()
{
return '{{question_attributes}}';
}
/** @inheritdoc */
public function primaryKey()
{
return 'qaid';
}
/** @inheritdoc */
public function relations()
{
return array(
/** NB! do not use this relation use $this->question instead @see getQuestion() */
'qid' => array(self::BELONGS_TO, 'Question', 'qid', 'together' => true),
);
}
/**
* This defaultScope indexes the ActiveRecords given back by attribute name
* Important: This does not work if you want to retrieve records for more than one question at a time.
* In that case disable the defaultScope by using MyModel::model()->resetScope()->findAll();
* @return array Scope that indexes the records by their attribute bane
*/
public function defaultScope()
{
return array('index'=>'attribute');
}
/** @inheritdoc */
public function rules()
{
return array(
array('qid,attribute', 'required'),
array('value', 'LSYii_Validators'),
);
}
/**
* @param integer $iQuestionID
* @param string $sAttributeName
* @param string $sValue
* @return CDbDataReader
*/
public function setQuestionAttribute($iQuestionID, $sAttributeName, $sValue)
{
$oModel = new self;
$aResult = $oModel->findAll('attribute=:attributeName and qid=:questionID', array(':attributeName'=>$sAttributeName, ':questionID'=>$iQuestionID));
if (!empty($aResult)) {
$oModel->updateAll(array('value'=>$sValue), 'attribute=:attributeName and qid=:questionID', array(':attributeName'=>$sAttributeName, ':questionID'=>$iQuestionID));
} else {
$oModel = new self;
$oModel->attribute = $sAttributeName;
$oModel->value = $sValue;
$oModel->qid = $iQuestionID;
$oModel->save();
}
return Yii::app()->db->createCommand()
->select()
->from($this->tableName())
->where(array('and', 'qid=:qid'))->bindParam(":qid", $qid)
->order('qaid asc')
->query();
}
/**
* Set attributes for multiple questions
*
* NOTE: We can't use self::setQuestionAttribute() because it doesn't check for question types first.
* TODO: the question type check should be done via rules, or via a call to a question method
* TODO: use an array for POST values, like for a form submit So we could parse it from the controller instead of using $_POST directly here
*
* @var integer $iSid the sid to update (only to check permission)
* @var array $aQidsAndLang an array containing the list of primary keys for questions ( {qid, lang} )
* @var array $aAttributesToUpdate array continaing the list of attributes to update
* @var array $aValidQuestionTypes the question types we can update for those attributes
*/
public function setMultiple($iSid, $aQidsAndLang, $aAttributesToUpdate, $aValidQuestionTypes)
{
// Permissions check
if (Permission::model()->hasSurveyPermission($iSid, 'surveycontent', 'update')) {
// For each question
foreach ($aQidsAndLang as $sQidAndLang) {
$aQidAndLang = explode(',', $sQidAndLang); // Each $aQidAndLang correspond to a question primary key, which is a pair {qid, lang}.
$iQid = $aQidAndLang[0]; // Those pairs are generated by CGridView
$sLanguage = $aQidAndLang[1];
// We need to generate a question object to check for the question type
// So, we can also force the sid: we don't allow to update questions on different surveys at the same time (permission check is by survey)
$oQuestion = Question::model()->find('qid=:qid and language=:language and sid=:sid', array(":qid"=>$iQid, ":language"=>$sLanguage, ":sid"=>$iSid));
// For each attribute
foreach ($aAttributesToUpdate as $sAttribute) {
// TODO: use an array like for a form submit, so we can parse it from the controller instead of using $_POST directly here
$sValue = Yii::app()->request->getPost($sAttribute);
$iInsertCount = QuestionAttribute::model()->findAllByAttributes(array('attribute'=>$sAttribute, 'qid'=>$iQid));
// We check if we can update this attribute for this question type
// TODO: if (in_array($oQuestion->attributes, $sAttribute))
if (in_array($oQuestion->type, $aValidQuestionTypes)) {
if (count($iInsertCount) > 0) {
// Update
QuestionAttribute::model()->updateAll(array('value'=>$sValue), 'attribute=:attribute AND qid=:qid', array(':attribute'=>$sAttribute, ':qid'=>$iQid));
} else {
// Create
$oAttribute = new QuestionAttribute;
$oAttribute->qid = $iQid;
$oAttribute->value = $sValue;
$oAttribute->attribute = $sAttribute;
$oAttribute->save();
}
}
}
}
}
}
/**
* Returns Question attribute array name=>value
*
* @access public
* @param int $iQuestionID
* @param string $sLanguage restrict to this language (@todo : add it in qanda)
* @return array|boolean
* @throws CException
*/
public function getQuestionAttributes($iQuestionID, $sLanguage = null)
{
$iQuestionID = (int) $iQuestionID;
static $aQuestionAttributesStatic = array(); // TODO : replace by Yii::app()->cache
// Limit the size of the attribute cache due to memory usage
$aQuestionAttributesStatic = array_splice($aQuestionAttributesStatic, -1000, null, true);
if (isset($aQuestionAttributesStatic[$iQuestionID])) {
return $aQuestionAttributesStatic[$iQuestionID];
}
$aQuestionAttributes = array();
$oQuestion = Question::model()->with('survey')->find("qid=:qid", array('qid'=>$iQuestionID)); // Maybe take parent_qid attribute before this qid attribute
if ($oQuestion) {
if ($sLanguage) {
$aLanguages = array($sLanguage);
} else {
$aLanguages = $oQuestion->survey->allLanguages;
}
// Get all atribute set for this question
$sType = $oQuestion->type;
// For some reason this happened in bug #10684
if ($sType == null) {
throw new \CException("Question is corrupt: no type defined for question ".$iQuestionID);
}
$aAttributeNames = questionHelper::getQuestionAttributesSettings($sType);
/* Get whole existing attribute for this question in an array*/
$oAttributeValues = QuestionAttribute::model()->findAll("qid=:qid", array('qid'=>$iQuestionID));
foreach ($oAttributeValues as $oAttributeValue) {
if ($oAttributeValue->attribute == 'question_template') {
$aAttributeValues['question_template'] = $oAttributeValue->value;
$aAttributeNames = Question::getQuestionTemplateAttributes($aAttributeNames, $aAttributeValues, $oQuestion);
break;
}
}
$aAttributeValues = array();
foreach ($oAttributeValues as $oAttributeValue) {
if ($oAttributeValue->language) {
$aAttributeValues[$oAttributeValue->attribute][$oAttributeValue->language] = $oAttributeValue->value;
} else {
/* Don't replace existing language, use '' for null key (and for empty string) */
$aAttributeValues[$oAttributeValue->attribute][''] = $oAttributeValue->value;
}
}
// Fill with aQuestionAttributes with default attribute or with aAttributeValues
// Can not use array_replace due to i18n
foreach ($aAttributeNames as $aAttribute) {
if ($aAttribute['i18n'] == false) {
if (isset($aAttributeValues[$aAttribute['name']][''])) {
$aQuestionAttributes[$aAttribute['name']] = $aAttributeValues[$aAttribute['name']][''];
} elseif (isset($aAttributeValues[$aAttribute['name']])) {
/* Some survey have language is set for attribute without language (see #11980). This must fix for public survey and not only for admin. */
$aQuestionAttributes[$aAttribute['name']] = reset($aAttributeValues[$aAttribute['name']]);
} else {
$aQuestionAttributes[$aAttribute['name']] = $aAttribute['default'];
}
} else {
foreach ($aLanguages as $sLanguage) {
if (isset($aAttributeValues[$aAttribute['name']][$sLanguage])) {
$aQuestionAttributes[$aAttribute['name']][$sLanguage] = $aAttributeValues[$aAttribute['name']][$sLanguage];
} elseif (isset($aAttributeValues[$aAttribute['name']][''])) {
$aQuestionAttributes[$aAttribute['name']][$sLanguage] = $aAttributeValues[$aAttribute['name']][''];
} else {
$aQuestionAttributes[$aAttribute['name']][$sLanguage] = $aAttribute['default'];
}
}
}
}
} else {
return false; // return false but don't set $aQuestionAttributesStatic[$iQuestionID]
}
$aQuestionAttributesStatic[$iQuestionID] = $aQuestionAttributes;
return $aQuestionAttributes;
}
public static function insertRecords($data)
{
$attrib = new self;
foreach ($data as $k => $v) {
$attrib->$k = $v;
}
return $attrib->save();
}
/**
* @param string $fields
* @param mixed $condition
* @param string $orderby
* @return array
*/
public function getQuestionsForStatistics($fields, $condition, $orderby = false)
{
$command = Yii::app()->db->createCommand()
->select($fields)
->from($this->tableName())
->where($condition);
if ($orderby != false) {
$command->order($orderby);
}
return $command->queryAll();
}
/**
* @return Question
*/
public function getQuestion()
{
$criteria = new CDbCriteria();
$criteria->addCondition('qid=:qid');
$criteria->params = [':qid'=>$this->qid];
if ($this->language) {
$criteria->addCondition('language=:language');
$criteria->params = [':qid'=>$this->qid, ':language'=>$this->language];
}
/** @var Question $model */
$model = Question::model()->find($criteria);
return $model;
}
/**
* @return Survey
*/
public function getSurvey()
{
return $this->question->survey;
}
}