-
Notifications
You must be signed in to change notification settings - Fork 988
/
SurveysGroupsController.php
445 lines (402 loc) · 17.7 KB
/
SurveysGroupsController.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
<?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.
*
* Surveys Groups Controller
*/
use LimeSurvey\Models\Services\SurveysGroupCreator;
/**
* Class SurveysGroupsController
*/
class SurveysGroupsController extends Survey_Common_Action
{
/**
* Displays a particular model.
*
* @param integer $id the ID of the model to be displayed
* @return void
*/
public function view($id)
{
$this->render('view', array(
'model' => $this->loadModel($id),
));
}
/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*
* @return void
* @throws CHttpException
*/
public function create()
{
if (!Permission::model()->hasGlobalPermission('surveysgroups', 'create')) {
throw new CHttpException(403, gT("You do not have permission to access this page."));
}
$model = new SurveysGroups();
/* Move to SurveysGroup model init ? */
$model->owner_id = Yii::app()->user->id;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
$user = Yii::app()->user;
$request = Yii::app()->request;
if ($request->getPost('SurveysGroups')) {
$service = new SurveysGroupCreator(
$request,
$user,
$model,
new SurveysGroupsettings()
);
if ($service->save()) {
$this->getController()->redirect(
App()->createUrl("admin/surveysgroups/sa/update", array('id' => $model->gsid, '#' => 'settingsForThisGroup'))
);
}
} else {
$model->name = SurveysGroups::getNewCode();
}
$aData = array(
'model' => $model,
'action' => App()->createUrl("admin/surveysgroups/sa/create", array('#' => 'settingsForThisGroup')),
'pageTitle' => gT('Create survey group'),
);
$aData['aRigths'] = array(
'update' => true,
'delete' => false,
'owner_id' => true,
);
$aData['fullpagebar'] = array(
'savebutton' => array(
'form' => 'surveys-groups-form'
),
'saveandclosebutton' => [
'form' => 'surveys-groups-form',
],
'white_closebutton' => array(
'url' => App()->createUrl('surveyAdministration/listsurveys', ['#' => 'surveygroups']),
),
);
/* User for dropdown */
$aUserIds = getUserList('onlyuidarray');
$userCriteria = new CDbCriteria();
$userCriteria->select = array("uid", "users_name", "full_name");
$userCriteria->order = "full_name";
$userCriteria->addInCondition('uid', $aUserIds);
$aData['oUsers'] = User::model()->findAll($userCriteria);
$this->_renderWrappedTemplate('surveysgroups', 'create', $aData);
}
/**
* Show and updates a particular model.
* If update is successful, the browser will be redirected to the 'view' page.
*
* @param integer $id the ID of the model to be updated
* @return void
* @throws CHttpException
*/
public function update(int $id)
{
$model = $this->loadModel($id);
if (!empty(App()->getRequest()->getPost('SurveysGroups'))) {
if (!$model->hasPermission('group', 'update')) {
throw new CHttpException(403, gT("You do not have permission to access this page."));
}
$postSurveysGroups = App()->getRequest()->getPost('SurveysGroups');
/* Mimic survey system : only owner and superadmin can update owner … */
/* After update : potential loose of rights on SurveysGroups */
if (
$model->owner_id != Yii::app()->user->id
&& !Permission::model()->hasGlobalPermission('superadmin', 'read')
) {
$postSurveysGroups['owner_id'] = $model->owner_id;
}
if ($model->gsid == 1) {
/* Move this to model */
$postSurveysGroups['alwaysavailable'] = 1;
}
// parent_id control
if (!empty($postSurveysGroups['parent_id'])) {
$parentId = $postSurveysGroups['parent_id'] ;
/* Check permission */
$aAvailableParents = $model->getParentGroupOptions($model->gsid);
if (!array_key_exists($parentId, $aAvailableParents)) {
Yii::app()->setFlashMessage(sprintf(gT("You don't have rights on Survey group"), CHtml::encode($parentId)), 'error');
$postSurveysGroups['parent_id'] = $model->parent_id;
}
/* avoid loop */
$ParentSurveyGroup = $this->loadModel($parentId);
$aParentsGsid = $ParentSurveyGroup->getAllParents(true);
if (in_array($model->gsid, $aParentsGsid)) {
Yii::app()->setFlashMessage(gT("A child group can't be set as parent group"), 'error');
$this->getController()->redirect($this->getController()->createUrl('surveyAdministration/listsurveys', array("#" => 'surveygroups')));
}
}
$model->attributes = $postSurveysGroups;
if ($model->save()) {
if (App()->request->getPost('saveandclose') !== null) {
$this->getController()->redirect($this->getController()->createUrl('surveyAdministration/listsurveys', array("#" => 'surveygroups')));
}
}
}
$oSurveySearch = new Survey('search');
$oSurveySearch->gsid = $model->gsid;
$aData = array(
'model' => $model,
'action' => App()->createUrl("admin/surveysgroups/sa/update", array('id' => $model->gsid, '#' => 'settingsForThisGroup')),
'pageTitle' => gT('Update survey group: ') . $model->title,
);
$aData['oSurveySearch'] = $oSurveySearch;
$aData['aRigths'] = array(
'update' => $model->hasPermission('group', 'update'),
'delete' => $model->hasPermission('group', 'delete'),
'owner_id' => $model->owner_id == Yii::app()->user->id || Permission::model()->hasGlobalPermission('superadmin', 'read')
);
$updateRightsForm = $aData['aRigths']['update'] ? 'surveys-groups-form' : null;
$aData['fullpagebar'] = [
'returnbutton' => [
'url' => 'surveyAdministration/listsurveys#surveygroups',
'text' => gT('Back'),
],
'savebutton' => [
'form' => $updateRightsForm,
],
'saveandclosebutton' => [
'$updateRightsForm',
],
];
/* User for dropdown */
$aUserIds = getUserList('onlyuidarray');
if (!in_array($model->owner_id, $aUserIds)) {
$aUserIds[] = $model->owner_id;
}
$userCriteria = new CDbCriteria();
$userCriteria->select = array("uid", "users_name", "full_name");
$userCriteria->order = "full_name";
$userCriteria->addInCondition('uid', $aUserIds);
$aData['oUsers'] = User::model()->findAll($userCriteria);
$oTemplateOptions = new TemplateConfiguration();
$oTemplateOptions->scenario = 'surveygroup';
$aData['templateOptionsModel'] = $oTemplateOptions;
// Page size
if (Yii::app()->request->getParam('pageSize')) {
Yii::app()->user->setState('pageSizeTemplateView', (int) Yii::app()->request->getParam('pageSize'));
}
$aData['pageSize'] = Yii::app()->user->getState('pageSizeTemplateView', Yii::app()->params['defaultPageSize']); // Page size
$this->_renderWrappedTemplate('surveysgroups', 'update', $aData);
}
/**
* Show the survey settings menue for a particular group
* @param integer $id group id, used for permission control
* @return void
*/
public function surveysettingmenues($id)
{
if (!$this->loadModel($id)->hasPermission('surveysettings', 'read')) {
throw new CHttpException(403, gT("You do not have permission to access this page."));
}
/* Can not call gloalsettings contoller fuinction sice _construct check access … */
$menues = Surveymenu::model()->getMenuesForGlobalSettings();
Yii::app()->getController()->renderPartial('super/_renderJson', ['data' => $menues[0]]);
}
/**
* Updates a particular model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id the ID of the model to be updated
* @throws CHttpException
* @todo : find where it shown
* @todo : fix $_POST call
*/
public function surveySettings(int $id)
{
$bRedirect = 0;
/** @var SurveysGroups $model */
$model = $this->loadModel($id);
if (!$model->hasPermission('surveysettings', 'read')) {
throw new CHttpException(403, gT("You do not have permission to access this page."));
}
$aData = array(
'model' => $model
);
$sPartial = Yii::app()->request->getParam('partial', '_generaloptions_panel');
/** @var SurveysGroupsettings $oSurvey */
$oSurvey = SurveysGroupsettings::model()->findByPk($model->gsid);
$oSurvey->setOptions(); //this gets the "values" from the group that inherits to this group ...
$oSurvey->owner_id = $model->owner_id;
if (App()->getRequest()->isPostRequest && !$model->hasPermission('surveysettings', 'update')) {
throw new CHttpException(403, gT("You do not have permission to update survey settings."));
}
//every $_POST checked here is one of the switchers(On|Off|Inherit) names
// Name of sidemenulink => name of input field
// "General settings" => 'template'
// "Presentation" => 'showxquestions'
// "Pariticipant setting" => 'anonymized'
// "Notification & data" => 'datestamp'
// "Publication & access" => 'listpublic'
if (
isset($_POST['template']) || isset($_POST['showxquestions']) || isset($_POST['anonymized'])
|| isset($_POST['datestamp']) || isset($_POST['listpublic'])
) {
$oSurvey->attributes = $_POST;
if (isset($_POST['listpublic'])) {
//what is usecaptcha used for? see saveTranscribeCaptchaOptions method description ...
// in default group this is set to 'N' ... (this means 'none' no captcha for survey access, regigstration
// and 'save&load'
$oSurvey->usecaptcha = Survey::saveTranscribeCaptchaOptions();
}
if ($oSurvey->save()) {
$bRedirect = 1;
}
}
$users = getUserList();
$aData['users'] = array();
$inheritOwner = empty($oSurvey['ownerLabel']) ? $oSurvey['owner_id'] : $oSurvey['ownerLabel'];
$aData['users']['-1'] = gT('Inherit') . ' [' . $inheritOwner . ']';
foreach ($users as $user) {
$aData['users'][$user['uid']] = $user['user'] . ($user['full_name'] ? ' - ' . $user['full_name'] : '');
}
// Sort users by name
asort($aData['users']);
$aData['oSurvey'] = $oSurvey;
if ($bRedirect && App()->request->getPost('saveandclose') !== null) {
$this->getController()->redirect($this->getController()->createUrl('surveyAdministration/listsurveys', array("#" => 'surveygroups')));
}
// Page size
if (Yii::app()->request->getParam('pageSize')) {
Yii::app()->user->setState('pageSizeTemplateView', (int) Yii::app()->request->getParam('pageSize'));
}
$aData['pageSize'] = Yii::app()->user->getState('pageSizeTemplateView', Yii::app()->params['defaultPageSize']); // Page size
Yii::app()->clientScript->registerPackage('bootstrap-switch', LSYii_ClientScript::POS_BEGIN);
Yii::app()->clientScript->registerPackage('globalsidepanel');
$aData['aDateFormatDetails'] = getDateFormatData(Yii::app()->session['dateformat']);
$aData['jsData'] = [
'sgid' => $id,
'baseLinkUrl' => 'admin/surveysgroups/sa/surveysettings/id/' . $id,
'getUrl' => Yii::app()->createUrl(
'admin/surveysgroups/sa/surveysettingmenues',
array('id' => $id)
),
'i10n' => [
'Survey settings' => gT('Survey settings')
]
];
$buttons = [];
// White Close Button
$buttons['white_closebutton'] = array(
'url' => App()->createUrl('surveyAdministration/listsurveys', array('#' => 'surveygroups')),
);
if ($model->hasPermission('surveysettings', 'update')) {
// Save Button
$buttons['savebutton'] = [
'form' => 'survey-settings-options-form'
];
// Save and Close butotn
$buttons['saveandclosebutton'] = array(
'form' => 'survey-settings-options-form'
);
}
$aData['partial'] = $sPartial;
// Page Title
$aData['pageTitle'] = gT('Survey settings for group: ') . $model->title;
$aData['fullpagebar'] = $buttons;
$this->_renderWrappedTemplate('surveysgroups', 'surveySettings', $aData);
}
/**
* Deletes a particular model.
* If deletion is successful, the browser will be redirected to the 'admin' page.
* @param integer $id the ID of the model to be deleted
*/
public function delete($id)
{
$oGroupToDelete = $this->loadModel($id);
if (!$oGroupToDelete->hasPermission('group', 'delete')) {
throw new CHttpException(403, gT("You do not have permission to access this page."));
}
$sGroupTitle = $oGroupToDelete->title;
$returnUrl = App()->getRequest()->getPost('returnUrl', array('surveyAdministration/listsurveys', '#' => 'surveygroups'));
if ($oGroupToDelete->gsid == 1) {
Yii::app()->setFlashMessage(gT("You can't delete the default survey group!"), 'error');
$this->getController()->redirect($returnUrl);
} elseif ($oGroupToDelete->hasSurveys) {
Yii::app()->setFlashMessage(gT("You can't delete a group if it's not empty!"), 'error');
$this->getController()->redirect($returnUrl);
} elseif ($oGroupToDelete->hasChildGroups) {
Yii::app()->setFlashMessage(gT("You can't delete a group because one or more groups depend on it as parent!"), 'error');
$this->getController()->redirect($returnUrl);
} else {
$oGroupToDelete->delete();
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if (!App()->getRequest()->getQuery('ajax')) {
Yii::app()->setFlashMessage(sprintf(gT("The survey group '%s' was deleted."), CHtml::encode($sGroupTitle)), 'success');
$this->getController()->redirect($returnUrl);
}
}
}
/**
* Lists all models
* Only list SurveysGroup according to Permission, user must just be loggued.
* @return void
*/
public function index()
{
$model = new SurveysGroups('search');
$aData = array(
'model' => $model
);
$this->_renderWrappedTemplate('surveysgroups', 'index', $aData);
}
/**
* Manages all models.
* @TODO : Remove
*/
public function admin()
{
/* @see next comment : throw 500 error */
throw new CHttpException(400, gT("Invalid action"));
$model = new SurveysGroups('search'); // @todo : fix this : need update permission
$model->unsetAttributes(); // clear any default values
if (!empty(App()->getRequest()->getParam('SurveysGroups'))) {
$model->attributes = App()->getRequest()->getParam('SurveysGroups');
}
/* Throw : SurveysGroupsController and its behaviors do not have a method or closure named "render". */
$this->render('admin', array(
'model' => $model,
));
}
/**
* Returns the data model based on the primary key given in the GET variable.
* If the data model is not found, an HTTP exception will be raised.
* @param integer $id the ID of the model to be loaded
* @return SurveysGroups the loaded model
* @throws CHttpException
*/
public function loadModel($id)
{
$model = SurveysGroups::model()->findByPk($id);
if ($model === null) {
throw new CHttpException(404, 'The requested page does not exist.');
}
if (!$model->hasPermission(SurveysGroups::getMinimalPermissionRead())) {
throw new CHttpException(403, gT("You do not have permission to access this page."));
}
return $model;
}
/**
* Performs the AJAX validation.
* @param SurveysGroups $model the model to be validated
*/
protected function performAjaxValidation($model)
{
if (App()->getRequest()->getPost('ajax') === 'surveys-groups-form') {
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
}