add support for seperate qusetion requests

This commit is contained in:
Ross
2021-02-28 15:17:39 +00:00
parent d066d283a4
commit b2c4f2b33c
2 changed files with 89 additions and 13 deletions
+28 -1
View File
@@ -1,3 +1,4 @@
import * as helper from "./helpers.js";
/**
* Submits answers
*/
@@ -67,7 +68,7 @@ export function postAnswers(ans) {
alert(`${data.question_count} answers sucessfully submitted.`);
}
} else {
submissionError(data, ans);
submissionError(data, ans);
}
})
.fail((e) => {
@@ -108,3 +109,29 @@ function submissionError(data, ans) {
"z-index": 5000,
});
}
// TODO: async request
export function getQuestion(url, question_number, question_total) {
console.log("Downloading ", url, question_number, question_total);
return $.ajax({
dataType: "json",
url: url,
progress: function (e) {
console.log(e);
if (e.lengthComputable) {
var completedPercentage = Math.round((e.loaded * 100) / e.total);
$("#progress").html(
`Downloading [${question_number}/${question_total}]${completedPercentage}%<br/>${helper.formatBytes(
e.total
)}`
);
}
},
//success: function (data) {
// strReturn = data;
//},
//async: false,
});
}
+61 -12
View File
@@ -53,13 +53,18 @@ function retrievePacketList() {
url = window.config.exam_query_url;
}
$.ajax({dataType: "json", cache: false, url: url, success : function (data) {
if (data.hasOwnProperty("exams")) {
loadExamList(data);
} else {
loadPacketList(data);
}
},})
$.ajax({
dataType: "json",
cache: false,
url: url,
success: function (data) {
if (data.hasOwnProperty("exams")) {
loadExamList(data);
} else {
loadPacketList(data);
}
},
})
.done(function () {})
.fail(function () {
$.getJSON("packets/packets.json", function (data) {
@@ -133,12 +138,14 @@ async function loadExamList(data) {
if ($(`#packet-list .${exam.type}`).length) {
list = $(`#packet-list .${exam.type}`);
} else {
list = $("#packet-list").append(`<div class='packet-list ${exam.type}'><span class='packet-list-title'>${exam.type}</span><br/></div>`);
list = $("#packet-list").append(
`<div class='packet-list ${exam.type}'><span class='packet-list-title'>${exam.type}</span><br/></div>`
);
}
} else {
list = $("#packet-list");
}
list.append(
$(`<div class='packet-button${c}' title='Load packet'></div>`)
.text(name)
@@ -204,7 +211,9 @@ async function loadPacketList(data) {
if ($(`#packet-list .${packet.type}`)) {
list = $(`#packet-list .${packet.type}`);
} else {
list = $(`<div class='packet-list ${packet.type}'>${packet.type}<br/></div>`);
list = $(
`<div class='packet-list ${packet.type}'>${packet.type}<br/></div>`
);
$("#packet-list").append(list);
}
} else {
@@ -259,7 +268,8 @@ function loadPacketFromAjax(path) {
},
})
.done(function (data) {
setUpPacket(data, path.split("/").pop());
//setUpPacket(data, path.split("/").pop());
setUpPacket(data, path);
$("#options-panel").hide();
})
.fail(function () {
@@ -393,7 +403,8 @@ window.db = db;
* Will then call setUpQuestions() to load the packet
* @param {JSON} data
*/
function setUpPacket(data, packet_name) {
function setUpPacket(data, path) {
let packet_name = path;
// Load the exam name from the json packet (if it exists)
if (data.hasOwnProperty("exam_name")) {
packet_name = data.exam_name;
@@ -427,6 +438,44 @@ function setUpPacket(data, packet_name) {
window.exam_time = data.exam_time;
}
// If the question_requests is set we need to do a request for each question
if (data.hasOwnProperty("question_requests") && data["question_requests"]) {
const question_total = Object.keys(data["questions"]).length;
let question_number = 0;
var requests = [];
var request_numbers = [];
// For loop to generate requests
for (const n in data["questions"]) {
const question_url = `${path}/${n}`;
question_number++;
console.log("Creating ", question_url, question_number, question_total);
//$("#progress").html(`Downloading [${question_number}/${question_total}]`);
const request = interact.getQuestion(
question_url,
question_number,
question_total
);
requests.push(request)
request_numbers.push(n)
//data["questions"][n] = question_json;
}
$.when.apply($,requests).then(function(){
console.log(arguments); //array of responses [0][data, status, xhrObj],[1][data, status, xhrObj]...
})
} else {
// Just carry on loading
loadSession();
}
}
function loadSession() {
// Either continue session or create a new one
db.session
// .where("status")