ska-referee-test/ref-test/quiz/static/js/quiz.js

302 lines
8.9 KiB
JavaScript
Raw Normal View History

2021-11-30 03:11:28 +00:00
// Click Listeners
$("input[name='font-select']").change(function(){
let $choice = $(this).val();
set_font($choice);
});
$("input[name='font-size']").change(function(){
let $choice = $(this).val();
set_font_size($choice);
});
$("input[name='bg-select']").change(function(){
let $choice = $(this).val();
set_bg_colour($choice);
});
$("#btn-toggle-navigator").click(function(event){
if ($quiz_navigator.is(":hidden")) {
if ($quiz_settings.is(":visible")) {
toggle_settings = true;
$quiz_settings.hide();
}
$quiz_render.hide();
$quiz_navigator.show();
toggle_navigator = false;
} else {
$quiz_navigator.hide();
if (toggle_settings) {
$quiz_settings.show();
toggle_settings = false;
} else {
$quiz_render.show();
}
}
event.preventDefault();
});
$("#btn-toggle-settings").click(function(event){
if (($quiz_settings).is(":hidden")) {
if ($quiz_navigator.is(":visible")) {
toggle_navigator = true;
$quiz_navigator.hide();
}
$quiz_render.hide();
$quiz_settings.show();
toggle_settings = false;
} else {
$quiz_settings.hide();
if (toggle_navigator) {
$quiz_navigator.show();
toggle_navigator = false;
} else {
$quiz_render.show();
}
}
event.preventDefault();
});
$(".btn-dummy").click(function(event){
event.preventDefault();
});
$(".q-question-nav").click(function(event){
check_answered();
if ($(this).attr("id") == "q-nav-next") {
if (current_question < questions.length) {
current_question ++;
}
} else if ($(this).attr("id") == "q-nav-prev") {
if (current_question > 0) {
current_question --;
}
}
render_question();
check_flag();
event.preventDefault();
});
$("#q-nav-flag").click(function(event){
if (question_status[current_question] != 1) {
question_status[current_question] = 1;
$(this).removeClass().addClass("btn btn-warning");
} else {
question_status[current_question] = 0;
$(this).removeClass().addClass("btn btn-secondary");
}
event.preventDefault();
});
$("#btn-start-quiz").click(function(event){
$(this).hide();
$(".btn-quiz-return").show();
$(".quiz-console").show();
$("#quiz-settings").hide();
$("#quiz-navigator").hide();
$(".quiz-start-text").hide();
$.ajax({
url: `/api/questions/`,
type: 'POST',
data: JSON.stringify({'_id': _id}),
contentType: "application/json",
success: function(response) {
time_limit_data = response.time_limit;
questions = response.questions;
total_questions = questions.length;
window.localStorage.setItem('questions', JSON.stringify(questions));
render_question();
check_flag();
},
error: function(response) {
console.log(response);
}
});
event.preventDefault();
});
// Functions
function set_font(value = 'osdefault') {
let font_styles = ['arial', 'comicsans', 'opendyslexic', 'tahoma', 'verdana']
for (let i = 0; i < font_styles.length; i ++) {
if (font_styles[i] != value) {
$("body").removeClass( `q-f-${font_styles[i]}` );
};
};
if (value != 'osdefault') {
$("body").addClass(`q-f-${value}`);
};
display_settings['font-select'] = value;
window.localStorage.setItem('display_settings', JSON.stringify(display_settings));
$('input[name="font-select"][value="' + value + '"]').prop('checked', true);
}
function set_font_size(value = '14pt') {
let font_sizes = ['12pt', '16pt', '18pt']
for (let i = 0; i < font_sizes.length; i ++) {
if (font_sizes[i] != value) {
$("body").removeClass( `q-f-${font_sizes[i]}` );
};
};
if (value != '14pt') {
$("body").addClass(`q-f-${value}`);
};
display_settings['font-size'] = value;
window.localStorage.setItem('display_settings', JSON.stringify(display_settings));
$('input[name="font-size"][value="' + value + '"]').prop('checked', true);
}
function set_bg_colour(value = 'bg-light') {
let backgrounds = ['bg-light', 'q-bg-light-1', 'q-bg-light-2', 'alert-primary', 'alert-secondary', 'alert-dark', 'bg-dark']
for (let i = 0; i < backgrounds.length; i ++) {
if (backgrounds[i] != value) {
$("body").removeClass(backgrounds[i]);
if (backgrounds[i] == 'bg-dark') {
$("body").removeClass('text-light');
};
if (backgrounds[i] == 'alert-primary' || backgrounds[i] == 'alert-secondary' || backgrounds[i] == 'alert-dark') {
$("body").removeClass('text-dark');
};
};
};
$("body").addClass(value);
if (value == 'bg-dark') {
$("body").addClass('text-light');
};
if (value == 'alert-primary' || value == 'alert-secondary' || value == 'alert-dark') {
$("body").addClass('text-dark');
};
display_settings['bg-select'] = value;
window.localStorage.setItem('display_settings', JSON.stringify(display_settings));
$('input[name="bg-select"][value="' + value + '"]').prop('checked', true);
}
function get_settings_from_storage() {
let display_settings = window.localStorage.getItem('display_settings')
if (display_settings != null) {
return JSON.parse(display_settings);
};
return {
'font-select': 'osdefault',
'font-size': '14pt',
'bg-select': 'bg-light'
}
}
function apply_settings(settings) {
set_font(settings['font-select']);
set_font_size(settings['font-size']);
set_bg_colour(settings['bg-select']);
}
function render_question() {
if (current_question == 0) {
$nav_prev.addClass('disabled');
}
if (current_question == questions.length - 1) {
$nav_next.addClass('disabled');
}
if ($nav_prev.hasClass('disabled') && current_question > 0) {
$nav_prev.removeClass('disabled');
}
if ($nav_next.hasClass('disabled') && current_question < questions.length - 1) {
$nav_next.removeClass('disabled');
}
var question = questions[current_question];
let header_text = question.question_header;
var block_length = 0;
if ('block_length' in question) {
block_length = question['block_length'];
};
var block_q_no = 0;
if ('block_q_no' in question) {
block_q_no = question['block_q_no'];
}
header_text = header_text.replace('<block_remaining>', (block_length - block_q_no).toString());
$question_header.html(header_text);
$question_text.html(question.text);
$question_title.html(`Question ${current_question + 1} of ${ questions.length }.`);
var options = question.options;
var options_output = '';
for (let i = 0; i < options.length; i ++) {
options_output += `<div class="form-check">
<input type="radio" class="form-check-input quiz-option" id="q${current_question}-${i}" name="${question.q_no}" value="${options[i]}">
<label for="q${current_question}-${i}" class="form-check-label">${options[i]}</label>
</div>`;
}
$question_options.html(options_output);
}
function check_answered() {
var question = questions[current_question];
var name = question.q_no;
if (!$(`input[name='${name}']:checked`).val() && question_status[current_question] == 0) {
question_status[current_question] = -1;
}
}
function check_flag() {
if (!(current_question in question_status)) {
question_status[current_question] = 0;
}
switch (question_status[current_question]) {
case -1:
$nav_flag.removeClass().addClass('btn btn-danger');
break;
case 1:
$nav_flag.removeClass().addClass('btn btn-warning');
break;
default:
$nav_flag.removeClass().addClass('btn btn-secondary');
}
}
// Variable Definitions
const _id = window.localStorage.getItem('_id');
var current_question = 0;
var total_questions = 0;
var question_status = {};
var answers = {};
var questions = []
var time_limit_data = ''
var display_settings = get_settings_from_storage();
const $quiz_settings = $("#quiz-settings");
const $quiz_navigator = $("#quiz-navigator");
const $quiz_render = $("#quiz-render");
const $nav_flag = $("#q-nav-flag");
const $nav_next = $("#q-nav-next");
const $nav_prev = $("#q-nav-prev");
var toggle_settings = false;
var toggle_navigator = false;
const $question_title = $("#quiz-question-title");
const $question_header = $("#quiz-question-header");
const $question_text = $("#quiz-question-text");
const $question_options = $("#quiz-question-options");
// Execution on Load
apply_settings(display_settings);
// TODO Build navigator
// TODO Navigator Link button behaviour
// TODO Resume Exam button
// TODO Load state from storage
// TODO Answer Registry