-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquest.php
executable file
·204 lines (163 loc) · 5.78 KB
/
quest.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
<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/include/main_func.php';
auth(0);
/****************************************************
* AJAX Responses
***************************************************/
if (array_key_exists('submit', $_GET)) {
// save data and return nextpage
$clean = my_clean($_POST);
$questions = array();
$answers = array();
foreach ($clean as $q => $a) {
if (substr($q, 0, 1) == 'q' && is_numeric(substr($q, 1))) {
$questions[] = $q;
$answers[] = '"' . $a . '"';
}
}
// record data in quest_data
foreach ($clean as $qu => $a) {
if (substr($qu, 0, 1) == 'q' && is_numeric(substr($qu, 1))) {
$q = sprintf('INSERT INTO quest_data (quest_id, user_id, session_id, question_id, dv, `order`, dt)
VALUES(%d, %d, %d, %d, "%s", %d, "%s")',
$clean['quest_id'],
$_SESSION['user_id'],
$_SESSION['session_id'],
str_replace('q', '', $qu),
$a,
$order++,
date('Y-m-d H:i:s')
);
$q = str_replace('"NULL"', 'NULL', $q);
$query = new myQuery($q);
}
}
// send to feedback page
echo 'url;/fb?type=quest&id=' . $clean['quest_id'];
exit;
}
/****************************************************
* Display Questionnaire
***************************************************/
// set up questionnaire
require_once $_SERVER['DOCUMENT_ROOT'] . '/include/classes/quest.php';
$quest_id=$_GET['id'];
$q = new questionnaire($quest_id);
if (!$q->check_exists()) {
header('Location: /');
exit;
}
if (!$q->check_eligible()) {
if (in_array($_SESSION['status'], $RES_STATUS)) {
$ineligible = "<p class='error'>You would not be able to see this questionnaire because of your age or sex if you were a non-researcher.</p>";
} else {
header('Location: /fb?ineligible&type=quest&id=' . $quest_id); exit;
}
}
$title = array(
$q->get_name()
);
$styles = array();
$page = new page($title);
$page->set_menu(false);
$page->displayHead($styles);
$page->displayBody();
echo $ineligible;
$q->print_form();
?>
<div id='dialog-confirm' class='dialog'>
<p>You have not answered some questions. Are you sure you want to submit?</p>
</div>
<!--*************************************************-->
<!-- !Javascripts for this page -->
<!--*************************************************-->
<script>
// prevent back button
history.pushState(null, null, location.href);
history.back();
history.forward();
window.onpopstate = function () { history.go(1); };
$(function() {
// cancel empty alert on change for select field
$('#qTable select, #qTable input, #qTable textarea').change( function() {
if ($(this).val() != "NULL") {
$(this).closest('#qTable > tbody > tr').removeClass('emptyAlert');
}
});
// update time fields
$('.time').on('change', '.selectnum', function() {
var t = $(this).siblings("input:hidden");
console.log(t.attr('id'));
var h = t.siblings('.selectnum').eq(0).val();
var m = t.siblings('.selectnum').eq(1).val();
t.val(h + ":" + m);
console.log("set time to ", h + ":" + m);
});
});
// form is submitted
function submitQ(quest_id) {
// check for empty questions
var fields = {};
$.each($('#maincontent form').serializeArray(), function(index,value) {
fields[value.name] = value.value;
});
$('div.slider').each(function() {
fields[$(this).attr("id")] = $(this).slider("value");
});
var emptyFields = 0;
// look through visible questionnaire rows (only rows that have id and not ranking rows) for empty variables
$('#qTable > tbody > tr[id]:not(.ranking):not(.msg):visible').each( function(i) {
$(this).removeClass('emptyAlert');
var qid = $(this).attr('id').replace('_row','');
if (fields[qid] == '' || fields[qid] == null || fields[qid] == 'NULL') {
$(this).addClass('emptyAlert');
emptyFields++;
}
});
if (emptyFields > 0) {
$('#dialog-confirm').dialog({
resizable: false,
modal: true,
title: "Missing Data",
show: 'fade',
buttons: {
"Submit with missing info": function() {
$(this).dialog("close");
recordAnswers();
},
"Go back to questionnaire": function() {
$(this).dialog("close");
}
}
});
} else {
recordAnswers();
}
}
function recordAnswers() {
var theData = $('#maincontent form').serializeArray();
$('div.slider').each(function() {
theData[theData.length] = {
name: $(this).attr("id"),
value: $(this).slider("value")
};
});
// record answers
$.ajax({
type: 'POST',
url: '/include/scripts/record_quest', //'/quest?submit',
data: theData,
success: function(response) {
parsedResponse = response.split(';');
if (parsedResponse[0] == 'url') {
window.location.href=parsedResponse[1];
} else {
$('<div />').html(response).dialog();
}
}
});
}
</script>
<?php
$page->displayFooter();
?>