diff --git a/js/interact.js b/js/interact.js index 0cacc68..a829979 100644 --- a/js/interact.js +++ b/js/interact.js @@ -1,4 +1,5 @@ import * as helper from "./helpers.js"; +import * as config from "./config.js"; /** * Submits answers */ @@ -167,3 +168,24 @@ export function getQuestion(url, question_number, question_total) { //async: false, }); } + +export function postSavedAnswer(type, qid, answer, e, db_object, db) { +console.log("post", type, qid, answer, e) +return $.ajax({ + type: "POST", + url: config.question_answer_submit_url, + data: JSON.stringify({ qid: `${type}/${qid}`, answer: answer, status: 2 }), + contentType: "application/json; charset=utf-8", + dataType: "json", + success: function(data){ + db_object.submitted = true; + db.user_answers.put(db_object); + e.target.remove() + console.log(data); + + }, + error: function(errMsg) { + alert(errMsg); + } +}); +} diff --git a/js/main.js b/js/main.js index 70f99b3..fb6f508 100644 --- a/js/main.js +++ b/js/main.js @@ -61,7 +61,7 @@ const db = new Dexie("answers_database"); db.version(1).stores({ answers: "[aid+cid+eid+qid+qidn], [aid+cid+eid], qid, ans", flags: "[aid+cid+eid+qid+qidn], [aid+cid+eid], qid", - user_answers: "qid, ans", + user_answers: "[qid+type+ans], [qid+type]", session: "[packet+aid], packet, aid, status, date, score, max_score, exam_time, time_left, question_order, questions_answered, total_questions", }); @@ -432,12 +432,14 @@ function loadPacketFromAjax(path, eid, generated) { .then((exam) => { if ( exam == undefined || - Date.parse(exam["generated"]) != Date.parse(generated) + (Date.parse(exam["generated"].split("+")[0]) != Date.parse(generated.split("+")[0])) ) { + console.log("AjaxRequestPacket:", eid); ajaxRequestionPacket(true); } else { // We have an up to date exam in the database // TODO: check the questions are valid + console.log("LoadLocalPacket:", eid); use_local_question_cache = true; setUpPacket(exam, path); $("#options-panel").hide(); @@ -1130,7 +1132,7 @@ async function loadQuestion(n, section = 1, force_reload = false) { if (answer != undefined) { $(".long-answer").text(answer.ans); } - markAnswer(qid, question_data); + markAnswer(qid, question_data, n); }) .catch(function (error) { console.log("error-", error); @@ -1192,7 +1194,7 @@ async function loadQuestion(n, section = 1, force_reload = false) { if (answer != undefined) { $(".long-answer").text(answer.ans); } - markAnswer(qid, question_data); + markAnswer(qid, question_data, n); }) .catch(function (error) { console.log(error); @@ -1300,7 +1302,7 @@ async function loadQuestion(n, section = 1, force_reload = false) { } // We only want to mark once... if (qidn_count == 5) { - markAnswer(qid, question_data); + markAnswer(qid, question_data, n); } }) .catch(function (error) { @@ -1757,12 +1759,14 @@ function reviewQuestions() { }); db.user_answers - .get({ qid: qid }) + .where({ type: question_type, qid: qid }).toArray() .then(function (answers) { // Merge the question answers with the user saved answers let question_answers = question["answers"]; if (answers != undefined) { - question_answers = question_answers.concat(answers.ans); + for (let i = 0; i < answers.length; i++) { + question_answers = question_answers.concat(answers[i].ans); + } } let section_1_answer = current_answers[[qid, "1"]]; @@ -1880,7 +1884,7 @@ function reviewQuestions() { el.append( "[Mark correct]" ).click(() => { - markCorrect(qid, section_2_answer); + markCorrect(qid, section_2_answer, question_type); reviewQuestions(); }); } @@ -1961,8 +1965,9 @@ function reviewQuestions() { * @param {*} qid - * @param {*} current_question - */ -function markAnswer(qid, current_question) { +function markAnswer(qid, current_question, n) { const type = current_question.type; + console.log("Mark", current_question) let option = null; if (review_mode == true) { @@ -2016,15 +2021,23 @@ function markAnswer(qid, current_question) { // Load user saved answers db.user_answers - .get({ qid: qid }) + .where({ type: type, qid: qid }).toArray() .then(function (saved_user_answers) { // check if given answer matches a user saved answer if (saved_user_answers != undefined) { - saved_user_answers.ans.forEach(function (saved_answer, n) { + console.log(saved_user_answers) + saved_user_answers.forEach(function (saved_answer_object, n) { + let saved_answer = saved_answer_object.ans; + console.log("ua", user_answer, saved_answer) ul.append("