forked from mholt/ysaward
-
Notifications
You must be signed in to change notification settings - Fork 0
/
survey.php
218 lines (185 loc) · 6.48 KB
/
survey.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
<?php
require_once("lib/init.php");
protectPage();
// Get a list of all visible questions
$q = "SELECT ID FROM SurveyQuestions WHERE Visible=1 AND WardID={$MEMBER->WardID} ORDER BY ID ASC";
$r = DB::Run($q);
?>
<!DOCTYPE html>
<html>
<head>
<title>My survey — <?php echo $WARD ? $WARD->Name." Ward" : SITE_NAME; ?></title>
<?php include "includes/head.php"; ?>
</head>
<body>
<?php include "includes/header.php"; ?>
<form method="post" action="/api/saveanswers">
<div class="text-center">
<?php
// Just finished registration? Make sure they feel like they're not
// done until the survey is filled out.
if (isset($_SESSION['isNew'])):
?>
<h1>Register</h1>
<b>Please fill out this survey</b> and then you're done! Thank you.
<hr class="line">
<?php else: ?>
<h1>Survey answers</h1>
<?php endif; ?>
</div>
<div class="grid-container" style="font-size: 16px;">
<div class="grid-100 text-center" style="font-size: 12px;">
<i>Questions marked with <span class="req">*</span> require an answer.</i>
<hr>
</div>
<?php
$i = 0;
// Display questions / answers
while ($row = mysql_fetch_array($r)):
// Load the question...
$sq = SurveyQuestion::Load($row['ID']);
// Load this member's answer
$ans = $sq->Answers($MEMBER->ID());
// Create the name for this question's answer's input field
$inputName = 'answers['.$sq->ID().']'; // Array idx is question ID
// Is this question designed to have radio buttons or checkboxes?
$multAnsOpt = $sq->QuestionType == QuestionType::MultipleChoice
|| $sq->QuestionType == QuestionType::MultipleAnswer;
// Okay, now which one?
$inputType = "radio";
if ($sq->QuestionType == QuestionType::MultipleAnswer)
{
$inputType = "checkbox";
$inputName .= '[]'; // Store each check, not just the last one
}
$inputID = "field".$i;
// Get answer options if needed
$ansOpt = array();
if ($multAnsOpt)
$ansOpt = $sq->AnswerOptions();
// Does this question have more than one answer, potentially?
$multAns = $sq->QuestionType == QuestionType::MultipleAnswer
|| $sq->QuestionType == QuestionType::CSV;
?>
<div class="grid-50" style="padding-bottom: 20px;">
<b>
<label for="<?php echo $inputID; ?>">
<?php echo $sq->Question; ?>
</label>
</b>
<?php if ($sq->Required) echo '<span class="req">*</span>'; ?>
</div>
<div class="grid-50">
<?php
// Format and display answer field(s) for this question depending on its type
if ($sq->QuestionType == QuestionType::FreeResponse):
?>
<textarea rows="5" name="<?php echo $inputName; ?>" id="<?php echo $inputID; ?>" placeholder="Type your answer here"><?php echo is_object($ans) ? $ans->AnswerValue : ''; ?></textarea>
<?php
elseif ($sq->QuestionType == QuestionType::MultipleChoice
|| $sq->QuestionType == QuestionType::MultipleAnswer):
foreach ($ansOpt as $opt)
{
echo '<label><input type="'.$inputType.'" value="'.htmlentities($opt->AnswerValue()).'" name="'.$inputName.'"';
echo 'data-label="'.$opt->AnswerValue().'"';
$ansArray = $ans ? $ans->AnswerArray() : array();
foreach($ansArray as $oneAns)
{
if (trim($oneAns) == $opt->AnswerValue())
{
echo ' checked';
break;
}
}
echo '><br>';
}
elseif ($sq->QuestionType == QuestionType::YesNo):
?>
<input type="radio" name="<?php echo $inputName; ?>" value="Yes" data-label="Yes"<?php echo is_object($ans) ? $ans->AnswerValue == "Yes" ? 'checked' : '' : ''; ?>></label>
<br>
<label><input type="radio" name="<?php echo $inputName; ?>" value="No" data-label="No"<?php echo is_object($ans) ? $ans->AnswerValue == "No" ? 'checked' : '' : ''; ?>></label>
<?php
elseif ($sq->QuestionType == QuestionType::ScaleOneToFive):
?>
<input type="range" min="1" max="5" value="<?php echo is_object($ans) && is_numeric($ans->AnswerValue) ? $ans->AnswerValue : '3' ?>" step="1" id="<?php echo $inputName; ?>" name="<?php echo $inputName; ?>">
<output id="<?php echo $sq->ID(); ?>"><?php echo is_object($ans) && is_numeric($ans->AnswerValue) ? $ans->AnswerValue : '3' ?></output>
<?php
elseif ($sq->QuestionType == QuestionType::Timestamp):
?>
<input type="text" name="<?php echo $inputName; ?>" class="timestamp" value="<?php echo is_object($ans) ? date('d M Y, g:i A', strtotime($ans->AnswerValue)) : '' ?>" placeholder="Type a date and/or time">
<?php
elseif ($sq->QuestionType == QuestionType::CSV):
?>
<!--Please list <i>one per line</i>:<br>-->
<textarea rows="5" name="<?php echo $inputName; ?>" placeholder="List one per line"><?php echo formatForClient(is_object($ans) ? $ans->AnswerValue : ''); ?></textarea>
<?php
else:
echo '';
endif;
?>
</div>
<hr class="clear"><br><br>
<?php
$i ++;
endwhile;
?>
<div class="text-center">
<button type="submit"><?php echo isset($_SESSION['isNew']) ? "Finish" : "Save"; ?></button>
<br><br>
</div>
</div>
</form>
<?php include "includes/footer.php"; ?>
<?php include "includes/nav.php"; ?>
<script>
$(function()
{
// Show the value of sliders on page load
$('output').each(function() {
$(this).text($(this).prev().val());
});
// As sliders are moved, change the value displayed by them.
$('input[type=range]').change(function() {
$(this).next('output').text($(this).val());
});
// Verify date/time inputs as they're entered
$('.timestamp').change(function()
{
var self = $(this);
$.get('/api/tryparsedate.php', {
input: $(this).val()
})
.success(function()
{
self.css('color', '');
$('[type=submit]').prop('disabled', false);
})
.fail(function(jqxhr)
{
self.css('color', '#CC0000');
$('[type=submit]').prop('disabled', true);
alert(jqxhr.responseText || "Please type a better date, for example: July 3, 1990.");
});
});
// Capture form submit and save the answers
$('form').hijax({
before: function() {
$('[type=submit]').showSpinner();
},
complete: function(xhr) {
if (xhr.status == 200)
{
_gaq.push(['_trackEvent', 'Account', 'Submit Form', 'Survey Answers']);
$.sticky(xhr.responseText || "Saved your survey. thanks!");
if (xhr.responseText.toLowerCase().indexOf("welcome") > -1) // New member just finished registering; take to directory.
setTimeout(function() { window.location = '/directory'; }, 3000);
}
else
$.sticky(xhr.responseText || "There was a problem and your survey was probably not saved. Check your Internet connection and try again.", { classList: "error" });
$('[type=submit]').hideSpinner();
}
});
});
</script>
</body>
</html>