/
ParticipantAttributeName.php
525 lines (482 loc) · 22.7 KB
/
ParticipantAttributeName.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
<?php
/*
* LimeSurvey
* Copyright (C) 2007-2011 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.
*
*/
/**
* This is the model class for table "{{{{participant_attribute_names}}}}".
*
* The followings are the available columns in table '{{{{participant_attribute_names}}}}':
* @property integer $attribute_id
* @property string $attribute_type
* @property string $visible
*/
class ParticipantAttributeName extends LSActiveRecord
{
/**
* Returns the primary key of this table
*
* @access public
* @return string
*/
public function primaryKey() {
return 'attribute_id';
}
/**
* Returns the static model of ParticipantAttributeName table
*
* @static
* @access public
* @param string $class
* @return ParticipantAttributeName
*/
public static function model($class = __CLASS__) {
$model = parent::model($class);
$keys = $model->tableSchema->primaryKey;
if (is_array($keys) && count($keys)== 2) {
// Fix the primary key, needed for PgSQL http://bugs.limesurvey.org/view.php?id=6707
// First load the helper
Yii::app()->loadHelper('update/updatedb');
$dbType = setsDBDriverName();
$table = 'participant_attribute_names';
if ($dbType == 'mysql') {
// Only for mysql first remove auto increment
alterColumn($model->tableName(), $model->primaryKey(), $model->tableSchema->getColumn($model->primaryKey())->dbType, false);
}
dropPrimaryKey($table);
addPrimaryKey($table, (array) $model->primaryKey());
if ($dbType == 'mysql') {
// Add back auto increment
alterColumn($model->tableName(), $model->primaryKey(), 'autoincrement');
}
// Refresh all schema data now just to make sure
Yii::app()->db->schema->refresh();
$model->refreshMetaData();
}
return $model;
}
/**
* @return string the associated database table name
*/
public function tableName() {
return '{{participant_attribute_names}}';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that will receive user inputs.
return array(
array('defaultname','filter','filter' => 'strip_tags'),
array('attribute_type, visible', 'required'),
array('attribute_type', 'length', 'max'=>4),
array('visible', 'length', 'max'=>5),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('attribute_id, attribute_type, visible', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'participant_attribute_names_lang'=>array(self::HAS_MANY, 'ParticipantAttributeNameLang', 'attribute_id'),
'participant_attribute'=>array(self::HAS_ONE, 'ParticipantAttribute', 'attribute_id')
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'attribute_id' => 'Attribute',
'attribute_type' => 'Attribute Type',
'visible' => 'Visible',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('attribute_id',$this->attribute_id);
$criteria->compare('attribute_type',$this->attribute_type,true);
$criteria->compare('visible',$this->visible,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
function getAllAttributes()
{
$aResult=Yii::app()->db->createCommand()->select('{{participant_attribute_names}}.*')
->from('{{participant_attribute_names}}')
->order('{{participant_attribute_names}}.attribute_id')
->queryAll();
return $aResult;
}
function getAllAttributesValues()
{
return Yii::app()->db->createCommand()->select('*')->from('{{participant_attribute_values}}')->queryAll();
}
/**
* Get an array of CPDB attributes
*
* @param mixed $sLanguageFilter
*/
function getVisibleAttributes($sLanguageFilter=null)
{
if ($sLanguageFilter==null) $sLanguageFilter=Yii::app()->session['adminlang'];
$output=array();
//First get all the distinct id's that are visible
$ids = ParticipantAttributeName::model()->findAll("visible = 'TRUE'");
//Then find a language for each one - the current $lang, if possible, english second, otherwise, the first in the list
foreach($ids as $id) {
$langs=ParticipantAttributeNameLang::model()->findAll("attribute_id = :attribute_id", array(":attribute_id"=>$id->attribute_id));
$language=null;
foreach($langs as $lang) {
//If we can find a language match, set the language and exit
if($lang->lang == $sLanguageFilter) {
$language = $lang->lang;
$attribute_name = $lang->attribute_name;
break;
}
if($lang->lang == "en") {
$language = $lang->lang;
$attribute_name = $lang->attribute_name;
}
}
if($language==null) {
$language=$langs[0]->lang;
$attribute_name=$langs[0]->attribute_name;
}
$output[$id->attribute_id]=array("attribute_id"=>$id->attribute_id,
"attribute_type"=>$id->attribute_type,
"visible"=>$id->visible,
"attribute_name"=>$attribute_name,
"lang"=>$language);
}
return $output;
}
/**
* Returns a list of attributes, with name and value. Currently not working for alternate languages
*
* @param mixed $participant_id the id of the participant to return values/names for (if empty, returns all)
*/
function getParticipantVisibleAttribute($participant_id)
{
$output=array();
if($participant_id != ''){
$findCriteria=new CDbCriteria();
$findCriteria->addCondition('participant_id = :participant_id');
$findCriteria->params = array(':participant_id'=>$participant_id);
$records=ParticipantAttributeName::model()->with('participant_attribute_names_lang', 'participant_attribute')
->findAll($findCriteria);
foreach($records as $row) { //Iterate through each attribute
$thisname="";
$thislang="";
foreach($row->participant_attribute_names_lang as $names) { //Iterate through each language version of this attribute
if($thisname=="") {$thisname=$names->attribute_name; $thislang=$names->lang;} //Choose the first item by default
if($names->lang == Yii::app()->session['adminlang']) {$thisname=$names->attribute_name; $thislang=$names->lang;} //Override the default with the admin language version if found
}
$output[]=array('participant_id'=>$row->participant_attribute->participant_id,
'attribute_id'=>$row->attribute_id,
'attribute_type'=>$row->attribute_type,
'attribute_display'=>$row->visible,
'attribute_name'=>$thisname,
'value'=>$row->participant_attribute->value,
'lang'=>$thislang);
}
return $output;
} else {
$findCriteria=new CDbCriteria();
$records=ParticipantAttributeName::model()->with('participant_attribute_names_lang', 'participant_attribute')->findAll($findCriteria);
foreach($records as $row) { //Iterate through each attribute
$thisname="";
$thislang="";
foreach($row->participant_attribute_names_lang as $names) { //Iterate through each language version of this attribute
if($thisname=="") {$thisname=$names->attribute_name; $thislang=$names->lang;} //Choose the first item by default
if($names->lang == Yii::app()->session['adminlang']) {$thisname=$names->attribute_name; $thislang=$names->lang;} //Override the default with the admin language version if found
}
$output[]=array('participant_id'=>$row->participant_attribute->participant_id,
'attribute_id'=>$row->attribute_id,
'attribute_type'=>$row->attribute_type,
'attribute_display'=>$row->visible,
'attribute_name'=>$thisname,
'value'=>$row->participant_attribute->value,
'lang'=>$thislang);
}
return $output;
}
}
function getAttributeValue($participantid,$attributeid)
{
$data = Yii::app()->db->createCommand()
->select('*')
->from('{{participant_attribute}}')
->where('participant_id = :participant_id AND attribute_id = :attribute_id')
->bindValues(array(':participant_id'=>$participantid, ':attribute_id'=>$attributeid))
->queryRow();
return $data;
}
function getCPDBAttributes($count = false, $limit = -1, $offset = -1)
{
$findCriteria=new CDbCriteria();
$findCriteria->offset=$offset;
$findCriteria->limit=$limit;
$output=array();
$records = ParticipantAttributeName::model()->with('participant_attribute_names_lang')->findAll($findCriteria);
foreach($records as $row) { //Iterate through each attribute
$thisname="";
$thislang="";
foreach($row->participant_attribute_names_lang as $names) { //Iterate through each language version of this attribute
if($thisname=="") {$thisname=$names->attribute_name; $thislang=$names->lang;} //Choose the first item by default
if($names->lang == Yii::app()->session['adminlang']) {$thisname=$names->attribute_name; $thislang=$names->lang;} //Override the default with the admin language version if found
}
$output[]=array('attribute_id'=>$row->attribute_id,
'attribute_type'=>$row->attribute_type,
'attribute_display'=>$row->visible,
'attribute_name'=>$thisname,
'lang'=>$thislang);
}
/* $command = Yii::app()->db->createCommand()
->from('{{participant_attribute_names}}')
->leftjoin('{{participant_attribute_names_lang}}', '{{participant_attribute_names}}.attribute_id = {{participant_attribute_names_lang}}.attribute_id')
->where('lang = "'.Yii::app()->session['adminlang'].'"')
->limit(intval($limit), intval($offset)); */
if (empty($count))
{
return $output;
}
else
{
return count($output);
}
}
function getAttributesValues($attribute_id)
{
return Yii::app()->db->createCommand()
->select('*')
->from('{{participant_attribute_values}}')
->where('attribute_id = :attribute_id')
->bindParam(":attribute_id", $attribute_id, PDO::PARAM_INT)
->queryAll();
}
// this is a very specific function used to get the attributes that are not present for the participant
function getnotaddedAttributes($attributeid)
{
$output = array();
$notin=array();
foreach($attributeid as $row)
{
$notin[] = $row;
}
$criteria = new CDbCriteria();
$alias = $this->getTableAlias();
$criteria->addNotInCondition("$alias.attribute_id", $attributeid);
$records = ParticipantAttributeName::model()->with('participant_attribute_names_lang')->findAll($criteria);
foreach($records as $row) { //Iterate through each attribute
$thisname="";
$thislang="";
foreach($row->participant_attribute_names_lang as $names) { //Iterate through each language version of this attribute
if($thisname=="") {$thisname=$names->attribute_name; $thislang=$names->lang;} //Choose the first item by default
if($names->lang == Yii::app()->session['adminlang']) {$thisname=$names->attribute_name; $thislang=$names->lang;} //Override the default with the admin language version if found
}
$output[]=array('attribute_id'=>$row->attribute_id,
'attribute_type'=>$row->attribute_type,
'attribute_display'=>$row->visible,
'attribute_name'=>$thisname,
'lang'=>$thislang);
}
return $output;
}
/**
* Adds the data for a new attribute
*
* @param mixed $data
*/
function storeAttribute($data)
{
// Do not allow more than 60 attributes because queries will break because of too many joins
if (ParticipantAttributeName::model()->count()>59)
{
return false;
};
$oParticipantAttributeName=new ParticipantAttributeName;
$oParticipantAttributeName->attribute_type=$data['attribute_type'];
$oParticipantAttributeName->defaultname=$data['defaultname'];
$oParticipantAttributeName->visible=$data['visible'];
$oParticipantAttributeName->save();
$iAttributeID = $oParticipantAttributeName->attribute_id;
$oParticipantAttributeNameLang=new ParticipantAttributeNameLang;
$oParticipantAttributeNameLang->attribute_id= intval($iAttributeID);
$oParticipantAttributeNameLang->attribute_name= $data['attribute_name'];
$oParticipantAttributeNameLang->lang= Yii::app()->session['adminlang'];
$oParticipantAttributeNameLang->save();
return $iAttributeID;
}
function editParticipantAttributeValue($data)
{
$query = ParticipantAttribute::model()->find('participant_id = :participant_id AND attribute_id=:attribute_id',
array(':participant_id'=>$data['participant_id'],
':attribute_id'=>$data['attribute_id'])
);
if(count($query) == 0)
{
Yii::app()->db->createCommand()
->insert('{{participant_attribute}}',$data);
}
else
{
Yii::app()->db->createCommand()
->update('{{participant_attribute}}',
$data,
'participant_id = :participant_id2 AND attribute_id = :attribute_id2',
array(':participant_id2' => $data['participant_id'], ':attribute_id2'=>$data['attribute_id']));
}
}
function delAttribute($attid)
{
Yii::app()->db->createCommand()->delete('{{participant_attribute_names_lang}}', 'attribute_id = '.$attid);
Yii::app()->db->createCommand()->delete('{{participant_attribute_names}}', 'attribute_id = '.$attid);
Yii::app()->db->createCommand()->delete('{{participant_attribute_values}}', 'attribute_id = '.$attid);
Yii::app()->db->createCommand()->delete('{{participant_attribute}}', 'attribute_id = '.$attid);
}
function delAttributeValues($attid,$valid)
{
Yii::app()->db
->createCommand()
->delete('{{participant_attribute_values}}', 'attribute_id = '.$attid.' AND value_id = '.$valid);
}
function getAttributeNames($attributeid)
{
return Yii::app()->db->createCommand()->where("attribute_id = :attribute_id")->from('{{participant_attribute_names_lang}}')->select('*')->bindParam(":attribute_id", $attributeid, PDO::PARAM_INT)->queryAll();
}
function getAttributeName($attributeid, $lang='en')
{
return Yii::app()->db->createCommand()->where("attribute_id = :attribute_id AND lang = :lang")->from('{{participant_attribute_names_lang}}')->select('*')->bindParam(":attribute_id", $attributeid, PDO::PARAM_INT)->bindParam(":lang", $lang, PDO::PARAM_STR)->queryRow();
}
function getAttribute($attribute_id)
{
$data = Yii::app()->db->createCommand()->from('{{participant_attribute_names}}')->where('{{participant_attribute_names}}.attribute_id = '.$attribute_id)->select('*')->queryRow();
return $data;
}
function saveAttribute($data)
{
if (empty($data['attribute_id']))
{
return;
}
$insertnames = array();
if (!empty($data['attribute_type']))
{
$insertnames['attribute_type'] = $data['attribute_type'];
}
if (!empty($data['visible']))
{
$insertnames['visible'] = $data['visible'];
}
if (!empty($data['defaultname']))
{
$insertnames['defaultname'] = $data['defaultname'];
}
if (!empty($insertnames))
{
$oParticipantAttributeName=ParticipantAttributeName::model()->findByPk($data['attribute_id']);
foreach ($insertnames as $sFieldname=>$sValue)
{
$oParticipantAttributeName->$sFieldname=$sValue;
}
$oParticipantAttributeName->save();
}
if (!empty($data['attribute_name']))
{
$oParticipantAttributeNameLang=ParticipantAttributeNameLang::model()->findByPk(array('attribute_id'=>$data['attribute_id'],'lang'=>Yii::app()->session['adminlang']));
$oParticipantAttributeNameLang->attribute_name=$data['attribute_name'];
$oParticipantAttributeNameLang->save();
}
}
function saveAttributeLanguages($data)
{
$query = Yii::app()->db->createCommand()->from('{{participant_attribute_names_lang}}')->where('attribute_id = :attribute_id AND lang = :lang')->select('*')->bindParam(":attribute_id", $data['attribute_id'], PDO::PARAM_INT)->bindParam(":lang", $data['lang'], PDO::PARAM_STR)->queryAll();
if (count($query) == 0)
{
// A record does not exist, insert one.
$oParticipantAttributeNameLang=new ParticipantAttributeNameLang;
$oParticipantAttributeNameLang->attribute_id=$data['attribute_id'];
$oParticipantAttributeNameLang->attribute_name=$data['attribute_name'];
$oParticipantAttributeNameLang->lang=$data['lang'];
$oParticipantAttributeNameLang->save();
}
else
{
$oParticipantAttributeNameLang=ParticipantAttributeNameLang::model()->findByPk(array('attribute_id'=>$data['attribute_id'],'lang'=>$data['lang']));
$oParticipantAttributeNameLang->attribute_name=$data['attribute_name'];
$oParticipantAttributeNameLang->save();
}
}
function storeAttributeValues($data)
{
foreach ($data as $record) {
Yii::app()->db->createCommand()->insert('{{participant_attribute_values}}',$record);
}
}
function storeAttributeCSV($data)
{
$oParticipantAttributeName=new ParticipantAttributeName;
$oParticipantAttributeName->attribute_type=$data['attribute_type'];
$oParticipantAttributeName->defaultname=$data['defaultname'];
$oParticipantAttributeName->visible=$data['visible'];
$oParticipantAttributeName->save();
$iAttributeID = $oParticipantAttributeName->attribute_id;
$oParticipantAttributeNameLang=new ParticipantAttributeNameLang;
$oParticipantAttributeNameLang->attribute_id=$iAttributeID;
$oParticipantAttributeNameLang->attribute_name=$data['defaultname'];
$oParticipantAttributeNameLang->lang=Yii::app()->session['adminlang'];
$oParticipantAttributeNameLang->save();
return $iAttributeID;
}
//updates the attribute values in participant_attribute_values
function saveAttributeValue($data)
{
Yii::app()->db->createCommand()
->update('{{participant_attribute_values}}', $data, "attribute_id = :attribute_id AND value_id = :value_id", array(":attribute_id" => $data['attribute_id'], ":value_id" => $data['value_id']));
//->bindParam(":attribute_id", $data['attribute_id'], PDO::PARAM_INT)->bindParam(":value_id", $data['value_id'], PDO::PARAM_INT);
}
function saveAttributeVisible($attid,$visiblecondition)
{
$attribute_id = explode("_", $attid);
$data=array('visible'=>$visiblecondition);
if($visiblecondition == "")
{
$data=array('visible'=>'FALSE');
}
Yii::app()->db->createCommand()->update('{{participant_attribute_names}}',$data,'attribute_id = :attribute_id')->bindParam(":attribute_id", $attribute_id[1], PDO::PARAM_INT);
}
function getAttributeID()
{
$query = Yii::app()->db->createCommand()->select('attribute_id')->from('{{participant_attribute_names}}')->order('attribute_id','desc')->queryAll();
return $query;
}
function saveParticipantAttributeValue($data)
{
Yii::app()->db->createCommand()->insert('{{participant_attribute}}', $data);
}
}