From f5d2614b110c3cfe70c49590d6a3db9997556e1e Mon Sep 17 00:00:00 2001 From: Aleksandr Shevchuk Date: Sun, 8 Nov 2026 15:41:05 +0200 Subject: [PATCH 1/4] feat(embed): SCORM 1.2 ids a person reads, and responses only where 1.2 takes them An interaction's id is now the question's number and first words, in letters, digits and _ alone (Q3_which_rivers_cross_germany), or the number alone, so no id carries a colon or a slash a strict LMS refuses. A choice, true-false, matching or sequencing answer carries its result without H5P's notation, which is SCORM 2004's; fill-in and numeric answers keep their response and the right one. SCORM 2004 is unchanged. Closes #157 --- design/scorm.md | 25 +++++-- packages/embed/README.md | 13 ++-- packages/embed/scorm/scorm.js | 50 ++++++++------ packages/embed/tests/scorm.test.ts | 105 +++++++++++++++++++++-------- 4 files changed, 131 insertions(+), 62 deletions(-) diff --git a/design/scorm.md b/design/scorm.md index 67dd3392..ef22db95 100644 --- a/design/scorm.md +++ b/design/scorm.md @@ -53,12 +53,22 @@ one row each; an LMS added here gets a row there. both again and calls `LMSFinish`. **Each answer goes into `cmi.interactions`** (#124, 2026-11-04, asked for by two of three commenters on the Reddit demand check): every `answered` statement the element relays, by the - part's `subContentId`, with the question's words as the id, since 1.2 has no description field - and the id is what a report shows (a slug in printable ASCII, which 1.2's `CMIIdentifier` - allows and nothing else; the key for a question with no words; `-2` on a second question with - the same words), the xAPI type as 1.2 names it (`long-fill-in` as `fill-in`, `other` and an - unknown type left out), the pattern and the response cut to 255 (`CMIFeedback`; H5P's `0[,]2` - goes in as it is, which Moodle takes), `correct` or `wrong` from `success`, or from the score + part's `subContentId`, with an id a person reads, since 1.2 has no description field and the id + is what a report shows: the question's number, its index from 1, and its first words, 40 + characters cut at a word, in letters, digits and `_` alone, as in + `Q3_which_rivers_cross_germany`; the number alone where the words have no Latin letters. That + is the form a commenter on the Reddit SCORM post uses (2026-11-08): 1.2 calls a + `CMIIdentifier` alphanumeric, and an LMS that holds it to that refuses the colon and the slash + that the earlier fallback for a question with no words, its activity's URL, could carry. The + number keeps two questions with the same words apart, where a `-2` did until then; in content + answered out of order, a Course Presentation or a book, it follows the order of answering, as + the index does. Then the xAPI type as 1.2 names it (`long-fill-in` as `fill-in`, `other` and an + unknown type left out), and the pattern and the response cut to 255 (`CMIFeedback`) **only + where they are the learner's own text or number, `fill-in` and `numeric`**: 1.2 holds a + choice, true-false, matching or sequencing answer to single characters, which H5P's `0[,]2` + and `true`, written in 2004's notation, are not. Moodle took them as they are, but a strict LMS + refuses them and LMSs read 1.2's form differently, the same commenter's reason to send none, + so since 2026-11-08 those answers carry their result alone. Then `correct` or `wrong` from `success`, or from the score where the content says nothing, as Fill in the Blanks does not (full marks, none, or the marks themselves, a number 1.2 takes there), `neutral` with neither, the marks as `weighting`, `result.duration` as `latency`, the time of day. **One index per question, written over on a @@ -66,7 +76,8 @@ one row each; an LMS added here gets a row there. learner (`get_scorm_question_count` takes the highest index found), so an entry per attempt would set one learner's columns against another's. The index and the id come from the questions kept with the state (`questions` in `suspend_data`, a `[key, id]` per index, so that - the second of two questions with the same words keeps its `-2` in a later session), after + a question keeps its id in a later session, and one an older zip kept keeps the id that zip + gave it), after `cmi.interactions._count`, which 1.2 lets a SCO read and Moodle reloads from the attempt's tracks; an attempt the LMS started over counts none, and they start again. The count is read once, at launch, where an attempt starts over (#130): read on every answer, an LMS whose count diff --git a/packages/embed/README.md b/packages/embed/README.md index ed99b671..78bdb2df 100644 --- a/packages/embed/README.md +++ b/packages/embed/README.md @@ -144,11 +144,14 @@ package, the player and the H5P runtime, and a launch page that reports to the L - the score out of 100, and `passed` or `failed` when the content says whether it was passed, `completed` when it does not. The content's last result counts, as on an H5P site; -- each question's answer, as `cmi.interactions`: the question's words as the id, its type, the - answer given and the right one, `correct` or `wrong`, the marks it was worth and the time it - took. One interaction per question, written over when it is answered again, which is how - Moodle's interactions report lines learners up. SCORM 1.2 lets the content write these and - never read them; the LMS's reports do; +- each question's answer, as `cmi.interactions`: an id a report reads, the question's number and + first words, such as `Q3_which_rivers_cross_germany`; its type, `correct` or `wrong`, the marks + it was worth and the time it took; and for an answer the learner typed, fill-in or numeric, the + answer given and the right one. SCORM 1.2 writes a choice, true-false, matching or sequencing + answer in a form of its own, which LMSs read differently, so those carry their result alone. + One interaction per question, written over when it is answered again, which is how Moodle's + interactions report lines learners up. SCORM 1.2 lets the content write these and never read + them; the LMS's reports do; - the time spent in each session; - the learner's place: the content's saved state goes into `cmi.suspend_data`, compressed, and the next launch continues from it. SCORM 1.2 keeps 4,096 characters; compressed, a quiz's state diff --git a/packages/embed/scorm/scorm.js b/packages/embed/scorm/scorm.js index e991ea64..5f75fa77 100644 --- a/packages/embed/scorm/scorm.js +++ b/packages/embed/scorm/scorm.js @@ -70,8 +70,10 @@ const SUSPEND_DATA_LIMIT = scorm2004 ? 64000 : 4096 * characters; in 2004 the longest the format allows, 4,000. */ const FEEDBACK_LIMIT = scorm2004 ? 4000 : 255 -/** An interaction's id, a `CMIIdentifier`: 255 characters of printable ASCII, no spaces. */ -const IDENTIFIER_LIMIT = 255 +/** How much of a question's words a SCORM 1.2 id carries after its number, cut at a word. */ +const ID_WORDS_LIMIT = 40 +/** Latin letters that no decomposition takes to ASCII, as an id spells them. */ +const FOLDED = /** @type {Record} */ ({ ß: 'ss', æ: 'ae', œ: 'oe', ø: 'o', đ: 'd', ł: 'l', þ: 'th', ð: 'd' }) /** What 2004 keeps of a question's words, `cmi.interactions.n.description`: 250 characters. */ const DESCRIPTION_LIMIT = 250 /** The verb of a question answered. */ @@ -250,27 +252,31 @@ function questionText(statement, language) { /** * An interaction's id. In SCORM 1.2 it is what a report shows, since 1.2 has no description: the - * question's words as a slug in printable ASCII, else the question's key with anything else taken - * out, else `question`. In 2004, where the words go into the description, an identifier, as 2004 - * asks one to be a URI: the part's id as a `urn:uuid:`, else the activity's own, else a `urn:h5p:`. + * question's number, its index from 1, and its first words, as in `Q3_which_rivers_cross_germany`, + * in letters, digits and `_` alone, which every LMS takes as a `CMIIdentifier`; the number alone + * for a question with no words in Latin letters. The number keeps two questions with the same + * words apart. In 2004, where the words go into the description, an identifier, as 2004 asks one + * to be a URI: the part's id as a `urn:uuid:`, else the activity's own, else a `urn:h5p:`. * @param {string} text * @param {string} key + * @param {number} index */ -function interactionId(text, key) { +function interactionId(text, key, index) { if (scorm2004) { if (/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(key)) return `urn:uuid:${key.toLowerCase()}` if (/^[a-z][a-z0-9+.-]*:[\x21-\x7e]+$/i.test(key) && key.length <= 4000) return key return `urn:h5p:${encodeURIComponent(key)}` } - const slug = text + const words = text + .toLowerCase() + .replace(/[ßæœøđłþð]/g, (letter) => FOLDED[letter] ?? '') .normalize('NFKD') .replace(/[̀-ͯ]/g, '') - .toLowerCase() - .replace(/[^a-z0-9]+/g, '-') - .replace(/^-+|-+$/g, '') - .slice(0, 80) - .replace(/-+$/, '') - return (slug || key.replace(/[^\x21-\x7e]/g, '')).slice(0, IDENTIFIER_LIMIT) || 'question' + .replace(/[^a-z0-9]+/g, '_') + .replace(/^_+|_+$/g, '') + const cut = words.lastIndexOf('_', ID_WORDS_LIMIT) + const shown = words.length <= ID_WORDS_LIMIT ? words : words.slice(0, cut > 0 ? cut : ID_WORDS_LIMIT) + return shown ? `Q${index + 1}_${shown}` : `Q${index + 1}` } /** The package's build, as the writer stamped it: what saved state is kept against. */ @@ -434,7 +440,7 @@ if (questions.length > launchCount) questions = questions.slice(0, launchCount) /** * The question's index and the id it was written under: those it has, else the next index and - * an id no other question has, so that two with the same words differ. + * the id that names it. A question kept by an older zip, in the state it wrote, keeps its old id. * @param {AnsweredStatement} statement * @returns {{ index: number, id: string }} */ @@ -442,11 +448,8 @@ function interactionFor(statement) { const key = questionKey(statement) const known = questions.findIndex((entry) => entry?.[0] === key) if (known !== -1) return { index: known, id: /** @type {[string, string]} */ (questions[known])[1] } - const base = interactionId(questionText(statement, language), key) - const taken = new Set(questions.map((entry) => entry?.[1])) - let id = base - for (let n = 2; taken.has(id); n++) id = `${base.slice(0, IDENTIFIER_LIMIT - 1 - String(n).length)}-${n}` const index = Math.max(questions.length, launchCount) + const id = interactionId(questionText(statement, language), key, index) questions[index] = [key, id] return { index, id } } @@ -469,12 +472,15 @@ function recordAnswer(statement) { const type = typeof definition?.interactionType === 'string' ? types[definition.interactionType] : undefined if (type) set(at('type'), type) // 2004 takes a pattern and a response only after the type that gives their form, and refuses - // them without one (408); 1.2 takes them as they come. - const typed = Boolean(type) || !scorm2004 + // them without one (408); H5P writes them in 2004's own notation, which xAPI took over. 1.2 gets + // them only where they are the learner's own text or number, `fill-in` and `numeric`: it holds a + // choice, true-false, matching or sequencing answer to single characters, which H5P's `0[,]2` and + // `true` are not, and LMSs part ways on those, so such an answer carries its result alone. + const withResponse = scorm2004 ? Boolean(type) : type === 'fill-in' || type === 'numeric' const pattern = Array.isArray(definition?.correctResponsesPattern) ? definition.correctResponsesPattern[0] : undefined - if (typed && typeof pattern === 'string') set(at('correct_responses.0.pattern'), pattern.slice(0, FEEDBACK_LIMIT)) + if (withResponse && typeof pattern === 'string') set(at('correct_responses.0.pattern'), pattern.slice(0, FEEDBACK_LIMIT)) const result = statement.result ?? {} - if (typed && typeof result.response === 'string') set(at(scorm2004 ? 'learner_response' : 'student_response'), result.response.slice(0, FEEDBACK_LIMIT)) + if (withResponse && typeof result.response === 'string') set(at(scorm2004 ? 'learner_response' : 'student_response'), result.response.slice(0, FEEDBACK_LIMIT)) set(at('result'), outcomeOf(result)) if (typeof result.score?.max === 'number') set(at('weighting'), String(result.score.max)) const latency = latencyOf(result.duration) diff --git a/packages/embed/tests/scorm.test.ts b/packages/embed/tests/scorm.test.ts index 21ebc67f..107659b0 100644 --- a/packages/embed/tests/scorm.test.ts +++ b/packages/embed/tests/scorm.test.ts @@ -573,11 +573,19 @@ describe('in an LMS', () => { expect(kept).toMatchObject({ 'cmi.core.score.min': '0', 'cmi.core.score.max': '100', 'cmi.core.score.raw': '66.67' }) expect(kept['cmi.core.session_time']).toMatch(/^\d{2,4}:\d{2}:\d{2}\.\d{2}$/) // Each question's answer as an interaction, by index, as Moodle's report reads them: the - // question's words as the id, since SCORM 1.2 has no description field. + // question's number and words as the id, since SCORM 1.2 has no description field. expect(Object.keys(kept).filter((key) => /^cmi\.interactions\.\d+\.id$/.test(key))).toHaveLength(4) - expect(kept['cmi.interactions.0.id']).toMatch(/^[a-z0-9-]{3,80}$/) + expect(kept['cmi.interactions.0.id']).toMatch(/^Q1_[a-z0-9_]{1,40}$/) + expect(kept['cmi.interactions.2.id']).toMatch(/^Q3_[a-z0-9_]{1,40}$/) expect(kept).toMatchObject({ 'cmi.interactions.0.type': 'choice', 'cmi.interactions.0.result': 'correct', 'cmi.interactions.1.type': 'true-false', 'cmi.interactions.1.result': 'correct', 'cmi.interactions.2.type': 'fill-in', 'cmi.interactions.2.result': 'correct' }) - expect(kept['cmi.interactions.0.student_response']).toBe(kept['cmi.interactions.0.correct_responses.0.pattern']) + // The typed-in answer with its response and the right one; the choice and the true-false with + // their result alone, since 1.2 writes those answers in a form of its own. + expect(kept['cmi.interactions.2.student_response']).toBeTruthy() + expect(kept['cmi.interactions.2.correct_responses.0.pattern']).toBeTruthy() + for (const index of [0, 1]) { + expect(kept[`cmi.interactions.${index}.student_response`]).toBeUndefined() + expect(kept[`cmi.interactions.${index}.correct_responses.0.pattern`]).toBeUndefined() + } expect(kept['cmi.interactions.0.weighting']).toMatch(/^\d+$/) expect(kept['cmi.interactions.0.latency']).toMatch(/^\d{2,4}:\d{2}:\d{2}\.\d{2}$/) expect(kept['cmi.interactions.0.time']).toMatch(/^\d{2}:\d{2}:\d{2}$/) @@ -734,68 +742,71 @@ describe('in an LMS', () => { // keeps no state of its own sends nothing else that would keep them. const [wrote, committed] = (await calls(page)).slice(-2) expect([wrote[0], wrote[1], committed[0]]).toEqual(['LMSSetValue', 'cmi.suspend_data', 'LMSCommit']) - expect((await decoded(first.sco, wrote[2])).questions).toEqual([['a', 'which-rivers-cross-germany']]) - // Another question with the same words takes another id. + expect((await decoded(first.sco, wrote[2])).questions).toEqual([['a', 'Q1_which_rivers_cross_germany']]) + // Another question with the same words takes its own number. await answer(first.sco, 'b', 'Which rivers cross Germany?') let kept = await values(page) expect(kept).toMatchObject({ - 'cmi.interactions.0.id': 'which-rivers-cross-germany', + 'cmi.interactions.0.id': 'Q1_which_rivers_cross_germany', 'cmi.interactions.0.type': 'choice', - 'cmi.interactions.0.correct_responses.0.pattern': '0[,]2', - 'cmi.interactions.0.student_response': '0[,]1', 'cmi.interactions.0.result': 'wrong', 'cmi.interactions.0.weighting': '2', 'cmi.interactions.0.latency': '00:00:12.50', - 'cmi.interactions.1.id': 'which-rivers-cross-germany-2' + 'cmi.interactions.1.id': 'Q2_which_rivers_cross_germany' }) expect(kept['cmi.interactions.0.time']).toMatch(/^\d{2}:\d{2}:\d{2}$/) + // 1.2 writes a choice in single characters, which H5P's `0[,]2` is not: the result alone. + expect(kept['cmi.interactions.0.correct_responses.0.pattern']).toBeUndefined() + expect(kept['cmi.interactions.0.student_response']).toBeUndefined() - // Answered again: the same index, the new answer, nothing added. + // Answered again: the same index, the new result, nothing added. await answer(first.sco, 'a', 'Which rivers cross Germany?', {}, { response: '0[,]2', success: true, score: { raw: 2, max: 2 } }) kept = await values(page) - expect(kept).toMatchObject({ 'cmi.interactions.0.student_response': '0[,]2', 'cmi.interactions.0.result': 'correct' }) + expect(kept).toMatchObject({ 'cmi.interactions.0.result': 'correct' }) expect(kept['cmi.interactions.2.id']).toBeUndefined() - // An Essay's long answer, as `fill-in`, cut to what 1.2 keeps; a type 1.2 has not, left out; - // a question with no words, named by its id; a result that says nothing, neutral; one that - // says nothing but scores, as Fill in the Blanks does, by its marks. + // An Essay's long answer, as `fill-in`, its response cut to what 1.2 keeps; a type 1.2 has not, + // left out, with its response; a question with no words, named by its number alone; a result + // that says nothing, neutral; one that says nothing but scores, as Fill in the Blanks does, by + // its marks. await answer(first.sco, 'c', '', { interactionType: 'long-fill-in', correctResponsesPattern: [] }, { response: 'x'.repeat(300), success: undefined, score: undefined, duration: undefined }) await answer(first.sco, 'd', 'Other', { interactionType: 'other' }, { success: undefined, score: { raw: 1, max: 2 } }) kept = await values(page) - expect(kept).toMatchObject({ 'cmi.interactions.2.id': 'c', 'cmi.interactions.2.type': 'fill-in', 'cmi.interactions.2.result': 'neutral', 'cmi.interactions.3.id': 'other', 'cmi.interactions.3.result': '1' }) + expect(kept).toMatchObject({ 'cmi.interactions.2.id': 'Q3', 'cmi.interactions.2.type': 'fill-in', 'cmi.interactions.2.result': 'neutral', 'cmi.interactions.3.id': 'Q4_other', 'cmi.interactions.3.result': '1' }) expect(kept['cmi.interactions.2.student_response']).toHaveLength(255) expect(kept['cmi.interactions.2.correct_responses.0.pattern']).toBeUndefined() expect(kept['cmi.interactions.2.latency']).toBeUndefined() expect(kept['cmi.interactions.3.type']).toBeUndefined() + expect(kept['cmi.interactions.3.student_response']).toBeUndefined() // The questions go with the state, with no save of the content's, so the next session of the // attempt keeps their indexes and their ids. expect((await decoded(first.sco, (await values(page))['cmi.suspend_data'])).questions).toEqual([ - ['a', 'which-rivers-cross-germany'], - ['b', 'which-rivers-cross-germany-2'], - ['c', 'c'], - ['d', 'other'] + ['a', 'Q1_which_rivers_cross_germany'], + ['b', 'Q2_which_rivers_cross_germany'], + ['c', 'Q3'], + ['d', 'Q4_other'] ]) await leave(page) const second = await launch(page) - // The second of two questions with the same words, answered first this time, keeps its own id. - await answer(second.sco, 'b', 'Which rivers cross Germany?', {}, { response: '2' }) + // The second of two questions with the same words, answered first this time, keeps its number. + await answer(second.sco, 'b', 'Which rivers cross Germany?', {}, { success: true, score: { raw: 2, max: 2 } }) await answer(second.sco, 'e', 'A new one') kept = await values(page) - expect(kept).toMatchObject({ 'cmi.interactions.1.id': 'which-rivers-cross-germany-2', 'cmi.interactions.1.student_response': '2', 'cmi.interactions.4.id': 'a-new-one' }) + expect(kept).toMatchObject({ 'cmi.interactions.1.id': 'Q2_which_rivers_cross_germany', 'cmi.interactions.1.result': 'correct', 'cmi.interactions.4.id': 'Q5_a_new_one' }) expect(kept['cmi.interactions.5.id']).toBeUndefined() // An attempt the LMS started over, which it does at a launch, its interactions gone: they start - // again from the first index. + // again from the first index, and a question takes the number of its new one. await leave(page) await page.evaluate(() => { const lms = window as unknown as { values: Record } for (const key of Object.keys(lms.values)) if (key.startsWith('cmi.interactions.')) delete lms.values[key] }) const third = await launch(page) - await answer(third.sco, 'e', 'A new one', {}, { response: '1' }) + await answer(third.sco, 'e', 'A new one') kept = await values(page) - expect(kept).toMatchObject({ 'cmi.interactions.0.id': 'a-new-one', 'cmi.interactions.0.student_response': '1' }) + expect(kept).toMatchObject({ 'cmi.interactions.0.id': 'Q1_a_new_one', 'cmi.interactions.0.result': 'wrong' }) expect(kept['cmi.interactions.1.id']).toBeUndefined() await context.close() }) @@ -808,15 +819,53 @@ describe('in an LMS', () => { const first = await launch(page) for (const sub of ['a', 'b', 'c']) await answer(first.sco, sub, `Question ${sub}`) let kept = await values(page) - expect([0, 1, 2].map((index) => kept[`cmi.interactions.${index}.id`])).toEqual(['question-a', 'question-b', 'question-c']) + expect([0, 1, 2].map((index) => kept[`cmi.interactions.${index}.id`])).toEqual(['Q1_question_a', 'Q2_question_b', 'Q3_question_c']) // The next session of the attempt: the count it loaded, and new questions after it. await leave(page) const second = await launch(page) - await answer(second.sco, 'b', 'Question b', {}, { response: '2' }) + await answer(second.sco, 'b', 'Question b', {}, { success: true, score: { raw: 2, max: 2 } }) for (const sub of ['d', 'e']) await answer(second.sco, sub, `Question ${sub}`) kept = await values(page) - expect(kept).toMatchObject({ 'cmi.interactions.1.student_response': '2', 'cmi.interactions.3.id': 'question-d', 'cmi.interactions.4.id': 'question-e' }) + expect(kept).toMatchObject({ 'cmi.interactions.1.result': 'correct', 'cmi.interactions.3.id': 'Q4_question_d', 'cmi.interactions.4.id': 'Q5_question_e' }) + await context.close() + }) + + it('names an answer by its number and its first words, in letters, digits and _ alone', async () => { + const { context, page } = await lmsPage() + const { sco } = await launch(page) + // Letters folded to ASCII and cut at a word; no Latin letters, the number alone; a long word cut + // where its words end. + await answer(sco, 'a', 'Wie heißt die Straße über den Fluß, die nach Łódź führt?') + await answer(sco, 'b', 'Что такое вода?') + await answer(sco, 'c', 'Supercalifragilisticexpialidocious-and-then-some-more-letters') + const kept = await values(page) + expect([0, 1, 2].map((index) => kept[`cmi.interactions.${index}.id`])).toEqual(['Q1_wie_heisst_die_strasse_uber_den_fluss', 'Q2', 'Q3_supercalifragilisticexpialidocious_and']) + await context.close() + }) + + it('writes a SCORM 1.2 answer\'s response and the right one where they are the learner\'s own text or number', async () => { + const { context, page } = await lmsPage() + const { sco } = await launch(page) + await answer(sco, 'a', 'Name a river', { interactionType: 'fill-in', correctResponsesPattern: ['{case_matters=false}Rhine'] }, { response: 'Rhine', success: true }) + await answer(sco, 'b', 'How long is it, in km?', { interactionType: 'numeric', correctResponsesPattern: ['1233'] }, { response: '1230' }) + await answer(sco, 'c', 'It flows into the North Sea', { interactionType: 'true-false', correctResponsesPattern: ['true'] }, { response: 'false' }) + await answer(sco, 'd', 'Put them in order', { interactionType: 'sequencing', correctResponsesPattern: ['0[,]1[,]2'] }, { response: '1[,]0[,]2' }) + await answer(sco, 'e', 'Match them', { interactionType: 'matching', correctResponsesPattern: ['0[.]1[,]1[.]0'] }, { response: '0[.]0[,]1[.]1' }) + const kept = await values(page) + expect(kept).toMatchObject({ + 'cmi.interactions.0.student_response': 'Rhine', + 'cmi.interactions.0.correct_responses.0.pattern': '{case_matters=false}Rhine', + 'cmi.interactions.1.student_response': '1230', + 'cmi.interactions.1.correct_responses.0.pattern': '1233' + }) + // Their type and their result, but not H5P's notation, which 1.2 does not take for them. + for (const index of [2, 3, 4]) { + expect(kept[`cmi.interactions.${index}.type`]).toBe(['true-false', 'sequencing', 'matching'][index - 2]) + expect(kept[`cmi.interactions.${index}.result`]).toBe('wrong') + expect(kept[`cmi.interactions.${index}.student_response`]).toBeUndefined() + expect(kept[`cmi.interactions.${index}.correct_responses.0.pattern`]).toBeUndefined() + } await context.close() }) From d3edfeda4594e110a63d98e7e7eae044c599354a Mon Sep 17 00:00:00 2001 From: Aleksandr Shevchuk Date: Sun, 8 Nov 2026 15:45:46 +0200 Subject: [PATCH 2/4] fix(embed): read a question's character references before its 1.2 id is made H5P writes a question's words with   and ’ in them, which the id spelled out (Q1_don_rsquo_t). The words are read as text once, for the 1.2 id and the 2004 description alike. A dotless i folds to i. Part of #157 --- design/scorm.md | 32 ++++++++++++++++-------------- packages/embed/scorm/scorm.js | 15 ++++++++------ packages/embed/tests/scorm.test.ts | 11 +++++++++- 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/design/scorm.md b/design/scorm.md index ef22db95..2e7b89c7 100644 --- a/design/scorm.md +++ b/design/scorm.md @@ -54,21 +54,23 @@ one row each; an LMS added here gets a row there. **Each answer goes into `cmi.interactions`** (#124, 2026-11-04, asked for by two of three commenters on the Reddit demand check): every `answered` statement the element relays, by the part's `subContentId`, with an id a person reads, since 1.2 has no description field and the id - is what a report shows: the question's number, its index from 1, and its first words, 40 - characters cut at a word, in letters, digits and `_` alone, as in - `Q3_which_rivers_cross_germany`; the number alone where the words have no Latin letters. That - is the form a commenter on the Reddit SCORM post uses (2026-11-08): 1.2 calls a - `CMIIdentifier` alphanumeric, and an LMS that holds it to that refuses the colon and the slash - that the earlier fallback for a question with no words, its activity's URL, could carry. The - number keeps two questions with the same words apart, where a `-2` did until then; in content - answered out of order, a Course Presentation or a book, it follows the order of answering, as - the index does. Then the xAPI type as 1.2 names it (`long-fill-in` as `fill-in`, `other` and an - unknown type left out), and the pattern and the response cut to 255 (`CMIFeedback`) **only - where they are the learner's own text or number, `fill-in` and `numeric`**: 1.2 holds a - choice, true-false, matching or sequencing answer to single characters, which H5P's `0[,]2` - and `true`, written in 2004's notation, are not. Moodle took them as they are, but a strict LMS - refuses them and LMSs read 1.2's form differently, the same commenter's reason to send none, - so since 2026-11-08 those answers carry their result alone. Then `correct` or `wrong` from `success`, or from the score + is what a report shows: the question's number, its index from 1, and its first words, as text + (markup out, character references read, or `’` came out as `rsquo`), 40 characters cut + at a word, in letters, digits and `_` alone, as in `Q3_which_rivers_cross_germany`; the number + alone where the words have no Latin letters. After the form a commenter on the Reddit SCORM + post uses, `L2_Q3_refund_case` (2026-11-08): 1.2 calls a `CMIIdentifier` alphanumeric, and an + LMS that holds it to that refuses the colon and the slash that the earlier fallback for a + question with no words, its activity's URL, could carry. The number keeps two questions with + the same words apart, where a `-2` did until then; in content answered out of order, a Course + Presentation or a book, it follows the order of answering, as the index does. Then the xAPI + type as 1.2 names it (`long-fill-in` as `fill-in`, `other` and an unknown type left out), and + the pattern and the response cut to 255 (`CMIFeedback`) **only where they are the learner's + own text or number, `fill-in` and `numeric`**: 1.2 holds a choice, true-false, matching or + sequencing answer to single characters, which H5P's `0[,]2` and `true`, written in 2004's + notation, are not. Moodle took them as they are, but a strict LMS refuses them and LMSs read + 1.2's form differently, the same commenter's reason to send none, so since 2026-11-08 those + answers carry their result alone: a conformance fix, as `docs/compatibility.md` names one. + Then `correct` or `wrong` from `success`, or from the score where the content says nothing, as Fill in the Blanks does not (full marks, none, or the marks themselves, a number 1.2 takes there), `neutral` with neither, the marks as `weighting`, `result.duration` as `latency`, the time of day. **One index per question, written over on a diff --git a/packages/embed/scorm/scorm.js b/packages/embed/scorm/scorm.js index 5f75fa77..fac0514c 100644 --- a/packages/embed/scorm/scorm.js +++ b/packages/embed/scorm/scorm.js @@ -73,7 +73,7 @@ const FEEDBACK_LIMIT = scorm2004 ? 4000 : 255 /** How much of a question's words a SCORM 1.2 id carries after its number, cut at a word. */ const ID_WORDS_LIMIT = 40 /** Latin letters that no decomposition takes to ASCII, as an id spells them. */ -const FOLDED = /** @type {Record} */ ({ ß: 'ss', æ: 'ae', œ: 'oe', ø: 'o', đ: 'd', ł: 'l', þ: 'th', ð: 'd' }) +const FOLDED = /** @type {Record} */ ({ ß: 'ss', æ: 'ae', œ: 'oe', ø: 'o', đ: 'd', ł: 'l', þ: 'th', ð: 'd', ı: 'i' }) /** What 2004 keeps of a question's words, `cmi.interactions.n.description`: 250 characters. */ const DESCRIPTION_LIMIT = 250 /** The verb of a question answered. */ @@ -238,7 +238,9 @@ function questionKey(statement) { /** * The question's words, from the statement's description in the content's language, else the - * first it gives; a question type that writes markup there has it taken out. + * first it gives, as text: a question type that writes markup there has it taken out, and the + * character references it writes, ` ` and `’` among them, read, or a 1.2 id would + * spell them out (`Q1_don_rsquo_t`) and a 2004 description show them as they are. * @param {AnsweredStatement} statement * @param {string} language */ @@ -247,7 +249,9 @@ function questionText(statement, language) { if (!description || typeof description !== 'object') return '' const map = /** @type {Record} */ (description) const text = map[language] ?? map['en-US'] ?? Object.values(map)[0] - return typeof text === 'string' ? text.replace(/<[^>]*>/g, ' ') : '' + if (typeof text !== 'string') return '' + const read = new DOMParser().parseFromString(text.replace(/<[^>]*>/g, ' '), 'text/html').documentElement.textContent ?? '' + return read.replace(/\s+/g, ' ').trim() } /** @@ -269,7 +273,7 @@ function interactionId(text, key, index) { } const words = text .toLowerCase() - .replace(/[ßæœøđłþð]/g, (letter) => FOLDED[letter] ?? '') + .replace(new RegExp(`[${Object.keys(FOLDED).join('')}]`, 'g'), (letter) => FOLDED[letter] ?? '') .normalize('NFKD') .replace(/[̀-ͯ]/g, '') .replace(/[^a-z0-9]+/g, '_') @@ -463,8 +467,7 @@ function recordAnswer(statement) { // the type, which 2004 needs before the pattern and the response it gives the form of. set(at('id'), id) if (scorm2004) { - // As text: the character references a question type writes, `’` among them, read. - const words = (new DOMParser().parseFromString(questionText(statement, language), 'text/html').documentElement.textContent ?? '').replace(/\s+/g, ' ').trim() + const words = questionText(statement, language) if (words) set(at('description'), words.slice(0, DESCRIPTION_LIMIT)) } const definition = statement.object?.definition diff --git a/packages/embed/tests/scorm.test.ts b/packages/embed/tests/scorm.test.ts index 107659b0..65b62cae 100644 --- a/packages/embed/tests/scorm.test.ts +++ b/packages/embed/tests/scorm.test.ts @@ -839,8 +839,17 @@ describe('in an LMS', () => { await answer(sco, 'a', 'Wie heißt die Straße über den Fluß, die nach Łódź führt?') await answer(sco, 'b', 'Что такое вода?') await answer(sco, 'c', 'Supercalifragilisticexpialidocious-and-then-some-more-letters') + // Markup out and character references read, as a question type writes its words; a dotless ı. + await answer(sco, 'd', '

