-
Notifications
You must be signed in to change notification settings - Fork 16
/
survey.jinja2
103 lines (97 loc) · 4.19 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
{% 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" 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">
<script>
var result_txt = "";
var error_txt = "";
function postSurvey() {
$.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 class="panel panel-default">
<div class="panel-body">
<form id="survey_form" name="survey_form">
<vue-form-generator :schema="schema" :model="model" :options="formOptions"></vue-form-generator>
</form>
</div>
</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(){
$("input").prop('disabled', true);
return postSurvey();
}
});
</script>
<script type="text/javascript" src="/static/vue_survey_form.js"></script>
{% endblock %}