Streamlined post form handlers for admin console

This commit is contained in:
2021-12-01 08:26:08 +00:00
parent f0ba8777e3
commit fe61456922
18 changed files with 158 additions and 601 deletions

View File

@ -28,16 +28,16 @@ $("#btn-toggle-navigator").click(function(event){
$(".navigator-text").fadeIn();
$(".review-text").fadeOut();
toggle_navigator = false;
$quiz_navigator.focus();
$(window).scrollTop(0);
} else {
$quiz_navigator.fadeOut();
if (toggle_settings) {
$quiz_settings.fadeIn();
$quiz_settings.focus()
$(window).scrollTop(0);
toggle_settings = false;
} else {
$quiz_render.fadeIn();
$question_title.focus();
$(window).scrollTop(0);
}
}
event.preventDefault();
@ -51,17 +51,17 @@ $("#btn-toggle-settings").click(function(event){
}
$quiz_render.fadeOut();
$quiz_settings.fadeIn();
$quiz_settings.focus()
$(window).scrollTop(0);
toggle_settings = false;
} else {
$quiz_settings.fadeOut();
if (toggle_navigator) {
$quiz_navigator.fadeIn();
toggle_navigator = false;
$quiz_navigator.focus();
$(window).scrollTop(0);
} else {
$quiz_render.fadeIn();
$question_title.focus();
$(window).scrollTop(0);
}
}
event.preventDefault();
@ -71,7 +71,7 @@ $(".btn-quiz-return").click(function(event){
$quiz_navigator.fadeOut();
$quiz_settings.fadeOut();
$quiz_render.fadeIn();
$question_title.focus();
$(window).scrollTop(0);
toggle_settings = false;
toggle_navigator = false;
event.preventDefault();
@ -85,9 +85,10 @@ $("#navigator-container").on("click", ".q-navigator-button", function(event){
check_answered();
update_navigator();
current_question = parseInt($(this).attr("name"));
$quiz_navigator.fadeOut();
$quiz_render.fadeIn();
$question_title.focus();
$quiz_navigator.fadeOut();
$(window).scrollTop(0);
toggle_navigator = false;
toggle_settings = false;
render_question();
@ -164,25 +165,7 @@ $("#btn-start-quiz").click(function(event){
}
},
error: function(response) {
if (typeof response.responseJSON.error === 'string' || response.responseJSON.error instanceof String) {
alert.innerHTML = alert.innerHTML + `
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<i class="bi bi-exclamation-triangle-fill" title="Danger"></i>
${response.responseJSON.error}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
`;
} else if (response.responseJSON.error instanceof Array) {
for (var i = 0; i < response.responseJSON.error.length; i ++) {
alert.innerHTML = alert.innerHTML + `
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<i class="bi bi-exclamation-triangle-fill" title="Danger"></i>
${response.responseJSON.error[i]}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
`;
}
}
error_response(response);
}
});
@ -209,7 +192,7 @@ $("#q-review-answers").click(function(event){
$(".navigator-text").fadeOut();
$(".review-text").fadeIn();
toggle_navigator = false;
$quiz_navigator.focus();
$(window).scrollTop(0);
} else {
$quiz_navigator.fadeOut();
if (toggle_settings) {
@ -238,25 +221,7 @@ $(".quiz-button-submit").click(function(event){
window.location.href = `/result/`;
},
error: function(response) {
if (typeof response.responseJSON.error === 'string' || response.responseJSON.error instanceof String) {
alert.innerHTML = alert.innerHTML + `
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<i class="bi bi-exclamation-triangle-fill" title="Danger"></i>
${response.responseJSON.error}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
`;
} else if (response.responseJSON.error instanceof Array) {
for (var i = 0; i < response.responseJSON.error.length; i ++) {
alert.innerHTML = alert.innerHTML + `
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<i class="bi bi-exclamation-triangle-fill" title="Danger"></i>
${response.responseJSON.error[i]}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
`;
}
}
error_response(response);
}
});
@ -392,6 +357,7 @@ function render_question() {
}
$question_options.html(options_output);
$question_title.focus();
$(window).scrollTop(0);
}
function check_answered() {

View File

@ -4,7 +4,7 @@ $(document).ready(function() {
});
$('.test-code-input').keyup(function() {
var input = $(this).val().split("-").join("").split("—").join(""); // remove hyphens and mdashes
var input = $(this).val().split("-").join("").split("—").join("");
if (input.length > 0) {
input = input.match(new RegExp('.{1,4}', 'g')).join("—");
}
@ -15,11 +15,8 @@ $(document).ready(function() {
$('form[name=form-quiz-start]').submit(function(event) {
var $form = $(this);
var alert = document.getElementById('alert-box');
var data = $form.serialize();
alert.innerHTML = ''
$.ajax({
url: window.location.pathname,
type: 'POST',
@ -31,31 +28,39 @@ $('form[name=form-quiz-start]').submit(function(event) {
window.location.href = `/test/`;
},
error: function(response) {
if (typeof response.responseJSON.error === 'string' || response.responseJSON.error instanceof String) {
alert.innerHTML = alert.innerHTML + `
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<i class="bi bi-exclamation-triangle-fill" title="Danger"></i>
${response.responseJSON.error}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
`;
} else if (response.responseJSON.error instanceof Array) {
for (var i = 0; i < response.responseJSON.error.length; i ++) {
alert.innerHTML = alert.innerHTML + `
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<i class="bi bi-exclamation-triangle-fill" title="Danger"></i>
${response.responseJSON.error[i]}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
`;
}
}
error_response(response);
}
});
event.preventDefault();
});
function error_response(response) {
var alert = $("#alert-box");
alert.html('');
if (typeof response.responseJSON.error === 'string' || response.responseJSON.error instanceof String) {
alert.html(`
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<i class="bi bi-exclamation-triangle-fill" title="Danger"></i>
${response.responseJSON.error}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
`);
} else if (response.responseJSON.error instanceof Array) {
for (var i = 0; i < response.responseJSON.error.length; i ++) {
alert.html(`
<div class="alert alert-danger alert-dismissible fade show" role="alert">
<i class="bi bi-exclamation-triangle-fill" title="Danger"></i>
${response.responseJSON.error[i]}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
`);
}
}
}
// Dismiss Cookie Alert
$('#dismiss-cookie-alert').click(function(event){

View File

@ -59,10 +59,10 @@ def start():
test = db.tests.find_one({'test_code': test_code})
if not test:
return jsonify({'error': 'The exam code you entered is invalid.'}), 400
if test['expiry_date'] < datetime.utcnow():
return jsonify({'error': f'The exam code you entered expired on {test["expiry_date"].strftime("%d %b %Y")}.'}), 400
if test['expiry_date'] + timedelta(days=1) < datetime.utcnow():
return jsonify({'error': f'The exam code you entered expired on {test["expiry_date"].strftime("%d %b %Y")} UTC.'}), 400
if test['start_date'] > datetime.utcnow():
return jsonify({'error': f'The exam has not yet opened. Your exam code will be valid from {test["start_date"].strftime("%d %b %Y %H:%M")}.'}), 400
return jsonify({'error': f'The exam has not yet opened. Your exam code will be valid from {test["start_date"].strftime("%d %b %Y %H:%M")} UTC.'}), 400
entry = {
'_id': uuid4().hex,
'name': encrypt(name),