import * as helper from "./helpers.js"; // cid = null; window.cid = 1234; window.eid = 5678; window.exam_mode = false; window.packet_list = []; window.questions = null; window.question_order = []; window.number_of_questions = null; window.question_type = null; window.loaded_question = null; window.review = false; window.allow_self_marking = true; window.dfile = null; retrievePacketList(); /** * Retrieves a list of available packets via JSON ajax requests * Will initially try /packets/ (for example if index.php generates the list) * and then fallback to packets.json */ function retrievePacketList() { $.getJSON("packets/", function (data) { loadPacketList(data); }) .done(function () {}) .fail(function () { $.getJSON("packets/packets.json", function (data) { loadPacketList(data); }).fail(function () { console.log("No packet list available"); showLoginDialog(); }); }) .always(function () {}); // setUpPacket(questions); } /** * Generate a list of the packets that are available to be loaded * * @param {JSON} data - json containing available packets */ function loadPacketList(data) { window.packet_list = data.packets; window.packet_list.forEach(function (packet) { $("#packet-list").append( $("
").click(function () { loadPacketFromAjax("packets/" + packet); }) ); }); $("#options-panel").show(); } /** * Loads the requisite packet into the quiz system * @param {string} path - relative path to the packet json file */ function loadPacketFromAjax(path) { console.log("loading packet from " + path); $.getJSON(path, function (data) { setUpPacket(data); $("#options-panel").hide(); }) .done(function () {}) .fail(function () { console.log("Unable to load packet at: " + path); }); } /** * Build the currently loaded quiz */ function setUpQuestions() { if (window.questions == undefined) { return; } // Set an order for the questions window.question_order = []; Object.keys(window.questions).forEach(function (e) { window.question_order.push(e); // Make sure answers are arrays if (!Array.isArray(window.questions[e]["answers"])) { window.questions[e]["answers"] = [window.questions[e]["answers"]]; } }); console.log("pre", window.question_order); window.question_order = helper.shuffleArray(window.question_order); console.log("post", window.question_order); window.number_of_questions = Object.keys(window.questions).length; window.review = false; console.log(window.question_order); // Horrible way to get type of questions // We assume they are all of the same type.... if (window.question_type == null) { window.question_type = window.questions[Object.keys(window.questions)[0]].type; } if (window.exam_mode) { $("#options-button, #review-overlay-button").hide(); } else { $("#submit-button").hide(); } loadQuestion(0, 1, true); createQuestionListPanel(); } // Set up cornerstone (the dicom viewer) cornerstoneBase64ImageLoader.external.cornerstone = cornerstone; cornerstoneWebImageLoader.external.cornerstone = cornerstone; // cornerstoneWebImageLoader.configure({ // beforeSend: function(xhr) { // // Add custom headers here (e.g. auth tokens) // //xhr.setRequestHeader('x-auth-token', 'my auth token'); // } // }); cornerstoneWADOImageLoader.external.cornerstone = cornerstone; // cornerstoneWADOImageLoader.configure({ // beforeSend: function(xhr) { // // Add custom headers here (e.g. auth tokens) // //xhr.setRequestHeader('APIKEY', 'my auth token'); // } // }); cornerstoneTools.init(); // Set up database const db = new Dexie("answers_database"); db.version(1).stores({ answers: "[cid+eid+qid+qidn], [cid+eid], ans", flags: "[cid+eid+qid+qidn], [cid+eid]", user_answers: "qid, ans", }); /** * Parse the packet and extract metadata (if it exists) * Will then call setUpQuestions() to load the packet * @param {JSON} data */ function setUpPacket(data) { if (data.hasOwnProperty("exam_name")) { $(".exam-name").text("Exam: " + data.exam_name); } if (data.hasOwnProperty("eid")) { window.eid = data.eid; } if (data.hasOwnProperty("type")) { window.question_type = data.type; } if (data.hasOwnProperty("exam_mode")) { window.exam_mode = data.exam_mode; } if (data.hasOwnProperty("questions")) { window.questions = data.questions; } else { window.questions = data; } setUpQuestions(); } /** * Loads a specific question * @param {number} n - Question number to load * @param {number} section - Question section to focus * @param {boolean} force_reload - Force question to be reloaded if already loaded */ function loadQuestion(n, section = 1, force_reload = false) { // Make sure we have an integer n = parseInt(n); console.log(n); const qid = window.question_order[n]; console.log("qid", qid); const current_question = window.questions[qid]; console.log("N=", n, section, current_question); if (n == window.loaded_question && force_reload == false) { // Question already loaded setFocus(section); return; } window.loaded_question = n; // Set up question navigation $(".navigation[value='next']").off(); $(".navigation[value='previous']").off(); if (n == 0) { $(".navigation[value='previous']").attr("disabled", "disabled"); } else { $(".navigation[value='previous']") .removeAttr("disabled") .click(function () { loadQuestion(n - 1); }); } if (n == window.number_of_questions - 1) { $(".navigation[value='next']").attr("disabled", "disabled"); } else { $(".navigation[value='next']") .removeAttr("disabled") .click(function () { loadQuestion(n + 1); }); } const display_n = n + 1; if (current_question.title) { $(".question .title").get(0).innerHTML = '' + display_n + " " + current_question.title; } else { $(".question .title").get(0).innerHTML = '' + display_n + ""; } // Close any open figures let el = document.getElementById("dicom-image"); if (el != undefined) { cornerstone.disable(el); $(".canvas-panel").remove(); } // Set up thumbnails const thumbnails = $(".thumbs"); // Why are we checking this? if (thumbnails) { thumbnails.empty(); // TODO: figure captions (need to extend base json) current_question.images.forEach(function createThumbnail(image, id) { // For thumbnails we only want a single image if (Array.isArray(image)) { image = image[0]; // Do we want the middle image? } thumbnails.append( '' + display_n + '.1Case normal/abnormal
' + display_n + '.1' + current_question.question + '
' + display_n + '.1Observations
' + display_n + '.2Interpretation
' + display_n + '.3Principle Diagnosis
' + display_n + '.4Differential Diagnosis
' + display_n + '.5Management (if appropriate)