Don’t stop & go

') + await answer(sco, 'e', 'Kızılırmak nehri') const kept = await values(page) - expect([0, 1, 2].map((index) => kept[`cmi.interactions.${index}.id`])).toEqual(['Q1_wie_heisst_die_strasse_uber_den_fluss', 'Q2', 'Q3_supercalifragilisticexpialidocious_and']) + expect([0, 1, 2, 3, 4].map((index) => kept[`cmi.interactions.${index}.id`])).toEqual([ + 'Q1_wie_heisst_die_strasse_uber_den_fluss', + 'Q2', + 'Q3_supercalifragilisticexpialidocious_and', + 'Q4_don_t_stop_go', + 'Q5_kizilirmak_nehri' + ]) await context.close() }) From cc042f5fa8edc352ed074e9480439c57947d740e Mon Sep 17 00:00:00 2001 From: Aleksandr Shevchuk Date: Sun, 8 Nov 2026 16:35:04 +0200 Subject: [PATCH 3/4] fix(embed): write SCORM 1.2 fill-in answers as plain text MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit H5P writes a fill-in's answer and the right one in 2004's notation, which a 1.2 report shows as written: `{case_matters=false}stored[,]Range`. In 1.2 the right answer now loses its `{case_matters=…}` and `{lang=…}` modifiers, and the blanks in both answers are listed with commas, so a report reads `stored, Range`. A numeric answer is written only when it is a number, 1.2's CMIDecimal, so 2004's range `1[:]5` is left out. SCORM 2004 keeps the notation, which is its own. --- design/scorm.md | 6 +++++ packages/embed/README.md | 3 ++- packages/embed/scorm/scorm.js | 42 +++++++++++++++++++++++++----- packages/embed/tests/scorm.test.ts | 29 ++++++++++++++++----- 4 files changed, 67 insertions(+), 13 deletions(-) diff --git a/design/scorm.md b/design/scorm.md index 2e7b89c7..3438e216 100644 --- a/design/scorm.md +++ b/design/scorm.md @@ -70,6 +70,12 @@ one row each; an LMS added here gets a row there. notation, are not. Moodle took them as they are, but a strict LMS refuses them and LMSs read 1.2's form differently, the same commenter's reason to send none, so since 2026-11-08 those answers carry their result alone: a conformance fix, as `docs/compatibility.md` names one. + The two that keep theirs are written as a person reads them (`scorm12Answer`, the same day, + the owner's call when Moodle's report was found to show `{case_matters=false}stored[,]Range`): + the right answer without the `{case_matters=…}`, `{lang=…}` it opens with, a fill-in's blanks + listed with commas in place of `[,]`, every fill-in type in the pack writing them that way, so + `stored, Range`; and a numeric only where it is a number, 1.2's `CMIDecimal`, not 2004's range + `1[:]5`, though no library in the pack writes `numeric`. 2004 keeps the notation, its own. Then `correct` or `wrong` from `success`, or from the score where the content says nothing, as Fill in the Blanks does not (full marks, none, or the marks themselves, a number 1.2 takes there), `neutral` with neither, the marks as `weighting`, diff --git a/packages/embed/README.md b/packages/embed/README.md index 78bdb2df..f0ddcfdb 100644 --- a/packages/embed/README.md +++ b/packages/embed/README.md @@ -147,7 +147,8 @@ package, the player and the H5P runtime, and a launch page that reports to the L - each question's answer, as `cmi.interactions`: an id a report reads, the question's number and first words, such as `Q3_which_rivers_cross_germany`; its type, `correct` or `wrong`, the marks it was worth and the time it took; and for an answer the learner typed, fill-in or numeric, the - answer given and the right one. SCORM 1.2 writes a choice, true-false, matching or sequencing + answer given and the right one, as plain text: `stored, Range` where H5P writes + `{case_matters=false}stored[,]Range`. SCORM 1.2 writes a choice, true-false, matching or sequencing answer in a form of its own, which LMSs read differently, so those carry their result alone. One interaction per question, written over when it is answered again, which is how Moodle's interactions report lines learners up. SCORM 1.2 lets the content write these and never read diff --git a/packages/embed/scorm/scorm.js b/packages/embed/scorm/scorm.js index fac0514c..3c9d516b 100644 --- a/packages/embed/scorm/scorm.js +++ b/packages/embed/scorm/scorm.js @@ -225,6 +225,25 @@ function outcomeOf(result) { return raw >= max ? 'correct' : raw <= 0 ? wrong : String(Math.round(raw * 100) / 100) } +/** + * A SCORM 1.2 answer or right answer, for the two types 1.2 takes them for, as a report shows them + * to a person. H5P writes both in 2004's notation, which xAPI took over and 1.2 has not: a fill-in's + * right answer opens with `{case_matters=false}` and the like, and both part a blank from the next + * with `[,]`, so 1.2 gets `stored, Range` for `{case_matters=false}stored[,]Range`; the learner's + * own words are kept as typed. A numeric's is a number, 1.2's `CMIDecimal`, and anything else, such + * as 2004's range `1[:]5`, is not written. Nothing is written for what comes out empty. + * @param {unknown} text + * @param {string} type `fill-in` or `numeric` + * @param {boolean} right whether it is the right answer, whose modifiers go + * @returns {string | null} + */ +function scorm12Answer(text, type, right) { + if (typeof text !== 'string') return null + if (type === 'numeric') return /^-?(\d+(\.\d*)?|\.\d+)$/.test(text.trim()) ? text.trim() : null + const plain = (right ? text.replace(/^(\{[^{}]*\})+/, '') : text).split('[,]').join(', ') + return plain === '' ? null : plain +} + /** * Which question a statement is about: the part's id, which the core writes on every statement * about a part; the content's own, for a question that is the whole package, has none. @@ -476,14 +495,25 @@ function recordAnswer(statement) { if (type) set(at('type'), type) // 2004 takes a pattern and a response only after the type that gives their form, and refuses // them without one (408); H5P writes them in 2004's own notation, which xAPI took over. 1.2 gets - // them only where they are the learner's own text or number, `fill-in` and `numeric`: it holds a - // choice, true-false, matching or sequencing answer to single characters, which H5P's `0[,]2` and - // `true` are not, and LMSs part ways on those, so such an answer carries its result alone. - const withResponse = scorm2004 ? Boolean(type) : type === 'fill-in' || type === 'numeric' + // them only where they are the learner's own text or number, `fill-in` and `numeric`, as text a + // person reads (`scorm12Answer`): it holds a choice, true-false, matching or sequencing answer to + // single characters, which H5P's `0[,]2` and `true` are not, and LMSs part ways on those, so such + // an answer carries its result alone. const pattern = Array.isArray(definition?.correctResponsesPattern) ? definition.correctResponsesPattern[0] : undefined - if (withResponse && typeof pattern === 'string') set(at('correct_responses.0.pattern'), pattern.slice(0, FEEDBACK_LIMIT)) const result = statement.result ?? {} - if (withResponse && typeof result.response === 'string') set(at(scorm2004 ? 'learner_response' : 'student_response'), result.response.slice(0, FEEDBACK_LIMIT)) + /** @type {string | null} */ + let right = null + /** @type {string | null} */ + let given = null + if (scorm2004 && type) { + if (typeof pattern === 'string') right = pattern + if (typeof result.response === 'string') given = result.response + } else if (type === 'fill-in' || type === 'numeric') { + right = scorm12Answer(pattern, type, true) + given = scorm12Answer(result.response, type, false) + } + if (right !== null) set(at('correct_responses.0.pattern'), right.slice(0, FEEDBACK_LIMIT)) + if (given !== null) set(at(scorm2004 ? 'learner_response' : 'student_response'), given.slice(0, FEEDBACK_LIMIT)) set(at('result'), outcomeOf(result)) if (typeof result.score?.max === 'number') set(at('weighting'), String(result.score.max)) const latency = latencyOf(result.duration) diff --git a/packages/embed/tests/scorm.test.ts b/packages/embed/tests/scorm.test.ts index 65b62cae..091ff81b 100644 --- a/packages/embed/tests/scorm.test.ts +++ b/packages/embed/tests/scorm.test.ts @@ -578,10 +578,12 @@ describe('in an LMS', () => { expect(kept['cmi.interactions.0.id']).toMatch(/^Q1_[a-z0-9_]{1,40}$/) expect(kept['cmi.interactions.2.id']).toMatch(/^Q3_[a-z0-9_]{1,40}$/) expect(kept).toMatchObject({ 'cmi.interactions.0.type': 'choice', 'cmi.interactions.0.result': 'correct', 'cmi.interactions.1.type': 'true-false', 'cmi.interactions.1.result': 'correct', 'cmi.interactions.2.type': 'fill-in', 'cmi.interactions.2.result': 'correct' }) - // The typed-in answer with its response and the right one; the choice and the true-false with + // The typed-in answer with its response and the right one, as text a person reads: the blanks + // listed, and H5P's `{case_matters=false}` gone. The right one is each blank's first answer; the + // learner's "deflated" is the second the third blank takes. The choice and the true-false with // their result alone, since 1.2 writes those answers in a form of its own. - expect(kept['cmi.interactions.2.student_response']).toBeTruthy() - expect(kept['cmi.interactions.2.correct_responses.0.pattern']).toBeTruthy() + expect(kept['cmi.interactions.2.student_response']).toBe('stored, Range, deflated, front') + expect(kept['cmi.interactions.2.correct_responses.0.pattern']).toBe('stored, Range, compressed, front') for (const index of [0, 1]) { expect(kept[`cmi.interactions.${index}.student_response`]).toBeUndefined() expect(kept[`cmi.interactions.${index}.correct_responses.0.pattern`]).toBeUndefined() @@ -853,7 +855,7 @@ describe('in an LMS', () => { await context.close() }) - it('writes a SCORM 1.2 answer\'s response and the right one where they are the learner\'s own text or number', async () => { + it('writes a SCORM 1.2 answer\'s response and the right one where they are the learner\'s own text or number, as text a person reads', async () => { const { context, page } = await lmsPage() const { sco } = await launch(page) await answer(sco, 'a', 'Name a river', { interactionType: 'fill-in', correctResponsesPattern: ['{case_matters=false}Rhine'] }, { response: 'Rhine', success: true }) @@ -861,13 +863,25 @@ describe('in an LMS', () => { await answer(sco, 'c', 'It flows into the North Sea', { interactionType: 'true-false', correctResponsesPattern: ['true'] }, { response: 'false' }) await answer(sco, 'd', 'Put them in order', { interactionType: 'sequencing', correctResponsesPattern: ['0[,]1[,]2'] }, { response: '1[,]0[,]2' }) await answer(sco, 'e', 'Match them', { interactionType: 'matching', correctResponsesPattern: ['0[.]1[,]1[.]0'] }, { response: '0[.]0[,]1[.]1' }) + await answer(sco, 'f', 'Name two seas', { interactionType: 'fill-in', correctResponsesPattern: ['{case_matters=false}{lang=en}North Sea[,]Baltic'] }, { response: 'North Sea[,]Black Sea', success: false }) + await answer(sco, 'g', 'Roughly how long, in km?', { interactionType: 'numeric', correctResponsesPattern: ['1200[:]1300'] }, { response: 'about 1230', success: undefined, score: undefined }) const kept = await values(page) + // H5P's `{case_matters=false}` and `{lang=en}` taken off the right answer, and blanks parted by + // `[,]` listed, in the answer given as in the right one. expect(kept).toMatchObject({ 'cmi.interactions.0.student_response': 'Rhine', - 'cmi.interactions.0.correct_responses.0.pattern': '{case_matters=false}Rhine', + 'cmi.interactions.0.correct_responses.0.pattern': 'Rhine', 'cmi.interactions.1.student_response': '1230', - 'cmi.interactions.1.correct_responses.0.pattern': '1233' + 'cmi.interactions.1.correct_responses.0.pattern': '1233', + 'cmi.interactions.5.student_response': 'North Sea, Black Sea', + 'cmi.interactions.5.correct_responses.0.pattern': 'North Sea, Baltic', + 'cmi.interactions.6.type': 'numeric', + 'cmi.interactions.6.result': 'neutral' }) + // A numeric answer that is not a number, 1.2's `CMIDecimal`, is not written: neither 2004's + // range nor words. + expect(kept['cmi.interactions.6.student_response']).toBeUndefined() + expect(kept['cmi.interactions.6.correct_responses.0.pattern']).toBeUndefined() // Their type and their result, but not H5P's notation, which 1.2 does not take for them. for (const index of [2, 3, 4]) { expect(kept[`cmi.interactions.${index}.type`]).toBe(['true-false', 'sequencing', 'matching'][index - 2]) @@ -1038,6 +1052,9 @@ describe('in an LMS', () => { expect(kept).toMatchObject({ 'cmi.interactions.0.type': 'choice', 'cmi.interactions.0.result': 'correct', 'cmi.interactions.1.type': 'true-false', 'cmi.interactions.2.type': 'fill-in', 'cmi.interactions.3.result': 'incorrect' }) expect(kept['cmi.interactions.0.learner_response']).toBe(kept['cmi.interactions.0.correct_responses.0.pattern']) expect(kept['cmi.interactions.0.student_response']).toBeUndefined() + // 2004 takes H5P's notation, which is its own, as written: the fill-in's too. + expect(kept['cmi.interactions.2.correct_responses.0.pattern']).toBe('{case_matters=false}stored[,]Range[,]compressed[,]front') + expect(kept['cmi.interactions.2.learner_response']).toBe('stored[,]Range[,]deflated[,]front') expect(kept['cmi.interactions.0.latency']).toMatch(/^PT(\d+H)?(\d+M)?\d+(\.\d{1,2})?S$/) expect(kept['cmi.interactions.0.timestamp']).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{1,2})?Z$/) expect(Object.keys(kept).some((key) => key.startsWith('cmi.core.'))).toBe(false) From 5376040b82b8531241b5d7651a12b7ab6e5e0832 Mon Sep 17 00:00:00 2001 From: Aleksandr Shevchuk Date: Sun, 8 Nov 2026 16:46:54 +0200 Subject: [PATCH 4/4] fix(embed): empty a 1.2 answer's response when it is written over without one When a question is answered again, its interaction is written over in place, and SCORM 1.2 keeps any field nothing writes again. So a fill-in typed before and left empty now kept the old response, and in an attempt begun under an earlier zip, a choice kept the response and right answer that zip wrote in H5P's notation, beside the new result. In 1.2 an answer that is written over now empties the response and right answer it has none of. A question answered for the first time writes neither, as before. The README now says an attempt begun under an earlier zip keeps its ids, and that answers are plain text up to 255 characters, a numeric one only where it is a number. The design note says an id's words are cut at the 40th character when the first word runs longer. --- design/scorm.md | 8 ++++++-- packages/embed/README.md | 9 +++++---- packages/embed/scorm/scorm.js | 17 ++++++++++++----- packages/embed/tests/scorm.test.ts | 14 ++++++++++++-- 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/design/scorm.md b/design/scorm.md index 3438e216..980da972 100644 --- a/design/scorm.md +++ b/design/scorm.md @@ -56,7 +56,7 @@ one row each; an LMS added here gets a row there. part's `subContentId`, with an id a person reads, since 1.2 has no description field and the id is what a report shows: the question's number, its index from 1, and its first words, as text (markup out, character references read, or `’` came out as `rsquo`), 40 characters cut - at a word, in letters, digits and `_` alone, as in `Q3_which_rivers_cross_germany`; the number + at a word, or at the 40th where the first word runs longer, in letters, digits and `_` alone, as in `Q3_which_rivers_cross_germany`; the number alone where the words have no Latin letters. After the form a commenter on the Reddit SCORM post uses, `L2_Q3_refund_case` (2026-11-08): 1.2 calls a `CMIIdentifier` alphanumeric, and an LMS that holds it to that refuses the colon and the slash that the earlier fallback for a @@ -82,7 +82,11 @@ one row each; an LMS added here gets a row there. `result.duration` as `latency`, the time of day. **One index per question, written over on a retry**, read in Moodle's code: its interactions report has one column per index for every learner (`get_scorm_question_count` takes the highest index found), so an entry per attempt - would set one learner's columns against another's. The index and the id come from the + would set one learner's columns against another's. Written over in 1.2, a response or a right + answer the new answer has none of is emptied, not left (the same day, from the PR's review): + 1.2 keeps a field nothing writes again, so a fill-in typed then and left empty now, or a + choice's response in H5P's notation from a zip before the answers were held to their types, + would stand beside the new result. The index and the id come from the questions kept with the state (`questions` in `suspend_data`, a `[key, id]` per index, so that a question keeps its id in a later session, and one an older zip kept keeps the id that zip gave it), after diff --git a/packages/embed/README.md b/packages/embed/README.md index f0ddcfdb..48c1e86f 100644 --- a/packages/embed/README.md +++ b/packages/embed/README.md @@ -145,10 +145,11 @@ package, the player and the H5P runtime, and a launch page that reports to the L - the score out of 100, and `passed` or `failed` when the content says whether it was passed, `completed` when it does not. The content's last result counts, as on an H5P site; - each question's answer, as `cmi.interactions`: an id a report reads, the question's number and - first words, such as `Q3_which_rivers_cross_germany`; its type, `correct` or `wrong`, the marks - it was worth and the time it took; and for an answer the learner typed, fill-in or numeric, the - answer given and the right one, as plain text: `stored, Range` where H5P writes - `{case_matters=false}stored[,]Range`. SCORM 1.2 writes a choice, true-false, matching or sequencing + first words, such as `Q3_which_rivers_cross_germany`, though an attempt begun under an earlier + zip keeps the ids it began with; its type, `correct` or `wrong`, the marks it was worth and the + time it took; and for an answer the learner typed, fill-in or numeric, the answer given and the + right one, as plain text up to 255 characters: `stored, Range` where H5P writes + `{case_matters=false}stored[,]Range`, and a numeric answer only where it is a number. SCORM 1.2 writes a choice, true-false, matching or sequencing answer in a form of its own, which LMSs read differently, so those carry their result alone. One interaction per question, written over when it is answered again, which is how Moodle's interactions report lines learners up. SCORM 1.2 lets the content write these and never read diff --git a/packages/embed/scorm/scorm.js b/packages/embed/scorm/scorm.js index 3c9d516b..542023d0 100644 --- a/packages/embed/scorm/scorm.js +++ b/packages/embed/scorm/scorm.js @@ -70,7 +70,7 @@ const SUSPEND_DATA_LIMIT = scorm2004 ? 64000 : 4096 * characters; in 2004 the longest the format allows, 4,000. */ const FEEDBACK_LIMIT = scorm2004 ? 4000 : 255 -/** How much of a question's words a SCORM 1.2 id carries after its number, cut at a word. */ +/** How much of a question's words a SCORM 1.2 id carries after its number: cut at a word, or here where the first word is longer. */ const ID_WORDS_LIMIT = 40 /** Latin letters that no decomposition takes to ASCII, as an id spells them. */ const FOLDED = /** @type {Record} */ ({ ß: 'ss', æ: 'ae', œ: 'oe', ø: 'o', đ: 'd', ł: 'l', þ: 'th', ð: 'd', ı: 'i' }) @@ -465,21 +465,21 @@ if (questions.length > launchCount) questions = questions.slice(0, launchCount) * The question's index and the id it was written under: those it has, else the next index and * the id that names it. A question kept by an older zip, in the state it wrote, keeps its old id. * @param {AnsweredStatement} statement - * @returns {{ index: number, id: string }} + * @returns {{ index: number, id: string, again: boolean }} `again` for a question this attempt answered before */ function interactionFor(statement) { const key = questionKey(statement) const known = questions.findIndex((entry) => entry?.[0] === key) - if (known !== -1) return { index: known, id: /** @type {[string, string]} */ (questions[known])[1] } + if (known !== -1) return { index: known, id: /** @type {[string, string]} */ (questions[known])[1], again: true } const index = Math.max(questions.length, launchCount) const id = interactionId(questionText(statement, language), key, index) questions[index] = [key, id] - return { index, id } + return { index, id, again: false } } /** @param {AnsweredStatement} statement */ function recordAnswer(statement) { - const { index, id } = interactionFor(statement) + const { index, id, again } = interactionFor(statement) /** @param {string} field */ const at = (field) => `cmi.interactions.${index}.${field}` // The id first: it is the field that makes the record; then, in 2004, the question's words, and @@ -512,8 +512,15 @@ function recordAnswer(statement) { right = scorm12Answer(pattern, type, true) given = scorm12Answer(result.response, type, false) } + // An answer to a question answered before is written over the earlier one, and 1.2 keeps a field + // the new answer leaves out: a response the learner typed then and not now, or a choice's in H5P's + // notation from a zip before 1.2's answers were held to their types, in an attempt it began. So in + // 1.2 such a field is emptied, and the record shows nothing of another answer. + const clears = again && !scorm2004 if (right !== null) set(at('correct_responses.0.pattern'), right.slice(0, FEEDBACK_LIMIT)) + else if (clears) set(at('correct_responses.0.pattern'), '') if (given !== null) set(at(scorm2004 ? 'learner_response' : 'student_response'), given.slice(0, FEEDBACK_LIMIT)) + else if (clears) set(at('student_response'), '') set(at('result'), outcomeOf(result)) if (typeof result.score?.max === 'number') set(at('weighting'), String(result.score.max)) const latency = latencyOf(result.duration) diff --git a/packages/embed/tests/scorm.test.ts b/packages/embed/tests/scorm.test.ts index 091ff81b..57305e05 100644 --- a/packages/embed/tests/scorm.test.ts +++ b/packages/embed/tests/scorm.test.ts @@ -761,11 +761,17 @@ describe('in an LMS', () => { expect(kept['cmi.interactions.0.correct_responses.0.pattern']).toBeUndefined() expect(kept['cmi.interactions.0.student_response']).toBeUndefined() - // Answered again: the same index, the new result, nothing added. + // Answered again: the same index, the new result, nothing added. What an earlier answer left + // there, here a choice's response and right one in H5P's notation as a zip before 1.2's answers + // were held to their types wrote them in an attempt it began, is emptied, not left beside it. + await page.evaluate(() => Object.assign((window as unknown as { values: Record }).values, { 'cmi.interactions.0.student_response': '0[,]1', 'cmi.interactions.0.correct_responses.0.pattern': '0[,]2' })) await answer(first.sco, 'a', 'Which rivers cross Germany?', {}, { response: '0[,]2', success: true, score: { raw: 2, max: 2 } }) kept = await values(page) - expect(kept).toMatchObject({ 'cmi.interactions.0.result': 'correct' }) + expect(kept).toMatchObject({ 'cmi.interactions.0.result': 'correct', 'cmi.interactions.0.student_response': '', 'cmi.interactions.0.correct_responses.0.pattern': '' }) expect(kept['cmi.interactions.2.id']).toBeUndefined() + // A question answered for the first time writes neither, empty or not. + expect(kept['cmi.interactions.1.student_response']).toBeUndefined() + expect(kept['cmi.interactions.1.correct_responses.0.pattern']).toBeUndefined() // An Essay's long answer, as `fill-in`, its response cut to what 1.2 keeps; a type 1.2 has not, // left out, with its response; a question with no words, named by its number alone; a result @@ -882,6 +888,10 @@ describe('in an LMS', () => { // range nor words. expect(kept['cmi.interactions.6.student_response']).toBeUndefined() expect(kept['cmi.interactions.6.correct_responses.0.pattern']).toBeUndefined() + // The fill-in answered again with nothing typed: its earlier response emptied, its right one + // written again. + await answer(sco, 'a', 'Name a river', { interactionType: 'fill-in', correctResponsesPattern: ['{case_matters=false}Rhine'] }, { response: '', success: false }) + expect(await values(page)).toMatchObject({ 'cmi.interactions.0.student_response': '', 'cmi.interactions.0.correct_responses.0.pattern': 'Rhine', 'cmi.interactions.0.result': 'wrong' }) // Their type and their result, but not H5P's notation, which 1.2 does not take for them. for (const index of [2, 3, 4]) { expect(kept[`cmi.interactions.${index}.type`]).toBe(['true-false', 'sequencing', 'matching'][index - 2])