Skip to content

Commit

Permalink
feat: randomize question order between builds
Browse files Browse the repository at this point in the history
- set up data needed to intercept links
- filter out irrelevant questions on category click
- randomize and store the order of questions
- clear question data on session end or landing on the home page
- stub out script for updating pagination links
- set data attr on pagination links for easy selection
- update pagination links on page load
- provide span to hook into for replacing the question number
- update the displayed question number
  • Loading branch information
dustin-jw committed Sep 1, 2023
1 parent c0124d1 commit 6ef27f7
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 41 deletions.
12 changes: 9 additions & 3 deletions pages/all/flash-cards/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ eleventyExcludeFromCollections: true
<article>
<header class="cmp-game-header">
<h1 class="cmp-game-header__title">All Categories</h1>
<h2 class="cmp-game-header__question-count">Question {{ pagination.pageNumber + 1 }} of {{ questionTotal }}</h2>
<h2 class="cmp-game-header__question-count">Question <span data-question-number>{{ pagination.pageNumber + 1 }}</span> of {{ questionTotal }}</h2>
</header>

{% from 'macros/flash-card.njk' import flashCard %}
{{ flashCard(question) }}

<div class="cmp-pagination">
{% if questionTotal > pagination.pageNumber + 1 %}
<a href="/all/flash-cards/{{ pagination.pageNumber + 2 }}/">
<a href="/all/flash-cards/{{ pagination.pageNumber + 2 }}/" data-next-link>
Next Question
</a>
{% else %}
<span></span>
{% endif %}

{% if pagination.pageNumber + 1 > 1 %}
<a href="/all/flash-cards/{{ pagination.pageNumber }}/">
<a href="/all/flash-cards/{{ pagination.pageNumber }}/" data-prev-link>
Previous Question
</a>
{% else %}
Expand All @@ -49,3 +49,9 @@ eleventyExcludeFromCollections: true
</article>
</div>
{% endblock %}

{% block scripts %}
<script>
{% include 'js/update-pagination-links.js' %}
</script>
{% endblock %}
7 changes: 4 additions & 3 deletions pages/all/multiple-choice/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ eleventyExcludeFromCollections: true
<article>
<header class="cmp-game-header">
<h1 class="cmp-game-header__title">All Categories</h1>
<h2 class="cmp-game-header__question-count">Question {{ pagination.pageNumber + 1 }} of {{ questionTotal }}</h2>
<h2 class="cmp-game-header__question-count">Question <span data-question-number>{{ pagination.pageNumber + 1 }}</span> of {{ questionTotal }}</h2>
</header>

{% from 'macros/multiple-choice.njk' import multipleChoice %}
{{ multipleChoice(question) }}

<div class="cmp-pagination">
{% if questionTotal > pagination.pageNumber + 1 %}
<a href="/all/multiple-choice/{{ pagination.pageNumber + 2 }}/">
<a href="/all/multiple-choice/{{ pagination.pageNumber + 2 }}/" data-next-link>
Next Question
</a>
{% else %}
<span></span>
{% endif %}

{% if pagination.pageNumber + 1 > 1 %}
<a href="/all/multiple-choice/{{ pagination.pageNumber }}/">
<a href="/all/multiple-choice/{{ pagination.pageNumber }}/" data-prev-link>
Previous Question
</a>
{% else %}
Expand All @@ -53,5 +53,6 @@ eleventyExcludeFromCollections: true
{% block scripts %}
<script>
{% include 'js/multiple-choice.js' %}
{% include 'js/update-pagination-links.js' %}
</script>
{% endblock %}
7 changes: 4 additions & 3 deletions pages/all/short-answer/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ eleventyExcludeFromCollections: true
<article>
<header class="cmp-game-header">
<h1 class="cmp-game-header__title">All Categories</h1>
<h2 class="cmp-game-header__question-count">Question {{ pagination.pageNumber + 1 }} of {{ questionTotal }}</h2>
<h2 class="cmp-game-header__question-count">Question <span data-question-number>{{ pagination.pageNumber + 1 }}</span> of {{ questionTotal }}</h2>
</header>

{% from 'macros/short-answer.njk' import shortAnswer %}
{{ shortAnswer(question) }}

<div class="cmp-pagination">
{% if questionTotal > pagination.pageNumber + 1 %}
<a href="/all/short-answer/{{ pagination.pageNumber + 2 }}/">
<a href="/all/short-answer/{{ pagination.pageNumber + 2 }}/" data-next-link>
Next Question
</a>
{% else %}
<span></span>
{% endif %}

