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
|
<?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: results.php 6606 2009-04-09 18:26:36Z c_schmitz $ */
include_once("login_check.php");
// Todo: This is a dead script. Either use it or trash it. //OK: Here's how it works so far!
/*include this file in a php script
* The two "overall" functions are: * - giveMeRawDataFromFieldNames($surveyid, $gid, $qid, $fieldarray, responsestyle) * $surveyid = the survey id number * $gid = the group id for the question you want results for * $qid = the question id for the question you want results for * $fieldarray = a keyed array containing the fieldnames as key, and the value for of that fieldname that you want matches to * -ie: array ("1X2X34"=>"FL", "1X2X35"=>"Cork") * $responsestyle should be "full" - the alternative (which would just be the codes) is not yet coded * * This function will return a multi-level array. The first level is a numbered array with an * entry for every individual response. Within that array will be a further array, with an entry * for every possible answer to that question. Then, within each of those arrays will be * the question title, and the answer to that question for that record. * * As an example, a single answer type question (like a List, or Yes/No) * will look like this: * Array * ( * [0]=> Array * ( * [4X6X22] => Array * ( * [0] => What is your Employment Type? * [1] => Ongoing * ) * ) * [1]=> Array * ..... and so forth for each record in the responses table * * A multiple answer type question, such as "Multiple Choice" will look like this: * Array * ( * [0]=> Array * ( * [2X2X7C] => Array * ( * [0] => Choose your favourite foods [Cheese] * [1] => Yes * ) * [2X2X7I] => Array * ( * [0] => Choose your favourite foods [Ice Cream] * [1] => * ) * ) * * - makeSummaryFromRawData($results) * Feed this function the "raw results" string generated by the "giveMeRawDataFromFieldNames" * function and you'll be returned an array containing a summary/count of results * for every possible answer within that question. * * An example result for a single option question (ie: List or Yes/No) is: * * Array * ( * [4X6X22] => Array * ( * [question] => What is your Employment Type? * [summary] => Array * ( * [Ongoing] => 429 * [Fixed Term] => 61 * [None] => 4 * [Agency / Temp] => 4 * [Casual] => 1 * ) * * ) * ) * * An example result for a multiple answer type question is: * Array * ( * [2X2X7G] => Array * ( * [question] => Which of the following issues do you want addressed in negotiations for the next Enterprise Bargaining Agreement? [Accrual of Time In Lieu / Flex Time] * [summary] => Array * ( * [Yes] => 10 * [None] => 3 * ) * * ) * * [2X2X7K] => Array * ( * [question] => Which of the following issues do you want addressed in negotiations for the next Enterprise Bargaining Agreement? [Allowances / Reimbursement of Expenses] * [summary] => Array * ( * [Yes] => 5 * [None] => 8 * ) * * ) * ) * */
//THIS BIT IS JUST FOR DEBUGGING if (!empty($_GET['debug'])) { // $table = "survey_4"; // //$questionfields[]="4X6X22"; // //$questionfields[]="4X6X23"; // $limitby=array("4X6X22"=>"A", // "4X6X23"=>"B"); // $sql = buildSqlFromFieldnamesArray($limitby); // $questionfields=buildQuestionFields("4", "7", "29"); // if (!empty($questionfields)) { // $results = returnQuestionResults($table, $questionfields, $sql); // echo "TOTAL RESULTS: ".count($results); // echo "<pre>"; // print_r($results); // echo "</pre>"; // } $surveyid=$_GET['sid']; $gid=$_GET['gid']; $qid=$_GET['qid']; //$results = giveMeRawDataFromFieldNames("4", "6", "22", array(), "full"); //$results = giveMeRawDataFromFieldNames("2", "2", "7", array(), "full"); //$results = giveMeRawDataFromFieldNames("8", "18", "66", array(), "full"); //$results = giveMeRawDataFromFieldNames("29", "89", "559", array(), "full"); $results = giveMeRawDataFromFieldNames($surveyid, $gid, $qid, array(), "full");
$summary = makeSummaryFromRawData($results); foreach ($results as $result) { foreach ($result as $answer) {echo $answer[1];} } echo "<pre>"; print_r($results); echo "</pre>"; // // foreach ($summary as $sum) { // echo "<table width='400' align='center' border='1'>\n"; // echo "<tr><td colspan='2' align='center' bgcolor='silver'>".$sum['question']."</td></tr>\n"; // foreach ($sum['summary'] as $key=>$val) { // echo "<tr><td align='right' valign='top'><strong>$key</strong></td><td>$val</td></tr>\n"; // } // echo "</table><br />"; // } }
function makeSummaryFromRawData($results, $surveyid=null, $gid=null, $qid=null) {
//echo "<pre>";print_r($results);echo "</pre>"; if (empty($results)) { return array(); } if (!empty($qid)) { $thisquestion=getQuestionInfo($qid); } $rowcodes=array_keys($results[0]); // echo "<pre>";print_r($rowcodes);echo "</pre>";
$summary = array(); foreach ($results as $result) { foreach($rowcodes as $row) { //echo "<pre>";print_r($result);echo "</pre>";
if (is_array($result[$row])) { //echo "<pre>";print_r($result);echo "</pre>"; $summary[$row]['question']=$result[$row][0]; switch($thisquestion['type']) { case "T": case "S": case "H": if ($result[$row][1] != "") { if (!isset($summary[$row]['summary'][$clang->gT("Answered")])) { $summary[$row]['summary'][$clang->gT("Answered")]=1; } else { $summary[$row]['summary'][$clang->gT("Answered")]++; } } break; default: if (!isset($summary[$row]['summary'][$result[$row][1]])) { $summary[$row]['summary'][$result[$row][1]]=1; } else { $summary[$row]['summary'][$result[$row][1]]++; } break; } } if ($thisquestion['type'] == "N") { ksort($summary[$row]['summary'], SORT_NUMERIC); } } } //echo "<pre>";print_r($summary);echo "</pre>"; //fill in the blanks from answer table and sort if (isset($surveyid) && isset($qid) && $summary) { //$thissurvey=getSurveyInfo($surveyid); $rowcodes=array_keys($summary); switch($thisquestion['type']){ case "F": case "H": $answers=getLabelSet($thisquestion['lid']); break; case "!": case "L": case "O": $answers=getAnswersSingle($surveyid, $gid, $qid); $answers[]=array("code"=>"", "answer"=>$clang->gT("No answer")); break; case "W": case "Z": case "1": $answers=getLabelSet($thisquestion['lid']); $answers[]=array("code"=>"", "answer"=>$clang->gT("No answer")); break; case "C": $answers[]=array("code"=>"Y", "answer"=>$clang->gT("Yes")); $answers[]=array("code"=>"U", "answer"=>$clang->gT("Uncertain")); $answers[]=array("code"=>"N", "answer"=>$clang->gT("No")); break; case "E": $answers[]=array("code"=>"I", "answer"=>$clang->gT("Increase")); $answers[]=array("code"=>"S", "answer"=>$clang->gT("Same")); $answers[]=array("code"=>"D", "answer"=>$clang->gT("Decrease")); $answers[]=array("code"=>"", "answer"=>$clang->gT("No answer")); break; case "M": case "P": $answers[]=array("code"=>"Y", "answer"=>$clang->gT("Yes")); $answers[]=array("code"=>"", "answer"=>$clang->gT("No answer")); break; case "Y": $answers[]=array("code"=>"Y", "answer"=>$clang->gT("Yes")); $answers[]=array("code"=>"N", "answer"=>$clang->gT("No")); $answers[]=array("code"=>"", "answer"=>$clang->gT("No answer")); break; case "G": $answers[]=array("code"=>"M", "answer"=>$clang->gT("Male")); $answers[]=array("code"=>"F", "answer"=>$clang->gT("Female")); $answers[]=array("code"=>"", "answer"=>$clang->gT("No answer")); break; case "T": case "S": case "U": $answers[]=array("code"=>$clang->gT("Answered"), "answer"=>$clang->gT("Answered")); $answers[]=array("code"=>"", "answer"=>$clang->gT("No answer")); break; } // switch
if (isset($answers)) { foreach($rowcodes as $row) { $newarray[$row]['question']=$summary[$row]['question']; foreach($answers as $answer) { //echo "<pre>";print_r($answer);echo "</pre>"; if (isset($summary[$row]['summary'][$answer['answer']])) { $newarray[$row]['summary'][$answer['answer']]=$summary[$row]['summary'][$answer['answer']]; } else { $newarray[$row]['summary'][$answer['answer']]=0; } } } $summary=$newarray; } }
//Fix the output for multiple if ($thisquestion['type'] == "M" || $thisquestion['type'] == "P" || $thisquestion['type'] == "^" || $thisquestion['type'] == "1") { if (isset($newarray)) {unset($newarray);} $newarray[$thisquestion['sid']."X".$thisquestion['gid']."X".$thisquestion['qid']]['question']=$thisquestion['question']; foreach ($summary as $sum) { ereg("\[(.*)\]", $sum['question'], $regs); $newarray[$thisquestion['sid']."X".$thisquestion['gid']."X".$thisquestion['qid']]['summary'][$regs[1]]=$sum['summary'][$clang->gT("Yes")]; } $summary=$newarray; } // echo "<pre>";print_r($summary);echo "</pre>"; return $summary; }
function giveMeRawDataFromFieldNames($surveyid, $gid, $qid, $fieldlimiters=array(), $output="full") { //Builds output data for question $surveyid$gid$qid, limiting with $fieldlimiters array $questionfields = buildQuestionFields($surveyid, $qid); $sql = buildSqlFromFieldnamesArray($fieldlimiters); $tablename = "survey_".$surveyid; $fieldmap=createFieldMap($surveyid, "full"); //echo "<pre>"; print_r($answers); echo "</pre>"; list($questioninfo, $results) = returnQuestionResults($surveyid, $questionfields, $sql); //echo "<pre>"; print_r($questioninfo); echo "</pre>"; if (count($results) < 1) { return array(); } foreach ($questioninfo as $qi) { $questiontype=$qi['type']; } // echo "[$questiontype]<br />"; if ($output == "full") { GetBaseLanguageFromSurveyID($surveyid); //echo "<pre>"; print_r($answers); echo "</pre>"; switch($questiontype) { case "L": case "!": case "O": case "D": case "E": case "M": case "P": case "C": case "B": case "A": case "F": case "H": case "1": $answers = getAnswersSingle($surveyid, $gid, $qid); break; case "W": case "Z": $thisquestion=getQuestionInfo($qid); $answers=getLabelSet($thisquestion['lid']); break; case "Y": $answers[]=array("code"=>"Y", "answer"=>$clang->gT("Yes")); $answers[]=array("code"=>"N", "answer"=>$clang->gT("No")); break; case "G": $answers[]=array("code"=>"M", "answer"=>$clang->gT("Male")); $answers[]=array("code"=>"F", "answer"=>$clang->gT("Female")); break; } // switch $answers[]=array("code"=>"", "answer"=>$clang->gT("No answer")); $answers[]=array("code"=>"-oth-", "answer"=>$clang->gT("Other"));
switch($questiontype) { case "A": for ($i=1; $i <= 5; $i++) { $values[]=array("code"=>$i, "answer"=>$i); } break; case "B": for ($i=1; $i<=10; $i++) { $values[]=array("code"=>$i, "answer"=>$i); } break; case "E": $values[]=array("code"=>"I", "answer"=>$clang->gT("Increase")); $values[]=array("code"=>"S", "answer"=>$clang->gT("Same")); $values[]=array("code"=>"D", "answer"=>$clang->gT("Decrease")); break; case "C": case "M": case "P": $values[]=array("code"=>"Y", "answer"=>$clang->gT("Yes")); $values[]=array("code"=>"U", "answer"=>$clang->gT("Uncertain")); $values[]=array("code"=>"N", "answer"=>$clang->gT("No")); break; case "F": case "H": $thisquestion=getQuestionInfo($qid); $values = getLabelSet($thisquestion['lid']); break; } $values[]=array("code"=>"", "answer"=>$clang->gT("No answer"));
switch($questiontype) { case "L": case "!": case "O": case "Y": case "G": case "S": case "T": case "H": case "N": case "5": case "D": case "W": case "Z": //The first key needs to be expanded $firstkey=array_keys($results[0]); $firstkey=$firstkey[0]; $questions=arraySearchByKey($firstkey, $fieldmap, "fieldname", 1); //echo $firstkey; $i=0; foreach($results as $result) { $results[$i][$firstkey]=array($questions['question'], arraySubstitute($result[$firstkey], $answers)); $i++; } break; case "A": case "B": case "C": case "E": case "F": case "H": case "M": case "P": case "Q": case "1": $i=0; foreach($results as $result) { foreach($result as $key=>$val) { $questions=arraySearchByKey($key, $fieldmap, "fieldname", 1); if (substr($key, -7, 7) != "comment") { $code=substr($key, strlen($surveyid."X".$gid."X".$qid), strlen($key)-strlen($surveyid."X".$gid."X".$qid)); //echo $code; $results[$i][$key]=array($questions['question'], arraySubstitute($val, $values)); } } $i++; } break; } // switch } return $results; }
function buildSqlFromFieldnamesArray($fieldnamesarray) { //Expects an array like this: "1x2x3"=>"FL" //and builds SQL "where" statement out of it (without the "WHERE" at the front) if (count($fieldnamesarray)) { foreach ($fieldnamesarray as $key=>$val) { if ($val == "{NULL}") { $sql[] = "$key IS NULL"; } else { $sql[] = "$key = '$val'"; } } if (count($sql) > 1) { return implode(" AND\n", $sql); } else { return $sql[0]; } } }
function buildQuestionFields($surveyid, $qid) { //Takes a specific question, and returns an array containing //all the possible fieldnames for responses to that question $fieldmap=createFieldMap($surveyid); foreach ($fieldmap as $fields) { if ($fields['sid'] == $surveyid && $fields['qid'] == $qid && $fields['aid'] != "comment") { $questionfields[]=$fields['fieldname']; } } if (!empty($questionfields)) { return $questionfields; } else { return array("id"); } }
function returnQuestionResults($surveyid, $questionfields, $sql=null) { global $connect; //Returns uninterpreted raw results from survey table for question(s) //$table = survcey table name (ie: "survey_1") //$questionfields should contain an array of the question fields that are being returned //$sql is any additional "filtering" sql code $details=array(); $output=array(); foreach($questionfields as $questionfield) { $detailsarray=arraySearchByKey($questionfield, createFieldMap($surveyid), "fieldname"); foreach ($detailsarray as $dt) { $details[]=$dt; } } $table="survey_".$surveyid;
if (count($questionfields) > 1) { $selects = "`".implode("`, `", $questionfields)."`"; } else { $selects = "`".$questionfields[0]."`"; } $query = "SELECT $selects FROM $table"; if (!empty($sql)) { $query .= "\nWHERE $sql"; } $result = db_execute_assoc($query) or safe_diee("error getting results in returnQuestionResults<br />$query<br />".$connect->ErrorMsg()); while($row=$result->FetchRow()) { $output[]=$row; } // while return array($details, $output); }
function getAnswersSingle($surveyid, $gid, $qid) { global $dbprefix, $connect; $query = "SELECT * FROM ".db_table_name("answers")." WHERE qid=$qid ORDER BY sortorder, answer"; $result = db_execute_assoc($query); while($row = $result->FetchRow()) { $answer[]=array("code"=>$row['code'], "answer"=>$row['answer']); } // while return $answer; }
function getLabelSet($lid) { global $dbprefix, $connect; $query = "SELECT * FROM ".db_table_name("labels")." WHERE lid=$lid ORDER BY sortorder"; $result = db_execute_assoc($query) or safe_die($connect->ErrorMsg()); while($row = $result->FetchRow()) { if ($row['title'] <> '') { $answer[]=array("code"=>$row['code'], "answer"=>$row['title']); } } // while return $answer; }
function arraySubstitute($value, $substitutes) { foreach ($substitutes as $sub) { if ($value == $sub['code']) { return $sub['answer']; } } return $value; } ?>
|