-
Notifications
You must be signed in to change notification settings - Fork 988
/
QuotasController.php
442 lines (396 loc) · 16.6 KB
/
QuotasController.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
<?php
class QuotasController extends LSBaseController
{
/**
* @return string[] action filters
*/
public function filters()
{
return array(
'accessControl', // perform access control for CRUD operations
'postOnly + deleteAnswer, deleteQuota, insertQuotaAnswer', // we only allow deletion via POST request
);
}
/**
* Here we have to use the correct layout (NOT main.php)
*
* @param string $view
* @return bool
*/
protected function beforeRender($view)
{
$this->layout = 'layout_questioneditor';
LimeExpressionManager::SetSurveyId($this->aData['surveyid']);
LimeExpressionManager::StartProcessingPage(false, true);
return parent::beforeRender($view);
}
/**
* @param $surveyid
* @return void
*/
public function actionIndex($surveyid)
{
$surveyid = sanitize_int($surveyid);
if (!Permission::model()->hasSurveyPermission($surveyid, 'quotas')) {
throw new CHttpException(403, gT("You do not have permission on this survey."));
}
$oSurvey = Survey::model()->findByPk($surveyid);
$aData['surveyid'] = $oSurvey->sid;
// Set number of page
if (Yii::app()->getRequest()->getQuery('pageSize')) {
Yii::app()->user->setState('pageSize', (int) Yii::app()->getRequest()->getQuery('pageSize'));
}
$aData['iGridPageSize'] = Yii::app()->user->getState('pageSize', Yii::app()->params['defaultPageSize']);
$oDataProvider = new CArrayDataProvider($oSurvey->quotas, array(
'pagination' => array(
'pageSize' => $aData['iGridPageSize'],
'pageVar' => 'page'
),
));
//logic part here, get data for the index view
$oQuotasService = new \LimeSurvey\Models\Services\Quotas($oSurvey);
// Set number of page
if (Yii::app()->getRequest()->getQuery('pageSize')) {
Yii::app()->user->setState('pageSize', (int) Yii::app()->getRequest()->getQuery('pageSize'));
}
$aData['oDataProvider'] = new CArrayDataProvider($oSurvey->quotas, array(
'pagination' => array(
'pageSize' => Yii::app()->user->getState('pageSize', Yii::app()->params['defaultPageSize']),
'pageVar' => 'page'
),
));
// TopBar
$aData['topBar']['name'] = 'surveyTopbar_view';
$aData['topBar']['leftSideView'] = 'quotasTopbarLeft_view';
$topbarData = TopbarConfiguration::getSurveyTopbarData($oSurvey->sid);
$aData['topbar']['middleButtons'] = $this->renderPartial(
'/surveyAdministration/partial/topbar/surveyTopbarLeft_view',
$topbarData,
true
);
$aData['topbar']['rightButtons'] = $this->renderPartial(
'/surveyAdministration/partial/topbar_quotas/rightSideButtons',
[
'surveyid' => $oSurvey->sid
],
true
);
Yii::app()->loadHelper('admin.htmleditor');
$aData['title_bar']['title'] = $oSurvey->currentLanguageSettings->surveyls_title .
" (" . gT("ID") . ":" . $surveyid . ")";
$aData['subaction'] = gT("Survey quotas");
$aData['sidemenu']['state'] = false;
$this->aData = $aData;
$this->render('index', [
'quotasData' => $oQuotasService->getQuotaStructure(),
'oDataProvider' => $oDataProvider,
'oSurvey' => $oSurvey,
'iGridPageSize' => Yii::app()->user->getState('pageSize', Yii::app()->params['defaultPageSize'])
]);
}
/**
* @param $surveyid
* @return void
*/
public function actionQuickCSVReport($surveyid)
{
$surveyid = sanitize_int($surveyid);
if (!Permission::model()->hasSurveyPermission($surveyId, 'quotas')) {
throw new CHttpException(403, gT("You do not have permission on this survey."));
}
$oSurvey = Survey::model()->findByPk($surveyid);
/* Export a quickly done csv file */
header("Content-Disposition: attachment; filename=quotas-survey" . $surveyid . ".csv");
header("Content-type: text/comma-separated-values; charset=UTF-8");
echo gT("Quota name") . "," . gT("Limit") . "," . gT("Completed") . "," . gT("Remaining") . "\r\n";
if (!empty($oSurvey->quotas)) {
foreach ($oSurvey->quotas as $oQuota) {
$completed = $oQuota->completeCount;
echo $oQuota->name . "," . $oQuota->qlimit . "," .
$completed . "," . ($oQuota->qlimit - $completed) . "\r\n";
}
}
App()->end();
}
/**
* @param $surveyid
* @return void
* @throws CDbException
*/
public function actionAddNewQuota($surveyid)
{
$surveyid = sanitize_int($surveyid);
if (!Permission::model()->hasSurveyPermission($surveyid, 'quotas', 'create')) {
throw new CHttpException(403, gT("You do not have permission on this survey."));
}
Yii::app()->loadHelper('admin.htmleditor');
$oSurvey = Survey::model()->findByPk($surveyid);
$aData['surveyid'] = $oSurvey->sid;
$aData['thissurvey'] = getSurveyInfo($surveyid);
$aData['langs'] = $oSurvey->allLanguages;
$aData['baselang'] = $oSurvey->language;
$aData['sidemenu']['state'] = false;
$aData['subaction'] = gT("Survey quotas");
$topbarData = TopbarConfiguration::getSurveyTopbarData($oSurvey->sid);
$aData['topbar']['middleButtons'] = $this->renderPartial(
'/surveyAdministration/partial/topbar/surveyTopbarLeft_view',
$topbarData,
true
);
$aData['topbar']['rightButtons'] = $this->renderPartial(
'/surveyAdministration/partial/topbar/surveyTopbarRight_view',
[
'showSaveButton' => true
],
true
);
$aData['title_bar']['title'] = $oSurvey->currentLanguageSettings->surveyls_title .
" (" . gT("ID") . ":" . $surveyid . ")";
//$aData['surveybar']['savebutton']['form'] = 'frmeditgroup';
$oQuota = new Quota();
$oQuota->sid = $oSurvey->primaryKey;
$quotaService = new \LimeSurvey\Models\Services\Quotas($oSurvey);
if (App()->getRequest()->getPost('Quota')) {
$oQuota = $quotaService->saveNewQuota(App()->getRequest()->getPost('Quota'));
if (!$oQuota->getErrors()) {
Yii::app()->user->setFlash('success', gT("New quota saved"));
$this->redirect($this->createUrl("quotas/index/surveyid/$surveyid"));
}
}
// create QuotaLanguageSettings
foreach ($oSurvey->getAllLanguages() as $language) {
$oQuotaLanguageSetting = $quotaService->newQuotaLanguageSetting($oQuota, $language);
$aQuotaLanguageSettings[$language] = $oQuotaLanguageSetting;
}
$this->aData = $aData;
$this->render('newquota_view', [
'oQuota' => $oQuota,
'aQuotaLanguageSettings' => $aQuotaLanguageSettings
]);
}
/**
* @return void
*/
public function actionEditQuota()
{
$quotaId = sanitize_int(Yii::app()->request->getQuery('quota_id'));
$oQuota = $this->getQuotaWithPermission($quotaId, 'update');
$surveyid = $oQuota->sid;
$oSurvey = Survey::model()->findByPk($surveyid);
/* @var Quota $oQuota */
$oQuota = Quota::model()->findByPk($quotaId);
if (App()->getRequest()->getPost('Quota')) {
$quotaService = new \LimeSurvey\Models\Services\Quotas($oSurvey);
if ($quotaService->editQuota($oQuota, $_POST['Quota']) && !$oQuota->getErrors()) {
Yii::app()->user->setFlash('success', gT("Quota saved"));
$this->redirect($this->createUrl("quotas/index/surveyid/$surveyid"));
} else {
Yii::app()->user->setFlash('error', gT("Quota or quota languages could not be updated."));
}
}
$aQuotaLanguageSettings = [];
foreach ($oQuota->languagesettings as $languagesetting) {
/* url is decoded before usage @see https://github.com/LimeSurvey/LimeSurvey/blob/8d8420a4efcf8e71c4fccbb6708648ace263ca80/application/views/admin/survey/editLocalSettings_view.php#L60 */
$languagesetting['quotals_url'] = htmlspecialchars_decode((string) $languagesetting['quotals_url']);
$aQuotaLanguageSettings[$languagesetting->quotals_language] = $languagesetting;
}
$aData['surveyid'] = $surveyid;
$aData['sidemenu']['state'] = false;
$aData['title_bar']['title'] = $oSurvey->currentLanguageSettings->surveyls_title .
" (" . gT("ID") . ":" . $surveyid . ")";
$aData['subaction'] = gT("Survey quotas");
$topbarData = TopbarConfiguration::getSurveyTopbarData($oSurvey->sid);
$aData['topbar']['middleButtons'] = $this->renderPartial(
'/surveyAdministration/partial/topbar/surveyTopbarLeft_view',
$topbarData,
true
);
$aData['topbar']['rightButtons'] = $this->renderPartial(
'/surveyAdministration/partial/topbar/surveyTopbarRight_view',
[
'showSaveButton' => true
],
true
);
Yii::app()->loadHelper('admin.htmleditor');
$this->aData = $aData;
$this->render('editquota_view', [
'oQuota' => $oQuota,
'aQuotaLanguageSettings' => $aQuotaLanguageSettings
]);
}
/**
* @return void
*/
public function actionDeleteQuota()
{
$quotaId = Yii::app()->request->getPost('quota_id');
$oQuota = $this->getQuotaWithPermission($quotaId, 'delete');
$surveyid = $oQuota->sid;
Quota::model()->deleteByPk($quotaId);
QuotaLanguageSetting::model()->deleteAllByAttributes(array('quotals_quota_id' => $quotaId));
QuotaMember::model()->deleteAllByAttributes(array('quota_id' => $quotaId));
Yii::app()->user->setFlash('success', sprintf(gT("Quota with ID %s was deleted"), $quotaId));
$this->redirect($this->createUrl(["quotas/index/",[ 'surveyid' => $surveyid]]));
}
/**
* @return void
*/
public function actionNewAnswer()
{
$quotaId = Yii::app()->request->getParam('quota_id');
$quota = $this->getQuotaWithPermission($quotaId, 'delete');
$surveyid = $quota->sid;
$oSurvey = Survey::model()->findByPk($surveyid);
$aData['surveyid'] = $surveyid;
$sSubAction = Yii::app()->request->getParam('sSubaction', 'newanswer');
$renderView = array();
$quota = Quota::model()->findByPk($quotaId);
$aData['oQuota'] = $quota;
if (
($sSubAction == "newanswer" || ($sSubAction == "new_answer_two" && !isset($_POST['quota_qid']))) &&
Permission::model()->hasSurveyPermission($surveyid, 'quotas', 'create')
) {
$result = $oSurvey->quotableQuestions;
if (empty($result)) {
$renderView = 'newanswererror_view';
} else {
$renderView = 'newanswer_view';
}
}
$quotaService = new \LimeSurvey\Models\Services\Quotas($oSurvey);
if (
$sSubAction == "new_answer_two" && isset($_POST['quota_qid']) &&
Permission::model()->hasSurveyPermission($surveyid, 'quotas', 'create')
) {
$questionId = sanitize_int(Yii::app()->request->getPost('quota_qid'));
$oQuestion = Question::model()
->with('questionl10ns', array('language' => $oSurvey->language))
->findByPk(array('qid' => $questionId));
$aQuestionAnswers = $quotaService->getQuotaAnswers(
$questionId,
sanitize_int(Yii::app()->request->getPost('quota_id'))
);
$isAllAnswersSelected = $quotaService->allAnswersSelected($oQuestion, $aQuestionAnswers);
$aData['isAllAnswersSelected'] = $isAllAnswersSelected;
reset($aQuestionAnswers);
$aData['oQuestion'] = $oQuestion;
$aData['question_answers'] = $aQuestionAnswers;
//$aData['x'] = $cntQuestionAnswer; not needed in any view till now
$renderView = 'newanswertwo_view';
}
$aData['sBaseLang'] = $oSurvey->language;
$aData['sidemenu']['state'] = false;
$aData['title_bar']['title'] = $oSurvey->currentLanguageSettings->surveyls_title .
" (" . gT("ID") . ":" . $surveyid . ")";
$aData['subaction'] = gT("Survey quotas"); //title
$topbarData = TopbarConfiguration::getSurveyTopbarData($oSurvey->sid);
$aData['topbar']['middleButtons'] = $this->renderPartial(
'/surveyAdministration/partial/topbar/surveyTopbarLeft_view',
$topbarData,
true
);
$this->aData = $aData;
$this->render($renderView, $aData);
}
/**
* @return void
*/
public function actionInsertQuotaAnswer()
{
$quota_qid = Yii::app()->request->getPost('quota_qid');
$quota_id = Yii::app()->request->getPost('quota_id');
$quota_anscode = Yii::app()->request->getPost('quota_anscode');
$oQuota = $this->getQuotaWithPermission($quota_id, 'update');
$surveyid = $oQuota->sid;
$oQuotaMembers = new QuotaMember('create'); // Trigger the 'create' rules
$oQuotaMembers->sid = $surveyid;
$oQuotaMembers->qid = $quota_qid;
$oQuotaMembers->quota_id = $quota_id;
$oQuotaMembers->code = $quota_anscode;
if ($oQuotaMembers->save()) {
if (App()->getRequest()->getPost('createanother')) {
$this->redirect($this->createUrl(
'quotas/newAnswer',
[
'surveyid' => $surveyid,
'sSubAction' => 'newanswer',
'quota_id' => $quota_id
]
));
} else {
$this->redirect($this->createUrl('/quotas/index', ['surveyid' => $surveyid]));
}
} else {
// Save was not successful, redirect back
$this->redirect($this->createUrl(
'quotas/newAnswer',
[
'surveyid' => $surveyid,
'sSubAction' => 'newanswer',
'quota_id' => $quota_id
]
));
}
}
/**
* @return void
*/
public function actionDeleteAnswer()
{
$id = App()->request->getPost('quota_member_id');
$quotaMember = QuotaMember::model()->findByPk($id);
if (empty($quotaMember)) {
throw new CHttpException(404, gT("Quota member not found."));
}
$oQuota = $this->getQuotaWithPermission($quotaMember->quota_id, 'delete');
$surveyid = $oQuota->sid;
$quotaMember->delete();
$this->redirect($this->createUrl('/quotas/index', ['surveyid' => $oQuota->sid]));
}
/**
*
*/
public function actionMassiveAction($action, $surveyid)
{
$surveyid = sanitize_int($surveyid);
$oSurvey = Survey::model()->findByPk($surveyid);
$quotaService = new \LimeSurvey\Models\Services\Quotas($oSurvey);
if ($quotaService->checkActionPermissions($action)) {
$sItems = Yii::app()->request->getPost('sItems', '');
$aQuotaIds = json_decode($sItems);
$errors = $quotaService->multipleItemsAction(
$aQuotaIds,
$action,
Yii::app()->request->getPost('QuotaLanguageSetting', [])
);
if (empty($errors)) {
eT("OK!");
} else {
eT("Error!");
}
} else {
/* 403 error ? */
Yii::app()->user->setFlash('error', gT("Access denied."));
$this->redirect(Yii::app()->request->urlReferrer);
}
}
/**
* Get a quota after check exist and permission using permission on survey
* @param integer $quotaId
* @param string $sPermission to check (on survey quotas)
* throw Exception
* @return \Quota
*/
private function getQuotaWithPermission($quotaId, $sPermission = 'read')
{
$oQuota = Quota::model()->findByPk($quotaId);
if (empty($oQuota)) {
throw new CHttpException(404, gT("Quota not found."));
}
if (!Permission::model()->hasSurveyPermission($oQuota->sid, 'quotas', $sPermission)) {
throw new CHttpException(403, gT("You do not have permission on this quota."));
}
return $oQuota;
}
}