{% if pagination.pageNumber + 1 > 1 %}
<a href="/all/short-answer/{{ pagination.pageNumber }}/">
<a href="/all/short-answer/{{ pagination.pageNumber }}/" data-prev-link>
Previous Question
</a>
{% else %}
Expand All @@ -53,5 +53,6 @@ eleventyExcludeFromCollections: true
{% block scripts %}
<script>
{% include 'js/short-answer.js' %}
{% include 'js/update-pagination-links.js' %}
</script>
{% endblock %}
12 changes: 9 additions & 3 deletions pages/flash-cards/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@ eleventyExcludeFromCollections: true
<article>
<header class="cmp-game-header">
<h1 class="cmp-game-header__title">Category: {{ questionGroup.category }}</h1>
<h2 class="cmp-game-header__question-count">Question {{ questionGroup.pageNumber }} of {{ questionGroup.questionTotal }}</h2>
<h2 class="cmp-game-header__question-count">Question <span data-question-number>{{ questionGroup.pageNumber }}</span> of {{ questionGroup.questionTotal }}</h2>
</header>
{% from 'macros/flash-card.njk' import flashCard %}
{{ flashCard(questionGroup.question) }}

<div class="cmp-pagination">
{% if questionGroup.questionTotal > questionGroup.pageNumber %}
<a href="/flash-cards/{{ questionGroup.tagName }}/{{ questionGroup.pageNumber + 1 }}/">
<a href="/flash-cards/{{ questionGroup.tagName }}/{{ questionGroup.pageNumber + 1 }}/" data-next-link>
Next Question
</a>
{% else %}
<span></span>
{% endif %}

{% if questionGroup.pageNumber > 1 %}
<a href="/flash-cards/{{ questionGroup.tagName }}/{{ questionGroup.pageNumber - 1 }}/">
<a href="/flash-cards/{{ questionGroup.tagName }}/{{ questionGroup.pageNumber - 1 }}/" data-prev-link>
Previous Question
</a>
{% else %}
Expand All @@ -47,3 +47,9 @@ eleventyExcludeFromCollections: true
</article>
</div>
{% endblock %}

{% block scripts %}
<script>
{% include 'js/update-pagination-links.js' %}
</script>
{% endblock %}
51 changes: 30 additions & 21 deletions pages/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,41 @@
title: Home
seo_title: "Trivia11y: A Web Accessibility Quiz by Sparkbox"
description: A quiz game and study tool to help those at all skill levels to learn and test their accessibility knowledge. Flash cards, multiple choice, and short answer questions are available in multiple categories.
layout: default
---

<div class="obj-width-limiter">
<h1>Test Your Accessibility Knowledge</h1>
{% extends 'layout.njk' %}

<p class="util-limit-width">
Accessibility is important, but becoming an expert means taking the time to understand and memorize priorities, regulations, requirements, and techniques. So how can you determine whether you're up to speed? With games, of course!
</p>
{% block content%}
<div class="obj-width-limiter">
<h1>Test Your Accessibility Knowledge</h1>

<h2>Game Types</h2>
<p class="util-limit-width">
Accessibility is important, but becoming an expert means taking the time to understand and memorize priorities, regulations, requirements, and techniques. So how can you determine whether you're up to speed? With games, of course!
</p>

<p class="util-limit-width">
Choose a game format and then select a category to help you test your knowledge: Flash Cards for reviewing what you know, Short Answer questions for confirming you actually know your stuff, and Multiple Choice for simulating test conditions.
</p>
<h2>Game Types</h2>

{% from 'macros/categories.njk' import categories %}
<div class="obj-game-type">
<div class="obj-game-type__group">
{{ categories('flash-cards', 'Flash Cards', questionData.flashCardCategories) }}
</div>
<div class="obj-game-type__group">
{{ categories('short-answer', 'Short Answer', questionData.flashCardCategories) }}
</div>
<div class="obj-game-type__group">
{{ categories('multiple-choice', 'Multiple Choice', questionData.categories) }}
<p class="util-limit-width">
Choose a game format and then select a category to help you test your knowledge: Flash Cards for reviewing what you know, Short Answer questions for confirming you actually know your stuff, and Multiple Choice for simulating test conditions.
</p>

{% from 'macros/categories.njk' import categories %}
<div class="obj-game-type">
<div class="obj-game-type__group">
{{ categories('flash-cards', 'Flash Cards', questionData.flashCardCategories) }}
</div>
<div class="obj-game-type__group">
{{ categories('short-answer', 'Short Answer', questionData.flashCardCategories) }}
</div>
<div class="obj-game-type__group">
{{ categories('multiple-choice', 'Multiple Choice', questionData.categories) }}
</div>
</div>
</div>
</div>
{% endblock %}

{% block scripts %}
<script>
{% include 'js/intercept-category-navigation.js' %}
</script>
{% endblock %}
7 changes: 4 additions & 3 deletions pages/multiple-choice/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,23 @@ eleventyExcludeFromCollections: true
<article>
<header class="cmp-game-header">
<h1 class="cmp-game-header__title">Category: {{ questionGroup.category }}</h1>
<h2 class="cmp-game-header__question-count">Question {{ questionGroup.pageNumber }} of {{ questionGroup.questionTotal }}</h2>
<h2 class="cmp-game-header__question-count">Question <span data-question-number>{{ questionGroup.pageNumber }}</span> of {{ questionGroup.questionTotal }}</h2>
</header>

