/
SurveyActivator.php
460 lines (423 loc) · 18.4 KB
/
SurveyActivator.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
<?php
/**
* Service class to activate survey.
* @todo Move to models/services/survey/ folder.
*/
class SurveyActivator
{
/** @var Survey */
protected $survey;
/** @var array */
protected $tableDefinition = [];
/** @var array */
protected $timingsTableDefinition = [];
/** @var string */
protected $error;
/** @var bool */
protected $createSurveyDir = false;
/** @var boolean */
public $isSimulation = false;
/**
* @param Survey $survey
*/
public function __construct($survey)
{
$this->survey = $survey;
}
/**
* @return array
* @throws CException
*/
public function activate()
{
EmCacheHelper::init(['sid' => $this->survey->sid, 'active' => 'Y']);
EmCacheHelper::flush();
$event = new PluginEvent('beforeSurveyActivate');
$event->set('surveyId', $this->survey->primaryKey);
$event->set('simulate', $this->isSimulation);
App()->getPluginManager()->dispatchEvent($event);
$this->setMySQLDefaultEngine(Yii::app()->getConfig('mysqlEngine'));
if (!$this->showEventMessages($event)) {
return ['error' => 'plugin'];
}
$this->prepareResponsesTable();
if ($this->isSimulation) {
return array(
'dbengine' => Yii::app()->db->getDriverName(),
'dbtype' => Yii::app()->db->driverName,
'fields' => $this->tableDefinition
);
}
if (!$this->createResponseTable()) {
return ['error' => $this->error];
}
if (!$this->createTimingsTable()) {
return ['error' => 'timingstablecreation'];
}
if (!empty($this->error)) {
return ['error' => $this->error];
}
Yii::app()->db->createCommand()->update(
Survey::model()->tableName(),
['active' => 'Y'],
'sid=:sid',
[':sid' => $this->survey->primaryKey]
);
$aResult = array(
'status' => 'OK',
'pluginFeedback' => $event->get('pluginFeedback')
);
if (!$this->createSurveyDirectory()) {
$aResult['warning'] = 'nouploadsurveydir';
}
LimeExpressionManager::SetDirtyFlag();
$event = new PluginEvent('afterSurveyActivate');
$event->set('surveyId', $this->survey->sid);
$event->set('simulate', $this->isSimulation);
App()->getPluginManager()->dispatchEvent($event);
return $aResult;
}
/**
* See bug #09828: Ranking question : update allowed can broke Survey DB
* If max_subquestions is not set or is invalid : set it to actual answers numbers
*
* @return void
*/
public function fixQuestionAttributeForRanking($sFieldMap)
{
foreach ($sFieldMap as $aRow) {
switch ($aRow['type']) {
case Question::QT_R_RANKING:
$nrOfAnswers = Answer::model()->countByAttributes(
array('qid' => $aRow['qid'])
);
$oQuestionAttribute = QuestionAttribute::model()->find(
"qid = :qid AND attribute = 'max_subquestions'",
array(':qid' => $aRow['qid'])
);
if (empty($oQuestionAttribute)) {
$oQuestionAttribute = new QuestionAttribute();
$oQuestionAttribute->qid = $aRow['qid'];
$oQuestionAttribute->attribute = 'max_subquestions';
$oQuestionAttribute->value = $nrOfAnswers;
$oQuestionAttribute->save();
} elseif (intval($oQuestionAttribute->value) < 1) {
// Fix it if invalid : disallow 0, but need a sub question minimum for EM
$oQuestionAttribute->value = $nrOfAnswers;
$oQuestionAttribute->save();
}
}
}
}
/**
* For each question, create the appropriate field(s)
*
* @param string $collation
* @return void
*/
protected function prepareTableDefinition(string $collation, array $sFieldMap)
{
foreach ($sFieldMap as $aRow) {
switch ($aRow['type']) {
case 'seed':
$aTableDefinition[$aRow['fieldname']] = "string(31)";
break;
case 'startlanguage':
$aTableDefinition[$aRow['fieldname']] = "string(20) NOT NULL";
break;
case 'id':
$aTableDefinition[$aRow['fieldname']] = "pk";
break;
case "startdate":
case "datestamp":
$aTableDefinition[$aRow['fieldname']] = "datetime NOT NULL";
break;
case "submitdate":
$aTableDefinition[$aRow['fieldname']] = "datetime";
break;
case "lastpage":
$aTableDefinition[$aRow['fieldname']] = "integer";
break;
case Question::QT_N_NUMERICAL:
case Question::QT_K_MULTIPLE_NUMERICAL:
$aTableDefinition[$aRow['fieldname']] = (array_key_exists('encrypted', $aRow) && $aRow['encrypted'] == 'Y') ? "text" : (isset($aRow['answertabledefinition']) && !empty($aRow['answertabledefinition']) ? $aRow['answertabledefinition'] : "decimal (30,10)");
break;
case Question::QT_S_SHORT_FREE_TEXT:
$aTableDefinition[$aRow['fieldname']] = isset($aRow['answertabledefinition']) && !empty($aRow['answertabledefinition']) ? $aRow['answertabledefinition'] : "text";
break;
case Question::QT_L_LIST:
case Question::QT_EXCLAMATION_LIST_DROPDOWN:
case Question::QT_M_MULTIPLE_CHOICE:
case Question::QT_P_MULTIPLE_CHOICE_WITH_COMMENTS:
case Question::QT_O_LIST_WITH_COMMENT:
if ($aRow['aid'] != 'other' && strpos($aRow['aid'], 'comment') === false && strpos($aRow['aid'], 'othercomment') === false) {
$aTableDefinition[$aRow['fieldname']] = (array_key_exists('encrypted', $aRow) && $aRow['encrypted'] == 'Y') ? "text" : (isset($aRow['answertabledefinition']) && !empty($aRow['answertabledefinition']) ? $aRow['answertabledefinition'] : "string(5)") ;
} else {
$aTableDefinition[$aRow['fieldname']] = "text";
}
break;
case Question::QT_U_HUGE_FREE_TEXT:
case Question::QT_Q_MULTIPLE_SHORT_TEXT:
case Question::QT_T_LONG_FREE_TEXT:
case Question::QT_SEMICOLON_ARRAY_TEXT:
case Question::QT_COLON_ARRAY_NUMBERS:
$aTableDefinition[$aRow['fieldname']] = isset($aRow['answertabledefinition']) && !empty($aRow['answertabledefinition']) ? $aRow['answertabledefinition'] : "text";
break;
case Question::QT_D_DATE:
$aTableDefinition[$aRow['fieldname']] = (array_key_exists('encrypted', $aRow) && $aRow['encrypted'] == 'Y') ? "text" : (isset($aRow['answertabledefinition']) && !empty($aRow['answertabledefinition']) ? $aRow['answertabledefinition'] : "datetime");
break;
case Question::QT_5_POINT_CHOICE:
case Question::QT_G_GENDER:
case Question::QT_Y_YES_NO_RADIO:
case Question::QT_X_TEXT_DISPLAY:
$aTableDefinition[$aRow['fieldname']] = (array_key_exists('encrypted', $aRow) && $aRow['encrypted'] == 'Y') ? "text" : (isset($aRow['answertabledefinition']) && !empty($aRow['answertabledefinition']) ? $aRow['answertabledefinition'] : "string(1)");
break;
case Question::QT_I_LANGUAGE:
$aTableDefinition[$aRow['fieldname']] = (array_key_exists('encrypted', $aRow) && $aRow['encrypted'] == 'Y') ? "text" : (isset($aRow['answertabledefinition']) && !empty($aRow['answertabledefinition']) ? $aRow['answertabledefinition'] : "string(20)");
break;
case Question::QT_VERTICAL_FILE_UPLOAD:
$this->createSurveyDir = true;
if (strpos($aRow['fieldname'], "_")) {
$aTableDefinition[$aRow['fieldname']] = (array_key_exists('encrypted', $aRow) && $aRow['encrypted'] == 'Y') ? "text" : (isset($aRow['answertabledefinition']) && !empty($aRow['answertabledefinition']) ? $aRow['answertabledefinition'] : "integer");
} else {
$aTableDefinition[$aRow['fieldname']] = "text";
}
break;
case "ipaddress":
if ($this->survey->isIpAddr) {
$aTableDefinition[$aRow['fieldname']] = "text";
}
break;
case "url":
if ($this->survey->isRefUrl) {
$aTableDefinition[$aRow['fieldname']] = "text";
}
break;
case "token":
$aTableDefinition[$aRow['fieldname']] = 'string(' . Token::MAX_LENGTH . ')' . $collation;
break;
case Question::QT_ASTERISK_EQUATION:
$aTableDefinition[$aRow['fieldname']] = isset($aRow['answertabledefinition']) && !empty($aRow['answertabledefinition']) ? $aRow['answertabledefinition'] : "text";
break;
case Question::QT_R_RANKING:
$aTableDefinition[$aRow['fieldname']] = (array_key_exists('encrypted', $aRow) && $aRow['encrypted'] == 'Y') ? "text" : (isset($aRow['answertabledefinition']) && !empty($aRow['answertabledefinition']) ? $aRow['answertabledefinition'] : "string(5)");
break; default:
$aTableDefinition[$aRow['fieldname']] = (array_key_exists('encrypted', $aRow) && $aRow['encrypted'] == 'Y') ? "text" : (isset($aRow['answertabledefinition']) && !empty($aRow['answertabledefinition']) ? $aRow['answertabledefinition'] : "string(5)");
}
if (!$this->survey->isAnonymized && !array_key_exists('token', $aTableDefinition)) {
$aTableDefinition['token'] = 'string(' . Token::MAX_LENGTH . ')' . $collation;
}
}
$this->tableDefinition = $aTableDefinition;
}
/**
* @return void
*/
protected function prepareTimingsTable()
{
$timingsfieldmap = createTimingsFieldMap(
$this->survey->primaryKey,
"full",
false,
false,
$this->survey->language
);
$aTimingTableDefinition = array();
$aTimingTableDefinition['id'] = $this->tableDefinition['id'];
foreach (array_keys($timingsfieldmap) as $field) {
$aTimingTableDefinition[$field] = 'FLOAT';
}
$this->timingsTableDefinition = $aTimingTableDefinition;
}
/**
* @return string
*/
protected function getCollation()
{
// Specify case sensitive collations for the token
$collation = '';
if (Yii::app()->db->driverName == 'mysqli' || Yii::app()->db->driverName == 'mysql') {
$collation = " COLLATE 'utf8mb4_bin'";
}
if (
Yii::app()->db->driverName == 'sqlsrv'
|| Yii::app()->db->driverName == 'dblib'
|| Yii::app()->db->driverName == 'mssql'
) {
$collation = " COLLATE SQL_Latin1_General_CP1_CS_AS";
}
return $collation;
}
/**
* @return void
*/
protected function prepareSimulateQuery()
{
if ($this->isSimulation) {
$tempTrim = trim($this->tableDefinition);
$brackets = strpos($tempTrim, "(");
if ($brackets === false) {
$type = substr($tempTrim, 0, 2);
} else {
$type = substr($tempTrim, 0, 2);
}
$arrSim[] = array($type);
$this->tableDefinition = $arrSim;
}
}
/**
* @return void
*/
protected function prepareResponsesTable()
{
/** @var string */
$collation = $this->getCollation();
//Check for any additional fields for this survey and create necessary fields (token and datestamp)
$this->survey->fixInvalidQuestions();
//Get list of questions for the base language
$sFieldMap = createFieldMap($this->survey, 'full', true, false, $this->survey->language);
$this->fixQuestionAttributeForRanking($sFieldMap);
$this->prepareTableDefinition($collation, $sFieldMap);
$this->prepareSimulateQuery();
}
/**
* @return boolean
* @throws CDbException
* @throws CException
*/
protected function createResponseTable()
{
$sTableName = $this->survey->responsesTableName;
Yii::app()->loadHelper("database");
try {
Yii::app()->db->createCommand()->createTable($sTableName, $this->tableDefinition);
// Refresh schema cache just in case the table existed in the past
Yii::app()->db->schema->getTable($sTableName, true);
} catch (Exception $e) {
if (App()->getConfig('debug')) {
$this->error = $e->getMessage();
} else {
$this->error = 'surveytablecreation';
}
return false;
}
try {
if (isset($this->tableDefinition['token'])) {
Yii::app()->db->createCommand()->createIndex(
"idx_survey_token_{$this->survey->primaryKey}_" . rand(1, 50000),
$sTableName,
'token'
);
}
} catch (\Exception $e) {
}
$this->createResponseTableKeys();
return true;
}
/**
* @param PluginEvent $event
* @return boolean
*/
protected function showEventMessages($event)
{
$success = $event->get('success');
$message = $event->get('message');
if ($success === false) {
Yii::app()->user->setFlash('error', $message);
return false;
} elseif (!empty($message)) {
Yii::app()->user->setFlash('info', $message);
}
return true;
}
/**
* @return void
* @throws CDbException
* @throws CException
*/
protected function createResponseTableKeys()
{
$iAutoNumberStart = Yii::app()->db->createCommand()
->select('autonumber_start')
->from(Survey::model()->tableName())
->where('sid=:sid', [':sid' => $this->survey->primaryKey])
->queryScalar();
//if there is an autonumber_start field, start auto numbering here
if ($iAutoNumberStart !== false && $iAutoNumberStart > 0) {
if (Yii::app()->db->driverName == 'mssql' || Yii::app()->db->driverName == 'sqlsrv' || Yii::app()->db->driverName == 'dblib') {
mssql_drop_coulmn_with_constraints($this->survey->responsesTableName, 'id');
$sQuery = "ALTER TABLE {$this->survey->responsesTableName} ADD [id] int identity({$iAutoNumberStart},1)";
Yii::app()->db->createCommand($sQuery)->execute();
// Add back the primaryKey
Yii::app()->db->createCommand()->addPrimaryKey('PRIMARY_' . rand(1, 50000), $this->survey->responsesTableName, 'id');
} elseif (Yii::app()->db->driverName == 'pgsql') {
$sQuery = "SELECT setval(pg_get_serial_sequence('{$this->survey->responsesTableName}', 'id'),{$iAutoNumberStart},false);";
// FIXME @ not good
@Yii::app()->db->createCommand($sQuery)->execute();
} else {
$sQuery = "ALTER TABLE {$this->survey->responsesTableName} AUTO_INCREMENT = {$iAutoNumberStart}";
// FIXME @ not good
@Yii::app()->db->createCommand($sQuery)->execute();
}
}
}
/**
* @return boolean
*/
protected function createTimingsTable()
{
if ($this->survey->isSaveTimings) {
$this->prepareTimingsTable();
$sTableName = $this->survey->timingsTableName;
try {
Yii::app()->db->createCommand()->createTable($sTableName, $this->timingsTableDefinition);
// Refresh schema cache just in case the table existed in the past
Yii::app()->db->schema->getTable($sTableName, true);
} catch (\Exception $e) {
throw $e;
}
}
return true;
}
/**
* @return bool
*/
protected function createSurveyDirectory()
{
$iSurveyID = $this->survey->primaryKey;
// create the survey directory where the uploaded files can be saved
if ($this->createSurveyDir) {
if (!file_exists(Yii::app()->getConfig('uploaddir') . "/surveys/" . $iSurveyID . "/files")) {
if (!(mkdir(Yii::app()->getConfig('uploaddir') . "/surveys/" . $iSurveyID . "/files", 0777, true))) {
return false;
} else {
file_put_contents(Yii::app()->getConfig('uploaddir') . "/surveys/" . $iSurveyID . "/files/index.html", '<html><head></head><body></body></html>');
}
}
}
return true;
}
/**
* Set the default_storage_engine for mysql DB
* @param string $dbEngine
*/
private function setMySQLDefaultEngine($dbEngine)
{
/* empty dbEngine : out */
if (empty($dbEngine)) {
return;
}
$db = Yii::app()->db;
/* not DB : out */
if (empty($db)) {
return;
}
/* not mysql : out */
if (!in_array($db->driverName, [InstallerConfigForm::DB_TYPE_MYSQL, InstallerConfigForm::DB_TYPE_MYSQLI])) {
return;
}
/* seems OK, sysadmin allowed to broke system */
$db->createCommand(new CDbExpression(sprintf('SET default_storage_engine=%s;', $dbEngine)))
->execute();
}
}