diff --git a/design/scorm.md b/design/scorm.md index 67dd3392..980da972 100644 --- a/design/scorm.md +++ b/design/scorm.md @@ -53,20 +53,43 @@ 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, as text + (markup out, character references read, or `’` came out as `rsquo`), 40 characters cut + 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 + 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. + 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`, `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 - 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..48c1e86f 100644 --- a/packages/embed/README.md +++ b/packages/embed/README.md @@ -144,11 +144,16 @@ 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`, 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 + 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..542023d0 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, 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' }) /** What 2004 keeps of a question's words, `cmi.interactions.n.description`: 250 characters. */ const DESCRIPTION_LIMIT = 250 /** The verb of a question answered. */ @@ -223,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. @@ -236,7 +257,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 */ @@ -245,32 +268,38 @@ 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() } /** * 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(new RegExp(`[${Object.keys(FOLDED).join('')}]`, '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,34 +463,30 @@ 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 }} + * @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] } - 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}` + 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 // 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 @@ -469,12 +494,33 @@ 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`, 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 (typed && 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)) + /** @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) + } + // 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 21ebc67f..57305e05 100644 --- a/packages/embed/tests/scorm.test.ts +++ b/packages/embed/tests/scorm.test.ts @@ -573,11 +573,21 @@ 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, 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']).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() + } 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 +744,77 @@ 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. 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.student_response': '0[,]2', '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`, 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 +827,78 @@ 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') + // 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, 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() + }) + + 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 }) + 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' }) + 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': 'Rhine', + 'cmi.interactions.1.student_response': '1230', + '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() + // 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]) + 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() }) @@ -980,6 +1062,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)