-
Notifications
You must be signed in to change notification settings - Fork 985
/
statistics.php
660 lines (532 loc) · 25.3 KB
/
statistics.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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
<?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.
*
*/
/**
* Statistics Controller
*
* This controller performs statistics actions
*
* @package LimeSurvey
* @subpackage Backend
*/
class statistics extends Survey_Common_Action {
function __construct($controller, $id)
{
parent::__construct($controller, $id);
Yii::app()->loadHelper("surveytranslator");
}
/**
* Constructor
*/
public function run($surveyid = 0, $subaction = null)
{
$surveyid = sanitize_int($surveyid);
$imageurl = Yii::app()->getConfig("imageurl");
$aData = array('imageurl' => $imageurl);
/*
* We need this later:
* 1 - Array Dual Scale
* 5 - 5 Point Choice
* A - Array (5 Point Choice)
* B - Array (10 Point Choice)
* C - Array (Yes/No/Uncertain)
* D - Date
* E - Array (Increase, Same, Decrease)
* F - Array (Flexible Labels)
* G - Gender
* H - Array (Flexible Labels) by Column
* I - Language Switch
* K - Multiple Numerical Input
* L - List (Radio)
* M - Multiple choice
* N - Numerical Input
* O - List With Comment
* P - Multiple choice with comments
* Q - Multiple Short Text
* R - Ranking
* S - Short Free Text
* T - Long Free Text
* U - Huge Free Text
* X - Boilerplate Question
* Y - Yes/No
* ! - List (Dropdown)
* : - Array (Flexible Labels) multiple drop down
* ; - Array (Flexible Labels) multiple texts
* | - File Upload
Debugging help:
echo '<script language="javascript" type="text/javascript">alert("HI");</script>';
*/
//split up results to extend statistics -> NOT WORKING YET! DO NOT ENABLE THIS!
$showcombinedresults = 0;
/*
* this variable is used in the function shortencode() which cuts off a question/answer title
* after $maxchars and shows the rest as tooltip
*/
$maxchars = 50;
//we collect all the output within this variable
$statisticsoutput ='';
//output for chosing questions to cross query
$cr_statisticsoutput = '';
// This gets all the 'to be shown questions' from the POST and puts these into an array
$summary=returnGlobal('summary');
$statlang=returnGlobal('statlang');
//if $summary isn't an array we create one
if (isset($summary) && !is_array($summary)) {
$summary = explode("+", $summary);
}
//no survey ID? -> come and get one
if (!isset($surveyid)) {$surveyid=returnGlobal('sid');}
//still no survey ID -> error
$aData['surveyid'] = $surveyid;
// Set language for questions and answers to base language of this survey
$language = Survey::model()->findByPk($surveyid)->language;
$aData['language'] = $language;
//Call the javascript file
App()->getClientScript()->registerScriptFile( App()->getAssetManager()->publish( ADMIN_SCRIPT_PATH . 'statistics.js' ));
App()->getClientScript()->registerScriptFile( App()->getAssetManager()->publish( ADMIN_SCRIPT_PATH . 'json-js/json2.min.js'));
$aData['display']['menu_bars']['browse'] = gT("Quick statistics");
//Select public language file
$row = Survey::model()->find('sid = :sid', array(':sid' => $surveyid));
/*
* check if there is a datestamp available for this survey
* yes -> $datestamp="Y"
* no -> $datestamp="N"
*/
$datestamp = $row->datestamp;
// 1: Get list of questions from survey
/*
* We want to have the following data
* a) "questions" -> all table namens, e.g.
* qid
* sid
* gid
* type
* title
* question
* preg
* help
* other
* mandatory
* lid
* lid1
* question_order
* language
*
* b) "groups" -> group_name + group_order *
*/
//store all the data in $rows
$rows = Question::model()->getQuestionList($surveyid, $language);
//SORT IN NATURAL ORDER!
usort($rows, 'groupOrderThenQuestionOrder');
//put the question information into the filter array
$filters = array();
foreach ($rows as $row)
{
//store some column names in $filters array
$filters[]=array($row['qid'],
$row['gid'],
$row['type'],
$row['title'],
$row['group_name'],
flattenText($row['question']));
}
$aData['filters'] = $filters;
//var_dump($filters);
// SHOW ID FIELD
$grapherror = false;
$error = '';
if (!function_exists("gd_info")) {
$grapherror = true;
$error.='<br />'.gT('You do not have the GD Library installed. Showing charts requires the GD library to function properly.');
$error.='<br />'.gT('visit http://us2.php.net/manual/en/ref.image.php for more information').'<br />';
}
elseif (!function_exists("imageftbbox")) {
$grapherror = true;
$error.='<br />'.gT('You do not have the Freetype Library installed. Showing charts requires the Freetype library to function properly.');
$error.='<br />'.gT('visit http://us2.php.net/manual/en/ref.image.php for more information').'<br />';
}
if ($grapherror)
{
unset($_POST['usegraph']);
}
//pre-selection of filter forms
if (incompleteAnsFilterState() == "complete")
{
$selecthide="selected='selected'";
$selectshow="";
$selectinc="";
}
elseif (incompleteAnsFilterState() == "incomplete")
{
$selecthide="";
$selectshow="";
$selectinc="selected='selected'";
}
else
{
$selecthide="";
$selectshow="selected='selected'";
$selectinc="";
}
$aData['selecthide'] = $selecthide;
$aData['selectshow'] = $selectshow;
$aData['selectinc'] = $selectinc;
$aData['error'] = $error;
$survlangs = Survey::model()->findByPk($surveyid)->additionalLanguages;
$survlangs[] = Survey::model()->findByPk($surveyid)->language;
$aData['survlangs'] = $survlangs;
$aData['datestamp'] = $datestamp;
//if the survey contains timestamps you can filter by timestamp, too
//Output selector
//second row below options -> filter settings headline
$filterchoice_state=returnGlobal('filterchoice_state');
$aData['filterchoice_state'] = $filterchoice_state;
/*
* let's go through the filter array which contains
* ['qid'],
['gid'],
['type'],
['title'],
['group_name'],
['question'],
['lid'],
['lid1']);
*/
$currentgroup='';
$counter = 0;
foreach ($filters as $key1 => $flt)
{
//is there a previous question type set?
/*
* remember: $flt is structured like this
* ['qid'],
['gid'],
['type'],
['title'],
['group_name'],
['question'],
['lid'],
['lid1']);
*/
//SGQ identifier
//full question title
/*
* Check question type: This question types will be used (all others are separated in the if clause)
* 5 - 5 Point Choice
G - Gender
I - Language Switch
L - List (Radio)
M - Multiple choice
N - Numerical Input
| - File Upload
O - List With Comment
P - Multiple choice with comments
Y - Yes/No
! - List (Dropdown) )
*/
/////////////////////////////////////////////////////////////////////////////////////////////////
//This section presents the filter list, in various different ways depending on the question type
/////////////////////////////////////////////////////////////////////////////////////////////////
//let's switch through the question type for each question
switch ($flt[2])
{
case "K": // Multiple Numerical
//get answers
$result = Question::model()->getQuestionsForStatistics('title as code, question as answer', "parent_qid=$flt[0] AND language = '{$language}'", 'question_order');
$aData['result'][$key1]['key1'] = $result;
break;
case "Q": // Multiple Short Text
//get subqestions
$result = Question::model()->getQuestionsForStatistics('title as code, question as answer', "parent_qid=$flt[0] AND language = '{$language}'", 'question_order');
$aData['result'][$key1] = $result;
break;
//----------------------- ARRAYS --------------------------
case "A": // ARRAY OF 5 POINT CHOICE QUESTIONS
//get answers
$result = Question::model()->getQuestionsForStatistics('title, question', "parent_qid=$flt[0] AND language = '{$language}'", 'question_order');
$aData['result'][$key1] = $result;
break;
//just like above only a different loop
case "B": // ARRAY OF 10 POINT CHOICE QUESTIONS
$result = Question::model()->getQuestionsForStatistics('title, question', "parent_qid=$flt[0] AND language = '{$language}'", 'question_order');
$aData['result'][$key1] = $result;
break;
case "C": // ARRAY OF YES\No\gT("Uncertain") QUESTIONS
//get answers
$result = Question::model()->getQuestionsForStatistics('title, question', "parent_qid=$flt[0] AND language = '{$language}'", 'question_order');
$aData['result'][$key1] = $result;
break;
//similiar to the above one
case "E": // ARRAY OF Increase/Same/Decrease QUESTIONS
$result = Question::model()->getQuestionsForStatistics('title, question', "parent_qid=$flt[0] AND language = '{$language}'", 'question_order');
$aData['result'][$key1] = $result;
break;
case ";": //ARRAY (Multi Flex) (Text)
$result = Question::model()->getQuestionsForStatistics('title, question', "parent_qid=$flt[0] AND language = '{$language}' AND scale_id = 0", 'question_order');
$aData['result'][$key1] = $result;
foreach($result as $key => $row)
{
$fresult = Question::model()->getQuestionsForStatistics('title, question', "parent_qid=$flt[0] AND language = '{$language}' AND scale_id = 1", 'question_order');
$aData['fresults'][$key1][$key] = $fresult;
}
break;
case ":": //ARRAY (Multi Flex) (Numbers)
$result = Question::model()->getQuestionsForStatistics('title, question', "parent_qid=$flt[0] AND language = '{$language}' AND scale_id = 0", 'question_order');
$aData['result'][$key1] = $result;
foreach($result as $row)
{
$fresult = Question::model()->getQuestionsForStatistics('*', "parent_qid=$flt[0] AND language = '{$language}' AND scale_id = 1", 'question_order, title');
$aData['fresults'][$key1] = $fresult;
}
break;
/*
* For question type "F" and "H" you can use labels.
* The only difference is that the labels are applied to column heading
* or rows respectively
*/
case "F": // FlEXIBLE ARRAY
case "H": // ARRAY (By Column)
//Get answers. We always use the answer code because the label might be too long elsewise
$result = Question::model()->getQuestionsForStatistics('title, question', "parent_qid=$flt[0] AND language = '{$language}'", 'question_order');
$aData['result'][$key1] = $result;
//check all the answers
foreach($result as $row)
{
$fresult = Answer::model()->getQuestionsForStatistics('*', "qid=$flt[0] AND language = '{$language}'", 'sortorder, code');
$aData['fresults'][$key1] = $fresult;
}
//$statisticsoutput .= "\t\t\t\t<td>\n";
$counter=0;
break;
case "R": //RANKING
//get some answers
$result = Answer::model()->getQuestionsForStatistics('code, answer', "qid=$flt[0] AND language = '{$language}'", 'sortorder, answer');
$aData['result'][$key1] = $result;
break;
case "1": // MULTI SCALE
//get answers
$result = Question::model()->getQuestionsForStatistics('title, question', "parent_qid=$flt[0] AND language = '{$language}'", 'question_order');
$aData['result'][$key1] = $result;
//loop through answers
foreach($result as $key => $row)
{
//check if there is a dualscale_headerA/B
$dshresult = QuestionAttribute::model()->getQuestionsForStatistics('value', "qid=$flt[0] AND attribute = 'dualscale_headerA'", '');
$aData['dshresults'][$key1][$key] = $dshresult;
$fresult = Answer::model()->getQuestionsForStatistics('*', "qid=$flt[0] AND language = '{$language}' AND scale_id = 0", 'sortorder, code');
$aData['fresults'][$key1][$key] = $fresult;
$dshresult2 = QuestionAttribute::model()->getQuestionsForStatistics('value', "qid=$flt[0] AND attribute = 'dualscale_headerB'", '');
$aData['dshresults2'][$key1][$key] = $dshresult2;
}
break;
case "P": //P - Multiple choice with comments
case "M": //M - Multiple choice
//get answers
$result = Question::model()->getQuestionsForStatistics('title, question', "parent_qid = $flt[0] AND language = '$language'", 'question_order');
$aData['result'][$key1] = $result;
break;
/*
* This question types use the default settings:
* L - List (Radio)
O - List With Comment
P - Multiple choice with comments
! - List (Dropdown)
*/
default:
//get answers
$result = Answer::model()->getQuestionsForStatistics('code, answer', "qid=$flt[0] AND language = '$language'", 'sortorder, answer');
$aData['result'][$key1] = $result;
break;
} //end switch -> check question types and create filter forms
$currentgroup=$flt[1];
$counter++;
//temporary save the type of the previous question
//used to adjust linebreaks
$previousquestiontype = $flt[2];
}
// ----------------------------------- END FILTER FORM ---------------------------------------
Yii::app()->loadHelper('admin/statistics');
$helper = new statistics_helper();
$showtextinline=isset($_POST['showtextinline']) ? 1 : 0;
$aData['showtextinline'] = $showtextinline;
//Show Summary results
if (isset($summary) && $summary)
{
$usegraph=isset($_POST['usegraph']) ? 1 : 0;
$aData['usegraph'] = $usegraph;
$outputType = $_POST['outputtype'];
switch($outputType){
case 'html':
$statisticsoutput .= $helper->generate_statistics($surveyid,$summary,$summary,$usegraph,$outputType,'DD',$statlang);
break;
case 'pdf':
$helper->generate_statistics($surveyid,$summary,$summary,$usegraph,$outputType,'I',$statlang);
exit;
break;
case 'xls':
$helper->generate_statistics($surveyid,$summary,$summary,$usegraph,$outputType,'DD',$statlang);
exit;
break;
default:
break;
}
} //end if -> show summary results
$usegraph=isset($_POST['usegraph']) ? 1 : 0;
$aData['usegraph'] = $usegraph;
$aData['sStatisticsLanguage']=$statlang;
$aData['output'] = $statisticsoutput;
$aData['summary'] = $summary;
$error = '';
if (!function_exists("gd_info"))
{
$error .= '<br />'.gT('You do not have the GD Library installed. Showing charts requires the GD library to function properly.');
$error .= '<br />'.gT('visit http://us2.php.net/manual/en/ref.image.php for more information').'<br />';
}
else if (!function_exists("imageftbbox")) {
$error .= '<br />'.gT('You do not have the Freetype Library installed. Showing charts requires the Freetype library to function properly.');
$error .= '<br />'.gT('visit http://us2.php.net/manual/en/ref.image.php for more information').'<br />';
}
$aData['error'] = $error;
$aData['oStatisticsHelper'] = $helper;
$this->_renderWrappedTemplate('export', 'statistics_view', $aData);
}
/**
* Returns a simple list of values in a particular column, that meet the requirements of the SQL
*/
function listcolumn($surveyid, $column, $sortby="", $sortmethod="", $sorttype="")
{
Yii::app()->loadHelper('admin/statistics');
$helper = new statistics_helper();
$aData['data']=$helper->_listcolumn($surveyid, $column, $sortby, $sortmethod, $sorttype);
$aData['surveyid']=$surveyid;
$aData['column']=$column;
$aData['sortby']=$sortby;
$aData['sortmethod']=$sortmethod;
$aData['sorttype']=$sorttype;
App()->getClientScript()->reset();
$this->getController()->render('export/statistics_browse_view', $aData);
}
function graph()
{
Yii::app()->loadHelper('admin/statistics');
Yii::app()->loadHelper("surveytranslator");
// Initialise PCHART
require_once(Yii::app()->basePath . '/third_party/pchart/pchart/pChart.class');
require_once(Yii::app()->basePath . '/third_party/pchart/pchart/pData.class');
require_once(Yii::app()->basePath . '/third_party/pchart/pchart/pCache.class');
Yii::import('application.third_party.ar-php.Arabic', true);
$tempdir = Yii::app()->getConfig("tempdir");
$MyCache = new pCache($tempdir.'/');
$aData['success'] = 1;
$sStatisticsLanguage=sanitize_languagecode($_POST['sStatisticsLanguage']);
if (isset($_POST['cmd']) && isset($_POST['id'])) {
$sQCode=$_POST['id'];
if(!is_numeric(substr($sQCode,0,1))) {
// Strip first char when not numeric (probably T or D)
$sQCode=substr($sQCode,1);
}
list($qsid, $qgid, $qqid) = explode("X", substr($sQCode, 0), 3);
$aFieldmap=createFieldMap($qsid,'full',false,false,$sStatisticsLanguage);
$qtype=$aFieldmap[$sQCode]['type'];
$qqid=$aFieldmap[$sQCode]['qid'];
$aattr = getQuestionAttributeValues($qqid);
$field = substr($_POST['id'], 1);
switch ($_POST['cmd']) {
case 'showmap':
if (isset($aattr['location_mapservice'])) {
$aData['mapdata'] = array (
"coord" => getQuestionMapData($field, $qsid),
"zoom" => $aattr['location_mapzoom'],
"width" => $aattr['location_mapwidth'],
"height" => $aattr['location_mapheight']
);
QuestionAttribute::model()->setQuestionAttribute($qqid, 'statistics_showmap', 1);
} else {
$aData['success'] = 0;
}
break;
case 'hidemap':
if (isset($aattr['location_mapservice'])) {
$aData['success'] = 1;
QuestionAttribute::model()->setQuestionAttribute($qqid, 'statistics_showmap', 0);
} else {
$aData['success'] = 0;
}
break;
case 'showgraph':
if (isset($aattr['location_mapservice'])) {
$aData['mapdata'] = array (
"coord" => getQuestionMapData($field, $qsid),
"zoom" => $aattr['location_mapzoom'],
"width" => $aattr['location_mapwidth'],
"height" => $aattr['location_mapheight']
);
}
$bChartType = $qtype != "M" && $qtype != "P" && $aattr["statistics_graphtype"] == "1";
$adata = Yii::app()->session['stats'][$_POST['id']];
$aData['chartdata'] = createChart($qqid, $qsid, $bChartType, $adata['lbl'], $adata['gdata'], $adata['grawdata'], $MyCache, $sStatisticsLanguage, $qtype);
QuestionAttribute::model()->setQuestionAttribute($qqid, 'statistics_showgraph', 1);
break;
case 'hidegraph':
QuestionAttribute::model()->setQuestionAttribute($qqid, 'statistics_showgraph', 0);
break;
case 'showbar':
if ($qtype == "M" || $qtype == "P") {
$aData['success'] = 0;
break;
}
QuestionAttribute::model()->setQuestionAttribute($qqid, 'statistics_graphtype', 0);
$adata = Yii::app()->session['stats'][$_POST['id']];
$aData['chartdata'] = createChart($qqid, $qsid, 0, $adata['lbl'], $adata['gdata'], $adata['grawdata'], $MyCache, $sStatisticsLanguage, $qtype);
break;
case 'showpie':
if ($qtype == "M" || $qtype == "P") {
$aData['success'] = 0;
break;
}
QuestionAttribute::model()->setQuestionAttribute($qqid, 'statistics_graphtype', 1);
$adata = Yii::app()->session['stats'][$_POST['id']];
$aData['chartdata'] = createChart($qqid, $qsid, 1, $adata['lbl'], $adata['gdata'], $adata['grawdata'], $MyCache, $sStatisticsLanguage, $qtype);
break;
default:
$aData['success'] = 0;
break;
}
} else {
$aData['success'] = 0;
}
//$this->_renderWrappedTemplate('export', 'statistics_graph_view', $aData);
$this->getController()->renderPartial('export/statistics_graph_view', $aData);
}
/**
* 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 = 'export', $aViewUrls = array(), $aData = array())
{
$aData['display']['menu_bars'] = false;
$aData['display']['menu_bars']['browse'] = gT('Browse responses'); // browse is independent of the above
$aData['menu']['edition'] = true;
$aData['menu']['stats'] = true;
$aData['menu']['close'] = true;
$aData['sidebar']['state'] = "close";
$iSurveyId = $aData['surveyid'];
$surveyinfo = Survey::model()->findByPk($iSurveyId)->surveyinfo;
$aData["surveyinfo"] = $surveyinfo;
$aData['title_bar']['title'] = gT('Browse responses').': '.$surveyinfo['surveyls_title'];
parent::_renderWrappedTemplate($sAction, $aViewUrls, $aData);
}
}