diff --git a/js/main.js b/js/main.js index b0869c1..92d24b4 100644 --- a/js/main.js +++ b/js/main.js @@ -2948,28 +2948,104 @@ $("#btn-view-local-answers").click(async function(evt) { let answer_json = await db.answers.toArray(); if ($("#view-answers-overlay").length < 1) { + // Build a nicer overlay: filter by exam (eid), formatted table, and a collapsible raw JSON + save link + // Collect unique eids and a mapping + const examsMap = {}; + for (const a of answer_json) { + const eid = a.eid || "unknown"; + if (!examsMap[eid]) examsMap[eid] = []; + examsMap[eid].push(a); + } + + let examOptions = ""; + for (const eid of Object.keys(examsMap)) { + examOptions += ``; + } + $("body").append( `
-
${JSON.stringify(answer_json, null, 2)}
+
+ + + + Save JSON +
+
+
+ Raw JSON +
${JSON.stringify(answer_json, null, 2)}
+
` ); $("#view-answers-overlay").height(docHeight); - $("#view-answers-overlay").append("Save") - // Close button handler + // Close handler $("#close-view-answers").on("click", function() { $("#view-answers-overlay").remove(); }); - let file = new Blob([JSON.stringify(answer_json)], {type: "application/json"}) - + // Save link (raw JSON) - keep existing behaviour + let file = new Blob([JSON.stringify(answer_json)], {type: "application/json"}); let link = document.getElementById("save-failed-answers"); link.href = URL.createObjectURL(file); link.download = "answers.json"; + + // Render formatted view initially (all exams) + function renderFormatted(filterEid) { + const container = $("#view-answers-formatted"); + container.empty(); + const rows = []; + const source = (filterEid && filterEid !== 'all') ? (examsMap[filterEid] || []) : answer_json; + if (source.length === 0) { + container.append("
No answers available for selection.
"); + return; + } + + // Group by eid then by cid (candidate) + const byEid = {}; + for (const a of source) { + const eid = a.eid || 'unknown'; + const cid = a.cid || 'unknown'; + if (!byEid[eid]) byEid[eid] = {}; + if (!byEid[eid][cid]) byEid[eid][cid] = []; + byEid[eid][cid].push(a); + } + + for (const eidKey of Object.keys(byEid)) { + const eidSection = $(`

Exam ${eidKey}

`); + for (const cidKey of Object.keys(byEid[eidKey])) { + const answers = byEid[eidKey][cidKey]; + const table = $("
"); + table.append("CIDQIDSectionAnswer"); + const tbody = $(""); + for (const ans of answers) { + const qid = ans.qid || ''; + const qidn = ans.qidn || ''; + const text = (typeof ans.ans === 'object') ? JSON.stringify(ans.ans) : String(ans.ans); + const row = $(`${cidKey}${qid}${qidn}${$('
').text(text).html()}`); + tbody.append(row); + } + table.append(tbody); + eidSection.append($(`
Candidate ${cidKey} (${answers.length} answers)
`)); + eidSection.append(table); + } + container.append(eidSection); + } + } + + // Wire up filter and refresh + $("#view-answers-filter").on("change", function() { + renderFormatted($(this).val()); + }); + $("#view-answers-refresh").on("click", function() { + renderFormatted($("#view-answers-filter").val()); + }); + + renderFormatted('all'); } });