-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsummary.php
128 lines (114 loc) · 3.97 KB
/
summary.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
<?php
/**
* summary.php
* Author: Hiroyoshi Kurohara(Microgadget,inc.)
* Author EMail : kurohara@yk.rim.or.jp
* License: GPLv2 or Lator.
*/
namespace questionnaire;
define(__NAMESPACE__ . '\DEF_FETCHCOUNT', 4096);
function initialize_summary()
{
add_ns_action('wp_ajax_qstnr_summary', 'ajax_summary');
}
function summary()
{
wp_enqueue_script('amcharts', plugins_url('amcharts/amcharts/amcharts.js', __FILE__));
wp_enqueue_script('amcharts-pie', plugins_url('amcharts/amcharts/pie.js', __FILE__));
wp_enqueue_script('amcharts-serial', plugins_url('amcharts/amcharts/serial.js', __FILE__));
wp_enqueue_script('amcharts-serial', plugins_url('amcharts/amcharts/themes/light.js', __FILE__));
wp_enqueue_script('qstnr_summary', plugins_url('summary.js', __FILE__));
?>
<div class="qstnr-summary">
<div class="qstnr-summary-bg">
<p> no valid data
</div>
</div>
<?php
}
function summary_ne()
{
ob_start();
summary();
$html = ob_get_contents();
ob_end_clean();
return $html;
}
/**
* called when paged answer list requested.
*/
function ajax_summary()
{
$postid = $_GET['postid'];
if (! wp_verify_nonce($_GET['nonce'], QUESTIONNAIRE_NONCE . $postid) ) {
status_header(SC_BADREQUEST);
echo json_encode(array('success' => false));
die();
}
status_header(SC_OK);
echo json_encode(summary_total($postid), JSON_UNESCAPED_UNICODE);
die();
}
function summary_total($postid)
{
$sum = null;
for ($i = 0;;$i += DEF_FETCHCOUNT) {
$data_arr = get_answer_list($postid, $i, DEF_FETCHCOUNT);
foreach ($data_arr as $entry) {
$content = json_decode($entry->comment_content, true);
$itemlist = $content['itemlist'];
if (is_null($sum)) {
// initialize summary array.
$sum = $itemlist;
foreach ($sum as $index => $item) {
foreach ($item['selected'] as $key => $value) {
// change boolean value into integer 0.
$sum[$index]['selected'][$key] = 0;
}
$sum[$index]['valid'] = 0;
}
}
foreach ($itemlist as $index => $item) {
$isvalid = false;
if ($item['type'] !== 'text') {
if ($item['type'] == 'number') {
if($item['valid']) {
$isvalid = true;
if(!isset($sum[$index]['selected'][$item['value']])) {
$key = array_search ($item['value'], $sum[$index]['selections']);
if($key === false) {
//add new selection
$key = sizeof($sum[$index]['selections']);
$sum[$index]['selections'][] = $item['value'];
$sum[$index]['selected']['option_selected_' . $key] = 1;
$sum[$index]['selectedname'][] = 'option_selected_' . $key;
}
else {
$sum[$index]['selected']['option_selected_' . $key]++;
}
}
else {
$sum[$index]['selected'][$item['value']]++;
}
}
}
else {
foreach ($item['selected'] as $key => $value) {
if ($value === true) {
$isvalid = true;
$sum[$index]['selected'][$key]++ ;
}
}
}
}
if ($isvalid === true) {
$sum[$index]['valid']++;
}
}
}
if (count($data_arr) < DEF_FETCHCOUNT) {
break;
}
}
return $sum;
}