-
Notifications
You must be signed in to change notification settings - Fork 16
/
survey.jinja2
248 lines (221 loc) · 10.4 KB
/
survey.jinja2
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
{% extends "sitebase.jinja2" %}
{% set page_title = _("Participant Survey") %}
{% set show_breadcrumbs = True %}
{% block head %}
<script type="text/javascript" src="/static/vendor/js/vue-2.5.17.min.js"></script>
<script type="text/javascript">
// Override the hardecoded " selected" string in vfg.js
_SELECTED = "{{ _(' selected') }}"
</script>
<script type="text/javascript" src="/static/vendor/vue-form-generator-2.3.4/vfg.js"></script>
<link rel="stylesheet" type="text/css" href="/static/vendor/vue-form-generator-2.3.4/vfg.css">
<style type="text/css">
img#bristol-chart {
min-width: 300px;
max-width: 25%;
}
.vue-form-generator .field-radios .radio-list label {
padding-right: 25px;
display: inline-block;
}
.survey-nav-button {
padding: 6px 12px;
color: #ffffff!important;
background-color: #337ab7!important;
border: 1px solid #cccccc;
border-radius: 4px;
font-size: 14px;
font-weight: 400;
line-height: 1.42857143;
text-align: center;
display: none;
}
.form-group label span{
font-weight: 700;
}
.form-group {
padding-bottom: 5px;
}
.field-wrap {
font-size: 90%;
}
#form-progress {
width: 100%;
text-align: center;
display: none;
font-weight: bold;
}
</style>
<script>
// Overwrite and localize the messages used, based on the example from:
// https://github.com/vue-generators/vue-form-generator/issues/15#issuecomment-231033466
VueFormGenerator.validators.resources.invalidInteger = "{{ _('The value is not an integer') }}";
VueFormGenerator.validators.resources.invalidNumber = "{{ _('Invalid number') }}";
var result_txt = "";
var error_txt = "";
var form_submitted = 0;
function postSurvey() {
let fieldsetObject = document.getElementsByTagName("fieldset");
// workaround to make the earlier sections of the survey visible to the DOM so they submit, but hidden to the user
for(let i=1; i<(fieldsetObject.length-1); i++) {
fieldsetObject[i].style.position = "absolute";
fieldsetObject[i].style.top = "-9999px";
fieldsetObject[i].style.left = "-9999px";
fieldsetObject[i].style.display = "inline-block";
}
$.ajax({
type: "POST",
url: "{{ endpoint }}/accounts/{{ account_id }}/sources/{{ source_id }}/take_survey?survey_template_id={{ survey_template_id }}",
data: JSON.stringify(survey_model),
success: function (data, textStatus, jqXHR) {
result_txt = data;
},
error: function (jqXHR, textStatus, errorThrown) {
error_txt = jqXHR.responseText;
},
complete: function (data, textStatus, output_obj) {
// Replace the html on this page with the html successfully received from the ajax call.
// Note that the html we successfully received MAY be an html page reporting an error
// captured in the back end :)
if (result_txt !== ""){
document.open();
document.write(result_txt);
document.close();
} else {
// I can't imagine how we could get here, but just in case, at least the
// user sees *something* ...
if (error_txt === ""){
error_txt = textStatus;
}
let queryParam = encodeURIComponent(error_txt);
// Get customized error page that will show whatever error message we captured
// here in the front end
window.location.replace("{{ endpoint }}/error?error_msg=" + queryParam);
}
},
dataType: "html",
contentType: "application/json"
});
// always return false, preventing a traditional post of the form
return false;
}
function set_triggers(model, field) {
if ('triggered_by' in field && field.triggered_by !== null && field.triggered_by.length > 0) {
field.visible = function() {
let visible = false;
for (let entry of field.triggered_by) {
visible |= (model[entry['q_id']] === entry['response']);
}
return visible;
}
}
}
</script>
{% endblock %}
{% block breadcrumb %}
<li class="breadcrumb-item active" aria-current="page">{{ _('Participant Survey') }}</li>
{% endblock %}
{% block content %}
<div id="form-progress">{{ _('Progress') }}</div>
<div class="panel panel-default">
<div class="panel-body">
<form id="survey_form" name="survey_form">
<vue-form-generator :schema="schema" :model="model" :options="formOptions" @validated="onValidated"></vue-form-generator>
</form>
</div>
<input type="button" id="survey-nav-button-previous" class="survey-nav-button" value="<< {{ _('Previous Section') }}" onClick="changePage('previous');">
<input type="button" id="survey-nav-button-next" class="survey-nav-button" value="{{ _('Next Section') }} >>" onClick="changePage('next');">
</div>
<script type="text/javascript">
var survey_model = {};
var survey_schema= {{survey_schema|tojson}};
if (survey_schema.fields !== null) {
for (let field of survey_schema.fields) {
set_triggers(survey_model, field);
}
}
for (let group of survey_schema.groups) {
for (let field of group.fields) {
set_triggers(survey_model, field);
}
}
last_group = survey_schema.groups[survey_schema.groups.length-1];
last_group.fields.push({
buttonText: "{{ _('Submit Survey') }}",
type: "submit",
validateBeforeSubmit: true,
onSubmit: function(){
let confirm_submit = false;
let num_string_fields = document.getElementsByClassName("field-textArea").length + document.getElementsByClassName("field-input").length ;
if(form_submitted === 0) {
if(num_string_fields > 0 || Object.keys(survey_model).length>0) {
confirm_submit = confirm("{{ _('Please note: Once you\'ve submitted this survey, you\'ll be unable to edit your answers.') }}\r\n\r\n{{ _('If you\'d like to review or change any of your responses, please press the Cancel button. Otherwise, press OK to submit your responses.') }}\r\n\r\n{{ _('This warning will only appear once.') }}");
form_submitted = 1;
}
else {
alert("{{ _('It looks like you are trying to submit this survey without answering any questions. If you would prefer not to complete this survey, please return to the homepage.') }}")
}
} else {
confirm_submit = true;
}
if(confirm_submit === true) {
$("input").prop('disabled', true);
return postSurvey();
}
}
});
var active_section = 0;
var errors_on_page = 0;
function paginateForm() {
let fieldsetObject = document.getElementsByTagName("fieldset");
if(fieldsetObject.length <= 1) {
document.getElementById("survey-nav-button-next").style.display = "none";
} else {
document.getElementById("form-progress").style.display = "block";
document.getElementById("form-progress").innerHTML = "{{ _('Step') }} 1 {{ _('of') }} " + fieldsetObject.length + " - 0% {{ _('Complete') }}";
for(let i=1; i<fieldsetObject.length; i++) {
fieldsetObject[i].style.display = "none";
}
document.getElementById("survey-nav-button-next").style.display = "inline-block";
}
}
function changePage(direction) {
if(errors_on_page !== 0) {
alert("{{ _('Please correct the fields with errors.') }}");
return false;
}
let fieldsetObject = document.getElementsByTagName("fieldset");
let new_active_section = 0;
if(direction === "next") {
new_active_section = active_section + 1;
} else {
new_active_section = active_section - 1;
}
let display_active_section = new_active_section+1;
fieldsetObject[active_section].style.display = "none";
document.getElementById("form-progress").innerHTML = "{{ _('Step') }} " + display_active_section + " {{ _('of') }} " + fieldsetObject.length + " - " + Math.round((new_active_section/fieldsetObject.length)*100) + "% {{ _('Complete') }}";
fieldsetObject[new_active_section].style.display = "block";
if(new_active_section === 0) {
document.getElementById("survey-nav-button-previous").style.display = "none";
} else {
document.getElementById("survey-nav-button-previous").style.display = "inline-block";
}
if(new_active_section === (fieldsetObject.length-1)) {
document.getElementById("survey-nav-button-next").style.display = "none";
} else {
document.getElementById("survey-nav-button-next").style.display = "inline-block";
}
window.scrollTo(0,0);
active_section = new_active_section;
}
function onValidated(isValid,errors) {
if(isValid === false) {
errors_on_page = 1;
} else {
errors_on_page = 0;
}
}
$(paginateForm);
</script>
<script type="text/javascript" src="/static/vue_survey_form.js"></script>
{% endblock %}