@qti-components/delivery 0.1.0-next.1 → 0.1.0-next.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -1
- package/dist/lifecycle/item-lifecycle.d.ts +108 -0
- package/dist/lifecycle/item-lifecycle.js +157 -0
- package/dist/lifecycle/item-lifecycle.js.map +1 -0
- package/dist/lifecycle/item-rules.d.ts +100 -0
- package/dist/lifecycle/item-rules.js +34 -0
- package/dist/lifecycle/item-rules.js.map +1 -0
- package/dist/lifecycle/item-session.d.ts +27 -0
- package/dist/lifecycle/item-session.js +47 -0
- package/dist/lifecycle/item-session.js.map +1 -0
- package/dist/model/test-model.d.ts +24 -0
- package/dist/model/test-model.js +23 -2
- package/dist/model/test-model.js.map +1 -1
- package/dist/model/xml-node.d.ts +5 -0
- package/dist/model/xml-node.js +11 -0
- package/dist/model/xml-node.js.map +1 -1
- package/dist/random/selection.d.ts +36 -0
- package/dist/random/selection.js +52 -0
- package/dist/random/selection.js.map +1 -0
- package/dist/state/create-delivery-state.d.ts +12 -2
- package/dist/state/create-delivery-state.js +39 -4
- package/dist/state/create-delivery-state.js.map +1 -1
- package/dist/state/delivery-state.d.ts +70 -12
- package/dist/state/delivery-state.js +66 -3
- package/dist/state/delivery-state.js.map +1 -1
- package/dist/timing/time-limits.d.ts +50 -0
- package/dist/timing/time-limits.js +51 -0
- package/dist/timing/time-limits.js.map +1 -0
- package/dist/timing/timing.d.ts +48 -0
- package/dist/timing/timing.js +100 -0
- package/dist/timing/timing.js.map +1 -0
- package/package.json +1 -1
|
@@ -8,6 +8,8 @@ import type { XmlElement } from './xml-node.js';
|
|
|
8
8
|
export interface TestModel {
|
|
9
9
|
identifier: string;
|
|
10
10
|
title: string;
|
|
11
|
+
/** The test's own `qti-time-limits`; absent when it declares none. */
|
|
12
|
+
timeLimits?: TimeLimits;
|
|
11
13
|
testParts: TestPartModel[];
|
|
12
14
|
}
|
|
13
15
|
export interface TestPartModel {
|
|
@@ -16,6 +18,8 @@ export interface TestPartModel {
|
|
|
16
18
|
navigationMode: 'linear' | 'nonlinear';
|
|
17
19
|
submissionMode: 'individual' | 'simultaneous';
|
|
18
20
|
sessionControl: SessionControl;
|
|
21
|
+
/** The part's own `qti-time-limits`; absent when it declares none. */
|
|
22
|
+
timeLimits?: TimeLimits;
|
|
19
23
|
sections: SectionModel[];
|
|
20
24
|
}
|
|
21
25
|
export interface SectionModel {
|
|
@@ -24,6 +28,8 @@ export interface SectionModel {
|
|
|
24
28
|
title: string;
|
|
25
29
|
visible: boolean;
|
|
26
30
|
sessionControl: SessionControl;
|
|
31
|
+
/** The section's own `qti-time-limits`; absent when it declares none. */
|
|
32
|
+
timeLimits?: TimeLimits;
|
|
27
33
|
/** The item-refs and sections directly inside this one, in document order, as the XML has them. */
|
|
28
34
|
content: SectionContent[];
|
|
29
35
|
}
|
|
@@ -35,6 +41,8 @@ export interface ItemRefModel {
|
|
|
35
41
|
sessionControl: SessionControl;
|
|
36
42
|
/** What this item-ref's own `qti-item-session-control` declares, nothing inherited. */
|
|
37
43
|
declaredSessionControl: Partial<SessionControl>;
|
|
44
|
+
/** The item-ref's own `qti-time-limits`; absent when it declares none. */
|
|
45
|
+
timeLimits?: TimeLimits;
|
|
38
46
|
}
|
|
39
47
|
export type SectionContent = SectionModel | ItemRefModel;
|
|
40
48
|
/** `qti-item-session-control`, resolved for one level (QTI 3 info model §7.19). */
|
|
@@ -50,6 +58,22 @@ export interface SessionControl {
|
|
|
50
58
|
/** Applies only in a test part with individual submission. */
|
|
51
59
|
validateResponses: boolean;
|
|
52
60
|
}
|
|
61
|
+
/**
|
|
62
|
+
* `qti-time-limits` as one level declares it (QTI 3 info model §7.40, §2.8.5). Never inherited: each
|
|
63
|
+
* limit is on the time spent in its own level, the item-ref's on the item session's `duration`, a
|
|
64
|
+
* section's, part's or test's on all the time spent in it, navigation included.
|
|
65
|
+
*/
|
|
66
|
+
export interface TimeLimits {
|
|
67
|
+
/**
|
|
68
|
+
* Seconds; null when not declared. On a section or item-ref it applies only in a linear part
|
|
69
|
+
* (§2.8.5); `timeLimitRules` leaves it out elsewhere.
|
|
70
|
+
*/
|
|
71
|
+
minTime: number | null;
|
|
72
|
+
/** Seconds; null when not declared. */
|
|
73
|
+
maxTime: number | null;
|
|
74
|
+
/** Whether a response given after `maxTime` is still accepted. False when not declared (the XSD default). */
|
|
75
|
+
allowLateSubmission: boolean;
|
|
76
|
+
}
|
|
53
77
|
/**
|
|
54
78
|
* The values when no level declares an attribute: the XSD defaults, and 1 for `max-attempts`,
|
|
55
79
|
* which the spec text gives ("if it is unspecified it is treated as 1 for non-adaptive items").
|
package/dist/model/test-model.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { booleanAttribute, childElement, childElements, integerAttribute } from './xml-node.js';
|
|
1
|
+
import { booleanAttribute, childElement, childElements, integerAttribute, nonNegativeNumberAttribute } from './xml-node.js';
|
|
2
2
|
/**
|
|
3
3
|
* The values when no level declares an attribute: the XSD defaults, and 1 for `max-attempts`,
|
|
4
4
|
* which the spec text gives ("if it is unspecified it is treated as 1 for non-adaptive items").
|
|
@@ -29,6 +29,7 @@ export function buildTestModel(test) {
|
|
|
29
29
|
return {
|
|
30
30
|
identifier: test.getAttribute('identifier') ?? '',
|
|
31
31
|
title: test.getAttribute('title') ?? '',
|
|
32
|
+
...timeLimitsOf(test),
|
|
32
33
|
testParts: childElements(test, 'qti-test-part').map(buildTestPart)
|
|
33
34
|
};
|
|
34
35
|
}
|
|
@@ -39,6 +40,7 @@ function buildTestPart(part) {
|
|
|
39
40
|
navigationMode: part.getAttribute('navigation-mode') === 'linear' ? 'linear' : 'nonlinear',
|
|
40
41
|
submissionMode: part.getAttribute('submission-mode') === 'simultaneous' ? 'simultaneous' : 'individual',
|
|
41
42
|
sessionControl,
|
|
43
|
+
...timeLimitsOf(part),
|
|
42
44
|
sections: childElements(part, 'qti-assessment-section').map(section => buildSection(section, sessionControl))
|
|
43
45
|
};
|
|
44
46
|
}
|
|
@@ -50,6 +52,7 @@ function buildSection(section, inherited) {
|
|
|
50
52
|
title: section.getAttribute('title') ?? '',
|
|
51
53
|
visible: booleanAttribute(section, 'visible') ?? true,
|
|
52
54
|
sessionControl,
|
|
55
|
+
...timeLimitsOf(section),
|
|
53
56
|
content: childElements(section, 'qti-assessment-section', 'qti-assessment-item-ref').map(child => child.localName === 'qti-assessment-section'
|
|
54
57
|
? buildSection(child, sessionControl)
|
|
55
58
|
: buildItemRef(child, sessionControl))
|
|
@@ -63,7 +66,8 @@ function buildItemRef(itemRef, inherited) {
|
|
|
63
66
|
href: itemRef.getAttribute('href') ?? '',
|
|
64
67
|
categories: category ? category.split(/\s+/) : [],
|
|
65
68
|
sessionControl: resolveSessionControl(inherited, itemRef),
|
|
66
|
-
declaredSessionControl: declaredSessionControl(itemRef)
|
|
69
|
+
declaredSessionControl: declaredSessionControl(itemRef),
|
|
70
|
+
...timeLimitsOf(itemRef)
|
|
67
71
|
};
|
|
68
72
|
}
|
|
69
73
|
/** `inherited`, with what `owner`'s own `qti-item-session-control` declares on top. */
|
|
@@ -86,6 +90,23 @@ function declaredSessionControl(owner) {
|
|
|
86
90
|
};
|
|
87
91
|
return Object.fromEntries(Object.entries(declared).filter(([, value]) => value !== undefined));
|
|
88
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* `owner`'s own `qti-time-limits` as `{ timeLimits }`, or nothing when it declares none, so the
|
|
95
|
+
* model holds the key only where the XML has the element. A value that is not a non-negative number
|
|
96
|
+
* counts as not declared.
|
|
97
|
+
*/
|
|
98
|
+
function timeLimitsOf(owner) {
|
|
99
|
+
const element = childElement(owner, 'qti-time-limits');
|
|
100
|
+
if (!element)
|
|
101
|
+
return {};
|
|
102
|
+
return {
|
|
103
|
+
timeLimits: {
|
|
104
|
+
minTime: nonNegativeNumberAttribute(element, 'min-time') ?? null,
|
|
105
|
+
maxTime: nonNegativeNumberAttribute(element, 'max-time') ?? null,
|
|
106
|
+
allowLateSubmission: booleanAttribute(element, 'allow-late-submission') ?? false
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
}
|
|
89
110
|
/** The item-refs directly inside a section, in document order; not those of its subsections. */
|
|
90
111
|
export const ownItemRefs = (section) => section.content.filter((child) => child.kind === 'itemRef');
|
|
91
112
|
/** Every section, depth first in document order: a section before its subsections. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"test-model.js","sourceRoot":"","sources":["../../src/model/test-model.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,YAAY,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AA6DhG;;;GAGG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAA6B,MAAM,CAAC,MAAM,CAAC;IAC7E,WAAW,EAAE,CAAC;IACd,YAAY,EAAE,KAAK;IACnB,WAAW,EAAE,IAAI;IACjB,YAAY,EAAE,KAAK;IACnB,YAAY,EAAE,KAAK;IACnB,aAAa,EAAE,IAAI;IACnB,iBAAiB,EAAE,KAAK;CACzB,CAAC,CAAC;AAEH;;;;;;;;;GASG;AACH,MAAM,UAAU,cAAc,CAAC,IAAgB;IAC7C,IAAI,IAAI,CAAC,SAAS,KAAK,qBAAqB,EAAE,CAAC;QAC7C,MAAM,IAAI,SAAS,CAAC,+CAA+C,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;IAChG,CAAC;IACD,OAAO;QACL,UAAU,EAAE,IAAI,CAAC,YAAY,CAAC,YAAY,CAAC,IAAI,EAAE;QACjD,KAAK,EAAE,IAAI,CAAC,YAAY,CAAC,OAAO,CAAC,IAAI,EAAE;QACvC,SAAS,EAAE,aAAa,CAAC,IAAI,EAAE,eAAe,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC;KACnE,CAAC;AACJ,CAAC;AAED,SAAS,aAAa,CAAC,IAAgB;IACrC,M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oBAAoB,CAAC;KACnE,CAAC;IACF,OAAO,MAAM,CAAC,WAAW,CAAC,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC;AACjG,CAAC;AAED,gGAAgG;AAChG,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,OAAqB,EAAkB,EAAE,CACnE,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,KAAK,EAAyB,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC;AAErF,sFAAsF;AACtF,MAAM,UAAU,eAAe,CAAC,QAAwB;IACtD,OAAO,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;QACjC,OAAO;QACP,GAAG,eAAe,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,KAAK,EAAyB,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC;KACvG,CAAC,CAAC;AACL,CAAC;AAED,iFAAiF;AACjF,MAAM,UAAU,eAAe,CAAC,QAA0B;IACxD,OAAO,QAAQ,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,eAAe,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;AAC1G,CAAC","sourcesContent":["import { booleanAttribute, childElement, childElements, integerAttribute } from './xml-node.js';\n\nimport type { XmlElement } from './xml-node.js';\n\n/**\n * The structure and the resolved rules of a `qti-assessment-test`, read from its XML once per load.\n * Plain JSON, so a server can build it from the same file.\n *\n * Experimental (0.x): roadmap 1 phase T1; see `plans/test-model.md`.\n */\nexport interface TestModel {\n identifier: string;\n title: string;\n testParts: TestPartModel[];\n}\n\nexport interface TestPartModel {\n identifier: string;\n /** Defined on the test part only (QTI 3 XSD); a section never changes it. */\n navigationMode: 'linear' | 'nonlinear';\n submissionMode: 'individual' | 'simultaneous';\n sessionControl: SessionControl;\n sections: SectionModel[];\n}\n\nexport interface SectionModel {\n kind: 'section';\n identifier: string;\n title: string;\n visible: boolean;\n sessionControl: SessionControl;\n /** The item-refs and sections directly inside this one, in document order, as the XML has them. */\n content: SectionContent[];\n}\n\nexport interface ItemRefModel {\n kind: 'itemRef';\n identifier: string;\n href: string;\n categories: string[];\n sessionControl: SessionControl;\n /** What this item-ref's own `qti-item-session-control` declares, nothing inherited. */\n declaredSessionControl: Partial<SessionControl>;\n}\n\nexport type SectionContent = SectionModel | ItemRefModel;\n\n/** `qti-item-session-control`, resolved for one level (QTI 3 info model §7.19). */\nexport interface SessionControl {\n /** 0 is unlimited. Ignored for adaptive items, which end by `completionStatus`. */\n maxAttempts: number;\n showFeedback: boolean;\n allowReview: boolean;\n showSolution: boolean;\n allowComment: boolean;\n /** Applies only in a test part with individual submission. */\n allowSkipping: boolean;\n /** Applies only in a test part with individual submission. */\n validateResponses: boolean;\n}\n\n/**\n * The values when no level declares an attribute: the XSD defaults, and 1 for `max-attempts`,\n * which the spec text gives (\"if it is unspecified it is treated as 1 for non-adaptive items\").\n */\nexport const DEFAULT_SESSION_CONTROL: Readonly<SessionControl> = Object.freeze({\n maxAttempts: 1,\n showFeedback: false,\n allowReview: true,\n showSolution: false,\n allowComment: false,\n allowSkipping: true,\n validateResponses: false\n});\n\n/**\n * Build the model of a test from its `qti-assessment-test` element: the XML document under Node, the\n * rendered element in the browser. Reads attributes and child elements only, so no element needs\n * to be registered.\n *\n * Session control inherits per attribute: each one comes from the closest level that declares it\n * (item-ref, then each enclosing section, then the test part), and its default only when none does.\n * The spec: a test-part setting applies \"unless the setting is overridden\", and the defaults \"are\n * used only in the absence of any applicable constraint\" (implementation guide §4.4.2).\n */\nexport function buildTestModel(test: XmlElement): TestModel {\n if (test.localName !== 'qti-assessment-test') {\n throw new TypeError(`Expected a qti-assessment-test element, not ${String(test.localName)}.`);\n }\n return {\n identifier: test.getAttribute('identifier') ?? '',\n title: test.getAttribute('title') ?? '',\n testParts: childElements(test, 'qti-test-part').map(buildTestPart)\n };\n}\n\nfunction buildTestPart(part: XmlElement): TestPartModel {\n const sessionControl = resolveSessionControl(DEFAULT_SESSION_CONTROL, part);\n return {\n identifier: part.getAttribute('identifier') ?? '',\n navigationMode: part.getAttribute('navigation-mode') === 'linear' ? 'linear' : 'nonlinear',\n submissionMode: part.getAttribute('submission-mode') === 'simultaneous' ? 'simultaneous' : 'individual',\n sessionControl,\n sections: childElements(part, 'qti-assessment-section').map(section => buildSection(section, sessionControl))\n };\n}\n\nfunction buildSection(section: XmlElement, inherited: SessionControl): SectionModel {\n const sessionControl = resolveSessionControl(inherited, section);\n return {\n kind: 'section',\n identifier: section.getAttribute('identifier') ?? '',\n title: section.getAttribute('title') ?? '',\n visible: booleanAttribute(section, 'visible') ?? true,\n sessionControl,\n content: childElements(section, 'qti-assessment-section', 'qti-assessment-item-ref').map(child =>\n child.localName === 'qti-assessment-section'\n ? buildSection(child, sessionControl)\n : buildItemRef(child, sessionControl)\n )\n };\n}\n\nfunction buildItemRef(itemRef: XmlElement, inherited: SessionControl): ItemRefModel {\n const category = itemRef.getAttribute('category')?.trim();\n return {\n kind: 'itemRef',\n identifier: itemRef.getAttribute('identifier') ?? '',\n href: itemRef.getAttribute('href') ?? '',\n categories: category ? category.split(/\\s+/) : [],\n sessionControl: resolveSessionControl(inherited, itemRef),\n declaredSessionControl: declaredSessionControl(itemRef)\n };\n}\n\n/** `inherited`, with what `owner`'s own `qti-item-session-control` declares on top. */\nfunction resolveSessionControl(inherited: Readonly<SessionControl>, owner: XmlElement): SessionControl {\n return { ...inherited, ...declaredSessionControl(owner) };\n}\n\n/** Only the attributes `owner`'s `qti-item-session-control` declares, so an absent one inherits. */\nfunction declaredSessionControl(owner: XmlElement): Partial<SessionControl> {\n const element = childElement(owner, 'qti-item-session-control');\n if (!element) return {};\n const declared: Partial<SessionControl> = {\n maxAttempts: integerAttribute(element, 'max-attempts'),\n showFeedback: booleanAttribute(element, 'show-feedback'),\n allowReview: booleanAttribute(element, 'allow-review'),\n showSolution: booleanAttribute(element, 'show-solution'),\n allowComment: booleanAttribute(element, 'allow-comment'),\n allowSkipping: booleanAttribute(element, 'allow-skipping'),\n validateResponses: booleanAttribute(element, 'validate-responses')\n };\n return Object.fromEntries(Object.entries(declared).filter(([, value]) => value !== undefined));\n}\n\n/** The item-refs directly inside a section, in document order; not those of its subsections. */\nexport const ownItemRefs = (section: SectionModel): ItemRefModel[] =>\n section.content.filter((child): child is ItemRefModel => child.kind === 'itemRef');\n\n/** Every section, depth first in document order: a section before its subsections. */\nexport function sectionsInOrder(sections: SectionModel[]): SectionModel[] {\n return sections.flatMap(section => [\n section,\n ...sectionsInOrder(section.content.filter((child): child is SectionModel => child.kind === 'section'))\n ]);\n}\n\n/** Every item-ref inside the given sections, at any depth, in document order. */\nexport function itemRefsInOrder(sections: SectionContent[]): ItemRefModel[] {\n return sections.flatMap(child => (child.kind === 'itemRef' ? [child] : itemRefsInOrder(child.content)));\n}\n"]}
|
|
1
|
+
{"version":3,"file":"test-model.js","sourceRoot":"","sources":["../../src/model/test-model.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAChB,YAAY,EACZ,aAAa,EACb,gBAAgB,EAChB,0BAA0B,EAC3B,MAAM,eAAe,CAAC;AAsFvB;;;GAGG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAA6B,MAAM,CAAC,MAAM,CAAC;IAC7E,WAAW,EAAE,CAAC;IACd,YAAY,EAAE,KAAK;IACnB,WAAW,EAAE,IAAI;IACjB,YAAY,EAAE,KAAK;IACnB,YAAY,EAAE,KAAK;IACnB,aAAa,EAAE,IAAI;IACnB,iBAAiB,EAAE,KAAK;CACzB,CAAC,CAAC;AAEH;;;;;;;;;GASG;AACH,MAAM,UAAU,cAAc,CAAC,IAAgB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CAAC;AAED,gGAAgG;AAChG,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,OAAqB,EAAkB,EAAE,CACnE,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,KAAK,EAAyB,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC;AAErF,sFAAsF;AACtF,MAAM,UAAU,eAAe,CAAC,QAAwB;IACtD,OAAO,QAAQ,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;QACjC,OAAO;QACP,GAAG,eAAe,CAAC,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,KAAK,EAAyB,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC;KACvG,CAAC,CAAC;AACL,CAAC;AAED,iFAAiF;AACjF,MAAM,UAAU,eAAe,CAAC,QAA0B;IACxD,OAAO,QAAQ,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,eAAe,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;AAC1G,CAAC","sourcesContent":["import {\n booleanAttribute,\n childElement,\n childElements,\n integerAttribute,\n nonNegativeNumberAttribute\n} from './xml-node.js';\n\nimport type { XmlElement } from './xml-node.js';\n\n/**\n * The structure and the resolved rules of a `qti-assessment-test`, read from its XML once per load.\n * Plain JSON, so a server can build it from the same file.\n *\n * Experimental (0.x): roadmap 1 phase T1; see `plans/test-model.md`.\n */\nexport interface TestModel {\n identifier: string;\n title: string;\n /** The test's own `qti-time-limits`; absent when it declares none. */\n timeLimits?: TimeLimits;\n testParts: TestPartModel[];\n}\n\nexport interface TestPartModel {\n identifier: string;\n /** Defined on the test part only (QTI 3 XSD); a section never changes it. */\n navigationMode: 'linear' | 'nonlinear';\n submissionMode: 'individual' | 'simultaneous';\n sessionControl: SessionControl;\n /** The part's own `qti-time-limits`; absent when it declares none. */\n timeLimits?: TimeLimits;\n sections: SectionModel[];\n}\n\nexport interface SectionModel {\n kind: 'section';\n identifier: string;\n title: string;\n visible: boolean;\n sessionControl: SessionControl;\n /** The section's own `qti-time-limits`; absent when it declares none. */\n timeLimits?: TimeLimits;\n /** The item-refs and sections directly inside this one, in document order, as the XML has them. */\n content: SectionContent[];\n}\n\nexport interface ItemRefModel {\n kind: 'itemRef';\n identifier: string;\n href: string;\n categories: string[];\n sessionControl: SessionControl;\n /** What this item-ref's own `qti-item-session-control` declares, nothing inherited. */\n declaredSessionControl: Partial<SessionControl>;\n /** The item-ref's own `qti-time-limits`; absent when it declares none. */\n timeLimits?: TimeLimits;\n}\n\nexport type SectionContent = SectionModel | ItemRefModel;\n\n/** `qti-item-session-control`, resolved for one level (QTI 3 info model §7.19). */\nexport interface SessionControl {\n /** 0 is unlimited. Ignored for adaptive items, which end by `completionStatus`. */\n maxAttempts: number;\n showFeedback: boolean;\n allowReview: boolean;\n showSolution: boolean;\n allowComment: boolean;\n /** Applies only in a test part with individual submission. */\n allowSkipping: boolean;\n /** Applies only in a test part with individual submission. */\n validateResponses: boolean;\n}\n\n/**\n * `qti-time-limits` as one level declares it (QTI 3 info model §7.40, §2.8.5). Never inherited: each\n * limit is on the time spent in its own level, the item-ref's on the item session's `duration`, a\n * section's, part's or test's on all the time spent in it, navigation included.\n */\nexport interface TimeLimits {\n /**\n * Seconds; null when not declared. On a section or item-ref it applies only in a linear part\n * (§2.8.5); `timeLimitRules` leaves it out elsewhere.\n */\n minTime: number | null;\n /** Seconds; null when not declared. */\n maxTime: number | null;\n /** Whether a response given after `maxTime` is still accepted. False when not declared (the XSD default). */\n allowLateSubmission: boolean;\n}\n\n/**\n * The values when no level declares an attribute: the XSD defaults, and 1 for `max-attempts`,\n * which the spec text gives (\"if it is unspecified it is treated as 1 for non-adaptive items\").\n */\nexport const DEFAULT_SESSION_CONTROL: Readonly<SessionControl> = Object.freeze({\n maxAttempts: 1,\n showFeedback: false,\n allowReview: true,\n showSolution: false,\n allowComment: false,\n allowSkipping: true,\n validateResponses: false\n});\n\n/**\n * Build the model of a test from its `qti-assessment-test` element: the XML document under Node, the\n * rendered element in the browser. Reads attributes and child elements only, so no element needs\n * to be registered.\n *\n * Session control inherits per attribute: each one comes from the closest level that declares it\n * (item-ref, then each enclosing section, then the test part), and its default only when none does.\n * The spec: a test-part setting applies \"unless the setting is overridden\", and the defaults \"are\n * used only in the absence of any applicable constraint\" (implementation guide §4.4.2).\n */\nexport function buildTestModel(test: XmlElement): TestModel {\n if (test.localName !== 'qti-assessment-test') {\n throw new TypeError(`Expected a qti-assessment-test element, not ${String(test.localName)}.`);\n }\n return {\n identifier: test.getAttribute('identifier') ?? '',\n title: test.getAttribute('title') ?? '',\n ...timeLimitsOf(test),\n testParts: childElements(test, 'qti-test-part').map(buildTestPart)\n };\n}\n\nfunction buildTestPart(part: XmlElement): TestPartModel {\n const sessionControl = resolveSessionControl(DEFAULT_SESSION_CONTROL, part);\n return {\n identifier: part.getAttribute('identifier') ?? '',\n navigationMode: part.getAttribute('navigation-mode') === 'linear' ? 'linear' : 'nonlinear',\n submissionMode: part.getAttribute('submission-mode') === 'simultaneous' ? 'simultaneous' : 'individual',\n sessionControl,\n ...timeLimitsOf(part),\n sections: childElements(part, 'qti-assessment-section').map(section => buildSection(section, sessionControl))\n };\n}\n\nfunction buildSection(section: XmlElement, inherited: SessionControl): SectionModel {\n const sessionControl = resolveSessionControl(inherited, section);\n return {\n kind: 'section',\n identifier: section.getAttribute('identifier') ?? '',\n title: section.getAttribute('title') ?? '',\n visible: booleanAttribute(section, 'visible') ?? true,\n sessionControl,\n ...timeLimitsOf(section),\n content: childElements(section, 'qti-assessment-section', 'qti-assessment-item-ref').map(child =>\n child.localName === 'qti-assessment-section'\n ? buildSection(child, sessionControl)\n : buildItemRef(child, sessionControl)\n )\n };\n}\n\nfunction buildItemRef(itemRef: XmlElement, inherited: SessionControl): ItemRefModel {\n const category = itemRef.getAttribute('category')?.trim();\n return {\n kind: 'itemRef',\n identifier: itemRef.getAttribute('identifier') ?? '',\n href: itemRef.getAttribute('href') ?? '',\n categories: category ? category.split(/\\s+/) : [],\n sessionControl: resolveSessionControl(inherited, itemRef),\n declaredSessionControl: declaredSessionControl(itemRef),\n ...timeLimitsOf(itemRef)\n };\n}\n\n/** `inherited`, with what `owner`'s own `qti-item-session-control` declares on top. */\nfunction resolveSessionControl(inherited: Readonly<SessionControl>, owner: XmlElement): SessionControl {\n return { ...inherited, ...declaredSessionControl(owner) };\n}\n\n/** Only the attributes `owner`'s `qti-item-session-control` declares, so an absent one inherits. */\nfunction declaredSessionControl(owner: XmlElement): Partial<SessionControl> {\n const element = childElement(owner, 'qti-item-session-control');\n if (!element) return {};\n const declared: Partial<SessionControl> = {\n maxAttempts: integerAttribute(element, 'max-attempts'),\n showFeedback: booleanAttribute(element, 'show-feedback'),\n allowReview: booleanAttribute(element, 'allow-review'),\n showSolution: booleanAttribute(element, 'show-solution'),\n allowComment: booleanAttribute(element, 'allow-comment'),\n allowSkipping: booleanAttribute(element, 'allow-skipping'),\n validateResponses: booleanAttribute(element, 'validate-responses')\n };\n return Object.fromEntries(Object.entries(declared).filter(([, value]) => value !== undefined));\n}\n\n/**\n * `owner`'s own `qti-time-limits` as `{ timeLimits }`, or nothing when it declares none, so the\n * model holds the key only where the XML has the element. A value that is not a non-negative number\n * counts as not declared.\n */\nfunction timeLimitsOf(owner: XmlElement): { timeLimits?: TimeLimits } {\n const element = childElement(owner, 'qti-time-limits');\n if (!element) return {};\n return {\n timeLimits: {\n minTime: nonNegativeNumberAttribute(element, 'min-time') ?? null,\n maxTime: nonNegativeNumberAttribute(element, 'max-time') ?? null,\n allowLateSubmission: booleanAttribute(element, 'allow-late-submission') ?? false\n }\n };\n}\n\n/** The item-refs directly inside a section, in document order; not those of its subsections. */\nexport const ownItemRefs = (section: SectionModel): ItemRefModel[] =>\n section.content.filter((child): child is ItemRefModel => child.kind === 'itemRef');\n\n/** Every section, depth first in document order: a section before its subsections. */\nexport function sectionsInOrder(sections: SectionModel[]): SectionModel[] {\n return sections.flatMap(section => [\n section,\n ...sectionsInOrder(section.content.filter((child): child is SectionModel => child.kind === 'section'))\n ]);\n}\n\n/** Every item-ref inside the given sections, at any depth, in document order. */\nexport function itemRefsInOrder(sections: SectionContent[]): ItemRefModel[] {\n return sections.flatMap(child => (child.kind === 'itemRef' ? [child] : itemRefsInOrder(child.content)));\n}\n"]}
|
package/dist/model/xml-node.d.ts
CHANGED
|
@@ -22,3 +22,8 @@ export declare const childElement: (parent: XmlElement, localName: string) => Xm
|
|
|
22
22
|
export declare function booleanAttribute(element: XmlElement, name: string): boolean | undefined;
|
|
23
23
|
/** An `xs:int` attribute, or undefined when absent or not an integer. */
|
|
24
24
|
export declare function integerAttribute(element: XmlElement, name: string): number | undefined;
|
|
25
|
+
/**
|
|
26
|
+
* A non-negative `xs:double` attribute (QTI's NonNegativeDouble, such as `max-time`), or undefined
|
|
27
|
+
* when absent, not a number, negative, or not finite.
|
|
28
|
+
*/
|
|
29
|
+
export declare function nonNegativeNumberAttribute(element: XmlElement, name: string): number | undefined;
|
package/dist/model/xml-node.js
CHANGED
|
@@ -27,4 +27,15 @@ export function integerAttribute(element, name) {
|
|
|
27
27
|
const value = element.getAttribute(name)?.trim();
|
|
28
28
|
return value && /^[+-]?\d+$/.test(value) ? Number(value) : undefined;
|
|
29
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* A non-negative `xs:double` attribute (QTI's NonNegativeDouble, such as `max-time`), or undefined
|
|
32
|
+
* when absent, not a number, negative, or not finite.
|
|
33
|
+
*/
|
|
34
|
+
export function nonNegativeNumberAttribute(element, name) {
|
|
35
|
+
const value = element.getAttribute(name)?.trim();
|
|
36
|
+
if (!value || !/^[+]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?$/.test(value))
|
|
37
|
+
return undefined;
|
|
38
|
+
const number = Number(value);
|
|
39
|
+
return Number.isFinite(number) ? number :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,SAAS,CAAC;AACnB,CAAC;AAED,yEAAyE;AACzE,MAAM,UAAU,gBAAgB,CAAC,OAAmB,EAAE,IAAY;IAChE,MAAM,KAAK,GAAG,OAAO,CAAC,YAAY,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,CAAC;IACjD,OAAO,KAAK,IAAI,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;AACvE,CAAC","sourcesContent":["/**\n * The part of a DOM element that `delivery` reads: a browser `Element` and an `@xmldom/xmldom`\n * element both satisfy it, so the same code builds the model in the browser and under Node. Only\n * attributes and child elements, never upgraded properties, so nothing depends on a custom element\n * being registered.\n */\nexport interface XmlElement {\n readonly nodeType: number;\n readonly localName: string | null;\n readonly childNodes: ArrayLike<XmlNode>;\n /** Null when the attribute is absent, in the browser and in xmldom alike. */\n getAttribute(name: string): string | null;\n}\n\nexport interface XmlNode {\n readonly nodeType: number;\n}\n\nconst ELEMENT_NODE = 1;\n\n/** The child elements of `parent` with one of the given local names, in document order. */\nexport function childElements(parent: XmlElement, ...localNames: string[]): XmlElement[] {\n const nodes = parent.childNodes;\n const children: XmlElement[] = [];\n for (let index = 0; index < nodes.length; index++) {\n const node = nodes[index];\n if (node.nodeType === ELEMENT_NODE && localNames.includes((node as XmlElement).localName ?? '')) {\n children.push(node as XmlElement);\n }\n }\n return children;\n}\n\n/** The first child element with the given local name, or null. */\nexport const childElement = (parent: XmlElement, localName: string): XmlElement | null =>\n childElements(parent, localName)[0] ?? null;\n\n/** An `xs:boolean` attribute: `true`/`1` and `false`/`0`; anything else, or absent, is undefined. */\nexport function booleanAttribute(element: XmlElement, name: string): boolean | undefined {\n const value = element.getAttribute(name)?.trim();\n if (value === 'true' || value === '1') return true;\n if (value === 'false' || value === '0') return false;\n return undefined;\n}\n\n/** An `xs:int` attribute, or undefined when absent or not an integer. */\nexport function integerAttribute(element: XmlElement, name: string): number | undefined {\n const value = element.getAttribute(name)?.trim();\n return value && /^[+-]?\\d+$/.test(value) ? Number(value) : undefined;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"xml-node.js","sourceRoot":"","sources":["../../src/model/xml-node.ts"],"names":[],"mappings":"AAkBA,MAAM,YAAY,GAAG,CAAC,CAAC;AAEvB,2FAA2F;AAC3F,MAAM,UAAU,aAAa,CAAC,MAAkB,EAAE,GAAG,UAAoB;IACvE,MAAM,KAAK,GAAG,MAAM,CAAC,UAAU,CAAC;IAChC,MAAM,QAAQ,GAAiB,EAAE,CAAC;IAClC,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,KAAK,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QAClD,MAAM,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC;QAC1B,IAAI,IAAI,CAAC,QAAQ,KAAK,YAAY,IAAI,UAAU,CAAC,QAAQ,CAAE,IAAmB,CAAC,SAAS,IAAI,EAAE,CAAC,EAAE,CAAC;YAChG,QAAQ,CAAC,IAAI,CAAC,IAAkB,CAAC,CAAC;QACpC,CAAC;IACH,CAAC;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED,kEAAkE;AAClE,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,MAAkB,EAAE,SAAiB,EAAqB,EAAE,CACvF,aAAa,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC;AAE9C,qGAAqG;AACrG,MAAM,UAAU,gBAAgB,CAAC,OAAmB,EAAE,IAAY;IAChE,MAAM,KAAK,GAAG,OAAO,CAAC,YAAY,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,CAAC;IACjD,IAAI,KAAK,KAAK,MAAM,IAAI,KAAK,KAAK,GAAG;QAAE,OAAO,IAAI,CAAC;IACnD,IAAI,KAAK,KAAK,OAAO,IAAI,KAAK,KAAK,GAAG;QAAE,OAAO,KAAK,CAAC;IACrD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,yEAAyE;AACzE,MAAM,UAAU,gBAAgB,CAAC,OAAmB,EAAE,IAAY;IAChE,MAAM,KAAK,GAAG,OAAO,CAAC,YAAY,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,CAAC;IACjD,OAAO,KAAK,IAAI,YAAY,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;AACvE,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,0BAA0B,CAAC,OAAmB,EAAE,IAAY;IAC1E,MAAM,KAAK,GAAG,OAAO,CAAC,YAAY,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,CAAC;IACjD,IAAI,CAAC,KAAK,IAAI,CAAC,0CAA0C,CAAC,IAAI,CAAC,KAAK,CAAC;QAAE,OAAO,SAAS,CAAC;IACxF,MAAM,MAAM,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC;IAC7B,OAAO,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC;AACtD,CAAC","sourcesContent":["/**\n * The part of a DOM element that `delivery` reads: a browser `Element` and an `@xmldom/xmldom`\n * element both satisfy it, so the same code builds the model in the browser and under Node. Only\n * attributes and child elements, never upgraded properties, so nothing depends on a custom element\n * being registered.\n */\nexport interface XmlElement {\n readonly nodeType: number;\n readonly localName: string | null;\n readonly childNodes: ArrayLike<XmlNode>;\n /** Null when the attribute is absent, in the browser and in xmldom alike. */\n getAttribute(name: string): string | null;\n}\n\nexport interface XmlNode {\n readonly nodeType: number;\n}\n\nconst ELEMENT_NODE = 1;\n\n/** The child elements of `parent` with one of the given local names, in document order. */\nexport function childElements(parent: XmlElement, ...localNames: string[]): XmlElement[] {\n const nodes = parent.childNodes;\n const children: XmlElement[] = [];\n for (let index = 0; index < nodes.length; index++) {\n const node = nodes[index];\n if (node.nodeType === ELEMENT_NODE && localNames.includes((node as XmlElement).localName ?? '')) {\n children.push(node as XmlElement);\n }\n }\n return children;\n}\n\n/** The first child element with the given local name, or null. */\nexport const childElement = (parent: XmlElement, localName: string): XmlElement | null =>\n childElements(parent, localName)[0] ?? null;\n\n/** An `xs:boolean` attribute: `true`/`1` and `false`/`0`; anything else, or absent, is undefined. */\nexport function booleanAttribute(element: XmlElement, name: string): boolean | undefined {\n const value = element.getAttribute(name)?.trim();\n if (value === 'true' || value === '1') return true;\n if (value === 'false' || value === '0') return false;\n return undefined;\n}\n\n/** An `xs:int` attribute, or undefined when absent or not an integer. */\nexport function integerAttribute(element: XmlElement, name: string): number | undefined {\n const value = element.getAttribute(name)?.trim();\n return value && /^[+-]?\\d+$/.test(value) ? Number(value) : undefined;\n}\n\n/**\n * A non-negative `xs:double` attribute (QTI's NonNegativeDouble, such as `max-time`), or undefined\n * when absent, not a number, negative, or not finite.\n */\nexport function nonNegativeNumberAttribute(element: XmlElement, name: string): number | undefined {\n const value = element.getAttribute(name)?.trim();\n if (!value || !/^[+]?(\\d+(\\.\\d*)?|\\.\\d+)([eE][+-]?\\d+)?$/.test(value)) return undefined;\n const number = Number(value);\n return Number.isFinite(number) ? number : undefined;\n}\n"]}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { RandomSource } from './shuffle.js';
|
|
2
|
+
/** A child of a section that selection can pick: an item-ref or a subsection, which counts as one. */
|
|
3
|
+
export interface SelectionUnit {
|
|
4
|
+
/** `required="true"`: always in the selection. */
|
|
5
|
+
required: boolean;
|
|
6
|
+
}
|
|
7
|
+
/** A section's `qti-selection` (QTI 3 info model §5.129). */
|
|
8
|
+
export interface SelectionRule {
|
|
9
|
+
/** How many units to select. */
|
|
10
|
+
select: number;
|
|
11
|
+
/** Whether a unit may be selected more than once (its clones). Defaults to false. */
|
|
12
|
+
withReplacement?: boolean;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Which units of a section a `qti-selection` selects, as indices into `units` in authored order; a
|
|
16
|
+
* unit selected more than once (`with-replacement`) has its index repeated, the clones right after
|
|
17
|
+
* it. Ordering is a later, separate step (implementation guide §4.5.1: selection "should preserve
|
|
18
|
+
* the original order").
|
|
19
|
+
*
|
|
20
|
+
* Implementation guide §4.5.1, and exactly this use of `random`, so every runtime (a .NET port too)
|
|
21
|
+
* selects the same units for the same seed:
|
|
22
|
+
*
|
|
23
|
+
* 1. Every required unit is in.
|
|
24
|
+
* 2. The rest of the first `min(select, units.length)` come from the non-required units, each with
|
|
25
|
+
* an equal chance: a partial Fisher-Yates over the non-required indices in authored order, for
|
|
26
|
+
* `i` from 0 while fewer are picked than needed, swapping position `i` with
|
|
27
|
+
* `i + floor(random() * (remaining - i))`. No draw when all of them, or none, are needed.
|
|
28
|
+
* 3. With replacement and `select` above `units.length`, each unit is then in once, and each further
|
|
29
|
+
* clone is `floor(random() * units.length)`: every unit an equal chance, required or not, however
|
|
30
|
+
* often it was cloned before.
|
|
31
|
+
*
|
|
32
|
+
* Throws a `RangeError` for what QTI calls an error: a `select` that is not a whole number of zero or
|
|
33
|
+
* more, below the number of required units, or above the number of units without replacement (or
|
|
34
|
+
* above zero with no units at all).
|
|
35
|
+
*/
|
|
36
|
+
export declare function selectUnits(units: readonly SelectionUnit[], rule: SelectionRule, random: RandomSource): number[];
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
const randomValue = (source) => (typeof source === 'function' ? source() : source.random());
|
|
2
|
+
/**
|
|
3
|
+
* Which units of a section a `qti-selection` selects, as indices into `units` in authored order; a
|
|
4
|
+
* unit selected more than once (`with-replacement`) has its index repeated, the clones right after
|
|
5
|
+
* it. Ordering is a later, separate step (implementation guide §4.5.1: selection "should preserve
|
|
6
|
+
* the original order").
|
|
7
|
+
*
|
|
8
|
+
* Implementation guide §4.5.1, and exactly this use of `random`, so every runtime (a .NET port too)
|
|
9
|
+
* selects the same units for the same seed:
|
|
10
|
+
*
|
|
11
|
+
* 1. Every required unit is in.
|
|
12
|
+
* 2. The rest of the first `min(select, units.length)` come from the non-required units, each with
|
|
13
|
+
* an equal chance: a partial Fisher-Yates over the non-required indices in authored order, for
|
|
14
|
+
* `i` from 0 while fewer are picked than needed, swapping position `i` with
|
|
15
|
+
* `i + floor(random() * (remaining - i))`. No draw when all of them, or none, are needed.
|
|
16
|
+
* 3. With replacement and `select` above `units.length`, each unit is then in once, and each further
|
|
17
|
+
* clone is `floor(random() * units.length)`: every unit an equal chance, required or not, however
|
|
18
|
+
* often it was cloned before.
|
|
19
|
+
*
|
|
20
|
+
* Throws a `RangeError` for what QTI calls an error: a `select` that is not a whole number of zero or
|
|
21
|
+
* more, below the number of required units, or above the number of units without replacement (or
|
|
22
|
+
* above zero with no units at all).
|
|
23
|
+
*/
|
|
24
|
+
export function selectUnits(units, rule, random) {
|
|
25
|
+
const { select, withReplacement = false } = rule;
|
|
26
|
+
if (!Number.isInteger(select) || select < 0) {
|
|
27
|
+
throw new RangeError(`qti-selection select="${select}" is not a whole number of zero or more.`);
|
|
28
|
+
}
|
|
29
|
+
const required = units.flatMap((unit, index) => (unit.required ? [index] : []));
|
|
30
|
+
if (select < required.length) {
|
|
31
|
+
throw new RangeError(`qti-selection select="${select}" is less than the ${required.length} required items and subsections.`);
|
|
32
|
+
}
|
|
33
|
+
if (select > units.length && (!withReplacement || units.length === 0)) {
|
|
34
|
+
throw new RangeError(`qti-selection select="${select}" is more than the ${units.length} items and subsections, without with-replacement="true".`);
|
|
35
|
+
}
|
|
36
|
+
const optional = units.flatMap((unit, index) => (unit.required ? [] : [index]));
|
|
37
|
+
const needed = Math.min(select, units.length) - required.length;
|
|
38
|
+
if (needed < optional.length) {
|
|
39
|
+
for (let i = 0; i < needed; i++) {
|
|
40
|
+
const j = i + Math.floor(randomValue(random) * (optional.length - i));
|
|
41
|
+
[optional[i], optional[j]] = [optional[j], optional[i]];
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const occurrences = new Array(units.length).fill(0);
|
|
45
|
+
for (const index of [...required, ...optional.slice(0, needed)])
|
|
46
|
+
occurrences[index] = 1;
|
|
47
|
+
for (let clone = units.length; clone < select; clone++) {
|
|
48
|
+
occurrences[Math.floor(randomValue(random) * units.length)]++;
|
|
49
|
+
}
|
|
50
|
+
return occurrences.flatMap((count, index) => new Array(count).fill(index));
|
|
51
|
+
}
|
|
52
|
+
//# sourceMappingURL=selection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"selection.js","sourceRoot":"","sources":["../../src/random/selection.ts"],"names":[],"mappings":"AAgBA,MAAM,WAAW,GAAG,CAAC,MAAoB,EAAU,EAAE,CAAC,CAAC,OAAO,MAAM,KAAK,UAAU,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC;AAElH;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,UAAU,WAAW,CAAC,KAA+B,EAAE,IAAmB,EAAE,MAAoB;IACpG,MAAM,EAAE,MAAM,EAAE,eAAe,GAAG,KAAK,EAAE,GAAG,IAAI,CAAC;IACjD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,MAAM,GAAG,CAAC,EAAE,CAAC;QAC5C,MAAM,IAAI,UAAU,CAAC,yBAAyB,MAAM,0CAA0C,CAAC,CAAC;IAClG,CAAC;IACD,MAAM,QAAQ,GAAG,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IAChF,IAAI,MAAM,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC;QAC7B,MAAM,IAAI,UAAU,CAClB,yBAAyB,MAAM,sBAAsB,QAAQ,CAAC,MAAM,kCAAkC,CACvG,CAAC;IACJ,CAAC;IACD,IAAI,MAAM,G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AC,GAAG,CAAC,CAAC;IAExF,KAAK,IAAI,KAAK,GAAG,KAAK,CAAC,MAAM,EAAE,KAAK,GAAG,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;QACvD,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC;IAChE,CAAC;IAED,OAAO,WAAW,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,IAAI,KAAK,CAAS,KAAK,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;AACrF,CAAC","sourcesContent":["import type { RandomSource } from './shuffle.js';\n\n/** A child of a section that selection can pick: an item-ref or a subsection, which counts as one. */\nexport interface SelectionUnit {\n /** `required=\"true\"`: always in the selection. */\n required: boolean;\n}\n\n/** A section's `qti-selection` (QTI 3 info model §5.129). */\nexport interface SelectionRule {\n /** How many units to select. */\n select: number;\n /** Whether a unit may be selected more than once (its clones). Defaults to false. */\n withReplacement?: boolean;\n}\n\nconst randomValue = (source: RandomSource): number => (typeof source === 'function' ? source() : source.random());\n\n/**\n * Which units of a section a `qti-selection` selects, as indices into `units` in authored order; a\n * unit selected more than once (`with-replacement`) has its index repeated, the clones right after\n * it. Ordering is a later, separate step (implementation guide §4.5.1: selection \"should preserve\n * the original order\").\n *\n * Implementation guide §4.5.1, and exactly this use of `random`, so every runtime (a .NET port too)\n * selects the same units for the same seed:\n *\n * 1. Every required unit is in.\n * 2. The rest of the first `min(select, units.length)` come from the non-required units, each with\n * an equal chance: a partial Fisher-Yates over the non-required indices in authored order, for\n * `i` from 0 while fewer are picked than needed, swapping position `i` with\n * `i + floor(random() * (remaining - i))`. No draw when all of them, or none, are needed.\n * 3. With replacement and `select` above `units.length`, each unit is then in once, and each further\n * clone is `floor(random() * units.length)`: every unit an equal chance, required or not, however\n * often it was cloned before.\n *\n * Throws a `RangeError` for what QTI calls an error: a `select` that is not a whole number of zero or\n * more, below the number of required units, or above the number of units without replacement (or\n * above zero with no units at all).\n */\nexport function selectUnits(units: readonly SelectionUnit[], rule: SelectionRule, random: RandomSource): number[] {\n const { select, withReplacement = false } = rule;\n if (!Number.isInteger(select) || select < 0) {\n throw new RangeError(`qti-selection select=\"${select}\" is not a whole number of zero or more.`);\n }\n const required = units.flatMap((unit, index) => (unit.required ? [index] : []));\n if (select < required.length) {\n throw new RangeError(\n `qti-selection select=\"${select}\" is less than the ${required.length} required items and subsections.`\n );\n }\n if (select > units.length && (!withReplacement || units.length === 0)) {\n throw new RangeError(\n `qti-selection select=\"${select}\" is more than the ${units.length} items and subsections, without with-replacement=\"true\".`\n );\n }\n\n const optional = units.flatMap((unit, index) => (unit.required ? [] : [index]));\n const needed = Math.min(select, units.length) - required.length;\n if (needed < optional.length) {\n for (let i = 0; i < needed; i++) {\n const j = i + Math.floor(randomValue(random) * (optional.length - i));\n [optional[i], optional[j]] = [optional[j], optional[i]];\n }\n }\n const occurrences = new Array<number>(units.length).fill(0);\n for (const index of [...required, ...optional.slice(0, needed)]) occurrences[index] = 1;\n\n for (let clone = units.length; clone < select; clone++) {\n occurrences[Math.floor(randomValue(random) * units.length)]++;\n }\n\n return occurrences.flatMap((count, index) => new Array<number>(count).fill(index));\n}\n"]}
|
|
@@ -3,6 +3,16 @@ import type { DeliveryState } from './delivery-state.js';
|
|
|
3
3
|
export interface CreateDeliveryStateOptions {
|
|
4
4
|
clock: Clock;
|
|
5
5
|
seed?: string | null;
|
|
6
|
+
/**
|
|
7
|
+
* The items after selection and ordering, in the order the candidate meets them: the rendered
|
|
8
|
+
* test's item-ref identifiers, where a `with-replacement` clone is its instance id (`item-3#2`,
|
|
9
|
+
* roadmap 1 T4) and keeps that occurrence wherever ordering put it. An item-ref listed again
|
|
10
|
+
* without a suffix is its next free occurrence.
|
|
11
|
+
*/
|
|
12
|
+
order?: readonly string[];
|
|
6
13
|
}
|
|
7
|
-
/**
|
|
8
|
-
|
|
14
|
+
/**
|
|
15
|
+
* A fresh delivery state: the item instances in `order`, each `initial` with no attempts and
|
|
16
|
+
* `not_attempted` with no time spent, nothing on screen, no plugin entries, started now by the given clock.
|
|
17
|
+
*/
|
|
18
|
+
export declare function createDeliveryState({ clock, seed, order }: CreateDeliveryStateOptions): DeliveryState;
|
|
@@ -1,6 +1,41 @@
|
|
|
1
|
-
import { DELIVERY_STATE_VERSION } from './delivery-state.js';
|
|
2
|
-
/**
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { DELIVERY_STATE_VERSION, itemInstanceId, parseItemInstanceId, stoppedStopwatch } from './delivery-state.js';
|
|
2
|
+
/**
|
|
3
|
+
* A fresh delivery state: the item instances in `order`, each `initial` with no attempts and
|
|
4
|
+
* `not_attempted` with no time spent, nothing on screen, no plugin entries, started now by the given clock.
|
|
5
|
+
*/
|
|
6
|
+
export function createDeliveryState({ clock, seed = null, order = [] }) {
|
|
7
|
+
const parsed = order.map(parseItemInstanceId);
|
|
8
|
+
// Occurrences the order names itself, so a bare repeat never takes a clone's number.
|
|
9
|
+
const taken = new Set(parsed.filter(p => p.occurrence > 1).map(p => itemInstanceId(p.itemRefIdentifier, p.occurrence)));
|
|
10
|
+
const items = {};
|
|
11
|
+
const instances = parsed.map(({ itemRefIdentifier, occurrence }) => {
|
|
12
|
+
let id = itemInstanceId(itemRefIdentifier, occurrence);
|
|
13
|
+
if (occurrence === 1) {
|
|
14
|
+
while (Object.hasOwn(items, id) || taken.has(id))
|
|
15
|
+
id = itemInstanceId(itemRefIdentifier, ++occurrence);
|
|
16
|
+
}
|
|
17
|
+
else if (Object.hasOwn(items, id)) {
|
|
18
|
+
throw new RangeError(`The order lists the item instance ${id} twice.`);
|
|
19
|
+
}
|
|
20
|
+
items[id] = {
|
|
21
|
+
itemRefIdentifier,
|
|
22
|
+
occurrence,
|
|
23
|
+
lifecycle: 'initial',
|
|
24
|
+
numAttempts: 0,
|
|
25
|
+
completionStatus: 'not_attempted',
|
|
26
|
+
duration: stoppedStopwatch()
|
|
27
|
+
};
|
|
28
|
+
return id;
|
|
29
|
+
});
|
|
30
|
+
return {
|
|
31
|
+
version: DELIVERY_STATE_VERSION,
|
|
32
|
+
seed,
|
|
33
|
+
startedAt: clock.now(),
|
|
34
|
+
current: null,
|
|
35
|
+
order: instances,
|
|
36
|
+
timing: { test: stoppedStopwatch(), testParts: {}, sections: {} },
|
|
37
|
+
plugins: {},
|
|
38
|
+
items
|
|
39
|
+
};
|
|
5
40
|
}
|
|
6
41
|
//# sourceMappingURL=create-delivery-state.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-delivery-state.js","sourceRoot":"","sources":["../../src/state/create-delivery-state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"create-delivery-state.js","sourceRoot":"","sources":["../../src/state/create-delivery-state.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,cAAc,EAAE,mBAAmB,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAiBpH;;;GAGG;AACH,MAAM,UAAU,mBAAmB,CAAC,EAAE,KAAK,EAAE,IAAI,GAAG,IAAI,EAAE,KAAK,GAAG,EAAE,EAA8B;IAChG,MAAM,MAAM,GAAG,KAAK,CAAC,GAAG,CAAC,mBAAmB,CAAC,CAAC;IAC9C,qFAAqF;IACrF,MAAM,KAAK,GAAG,IAAI,GAAG,CACnB,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,UAAU,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,cAAc,CAAC,CAAC,CAAC,iBAAiB,EAAE,CAAC,CAAC,UAAU,CAAC,CAAC,CACjG,CAAC;IACF,MAAM,KAAK,GAA8C,EAAE,CAAC;IAC5D,MAAM,SAAS,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,EAAE,iBAAiB,EAAE,UAAU,EAAE,EAAE,EAAE;QACjE,IAAI,EAAE,GAAG,cAAc,CAAC,iBAAiB,EAAE,UAAU,CAAC,CAAC;QACvD,IAAI,UAAU,KAAK,CAAC,EAAE,CAAC;YACrB,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC;gBAAE,EAAE,GAAG,cAAc,CAAC,iBAAiB,EAAE,EAAE,UAAU,CAAC,CAAC;QACzG,CAAC;aAAM,IAAI,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,CAAC,EAAE,CAAC;YACpC,MAAM,IAAI,UAAU,CAAC,qCAAqC,EAAE,SAAS,CAAC,CAAC;QACzE,CAAC;QACD,KAAK,CAAC,EAAE,CAAC,GAAG;YACV,iBAAiB;YACjB,UAAU;YACV,SAAS,EAAE,SAAS;YACpB,WAAW,EAAE,CAAC;YACd,gBAAgB,EAAE,eAAe;YACjC,QAAQ,EAAE,gBAAgB,EAAE;SAC7B,CAAC;QACF,OAAO,EAAE,CAAC;IACZ,CAAC,CAAC,CAAC;IACH,OAAO;QACL,OAAO,EAAE,sBAAsB;QAC/B,IAAI;QACJ,SAAS,EAAE,KAAK,CAAC,GAAG,EAAE;QACtB,OAAO,EAAE,IAAI;QACb,KAAK,EAAE,SAAS;QAChB,MAAM,EAAE,EAAE,IAAI,EAAE,gBAAgB,EAAE,EAAE,SAAS,EAAE,EAAE,EAAE,QAAQ,EAAE,EAAE,EAAE;QACjE,OAAO,EAAE,EAAE;QACX,KAAK;KACN,CAAC;AACJ,CAAC","sourcesContent":["import { DELIVERY_STATE_VERSION, itemInstanceId, parseItemInstanceId, stoppedStopwatch } from './delivery-state.js';\n\nimport type { Clock } from './clock.js';\nimport type { DeliveryState, ItemDeliveryState, ItemInstanceId } from './delivery-state.js';\n\nexport interface CreateDeliveryStateOptions {\n clock: Clock;\n seed?: string | null;\n /**\n * The items after selection and ordering, in the order the candidate meets them: the rendered\n * test's item-ref identifiers, where a `with-replacement` clone is its instance id (`item-3#2`,\n * roadmap 1 T4) and keeps that occurrence wherever ordering put it. An item-ref listed again\n * without a suffix is its next free occurrence.\n */\n order?: readonly string[];\n}\n\n/**\n * A fresh delivery state: the item instances in `order`, each `initial` with no attempts and\n * `not_attempted` with no time spent, nothing on screen, no plugin entries, started now by the given clock.\n */\nexport function createDeliveryState({ clock, seed = null, order = [] }: CreateDeliveryStateOptions): DeliveryState {\n const parsed = order.map(parseItemInstanceId);\n // Occurrences the order names itself, so a bare repeat never takes a clone's number.\n const taken = new Set(\n parsed.filter(p => p.occurrence > 1).map(p => itemInstanceId(p.itemRefIdentifier, p.occurrence))\n );\n const items: Record<ItemInstanceId, ItemDeliveryState> = {};\n const instances = parsed.map(({ itemRefIdentifier, occurrence }) => {\n let id = itemInstanceId(itemRefIdentifier, occurrence);\n if (occurrence === 1) {\n while (Object.hasOwn(items, id) || taken.has(id)) id = itemInstanceId(itemRefIdentifier, ++occurrence);\n } else if (Object.hasOwn(items, id)) {\n throw new RangeError(`The order lists the item instance ${id} twice.`);\n }\n items[id] = {\n itemRefIdentifier,\n occurrence,\n lifecycle: 'initial',\n numAttempts: 0,\n completionStatus: 'not_attempted',\n duration: stoppedStopwatch()\n };\n return id;\n });\n return {\n version: DELIVERY_STATE_VERSION,\n seed,\n startedAt: clock.now(),\n current: null,\n order: instances,\n timing: { test: stoppedStopwatch(), testParts: {}, sections: {} },\n plugins: {},\n items\n };\n}\n"]}
|
|
@@ -1,24 +1,58 @@
|
|
|
1
|
-
|
|
2
|
-
* The one source of truth for one candidate in one test: what a resumed test, or a server taking
|
|
3
|
-
* over, needs to carry on. Written only through commands; every context is an input or a view
|
|
4
|
-
* derived from it, and anything derivable (done, can*, scores) is never stored here. Plain JSON,
|
|
5
|
-
* versioned, so a stored state can be read back by a later release (or a .NET port) and refused
|
|
6
|
-
* when it cannot.
|
|
7
|
-
*
|
|
8
|
-
* Experimental (0.x): roadmap 1 fills in the target fields, phase by phase; see
|
|
9
|
-
* `plans/delivery-state.md`.
|
|
10
|
-
*/
|
|
1
|
+
import type { ItemLifecycleState } from '../lifecycle/item-lifecycle.js';
|
|
11
2
|
export declare const DELIVERY_STATE_VERSION = 1;
|
|
3
|
+
/** QTI's values of the built-in `completionStatus` outcome (info model §2.2.2). */
|
|
4
|
+
export declare const COMPLETION_STATUSES: readonly ["not_attempted", "unknown", "incomplete", "completed"];
|
|
5
|
+
export type CompletionStatus = (typeof COMPLETION_STATUSES)[number];
|
|
12
6
|
/**
|
|
13
7
|
* One item session. With `with-replacement` selection a single item-ref appears more than once,
|
|
14
8
|
* so a session is keyed by its instance, never by the item-ref identifier alone.
|
|
15
9
|
*/
|
|
16
10
|
export type ItemInstanceId = string;
|
|
11
|
+
/**
|
|
12
|
+
* Time measured, never counted (roadmap 1 T5): the time of the stretches already ended, plus the
|
|
13
|
+
* moment the running stretch began. A timer only checks limits and repaints; it never adds time, so
|
|
14
|
+
* a throttled tab or a reload neither loses nor doubles any. Milliseconds, from the injected clock.
|
|
15
|
+
*/
|
|
16
|
+
export interface Stopwatch {
|
|
17
|
+
/** The time of every stretch already ended, in milliseconds. */
|
|
18
|
+
spent: number;
|
|
19
|
+
/** When the running stretch began (milliseconds since the epoch), or null when stopped. */
|
|
20
|
+
since: number | null;
|
|
21
|
+
}
|
|
22
|
+
/** A stopwatch that has not run. */
|
|
23
|
+
export declare const stoppedStopwatch: () => Stopwatch;
|
|
24
|
+
/**
|
|
25
|
+
* The time spent in the test and in each test part and section (QTI 3 info model §2.8.5): all the
|
|
26
|
+
* time the candidate session was in it, navigation included. A part or section has an entry from
|
|
27
|
+
* the first time it is entered.
|
|
28
|
+
*/
|
|
29
|
+
export interface DeliveryTiming {
|
|
30
|
+
test: Stopwatch;
|
|
31
|
+
testParts: Record<string, Stopwatch>;
|
|
32
|
+
sections: Record<string, Stopwatch>;
|
|
33
|
+
}
|
|
17
34
|
export interface ItemDeliveryState {
|
|
18
35
|
/** The `identifier` of the `qti-assessment-item-ref` this instance was selected from. */
|
|
19
36
|
itemRefIdentifier: string;
|
|
20
37
|
/** 1 for the first time the item-ref was selected, 2 for its first clone, and so on. */
|
|
21
38
|
occurrence: number;
|
|
39
|
+
/**
|
|
40
|
+
* The item session's state in QTI's lifecycle (`ITEM_LIFECYCLE_STATES`, roadmap 1 T3d). What the
|
|
41
|
+
* session may do (`itemRules`: the can* answers) is derived from it, never stored.
|
|
42
|
+
*/
|
|
43
|
+
lifecycle: ItemLifecycleState;
|
|
44
|
+
/**
|
|
45
|
+
* The attempts the candidate has ended. QTI's `numAttempts` counts an attempt as it begins; this
|
|
46
|
+
* delivery counts it when it ends, as a version 1 `QtiTestState` stores it (`plans/delivery-state.md`).
|
|
47
|
+
*/
|
|
48
|
+
numAttempts: number;
|
|
49
|
+
/** QTI's built-in `completionStatus`: `not_attempted` until the item is shown. */
|
|
50
|
+
completionStatus: CompletionStatus;
|
|
51
|
+
/**
|
|
52
|
+
* QTI's built-in `duration` (info model §2.2.2.2): the time the session was `interacting`, over
|
|
53
|
+
* all attempts and candidate sessions (decided 2026-12-01, roadmap 1 T5).
|
|
54
|
+
*/
|
|
55
|
+
duration: Stopwatch;
|
|
22
56
|
}
|
|
23
57
|
export interface DeliveryState {
|
|
24
58
|
version: typeof DELIVERY_STATE_VERSION;
|
|
@@ -31,12 +65,36 @@ export interface DeliveryState {
|
|
|
31
65
|
startedAt: number;
|
|
32
66
|
/** The item instance on screen, or null before the first navigation and after the end. */
|
|
33
67
|
current: ItemInstanceId | null;
|
|
68
|
+
/**
|
|
69
|
+
* Every item instance of the test after selection and ordering, in the order the candidate meets
|
|
70
|
+
* them; written once, when the test starts. Each one is a key of `items`.
|
|
71
|
+
*/
|
|
72
|
+
order: ItemInstanceId[];
|
|
73
|
+
/** The time spent in the test, its parts and its sections (roadmap 1 T5). */
|
|
74
|
+
timing: DeliveryTiming;
|
|
75
|
+
/**
|
|
76
|
+
* What test plugins keep, by plugin name: JSON, stored and restored without being read (a
|
|
77
|
+
* plugin reads its own entry).
|
|
78
|
+
*/
|
|
79
|
+
plugins: Record<string, unknown>;
|
|
80
|
+
/** One entry per instance in `order`. */
|
|
34
81
|
items: Record<ItemInstanceId, ItemDeliveryState>;
|
|
35
82
|
}
|
|
36
83
|
/** The instance id of the `occurrence`th selection of an item-ref: `item-3`, `item-3#2`. */
|
|
37
84
|
export declare const itemInstanceId: (itemRefIdentifier: string, occurrence?: number) => ItemInstanceId;
|
|
38
85
|
/**
|
|
39
|
-
*
|
|
40
|
-
*
|
|
86
|
+
* The item-ref and occurrence an instance id names, the inverse of `itemInstanceId`: `item-3#2` is
|
|
87
|
+
* the second selection of `item-3`. Only a suffix `itemInstanceId` writes (`#2` and up) counts; `#`
|
|
88
|
+
* cannot occur in a QTI identifier, so no authored identifier reads as a clone.
|
|
89
|
+
*/
|
|
90
|
+
export declare function parseItemInstanceId(id: ItemInstanceId): {
|
|
91
|
+
itemRefIdentifier: string;
|
|
92
|
+
occurrence: number;
|
|
93
|
+
};
|
|
94
|
+
/**
|
|
95
|
+
* Read a stored state back. Throws on a state this release cannot read rather than guessing, so a
|
|
96
|
+
* host never resumes from something half understood: a `RangeError` for another version, a
|
|
97
|
+
* `TypeError` for anything that is not a version 1 state (a missing or mistyped field, an `order`
|
|
98
|
+
* or `current` that names an instance `items` does not have).
|
|
41
99
|
*/
|
|
42
100
|
export declare function parseDeliveryState(value: unknown): DeliveryState;
|
|
@@ -8,21 +8,84 @@
|
|
|
8
8
|
* Experimental (0.x): roadmap 1 fills in the target fields, phase by phase; see
|
|
9
9
|
* `plans/delivery-state.md`.
|
|
10
10
|
*/
|
|
11
|
+
import { ITEM_LIFECYCLE_STATES } from '../lifecycle/item-lifecycle.js';
|
|
11
12
|
export const DELIVERY_STATE_VERSION = 1;
|
|
13
|
+
/** QTI's values of the built-in `completionStatus` outcome (info model §2.2.2). */
|
|
14
|
+
export const COMPLETION_STATUSES = ['not_attempted', 'unknown', 'incomplete', 'completed'];
|
|
15
|
+
/** A stopwatch that has not run. */
|
|
16
|
+
export const stoppedStopwatch = () => ({ spent: 0, since: null });
|
|
12
17
|
/** The instance id of the `occurrence`th selection of an item-ref: `item-3`, `item-3#2`. */
|
|
13
18
|
export const itemInstanceId = (itemRefIdentifier, occurrence = 1) => occurrence === 1 ? itemRefIdentifier : `${itemRefIdentifier}#${occurrence}`;
|
|
14
19
|
/**
|
|
15
|
-
*
|
|
16
|
-
*
|
|
20
|
+
* The item-ref and occurrence an instance id names, the inverse of `itemInstanceId`: `item-3#2` is
|
|
21
|
+
* the second selection of `item-3`. Only a suffix `itemInstanceId` writes (`#2` and up) counts; `#`
|
|
22
|
+
* cannot occur in a QTI identifier, so no authored identifier reads as a clone.
|
|
23
|
+
*/
|
|
24
|
+
export function parseItemInstanceId(id) {
|
|
25
|
+
const match = /^(.+)#([1-9]\d*)$/.exec(id);
|
|
26
|
+
const occurrence = match ? Number(match[2]) : 1;
|
|
27
|
+
return match && occurrence >= 2
|
|
28
|
+
? { itemRefIdentifier: match[1], occurrence }
|
|
29
|
+
: { itemRefIdentifier: id, occurrence: 1 };
|
|
30
|
+
}
|
|
31
|
+
const isRecord = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
32
|
+
const isTime = (value) => typeof value === 'number' && Number.isFinite(value) && value >= 0;
|
|
33
|
+
const isStopwatch = (value) => isRecord(value) && isTime(value.spent) && (value.since === null || isTime(value.since));
|
|
34
|
+
/**
|
|
35
|
+
* Read a stored state back. Throws on a state this release cannot read rather than guessing, so a
|
|
36
|
+
* host never resumes from something half understood: a `RangeError` for another version, a
|
|
37
|
+
* `TypeError` for anything that is not a version 1 state (a missing or mistyped field, an `order`
|
|
38
|
+
* or `current` that names an instance `items` does not have).
|
|
17
39
|
*/
|
|
18
40
|
export function parseDeliveryState(value) {
|
|
19
|
-
if (
|
|
41
|
+
if (!isRecord(value)) {
|
|
20
42
|
throw new TypeError('A delivery state is an object.');
|
|
21
43
|
}
|
|
22
44
|
const { version } = value;
|
|
23
45
|
if (version !== DELIVERY_STATE_VERSION) {
|
|
24
46
|
throw new RangeError(`Delivery state version ${String(version)} is not supported; expected ${DELIVERY_STATE_VERSION}.`);
|
|
25
47
|
}
|
|
48
|
+
const { seed, startedAt, current, order, timing, plugins, items } = value;
|
|
49
|
+
const fail = (field, expected) => {
|
|
50
|
+
throw new TypeError(`A delivery state's ${field} must be ${expected}.`);
|
|
51
|
+
};
|
|
52
|
+
if (seed !== null && typeof seed !== 'string')
|
|
53
|
+
fail('seed', 'a string or null');
|
|
54
|
+
if (typeof startedAt !== 'number' || !Number.isFinite(startedAt))
|
|
55
|
+
fail('startedAt', 'a number');
|
|
56
|
+
if (!isRecord(items))
|
|
57
|
+
fail('items', 'an object');
|
|
58
|
+
for (const [id, item] of Object.entries(items)) {
|
|
59
|
+
if (!isRecord(item) || typeof item.itemRefIdentifier !== 'string' || !Number.isInteger(item.occurrence)) {
|
|
60
|
+
fail(`items["${id}"]`, 'an object with an itemRefIdentifier and an integer occurrence');
|
|
61
|
+
}
|
|
62
|
+
const session = item;
|
|
63
|
+
if (!ITEM_LIFECYCLE_STATES.includes(session.lifecycle)) {
|
|
64
|
+
fail(`items["${id}"].lifecycle`, `one of ${ITEM_LIFECYCLE_STATES.join(', ')}`);
|
|
65
|
+
}
|
|
66
|
+
if (!Number.isInteger(session.numAttempts) || session.numAttempts < 0) {
|
|
67
|
+
fail(`items["${id}"].numAttempts`, 'a whole number, 0 or more');
|
|
68
|
+
}
|
|
69
|
+
if (!COMPLETION_STATUSES.includes(session.completionStatus)) {
|
|
70
|
+
fail(`items["${id}"].completionStatus`, `one of ${COMPLETION_STATUSES.join(', ')}`);
|
|
71
|
+
}
|
|
72
|
+
if (!isStopwatch(session.duration))
|
|
73
|
+
fail(`items["${id}"].duration`, 'a stopwatch');
|
|
74
|
+
}
|
|
75
|
+
const stopwatches = (record) => isRecord(record) && Object.values(record).every(isStopwatch);
|
|
76
|
+
if (!isRecord(timing) ||
|
|
77
|
+
!isStopwatch(timing.test) ||
|
|
78
|
+
!stopwatches(timing.testParts) ||
|
|
79
|
+
!stopwatches(timing.sections)) {
|
|
80
|
+
fail('timing', 'an object with a stopwatch for the test and records of stopwatches for testParts and sections');
|
|
81
|
+
}
|
|
82
|
+
const known = (id) => typeof id === 'string' && Object.hasOwn(items, id);
|
|
83
|
+
if (!Array.isArray(order) || !order.every(known))
|
|
84
|
+
fail('order', 'an array of instance ids in items');
|
|
85
|
+
if (current !== null && !known(current))
|
|
86
|
+
fail('current', 'null or an instance id in items');
|
|
87
|
+
if (!isRecord(plugins))
|
|
88
|
+
fail('plugins', 'an object');
|
|
26
89
|
return value;
|
|
27
90
|
}
|
|
28
91
|
//# sourceMappingURL=delivery-state.js.map
|