forked from LimeSurvey/LimeSurvey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatistics_user.php
459 lines (374 loc) · 16.8 KB
/
statistics_user.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
<?php
/*
* LimeSurvey
* Copyright (C) 2007 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: statistics_user.php 12008 2012-01-12 15:20:59Z gautamgupta $
*
*/
/*
* Created 12-2008 by Maziminke ([email protected])
*
* This file handles the "Show results to users" option:
* Survey Settings -> Presentation & navigation -> "Public statistics?"
*
* The admin can set a question attribute "public_statistics" for each question
* to determine whether the results of a certain question should be shown to the user
* after he/she has submitted the survey.
*
* See http://docs.limesurvey.org/tiki-index.php?page=Question+attributes#public_statistics
*/
//don't call this script directly!
if (isset($_REQUEST['homedir'])) {die('You cannot start this script directly');}
require_once(dirname(__FILE__).'/classes/core/startup.php');
require_once(dirname(__FILE__).'/config-defaults.php');
require_once(dirname(__FILE__).'/common.php');
require_once($homedir.'/classes/core/class.progressbar.php');
require_once(dirname(__FILE__).'/classes/core/language.php');
require_once($homedir.'/statistics_function.php');
//XXX enable/disable this for testing
//$publicgraphs = 1;
//$showaggregateddata = 1;
/*
* List of important settings:
* - publicstatistics: General survey setting which determines if public statistics for this survey
* should be shown at all.
*
* - publicgraphs: General survey setting which determines if public statistics for this survey
* should include graphs or only show a tabular overview.
*
* - public_statistics: Question attribute which has to be applied to each question so that
* its statistics will be shown to the user. If not set no statistics for this question will be shown.
*
* - filterout_incomplete_answers: Setting taken from config-defaults.php which determines if
* not completed answers will be filtered.
*/
$surveyid=returnglobal('sid');
$postlang = returnglobal('lang');
if (!$surveyid){
//This next line ensures that the $surveyid value is never anything but a number.
safe_die('You have to provide a valid survey ID.');
}
if ($surveyid)
{
$actquery="SELECT * FROM ".db_table_name('surveys')." WHERE sid=$surveyid and active='Y'";
$actresult=db_execute_assoc($actquery) or safe_die ("Couldn't access survey settings<br />$query<br />".$connect->ErrorMsg()); //Checked
if ($actresult->RecordCount() == 0) { safe_die('You have to provide a valid survey ID.'); }
else
{
$surveyinfo=getSurveyInfo($surveyid);
// CHANGE JSW_NZ - let's get the survey title for display
$thisSurveyTitle = $surveyinfo["name"];
// CHANGE JSW_NZ - let's get css from individual template.css - so define path
$thisSurveyCssPath = sGetTemplateURL($surveyinfo["template"]);
if ($surveyinfo['publicstatistics']!='Y')
{
safe_die('The public statistics for this survey are deactivated.');
}
//check if graphs should be shown for this survey
if ($surveyinfo['publicgraphs']=='Y')
{
$publicgraphs = 1;
} else {
$publicgraphs = 0;
}
}
}
//we collect all the output within this variable
$statisticsoutput ='';
//for creating graphs we need some more scripts which are included here
//True -> include
//False -> forget about charts
if (isset($publicgraphs) && $publicgraphs == 1)
{
require_once('classes/pchart/pchart/pChart.class');
require_once('classes/pchart/pchart/pData.class');
require_once('classes/pchart/pchart/pCache.class');
$MyCache = new pCache($tempdir.'/');
//$currentuser is created as prefix for pchart files
if (isset($_SERVER['REDIRECT_REMOTE_USER']))
{
$currentuser=$_SERVER['REDIRECT_REMOTE_USER'];
}
elseif (session_id())
{
$currentuser=substr(session_id(), 0, 15);
}
else
{
$currentuser="standard";
}
}
// Set language for questions and labels to base language of this survey
if (isset($postlang) && $postlang != null )
$language = $postlang;
else
$language = GetBaseLanguageFromSurveyID($surveyid);
//pick the best font file if font setting is 'auto'
if ($chartfontfile=='auto')
{
$chartfontfile='vera.ttf';
if ( $language=='ar')
{
$chartfontfile='KacstOffice.ttf';
}
elseif ($language=='fa' )
{
$chartfontfile='KacstFarsi.ttf';
}
}
//set survey language for translations
$clang = SetSurveyLanguage($surveyid, $language);
//Create header
sendcacheheaders();
echo getHeader();
/*
* only show questions where question attribute "public_statistics" is set to "1"
*/
$query = "SELECT q.* , group_name, group_order\n"
."FROM ".db_table_name("questions")." q, ".db_table_name("groups")." g, ".db_table_name("question_attributes")." qa\n"
."WHERE g.gid=q.gid\n"
."AND g.language='".$language."'\n"
."AND q.language='".$language."'\n"
."AND q.sid={$surveyid}\n"
."AND q.qid=qa.qid\n"
."AND q.parent_qid=0\n"
."AND qa.attribute='public_statistics'\n";
if ($databasetype=='mssql_n' or $databasetype=='mssql' or $databasetype=='odbc_mssql' or $databasetype=="mssqlnative")
{
$query .="AND CAST(CAST(qa.value as varchar) as int)='1'\n";
}
else
{
$query .="AND qa.value='1'\n";
}
//execute query
$result = db_execute_assoc($query) or safe_die("Couldn't do it!<br />$query<br />".$connect->ErrorMsg());
//store all the data in $rows
$rows = $result->GetRows();
//SORT IN NATURAL ORDER!
usort($rows, 'GroupOrderThenQuestionOrder');
//put the question information into the filter 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']));
}
//number of records for this survey
$totalrecords = 0;
//count number of answers
$query = "SELECT count(*) FROM ".db_table_name("survey_$surveyid");
//if incompleted answers should be filtert submitdate has to be not null
//this setting is taken from config-defaults.php
if ($filterout_incomplete_answers == true)
{
$query .= " WHERE ".db_table_name("survey_$surveyid").".submitdate is not null";
}
$result = db_execute_num($query) or safe_die ("Couldn't get total<br />$query<br />".$connect->ErrorMsg());
//$totalrecords = total number of answers
while ($row=$result->FetchRow())
{
$totalrecords=$row[0];
}
//this is the array which we need later...
$summary = array();
//...while this is the array from copy/paste which we don't want to replace because this is a nasty source of error
$allfields = array();
//---------- CREATE SGQA OF ALL QUESTIONS WHICH USE "PUBLIC_STATISTICS" ----------
/*
* let's go through the filter array which contains
* ['qid'],
['gid'],
['type'],
['title'],
['group_name'],
['question'];
*/
$currentgroup='';
// use to check if there are any question with public statistics
if(isset($filters)){
foreach ($filters as $flt)
{
//SGQ identifier
$myfield = "{$surveyid}X{$flt[1]}X{$flt[0]}";
//let's switch through the question type for each question
switch ($flt[2])
{
case "K": // Multiple Numerical
case "Q": // Multiple Short Text
//get answers
$query = "SELECT title as code, question as answer FROM ".db_table_name("questions")." WHERE parent_qid='$flt[0]' AND language = '{$language}' ORDER BY question_order";
$result = db_execute_num($query) or safe_die ("Couldn't get answers!<br />$query<br />".$connect->ErrorMsg());
//go through all the (multiple) answers
while ($row=$result->FetchRow())
{
$myfield2=$flt[2].$myfield.$row[0];
$allfields[] = $myfield2;
}
break;
case "A": // ARRAY OF 5 POINT CHOICE QUESTIONS
case "B": // ARRAY OF 10 POINT CHOICE QUESTIONS
case "C": // ARRAY OF YES\No\$clang->gT("Uncertain") QUESTIONS
case "E": // ARRAY OF Increase/Same/Decrease QUESTIONS
case "F": // FlEXIBLE ARRAY
case "H": // ARRAY (By Column)
//get answers
$query = "SELECT title as code, question as answer FROM ".db_table_name("questions")." WHERE parent_qid='$flt[0]' AND language = '{$language}' ORDER BY question_order";
$result = db_execute_num($query) or safe_die ("Couldn't get answers!<br />$query<br />".$connect->ErrorMsg());
//go through all the (multiple) answers
while ($row=$result->FetchRow())
{
$myfield2 = $myfield.$row[0];
$allfields[]=$myfield2;
}
break;
// all "free text" types (T, U, S) get the same prefix ("T")
case "T": // Long free text
case "U": // Huge free text
case "S": // Short free text
$myfield="T$myfield";
$allfields[] = $myfield;
break;
case ";": //ARRAY (Multi Flex) (Text)
case ":": //ARRAY (Multi Flex) (Numbers)
$query = "SELECT title, question FROM ".db_table_name("questions")." WHERE parent_qid='$flt[0]' AND language='{$language}' AND scale_id=0 ORDER BY question_order";
$result = db_execute_num($query) or die ("Couldn't get answers!<br />$query<br />".$connect->ErrorMsg());
while ($row=$result->FetchRow())
{
$fquery = "SELECT * FROM ".db_table_name("questions")." WHERE parent_qid={$flt[0]} AND language='{$language}' AND scale_id=1 ORDER BY question_order, title";
$fresult = db_execute_assoc($fquery);
while ($frow = $fresult->FetchRow())
{
$myfield2 = $myfield . $row[0] . "_" . $frow['title'];
$allfields[]=$myfield2;
}
}
break;
case "R": //RANKING
//get some answers
$query = "SELECT code, answer FROM ".db_table_name("answers")." WHERE qid='$flt[0]' AND language='{$language}' ORDER BY sortorder, answer";
$result = db_execute_assoc($query) or safe_die ("Couldn't get answers!<br />$query<br />".$connect->ErrorMsg());
//get number of answers
$count = $result->RecordCount();
//loop through all answers. if there are 3 items to rate there will be 3 statistics
for ($i=1; $i<=$count; $i++)
{
$myfield2 = "R" . $myfield . $i . "-" . strlen($i);
$allfields[]=$myfield2;
}
break;
//Boilerplate questions are only used to put some text between other questions -> no analysis needed
case "X": //This is a boilerplate question and it has no business in this script
break;
case "1": // MULTI SCALE
//get answers
$query = "SELECT title, question FROM ".db_table_name("questions")." WHERE parent_qid='$flt[0]' AND language='{$language}' ORDER BY question_order";
$result = db_execute_num($query) or safe_die ("Couldn't get answers!<br />$query<br />".$connect->ErrorMsg());
//loop through answers
while ($row=$result->FetchRow())
{
//----------------- LABEL 1 ---------------------
$myfield2 = $myfield . "$row[0]#0";
$allfields[]=$myfield2;
//----------------- LABEL 2 ---------------------
$myfield2 = $myfield . "$row[0]#1";
$allfields[]=$myfield2;
} //end WHILE -> loop through all answers
break;
case "P": //P - Multiple choice with comments
case "M": //M - Multiple choice
case "N": //N - Numerical input
case "D": //D - Date
$myfield2 = $flt[2].$myfield;
$allfields[]=$myfield2;
break;
default: //Default settings
$allfields[] = $myfield;
break;
} //end switch -> check question types and create filter forms
}
//end foreach -> loop through all questions with "public_statistics" enabled
}// end if -> for removing the error message in case there are no filters
$summary = $allfields;
//---------- CREATE STATISTICS ----------
//some progress bar stuff
// Create progress bar which is shown while creating the results
$prb = new ProgressBar();
$prb->pedding = 2; // Bar Pedding
$prb->brd_color = "#404040 #dfdfdf #dfdfdf #404040"; // Bar Border Color
$prb->setFrame(); // set ProgressBar Frame
$prb->frame['left'] = 50; // Frame position from left
$prb->frame['top'] = 80; // Frame position from top
$prb->addLabel('text','txt1',$clang->gT("Please wait ...")); // add Text as Label 'txt1' and value 'Please wait'
$prb->addLabel('percent','pct1'); // add Percent as Label 'pct1'
$prb->addButton('btn1',$clang->gT('Go back'),'?action=statistics&sid='.$surveyid); // add Button as Label 'btn1' and action '?restart=1'
//progress bar starts with 35%
$process_status = 35;
$prb->show(); // show the ProgressBar
// 1: Get list of questions with answers chosen
//"Getting Questions and Answers ..." is shown above the bar
$prb->setLabelValue('txt1',$clang->gT('Getting questions and answers ...'));
$prb->moveStep(5);
// creates array of post variable names
for (reset($_POST); $key=key($_POST); next($_POST))
{
$postvars[]=$key;
}
//show some main data at the beginnung
// CHANGE JSW_NZ - let's allow html formatted questions to show
$statisticsoutput .= "\n<div id='statsContainer'>\n"
."\t<div id='statsHeader'> \n"
."\t\t<div class='statsSurveyTitle'>"
."$thisSurveyTitle</div>\n"
."\t\t<div class='statsNumRecords'>"
.$clang->gT("Total records in survey")." : $totalrecords</div>\n";
//close statsHeader
$statisticsoutput .= "\t</div>\n";
//push progress bar from 35 to 40
$process_status = 40;
//Show Summary results
if (isset($summary) && $summary)
{
//"Generating Summaries ..." is shown above the progress bar
$prb->setLabelValue('txt1',$clang->gT('Generating summaries ...'));
$prb->moveStep($process_status);
//let's run through the survey // Fixed bug 3053 with array_unique
$runthrough=array_unique($summary);
//loop through all selected questions
foreach ($runthrough as $rt)
{
//update progress bar
if ($process_status < 100) $process_status++;
$prb->moveStep($process_status);
} // end foreach -> loop through all questions
$statisticsoutput .= generate_statistics($surveyid, $summary, $summary, $publicgraphs, 'html',null,$language,false);
//output
$statisticsoutput .= "<br />\n"
. "</div>\n";
} //end if -> show summary results
//done! set progress bar to 100%
if (isset($prb))
{
$prb->setLabelValue('txt1',$clang->gT('Completed'));
$prb->moveStep(100);
$prb->hide();
}
//output everything:
echo $statisticsoutput;
//output footer
echo getFooter();
//Delete all Session Data
$_SESSION['finished'] = true;
?>