/
questiongroup.php
412 lines (352 loc) · 15.4 KB
/
questiongroup.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
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
/*
* 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.
*
* $Id$
*/
/**
* questiongroup
*
* @package LimeSurvey
* @author
* @copyright 2011
* @version $Id$
* @access public
*/
class questiongroup extends Survey_Common_Action
{
/**
* questiongroup::import()
* Function responsible to import a question group.
*
* @access public
* @return void
*/
function import()
{
$action = $_POST['action'];
$surveyid = $_POST['sid'];
$clang = $this->getController()->lang;
if ($action == 'importgroup')
{
$importgroup = "\n";
$importgroup .= "\n";
$sFullFilepath = Yii::app()->getConfig('tempdir') . DIRECTORY_SEPARATOR . $_FILES['the_file']['name'];
$aPathInfo = pathinfo($sFullFilepath);
$sExtension = $aPathInfo['extension'];
if (!@move_uploaded_file($_FILES['the_file']['tmp_name'], $sFullFilepath))
{
$fatalerror = sprintf($clang->gT("An error occurred uploading your file. This may be caused by incorrect permissions in your %s folder."), $this->config->item('tempdir'));
}
// validate that we have a SID
if (!returnGlobal('sid'))
$fatalerror .= $clang->gT("No SID (Survey) has been provided. Cannot import question.");
if (isset($fatalerror))
{
@unlink($sFullFilepath);
$this->getController()->error($fatalerror);
}
Yii::app()->loadHelper('admin/import');
// IF WE GOT THIS FAR, THEN THE FILE HAS BEEN UPLOADED SUCCESFULLY
if (strtolower($sExtension) == 'csv')
$aImportResults = CSVImportGroup($sFullFilepath, $surveyid);
elseif (strtolower($sExtension) == 'lsg')
$aImportResults = XMLImportGroup($sFullFilepath, $surveyid);
else
$this->getController()->error('Unknown file extension');
fixLanguageConsistency($surveyid);
if (isset($aImportResults['fatalerror']))
{
unlink($sFullFilepath);
$this->getController()->error($aImportResults['fatalerror']);
}
unlink($sFullFilepath);
$aData['display'] = $importgroup;
$aData['surveyid'] = $surveyid;
$aData['aImportResults'] = $aImportResults;
$aData['sExtension'] = $sExtension;
//$aData['display']['menu_bars']['surveysummary'] = 'importgroup';
$this->_renderWrappedTemplate('survey/QuestionGroups', 'import_view', $aData);
// TMSW Conditions->Relevance: call LEM->ConvertConditionsToRelevance() after import
}
}
/**
* questiongroup::add()
* Load add new question group screen.
* @return
*/
function add($surveyid)
{
$surveyid = sanitize_int($surveyid);
$aViewUrls = $aData = array();
if (hasSurveyPermission($surveyid, 'surveycontent', 'read'))
{
$clang = $this->getController()->lang;
Yii::app()->session['FileManagerContext'] = "create:group:{$surveyid}";
Yii::app()->loadHelper('admin/htmleditor');
Yii::app()->loadHelper('surveytranslator');
$grplangs = Survey::model()->findByPk($surveyid)->additionalLanguages;
$baselang = Survey::model()->findByPk($surveyid)->language;
$grplangs[] = $baselang;
$grplangs = array_reverse($grplangs);
$this->getController()->_js_admin_includes(Yii::app()->getConfig('adminscripts') . 'questiongroup.js');
$aData['display']['menu_bars']['surveysummary'] = 'addgroup';
$aData['surveyid'] = $surveyid;
$aData['action'] = $aData['display']['menu_bars']['gid_action'] = 'addgroup';
$aData['grplangs'] = $grplangs;
$aData['baselang'] = $baselang;
$this->_renderWrappedTemplate('survey/QuestionGroups', 'addGroup_view', $aData);
}
}
/**
* Insert the new group to the database
*
* @access public
* @param int $surveyid
* @return void
*/
public function insert($surveyid)
{
if (hasSurveyPermission($surveyid, 'surveycontent', 'create'))
{
Yii::app()->loadHelper('surveytranslator');
$grplangs = Survey::model()->findByPk($surveyid)->additionalLanguages;
$baselang = Survey::model()->findByPk($surveyid)->language;
$grplangs[] = $baselang;
$errorstring = '';
foreach ($grplangs as $grouplang)
if (empty($_POST['group_name_' . $grouplang]))
$errorstring.= getLanguageNameFromCode($grouplang, false) . "\\n";
if ($errorstring != '')
$this->getController()->redirect($this->getController()->createUrl('admin/survey/sa/view/surveyid/' . $surveyid));
else
{
$first = true;
foreach ($grplangs as $grouplang)
{
//Clean XSS
$group_name = $_POST['group_name_' . $grouplang];
$group_description = $_POST['description_' . $grouplang];
$group_name = html_entity_decode($group_name, ENT_QUOTES, "UTF-8");
$group_description = html_entity_decode($group_description, ENT_QUOTES, "UTF-8");
// Fix bug with FCKEditor saving strange BR types
$group_name = fixCKeditorText($group_name);
$group_description = fixCKeditorText($group_description);
if ($first)
{
$aData = array(
'sid' => $surveyid,
'group_name' => $group_name,
'description' => $group_description,
'group_order' => getMaxGroupOrder($surveyid),
'language' => $grouplang,
'randomization_group' => $_POST['randomization_group'],
'grelevance' => $_POST['grelevance'],
);
$group = new Groups;
foreach ($aData as $k => $v)
$group->$k = $v;
$group->save();
$groupid = $group->gid;
$first = false;
}
else
{
//switchMSSQLIdentityInsert('groups',true);
$aData = array(
'gid' => $groupid,
'sid' => $surveyid,
'group_name' => $group_name,
'description' => $group_description,
'group_order' => getMaxGroupOrder($surveyid),
'language' => $grouplang,
'randomization_group' => $_POST['randomization_group']
);
$group = new Groups;
foreach ($aData as $k => $v)
$group->$k = $v;
$group->save();
}
}
// This line sets the newly inserted group as the new group
if (isset($groupid))
$gid = $groupid;
Yii::app()->session['flashmessage'] = Yii::app()->lang->gT("New question group was saved.");
}
$this->getController()->redirect($this->getController()->createUrl('admin/survey/sa/view/surveyid/' . $surveyid . '/gid/' . $gid));
}
}
/**
* Action to delete a question group.
*
* @access public
* @return void
*/
public function delete($iSurveyId, $iGroupId)
{
$iSurveyId = sanitize_int($iSurveyId);
if (hasSurveyPermission($iSurveyId, 'surveycontent', 'delete'))
{
LimeExpressionManager::RevertUpgradeConditionsToRelevance($iSurveyId);
$iGroupId = sanitize_int($iGroupId);
$clang = $this->getController()->lang;
$iGroupsDeleted = Groups::deleteWithDependency($iGroupId, $iSurveyId);
if ($iGroupsDeleted !== 1)
{
fixSortOrderGroups($iSurveyId);
Yii::app()->user->setFlash('flashmessage', $clang->gT('The question group was deleted.'));
}
else
Yii::app()->user->setFlash('flashmessage', $clang->gT('Group could not be deleted'));
LimeExpressionManager::UpgradeConditionsToRelevance($iSurveyId);
$this->getController()->redirect($this->getController()->createUrl('admin/survey/sa/view/surveyid/' . $iSurveyId));
}
}
/**
* questiongroup::edit()
* Load editing of a question group screen.
*
* @access public
* @param int $surveyid
* @param int $gid
* @return void
*/
public function edit($surveyid, $gid)
{
$clang = $this->getController()->lang;
$surveyid = sanitize_int($surveyid);
$gid = sanitize_int($gid);
$aViewUrls = $aData = array();
if (hasSurveyPermission($surveyid, 'surveycontent', 'read'))
{
Yii::app()->session['FileManagerContext'] = "edit:group:{$surveyid}";
Yii::app()->loadHelper('admin/htmleditor');
Yii::app()->loadHelper('surveytranslator');
$aAdditionalLanguages = Survey::model()->findByPk($surveyid)->additionalLanguages;
$aBaseLanguage = Survey::model()->findByPk($surveyid)->language;
$aLanguages = array_merge(array($aBaseLanguage), $aAdditionalLanguages);
$grplangs = array_flip($aLanguages);
// Check out the intgrity of the language versions of this group
$egresult = Groups::model()->findAllByAttributes(array('sid' => $surveyid, 'gid' => $gid));
foreach ($egresult as $esrow)
{
$esrow = $esrow->attributes;
// Language Exists, BUT ITS NOT ON THE SURVEY ANYMORE
if (!in_array($esrow['language'], $aLanguages))
{
Groups::model()->deleteAllByAttributes(array('sid' => $surveyid, 'gid' => $gid, 'language' => $esrow['language']));
}
else
{
$grplangs[$esrow['language']] = 'exists';
}
if ($esrow['language'] == $aBaseLanguage)
$basesettings = $esrow;
}
// Create groups in missing languages
while (list($key, $value) = each($grplangs))
{
if ($value != 'exists')
{
$basesettings['language'] = $key;
$group = new Groups;
foreach ($basesettings as $k => $v)
$group->$k = $v;
switchMSSQLIdentityInsert('groups', true);
$group->save();
switchMSSQLIdentityInsert('groups', false);
}
}
$first = true;
foreach ($aLanguages as $sLanguage)
{
$oResult = Groups::model()->findByAttributes(array('sid' => $surveyid, 'gid' => $gid, 'language' => $sLanguage));
$aData['aGroupData'][$sLanguage] = $oResult->attributes;
$aTabTitles[$sLanguage] = getLanguageNameFromCode($sLanguage, false);
if ($first)
{
$aTabTitles[$sLanguage].= ' (' . $clang->gT("Base language") . ')';
$first = false;
}
}
$aData['action'] = $aData['display']['menu_bars']['gid_action'] = 'editgroup';
$aData['surveyid'] = $surveyid;
$aData['gid'] = $gid;
$aData['tabtitles'] = $aTabTitles;
$aData['aBaseLanguage'] = $aBaseLanguage;
$this->_renderWrappedTemplate('survey/QuestionGroups', 'editGroup_view', $aData);
}
}
/**
* Provides an interface for updating a group
*
* @access public
* @param int $gid
* @return void
*/
public function update($gid)
{
$gid = (int) $gid;
$group = Groups::model()->findByAttributes(array('gid' => $gid));
$surveyid = $group->sid;
if (hasSurveyPermission($surveyid, 'surveycontent', 'update'))
{
Yii::app()->loadHelper('surveytranslator');
$grplangs = Survey::model()->findByPk($surveyid)->additionalLanguages;
$baselang = Survey::model()->findByPk($surveyid)->language;
array_push($grplangs, $baselang);
foreach ($grplangs as $grplang)
{
if (isset($grplang) && $grplang != "")
{
$group_name = $_POST['group_name_' . $grplang];
$group_description = $_POST['description_' . $grplang];
$group_name = html_entity_decode($group_name, ENT_QUOTES, "UTF-8");
$group_description = html_entity_decode($group_description, ENT_QUOTES, "UTF-8");
// Fix bug with FCKEditor saving strange BR types
$group_name = fixCKeditorText($group_name);
$group_description = fixCKeditorText($group_description);
$aData = array(
'group_name' => $group_name,
'description' => $group_description,
'randomization_group' => $_POST['randomization_group'],
'grelevance' => $_POST['grelevance'],
);
$condition = array(
'gid' => $gid,
'sid' => $surveyid,
'language' => $grplang
);
$group = Groups::model()->findByAttributes($condition);
foreach ($aData as $k => $v)
$group->$k = $v;
$ugresult = $group->save();
}
}
Yii::app()->session['flashmessage'] = Yii::app()->lang->gT("Question group successfully saved.");
$this->getController()->redirect($this->getController()->createUrl('admin/survey/sa/view/surveyid/' . $surveyid . '/gid/' . $gid));
}
}
/**
* Renders template(s) wrapped in header and footer
*
* @param string $sAction Current action, the folder to fetch views from
* @param string|array $aViewUrls View url(s)
* @param array $aData Data to be passed on. Optional.
*/
protected function _renderWrappedTemplate($sAction = 'survey/QuestionGroups', $aViewUrls = array(), $aData = array())
{
$this->getController()->_css_admin_includes(Yii::app()->getConfig('adminstyleurl')."superfish.css");
parent::_renderWrappedTemplate($sAction, $aViewUrls, $aData);
}
}