{% from 'macros/multiple-choice.njk' import multipleChoice %}
{{ multipleChoice(questionGroup.question) }}

<div class="cmp-pagination">
{% if questionGroup.questionTotal > questionGroup.pageNumber %}
<a href="/multiple-choice/{{ questionGroup.tagName }}/{{ questionGroup.pageNumber + 1 }}/">
<a href="/multiple-choice/{{ questionGroup.tagName }}/{{ questionGroup.pageNumber + 1 }}/" data-next-link>
Next Question
</a>
{% else %}
<span></span>
{% endif %}

{% if questionGroup.pageNumber > 1 %}
<a href="/multiple-choice/{{ questionGroup.tagName }}/{{ questionGroup.pageNumber - 1 }}/">
<a href="/multiple-choice/{{ questionGroup.tagName }}/{{ questionGroup.pageNumber - 1 }}/" data-prev-link>
Previous Question
</a>
{% else %}
Expand All @@ -53,5 +53,6 @@ eleventyExcludeFromCollections: true
{% block scripts %}
<script>
{% include 'js/multiple-choice.js' %}
{% include 'js/update-pagination-links.js' %}
</script>
{% endblock %}
6 changes: 6 additions & 0 deletions pages/questions.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
permalink: /questions.json
eleventyExcludeFromCollections: true
---

<%- JSON.stringify(questionData.questions) -%>
7 changes: 4 additions & 3 deletions pages/short-answer/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@ eleventyExcludeFromCollections: true
<article>
<header class="cmp-game-header">
<h1 class="cmp-game-header__title">Category: {{ questionGroup.category }}</h1>
<h2 class="cmp-game-header__question-count">Question {{ questionGroup.pageNumber }} of {{ questionGroup.questionTotal }}</h2>
<h2 class="cmp-game-header__question-count">Question <span data-question-number>{{ questionGroup.pageNumber }}</span> of {{ questionGroup.questionTotal }}</h2>
</header>

{% from 'macros/short-answer.njk' import shortAnswer %}
{{ shortAnswer(questionGroup.question) }}

<div class="cmp-pagination">
{% if questionGroup.questionTotal > questionGroup.pageNumber %}
<a href="/short-answer/{{ questionGroup.tagName }}/{{ questionGroup.pageNumber + 1 }}/">
<a href="/short-answer/{{ questionGroup.tagName }}/{{ questionGroup.pageNumber + 1 }}/" data-next-link>
Next Question
</a>
{% else %}
<span></span>
{% endif %}

{% if questionGroup.pageNumber > 1 %}
<a href="/short-answer/{{ questionGroup.tagName }}/{{ questionGroup.pageNumber - 1 }}/">
<a href="/short-answer/{{ questionGroup.tagName }}/{{ questionGroup.pageNumber - 1 }}/" data-prev-link>
Previous Question
</a>
{% else %}
Expand All @@ -52,5 +52,6 @@ eleventyExcludeFromCollections: true
{% block scripts %}
<script>
{% include 'js/short-answer.js' %}
{% include 'js/update-pagination-links.js' %}
</script>
{% endblock %}
50 changes: 50 additions & 0 deletions src/js/intercept-category-navigation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const getQuestions = async () => {
const response = await fetch('/questions.json');
const questions = await response.json();

return questions;
};

const getRelevantQuestions = (questions, category, isMultipleChoice) => {
return questions.filter((question) => {
if (!isMultipleChoice && question['Multiple Choice Only']) {
return false;
}

if (category !== 'All' && !question.Tags.some((tag) => tag === category)) {
return false;
}

return true;
});
};

(async () => {
sessionStorage.removeItem('questions');
sessionStorage.removeItem('questionStatus');
const questions = await getQuestions();
const links = document.querySelectorAll('.cmp-categories__link');

links.forEach((link) => {
link.addEventListener('click', (event) => {
event.preventDefault();

const relevantQuestions = getRelevantQuestions(
questions,
link.dataset.category,
link.dataset.type === 'Multiple Choice'
);

const questionOrder = Array.from(
{ length: relevantQuestions.length },
(_item, index) => `${link.dataset.baseLink}${index + 1}/`
).sort(() => (Math.random() > 0.5 ? 1 : -1));

sessionStorage.setItem('questions', JSON.stringify(questionOrder));

sessionStorage.setItem('currentQuestionIndex', '0');

window.location.href = questionOrder[0];
});
});
})();
Loading

0 comments on commit 6ef27f7

Please sign in to comment.