make quiz better by making them better, ...

This commit is contained in:
Arthur Vercruysse 2020-09-06 17:03:07 +02:00 committed by ajuvercr
parent cc183c7775
commit 3426c1944d
4 changed files with 58 additions and 46 deletions

View file

@ -104,36 +104,36 @@ antwoorden:
type: dropdown type: dropdown
antwoorden: antwoorden:
- tekst: Vlaams-Nationalistisch en conservatief - tekst: Vlaams-Nationalistisch en conservatief
verenigingenen: verenigingen:
- naam: kvhv - naam: kvhv
- naam: nsv - naam: nsv
- tekst: Antikapitalistisch - tekst: Antikapitalistisch
verenigingenen: verenigingen:
- naam: als - naam: als
- tekst: Christendemocraat - tekst: Christendemocraat
verenigingenen: verenigingen:
- naam: cds - naam: cds
- tekst: Zeer socialistisch - tekst: Zeer socialistisch
verenigingenen: verenigingen:
- naam: comac - naam: comac
- tekst: Groen en links - tekst: Groen en links
verenigingenen: verenigingen:
- naam: jgroen - naam: jgroen
- tekst: Vlaams-republikeins - tekst: Vlaams-republikeins
verenigingenen: verenigingen:
- naam: jongnva - naam: jongnva
- tekst: Socialistisch - tekst: Socialistisch
verenigingenen: verenigingen:
- naam: js - naam: js
- tekst: Liberaal - tekst: Liberaal
verenigingenen: verenigingen:
- naam: lvsv - naam: lvsv
- tekst: radio maken - tekst: radio maken

View file

@ -238,3 +238,19 @@ h2 {
#postcodeField[type="number"] { #postcodeField[type="number"] {
-moz-appearance: textfield; -moz-appearance: textfield;
} }
fieldset {
max-height: 60vh;
display: grid;
grid-template-columns: repeat(auto-fill, minmax(500px, 1fr));
overflow-y: auto;
margin: auto;
}
fieldset p {
margin: 15px 10px;
}
fieldset p label input {
margin-right: 10px;
}

View file

@ -5,23 +5,27 @@
</p> </p>
<input id="postcodeField" type=number> <input id="postcodeField" type=number>
<%else%> <%else%>
<select <% if question[:type] == "meerkeuze" %> multiple="multiple" <% end %> <form id="<%= number %>_form">
id="<%= (question[:tekst] || "") + (question[:vraag] || "") %>_option"> <fieldset>
<option value="" selected="selected disabled hidden">Maak uw keuze</option> <% question[:antwoorden].each_with_index do |answer, idx| %>
<% for answer in question[:antwoorden]%> <p>
<option value='<%= answer[:tekst] + (answer[:vraag] || "") %>'> <label style="display:flex">
<input type=<% if question[:type] == "meerkeuze" %>"checkbox"<% else %>"radio"<% end %>
value='<%=number+"#"+idx.to_s%>' name=<%= number %> >
<%= answer[:tekst] %> <%= answer[:tekst] %>
</option> </label>
<% end %> </p>
</select> <% end %>
<% for answer in question[:antwoorden]%> </fieldset>
<% if answer[:vraag] %> </form>
<div id='<%= html_escape(answer[:tekst] + answer[:vraag]) %>' style="display:none;"> <% question[:antwoorden].each_with_index do |answer, idx|%>
<%= render '/partials/question.*', question: answer %> <% if answer[:vraag] %>
</div> <div id='<%= number+"#"+idx.to_s %>' style="display:none;">
<% end %> <%= render '/partials/question.*', question: answer, number: number+"."+idx.to_s %>
</div>
<% end %> <% end %>
<% end %> <% end %>
<%end%>
<script> <script>
<% if question[:type] == "specialCase1" %> <% if question[:type] == "specialCase1" %>
(function setupPostCodeField() { (function setupPostCodeField() {
@ -67,40 +71,32 @@
const sub_questions = {}; const sub_questions = {};
// Create list of all student organisations this answer entails // Create list of all student organisations this answer entails
const question_results = {}; const question_results = {};
<% question[:antwoorden].each_with_index do |answer, idx| %>
<% for answer in question[:antwoorden] %> <% if answer[:vraag] %>sub_questions['<%= number+"#"+idx.to_s %>'] = document.getElementById('<%= number+"#"+idx.to_s %>');<% end %>
<% if answer[:vraag] %> question_results['<%= number+"#"+idx.to_s %>'] = <%= (answer[:verenigingen] || []).map { |vereniging| vereniging[:naam] }.to_a %>;
sub_questions['<%= html_escape(answer[:tekst] + answer[:vraag]) %>'] = document.getElementById('<%= html_escape(answer[:tekst] + answer[:vraag]) %>');
<% end %>
question_results['<%= answer[:tekst] + (answer[:vraag] || "") %>'] = [];
<% if answer[:verenigingen] %>
<% for vereniging in answer[:verenigingen] %>
question_results['<%= answer[:tekst] + (answer[:vraag] || "") %>'].push(
"<%= vereniging[:naam] %>"
);
<% end %>
<% end %>
<% end %> <% end %>
document.getElementById("<%= (question[:tekst] || "") + (question[:vraag] || "") %>_option").addEventListener('change', (event) => { const formElement = document.getElementById("<%= number %>_form");
formElement.addEventListener('change', () => {
for (let la of last_answered) { for (let la of last_answered) {
if (sub_questions[la]) { if (sub_questions[la]) {
sub_questions[la].style.display = "none"; sub_questions[la].style.display = "none";
// sub is the select dom element, that listens for changes // sub is the select dom element, that listens for changes
// When a parent is changed, it should notify it's last_answered child that it is changed // When a parent is changed, it should notify it's last_answered child that it is changed
const sub = document.getElementById(la+"_option"); // Notice the replace, we move from an answer to a subquestion
const sub = document.getElementById(la.replace("#", ".")+"_form");
if (sub) { if (sub) {
sub.value = null;
sub.dispatchEvent(new event.constructor(event.type, event)); [...sub.elements].filter(e => e.nodeName == "INPUT").forEach(e => e.checked = false);
sub.dispatchEvent(new Event('change'));
} }
} }
} }
const selected = getSelectValues(event.target); const selected = [...formElement.elements].filter(e => e.nodeName == "INPUT").filter(e => e.checked).map(e => e.value || e.text);
console.log(event.target);
// Update results // Update results
save_answers(selected.flatMap(e => question_results[e] || [])); save_answers(selected.flatMap(e => question_results[e] || []));
@ -113,7 +109,7 @@
for (let quest of quests) { for (let quest of quests) {
delete quest.style.removeProperty("display"); delete quest.style.removeProperty("display");
} }
}); }, false);
} }
<%end%> <%end%>
</script> </script>

View file

@ -1,7 +1,7 @@
<div style="margin: auto; width: 70%;"> <div style="margin: auto; width: 70%;">
<% @items.find_all("/quiz/*").each_with_index do |quest, i| %> <% @items.find_all("/quiz/*").each_with_index do |quest, i| %>
<div id="question_<%=i+1%>" class="question hidden"> <div id="question_<%=i+1%>" class="question hidden">
<%= render '/partials/question.*', question: quest %> <%= render '/partials/question.*', question: quest, number: i.to_s %>
</div> </div>
<% end %> <% end %>
</div> </div>