-
Notifications
You must be signed in to change notification settings - Fork 988
/
quotas.php
558 lines (482 loc) · 22 KB
/
quotas.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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
<?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.
*
*/
/**
* Quotas Controller
*
* This controller performs quota actions
*
* @package LimeSurvey
* @subpackage Backend
*/
class quotas extends Survey_Common_Action
{
function __construct($controller, $id)
{
parent::__construct($controller, $id);
// Load helpers
Yii::app()->loadHelper('surveytranslator');
// Sanitize/get globals/variables
$_POST['quotamax'] = sanitize_int(Yii::app()->request->getPost('quotamax'));
if (empty($_POST['autoload_url']))
{
$_POST['autoload_url'] = 0;
}
if (empty($_POST['quota_limit']) || !is_numeric(Yii::app()->request->getPost('quota_limit')) || Yii::app()->request->getPost('quota_limit') < 0)
{
$_POST['quota_limit'] = 0;
}
}
private function _getData($iSurveyId)
{
// Set the variables in an array
$aData['iSurveyId'] = $aData['surveyid'] = $iSurveyId;
$aData['sBaseLang'] = Survey::model()->findByPk($iSurveyId)->language;
$aData['aLangs'] = Survey::model()->findByPk($iSurveyId)->additionalLanguages;
array_unshift($aData['aLangs'], $aData['sBaseLang']);
$aData['action'] = $action = Yii::app()->request->getParam('action');
if (!isset($action))
$aData['action'] = 'quotas';
return $aData;
}
private function _checkPermissions($iSurveyId, $sPermission)
{
if (!empty($sPermission) && !(Permission::model()->hasSurveyPermission($iSurveyId, 'quotas', $sPermission))) {
Yii::app()->session['flashmessage'] =gT('Access denied!');
$this->_redirectToIndex($iSurveyId);
}
}
function _redirectToIndex($iSurveyId)
{
if(Permission::model()->hasSurveyPermission($iSurveyId, 'quotas','read'))
{
$this->getController()->redirect($this->getController()->createUrl("/admin/quotas/sa/index/surveyid/$iSurveyId"));
}
else
{
Yii::app()->session['flashmessage'] =gT('Access denied!');
$this->getController()->redirect($this->getController()->createUrl("admin/survey/sa/view/surveyid/$iSurveyId"));
}
}
function index($iSurveyId, $quickreport = false)
{
$iSurveyId = sanitize_int($iSurveyId);
$this->_checkPermissions($iSurveyId, 'read');
$aData = $this->_getData($iSurveyId);
$aViewUrls = array();
if ($quickreport == false)
{
$aViewUrls[] = 'viewquotas_view';
}
$totalquotas = 0;
$totalcompleted = 0;
$csvoutput = array();
$criteria = new CDbCriteria;
$criteria->select = '*';
$criteria->join = 'LEFT JOIN {{quota_languagesettings}} as qls ON (t.id = qls.quotals_quota_id)';
$criteria->condition = 'sid=:survey AND quotals_language=:lang';
$criteria->params = array(':survey' => $iSurveyId, ':lang' => $aData['sBaseLang']);
$criteria->order = 'name';
$aResult = Quota::model()->findAll($criteria);
//if there are quotas let's proceed
if (count($aResult) > 0)
{
$aViewUrls['output'] = '';
//loop through all quotas
foreach ($aResult as $aQuotaListing)
{
$totalquotas += $aQuotaListing['qlimit'];
$completed = getQuotaCompletedCount($iSurveyId, $aQuotaListing['id']);
$highlight = ($completed >= $aQuotaListing['qlimit']) ? '' : "style='color: orange'"; //Incomplete quotas displayed in red
$totalcompleted = $totalcompleted + $completed;
$csvoutput[] = $aQuotaListing['name'] . "," . $aQuotaListing['qlimit'] . "," . $completed . "," . ($aQuotaListing['qlimit'] - $completed) . "\r\n";
if ($quickreport != false)
{
continue;
}
$aData['quotalisting'] = $aQuotaListing;
$aData['highlight'] = $highlight;
$aData['completed'] = $completed;
$aData['totalquotas'] = $totalquotas;
$aData['totalcompleted'] = $totalcompleted;
$aViewUrls['output'] .= $this->getController()->renderPartial("/admin/quotas/viewquotasrow_view", $aData, true);
//check how many sub-elements exist for a certain quota
$aResults2 = QuotaMember::model()->findAllByAttributes(array('quota_id' => $aQuotaListing['id']));
//loop through all sub-parts
foreach ($aResults2 as $aQuotaQuestions)
{
$aQuestionAnswers = self::getQuotaAnswers($aQuotaQuestions['qid'], $iSurveyId, $aQuotaListing['id']);
$aData['question_answers'] = $aQuestionAnswers;
$aData['quota_questions'] = $aQuotaQuestions;
$aViewUrls['output'] .= $this->getController()->renderPartial('/admin/quotas/viewquotasrowsub_view', $aData, true);
}
}
}
else
{
// No quotas have been set for this survey
$aViewUrls[] = 'viewquotasempty_view';
}
$aData['totalquotas'] = $totalquotas;
$aData['totalcompleted'] = $totalcompleted;
if ($quickreport == false)
{
$aViewUrls[] = 'viewquotasfooter_view';
$this->_renderWrappedTemplate('quotas', $aViewUrls, $aData);
}
else
{
header("Content-Disposition: attachment; filename=results-survey" . $iSurveyId . ".csv");
header("Content-type: text/comma-separated-values; charset=UTF-8");
header("Pragma: public");
echo gT("Quota name") . "," . gT("Limit") . "," . gT("Completed") . "," . gT("Remaining") . "\r\n";
foreach ($csvoutput as $line)
{
echo $line;
}
die;
}
}
function insertquota($iSurveyId)
{
$iSurveyId = sanitize_int($iSurveyId);
$this->_checkPermissions($iSurveyId, 'create');
$aData = $this->_getData($iSurveyId);
$aLangs = $aData['aLangs'];
$oQuota = new Quota;
$oQuota->sid = $iSurveyId;
$oQuota->name = Yii::app()->request->getPost('quota_name');
$oQuota->qlimit = Yii::app()->request->getPost('quota_limit');
$oQuota->action = Yii::app()->request->getPost('quota_action');
$oQuota->autoload_url = Yii::app()->request->getPost('autoload_url');
$oQuota->save();
$iQuotaId = $oQuota->id;
//Iterate through each language, and make sure there is a quota message for it
$sError = '';
foreach ($aLangs as $sLang)
{
if (!$_POST['quotals_message_' . $sLang])
{
$sError .= getLanguageNameFromCode($sLang, false) . "\\n";
}
}
if ($sError != '')
{
$aData['sShowError'] = $sError;
}
else
//All the required quota messages exist, now we can insert this info into the database
{
foreach ($aLangs as $sLang) //Iterate through each language
{
//Clean XSS - Automatically provided by CI input class
$_POST['quotals_message_' . $sLang] = html_entity_decode($_POST['quotals_message_' . $sLang], ENT_QUOTES, "UTF-8");
// Fix bug with FCKEditor saving strange BR types
$_POST['quotals_message_' . $sLang] = fixCKeditorText($_POST['quotals_message_' . $sLang]);
$oQuotaLanguageSettings = new QuotaLanguageSetting;
$oQuotaLanguageSettings->quotals_quota_id = $iQuotaId;
$oQuotaLanguageSettings->quotals_language = $sLang;
$oQuotaLanguageSettings->quotals_name = Yii::app()->request->getPost('quota_name');
$oQuotaLanguageSettings->quotals_message = $_POST['quotals_message_' . $sLang];
$oQuotaLanguageSettings->quotals_url = $_POST['quotals_url_' . $sLang];
$oQuotaLanguageSettings->quotals_urldescrip = $_POST['quotals_urldescrip_' . $sLang];
$oQuotaLanguageSettings->save();
}
}
self::_redirectToIndex($iSurveyId);
}
function modifyquota($iSurveyId)
{
$iSurveyId = sanitize_int($iSurveyId);
$this->_checkPermissions($iSurveyId, 'update');
$aData = $this->_getData($iSurveyId);
$aLangs = $aData['aLangs'];
$oQuota = Quota::model()->findByPk(Yii::app()->request->getPost('quota_id'));
$oQuota->name = Yii::app()->request->getPost('quota_name');
$oQuota->qlimit = Yii::app()->request->getPost('quota_limit');
$oQuota->action = Yii::app()->request->getPost('quota_action');
$oQuota->autoload_url = Yii::app()->request->getPost('autoload_url');
$oQuota->save();
//Iterate through each language posted, and make sure there is a quota message for it
$sError = '';
foreach ($aLangs as $sLang)
{
if (!$_POST['quotals_message_' . $sLang])
{
$sError.= getLanguageNameFromCode($sLang, false) . "\\n";
}
}
if ($sError != '')
{
$aData['sShowError'] = $sError;
}
else
//All the required quota messages exist, now we can insert this info into the database
{
foreach ($aLangs as $sLang) //Iterate through each language
{
//Clean XSS - Automatically provided by CI
$_POST['quotals_message_' . $sLang] = html_entity_decode($_POST['quotals_message_' . $sLang], ENT_QUOTES, "UTF-8");
// Fix bug with FCKEditor saving strange BR types
$_POST['quotals_message_' . $sLang] = fixCKeditorText($_POST['quotals_message_' . $sLang]);
$oQuotaLanguageSettings = QuotaLanguageSetting::model()->findByAttributes(array('quotals_quota_id' => Yii::app()->request->getPost('quota_id'), 'quotals_language' => $sLang));
$oQuotaLanguageSettings->quotals_name = Yii::app()->request->getPost('quota_name');
$oQuotaLanguageSettings->quotals_message = $_POST['quotals_message_' . $sLang];
$oQuotaLanguageSettings->quotals_url = $_POST['quotals_url_' . $sLang];
$oQuotaLanguageSettings->quotals_urldescrip = $_POST['quotals_urldescrip_' . $sLang];
$oQuotaLanguageSettings->save();
}
} //End insert language based components
self::_redirectToIndex($iSurveyId);
}
function insertquotaanswer($iSurveyId)
{
$iSurveyId = sanitize_int($iSurveyId);
$this->_checkPermissions($iSurveyId, 'update');
$oQuotaMembers = new QuotaMember('create'); // Trigger the 'create' rules
$oQuotaMembers->sid = $iSurveyId;
$oQuotaMembers->qid = Yii::app()->request->getPost('quota_qid');
$oQuotaMembers->quota_id = Yii::app()->request->getPost('quota_id');
$oQuotaMembers->code = Yii::app()->request->getPost('quota_anscode');
if ($oQuotaMembers->save()) {
if (!empty($_POST['createanother']))
{
$_POST['action'] = "quotas";
$_POST['subaction'] = "new_answer";
$sSubAction = "new_answer";
self::new_answer($iSurveyId, $sSubAction);
}
else
{
self::_redirectToIndex($iSurveyId);
}
} else {
// Save was not successful, redirect back
$_POST['action'] = "quotas";
$_POST['subaction'] = "new_answer";
$sSubAction = "new_answer_two";
self::new_answer($iSurveyId, $sSubAction);
}
}
function delans($iSurveyId)
{
$iSurveyId = sanitize_int($iSurveyId);
$this->_checkPermissions($iSurveyId, 'update');
QuotaMember::model()->deleteAllByAttributes(array(
'id' => Yii::app()->request->getPost('quota_member_id'),
'qid' => Yii::app()->request->getPost('quota_qid'),
'code' => Yii::app()->request->getPost('quota_anscode'),
));
self::_redirectToIndex($iSurveyId);
}
function delquota($iSurveyId)
{
$iSurveyId = sanitize_int($iSurveyId);
$this->_checkPermissions($iSurveyId, 'delete');
Quota::model()->deleteByPk(Yii::app()->request->getPost('quota_id'));
QuotaLanguageSetting::model()->deleteAllByAttributes(array('quotals_quota_id' => Yii::app()->request->getPost('quota_id')));
QuotaMember::model()->deleteAllByAttributes(array('quota_id' => Yii::app()->request->getPost('quota_id')));
self::_redirectToIndex($iSurveyId);
}
function editquota($iSurveyId)
{
$iSurveyId = sanitize_int($iSurveyId);
$this->_checkPermissions($iSurveyId, 'update');
$aData = $this->_getData($iSurveyId);
$aLangs = $aData['aLangs'];
$aViewUrls = array();
$aQuotaInfo = Quota::model()->findByPk(Yii::app()->request->getPost('quota_id'));
$aData['quotainfo'] = $aQuotaInfo;
$first=true;
foreach ($aLangs as $sLanguage)
{
$aTabTitles[$sLanguage] = getLanguageNameFromCode($sLanguage, false);
if ($first)
{
$aTabTitles[$sLanguage].= ' (' . gT("Base language") . ')';
$first = false;
}
$aData['langquotainfo'] = QuotaLanguageSetting::model()->findByAttributes(array('quotals_quota_id' => Yii::app()->request->getPost('quota_id'), 'quotals_language' => $sLanguage));
$aData['lang'] = $sLanguage;
$aTabContents[$sLanguage] = $this->getController()->renderPartial('/admin/quotas/editquotalang_view', $aData, true);
}
$aData['aTabTitles']=$aTabTitles;
$aData['aTabContents']=$aTabContents;
$aViewUrls[] = 'editquota_view';
$this->_renderWrappedTemplate('quotas', $aViewUrls, $aData);
}
function new_answer($iSurveyId, $sSubAction = 'new_answer')
{
$iSurveyId = sanitize_int($iSurveyId);
$this->_checkPermissions($iSurveyId, 'update');
$aData = $this->_getData($iSurveyId);
$sBaseLang = $aData['sBaseLang'];
$aViewUrls = array();
if (($sSubAction == "new_answer" || ($sSubAction == "new_answer_two" && !isset($_POST['quota_qid']))) && Permission::model()->hasSurveyPermission($iSurveyId, 'quotas', 'create'))
{
$result = Quota::model()->findAllByPk(Yii::app()->request->getPost('quota_id'));
foreach ($result as $aQuotaDetails)
{
$quota_name = $aQuotaDetails['name'];
}
$result = Question::model()->findAllByAttributes(array('type' => array('G', 'M', 'Y', 'A', 'B', 'I', 'L', 'O', '!'), 'sid' => $iSurveyId, 'language' => $sBaseLang, 'parent_qid' => 0));
if (empty($result))
{
$aViewUrls[] = 'newanswererror_view';
}
else
{
$aData['newanswer_result'] = $result;
$aData['quota_name'] = $quota_name;
$aViewUrls[] = 'newanswer_view';
}
}
if ($sSubAction == "new_answer_two" && isset($_POST['quota_qid']) && Permission::model()->hasSurveyPermission($iSurveyId, 'quotas', 'create'))
{
$aResults = Quota::model()->findByPk(Yii::app()->request->getPost('quota_qid'));
$sQuotaName = $aResults['name'];
$aQuestionAnswers = self::getQuotaAnswers(Yii::app()->request->getPost('quota_qid'), $iSurveyId, Yii::app()->request->getPost('quota_id'));
$x = 0;
foreach ($aQuestionAnswers as $aQACheck)
{
if (isset($aQACheck['rowexists']))
$x++;
}
reset($aQuestionAnswers);
$aData['question_answers'] = $aQuestionAnswers;
$aData['x'] = $x;
$aData['quota_name'] = $sQuotaName;
$aViewUrls[] = 'newanswertwo_view';
}
$this->_renderWrappedTemplate('quotas', $aViewUrls, $aData);
}
function newquota($iSurveyId)
{
$iSurveyId = sanitize_int($iSurveyId);
$this->_checkPermissions($iSurveyId, 'create');
$aData = $this->_getData($iSurveyId);
$aData['thissurvey'] = getSurveyInfo($iSurveyId);
$aData['langs'] = $aData['aLangs'];
$aData['baselang'] = $aData['sBaseLang'];
$this->_renderWrappedTemplate('quotas', 'newquota_view', $aData);
}
/**
*
* @param type $iQuestionId
* @param type $iSurveyId
* @param type $iQuotaId
* @return array
*/
function getQuotaAnswers($iQuestionId, $iSurveyId, $iQuotaId)
{
$iQuestionId = sanitize_int($iQuestionId);
$iSurveyId = sanitize_int($iSurveyId);
$iQuotaId = sanitize_int($iQuotaId);
$aData = $this->_getData($iSurveyId);
$sBaseLang = $aData['sBaseLang'];
$this->_checkPermissions($iSurveyId, 'read');
$aQuestion = Question::model()->findByPk(array('qid' => $iQuestionId, 'language' => $sBaseLang));
$aQuestionType = $aQuestion['type'];
if ($aQuestionType == 'M')
{
$aResults = Question::model()->findAllByAttributes(array('parent_qid' => $iQuestionId));
$aAnswerList = array();
foreach($aResults as $aDbAnsList)
{
$tmparrayans = array('Title' => $aQuestion['title'], 'Display' => substr($aDbAnsList['question'], 0, 40), 'code' => $aDbAnsList['title']);
$aAnswerList[$aDbAnsList['title']] = $tmparrayans;
}
} elseif ($aQuestionType == 'G')
{
$aAnswerList = array(
'M' => array('Title' => $aQuestion['title'], 'Display' => gT("Male"), 'code' => 'M'),
'F' => array('Title' => $aQuestion['title'], 'Display' => gT("Female"), 'code' => 'F'));
} elseif ($aQuestionType == 'L' || $aQuestionType == 'O' || $aQuestionType == '!')
{
$aAnsResults = Answer::model()->findAllByAttributes(array('qid' => $iQuestionId, 'language' => $sBaseLang));
$aAnswerList = array();
foreach ($aAnsResults as $aDbAnsList)
{
$aAnswerList[$aDbAnsList['code']] = array('Title' => $aQuestion['title'], 'Display' => substr($aDbAnsList['answer'], 0, 40), 'code' => $aDbAnsList['code']);
}
} elseif ($aQuestionType == 'A')
{
$aAnsResults = Question::model()->findAllByAttributes(array('parent_qid' => $iQuestionId));
$aAnswerList = array();
foreach ($aAnsResults as $aDbAnsList)
{
for ($x = 1; $x < 6; $x++)
{
$tmparrayans = array('Title' => $aQuestion['title'], 'Display' => substr($aDbAnsList['question'], 0, 40) . ' [' . $x . ']', 'code' => $aDbAnsList['title']);
$aAnswerList[$aDbAnsList['title'] . "-" . $x] = $tmparrayans;
}
}
} elseif ($aQuestionType == 'B')
{
$aAnsResults = Answer::model()->findAllByAttributes(array('qid' => $iQuestionId, 'language' => $sBaseLang));
$aAnswerList = array();
foreach ($aAnsResults as $aDbAnsList)
{
for ($x = 1; $x < 11; $x++)
{
$tmparrayans = array('Title' => $aQuestion['title'], 'Display' => substr($aDbAnsList['answer'], 0, 40) . ' [' . $x . ']', 'code' => $aDbAnsList['code']);
$aAnswerList[$aDbAnsList['code'] . "-" . $x] = $tmparrayans;
}
}
} elseif ($aQuestionType == 'Y')
{
$aAnswerList = array(
'Y' => array('Title' => $aQuestion['title'], 'Display' => gT("Yes"), 'code' => 'Y'),
'N' => array('Title' => $aQuestion['title'], 'Display' => gT("No"), 'code' => 'N'));
} elseif ($aQuestionType == 'I')
{
$slangs = Survey::model()->findByPk($iSurveyId)->additionalLanguages;
array_unshift($slangs, $sBaseLang);
while (list($key, $value) = each($slangs))
{
$tmparrayans = array('Title' => $aQuestion['title'], 'Display' => getLanguageNameFromCode($value, false), $value);
$aAnswerList[$value] = $tmparrayans;
}
}
if (empty($aAnswerList))
{
return array();
}
else
{
// Now we mark answers already used in this quota as such
$aExistsingAnswers = QuotaMember::model()->findAllByAttributes(array('sid' => $iSurveyId, 'qid' => $iQuestionId, 'quota_id' => $iQuotaId));
foreach ($aExistsingAnswers as $aAnswerRow)
{
if (array_key_exists($aAnswerRow['code'], $aAnswerList))
{
$aAnswerList[$aAnswerRow['code']]['rowexists'] = '1';
}
}
return $aAnswerList;
}
}
/**
* 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 = 'quotas', $aViewUrls = array(), $aData = array())
{
App()->getClientScript()->registerPackage('jquery-superfish');
App()->getClientScript()->registerScriptFile(Yii::app()->getConfig('adminscripts') . '/quotas.js');
parent::_renderWrappedTemplate($sAction, $aViewUrls, $aData);
}
}