@pie-qti/assessment-player 0.1.27 → 1.0.0-beta.0
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/README.md +13 -0
- package/dist/core/AssessmentPlayer.d.ts +54 -14
- package/dist/core/AssessmentPlayer.js +204 -166
- package/dist/core/AssessmentPlayer.js.map +1 -1
- package/dist/core/AssessmentSessionCoordinator.d.ts +10 -2
- package/dist/core/AssessmentSessionCoordinator.js +24 -3
- package/dist/core/AssessmentSessionCoordinator.js.map +1 -1
- package/dist/core/ItemSessionController.d.ts +14 -3
- package/dist/core/ItemSessionController.js +31 -12
- package/dist/core/ItemSessionController.js.map +1 -1
- package/dist/core/TimeManager.d.ts +20 -7
- package/dist/core/TimeManager.js +20 -18
- package/dist/core/TimeManager.js.map +1 -1
- package/dist/core/testStructure.d.ts +65 -0
- package/dist/core/testStructure.js +336 -0
- package/dist/core/testStructure.js.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/integration/ReferenceBackendAdapter.d.ts +53 -5
- package/dist/integration/ReferenceBackendAdapter.js +317 -230
- package/dist/integration/ReferenceBackendAdapter.js.map +1 -1
- package/dist/integration/api-contract.d.ts +92 -49
- package/dist/integration/api-contract.js.map +1 -1
- package/dist/integration/assessment-item-scorer.d.ts +11 -1
- package/dist/integration/assessment-item-scorer.js +102 -12
- package/dist/integration/assessment-item-scorer.js.map +1 -1
- package/dist/integration/index.d.ts +1 -1
- package/dist/integration/index.js.map +1 -1
- package/dist/integration/toSectionComposition.js +1 -0
- package/dist/integration/toSectionComposition.js.map +1 -1
- package/dist/types/index.d.ts +10 -0
- package/dist/types/index.js.map +1 -1
- package/package.json +10 -10
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* QTI test structure shared by backends and the client: the itemSessionControl cascade, the
|
|
3
|
+
* session-start draw of selection and ordering, delivery order, and test-scope expressions.
|
|
4
|
+
*/
|
|
5
|
+
import { type DeclarationMap, type QtiValue } from '@pie-qti/qti-processing';
|
|
6
|
+
import type { SecureAssessment, SecureItemRef, SecureItemSessionControl, SecureSection, SecureSectionComponent, SecureTestPart, TestOutcomeValues } from '../integration/api-contract.js';
|
|
7
|
+
export type ResolvedItemSessionControl = Required<SecureItemSessionControl>;
|
|
8
|
+
/** QTI itemSessionControl defaults, applied where no level of the cascade sets an attribute. */
|
|
9
|
+
export declare const DEFAULT_ITEM_SESSION_CONTROL: Readonly<ResolvedItemSessionControl>;
|
|
10
|
+
/** True when the item root declares `adaptive="true"`. */
|
|
11
|
+
export declare function isAdaptiveItemXml(itemXml: string): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Effective itemSessionControl for an item: the test part, each enclosing section from outer to
|
|
14
|
+
* inner, then the item ref, each set attribute overriding the level above. Adaptive items allow
|
|
15
|
+
* unlimited attempts whatever maxAttempts says.
|
|
16
|
+
*/
|
|
17
|
+
export declare function resolveItemSessionControl(testPart: SecureTestPart, sections: readonly SecureSection[], item: SecureItemRef): ResolvedItemSessionControl;
|
|
18
|
+
/** Direct delivery components of a section, in document order. */
|
|
19
|
+
export declare function sectionComponents(section: SecureSection): SecureSectionComponent[];
|
|
20
|
+
export interface DeliveredItem {
|
|
21
|
+
item: SecureItemRef;
|
|
22
|
+
testPart: SecureTestPart;
|
|
23
|
+
/** Enclosing sections, outermost first. */
|
|
24
|
+
sections: SecureSection[];
|
|
25
|
+
}
|
|
26
|
+
/** Items in delivery order. Draws nothing: call it on an assessment the backend has drawn. */
|
|
27
|
+
export declare function deliveredItems(assessment: SecureAssessment): DeliveredItem[];
|
|
28
|
+
/**
|
|
29
|
+
* Draws selection and ordering for one session. Required children are always selected; selected
|
|
30
|
+
* children keep source order before ordering shuffles the non-fixed ones. A child selected more
|
|
31
|
+
* than once with replacement is cloned, its identifier and every descendant identifier suffixed
|
|
32
|
+
* `.2`, `.3` and so on. An invisible child section with keepTogether false is mixed into its
|
|
33
|
+
* parent's shuffle. The result carries no selection or ordering, so drawing it again changes
|
|
34
|
+
* nothing.
|
|
35
|
+
*/
|
|
36
|
+
export declare function drawAssessment(assessment: SecureAssessment, rng: () => number): SecureAssessment;
|
|
37
|
+
/** Fisher-Yates shuffle using a provided RNG (returns a new array). */
|
|
38
|
+
export declare function shuffleArray<T>(arr: readonly T[], rng: () => number): T[];
|
|
39
|
+
/** Canonical QTI 2.x element name for a QTI 2.x or QTI 3 (`qti-` kebab-case) element name. */
|
|
40
|
+
export declare function toCanonicalQtiName(name: string | null | undefined): string;
|
|
41
|
+
/** Reads QTI 2.x camelCase and QTI 3 `qti-` kebab-case element names alike. */
|
|
42
|
+
export declare const qtiElementNameMapper: {
|
|
43
|
+
version: string;
|
|
44
|
+
toCanonical: (elementName: string) => string;
|
|
45
|
+
toNative: (canonicalName: string) => string;
|
|
46
|
+
isValidElementName: () => boolean;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Declarations for the test's outcome variables, set to `outcomes` where given and to their
|
|
50
|
+
* defaults otherwise. A numeric outcome with no declared default starts at 0.
|
|
51
|
+
*/
|
|
52
|
+
export declare function testOutcomeDeclarations(assessment: SecureAssessment, outcomes?: TestOutcomeValues | null): DeclarationMap;
|
|
53
|
+
/**
|
|
54
|
+
* Adds item variables as `itemIdentifier.VARIABLE`, the form test-scope expressions use, and the
|
|
55
|
+
* `unprefixed` variables under their own identifiers where the test declares no such outcome.
|
|
56
|
+
*/
|
|
57
|
+
export declare function addItemVariableDeclarations(decls: DeclarationMap, itemVariables: Record<string, Record<string, QtiValue>>, unprefixed?: Record<string, QtiValue>): DeclarationMap;
|
|
58
|
+
/** Item variables with each variableMapping's source also readable as its target identifier. */
|
|
59
|
+
export declare function applyVariableMappings(variables: Record<string, QtiValue>, mappings: SecureItemRef['variableMappings']): Record<string, QtiValue>;
|
|
60
|
+
/** Evaluates a serialized test-scope expression (QTI 2.x or 3) against `decls`. */
|
|
61
|
+
export declare function evaluateTestExpression(expressionXml: string, decls: DeclarationMap, rng: () => number): QtiValue;
|
|
62
|
+
/** True only when the expression evaluates to boolean true; NULL and errors read as false. */
|
|
63
|
+
export declare function evaluateTestCondition(expressionXml: string, decls: DeclarationMap, rng: () => number): boolean;
|
|
64
|
+
/** Plain value of a QTI value, null for NULL, in the shape item template defaults take. */
|
|
65
|
+
export declare function plainQtiValue(value: QtiValue): unknown;
|
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* QTI test structure shared by backends and the client: the itemSessionControl cascade, the
|
|
3
|
+
* session-start draw of selection and ordering, delivery order, and test-scope expressions.
|
|
4
|
+
*/
|
|
5
|
+
import { buildExpression, DeclarationContext, evalExpr, OperatorRegistry, parseXml, qtiNull, qtiValue, } from '@pie-qti/qti-processing';
|
|
6
|
+
/** QTI itemSessionControl defaults, applied where no level of the cascade sets an attribute. */
|
|
7
|
+
export const DEFAULT_ITEM_SESSION_CONTROL = Object.freeze({
|
|
8
|
+
maxAttempts: 1,
|
|
9
|
+
showFeedback: false,
|
|
10
|
+
allowReview: true,
|
|
11
|
+
showSolution: false,
|
|
12
|
+
allowComment: false,
|
|
13
|
+
allowSkipping: true,
|
|
14
|
+
validateResponses: false,
|
|
15
|
+
});
|
|
16
|
+
const ITEM_SESSION_CONTROL_KEYS = Object.keys(DEFAULT_ITEM_SESSION_CONTROL);
|
|
17
|
+
const ADAPTIVE_ITEM_ROOT = /<(?:[\w-]+:)?(?:qti-assessment-item|assessmentItem)\b[^>]*\badaptive\s*=\s*["']true["']/;
|
|
18
|
+
/** True when the item root declares `adaptive="true"`. */
|
|
19
|
+
export function isAdaptiveItemXml(itemXml) {
|
|
20
|
+
return ADAPTIVE_ITEM_ROOT.test(itemXml);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Effective itemSessionControl for an item: the test part, each enclosing section from outer to
|
|
24
|
+
* inner, then the item ref, each set attribute overriding the level above. Adaptive items allow
|
|
25
|
+
* unlimited attempts whatever maxAttempts says.
|
|
26
|
+
*/
|
|
27
|
+
export function resolveItemSessionControl(testPart, sections, item) {
|
|
28
|
+
const out = { ...DEFAULT_ITEM_SESSION_CONTROL };
|
|
29
|
+
const levels = [testPart.itemSessionControl, ...sections.map((s) => s.itemSessionControl), item.itemSessionControl];
|
|
30
|
+
for (const level of levels) {
|
|
31
|
+
if (!level)
|
|
32
|
+
continue;
|
|
33
|
+
for (const key of ITEM_SESSION_CONTROL_KEYS) {
|
|
34
|
+
const value = level[key];
|
|
35
|
+
if (value === undefined || (typeof value === 'number' && !Number.isFinite(value)))
|
|
36
|
+
continue;
|
|
37
|
+
out[key] = value;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
if (isAdaptiveItemXml(item.itemXml))
|
|
41
|
+
out.maxAttempts = 0;
|
|
42
|
+
return out;
|
|
43
|
+
}
|
|
44
|
+
/** Direct delivery components of a section, in document order. */
|
|
45
|
+
export function sectionComponents(section) {
|
|
46
|
+
return section.children
|
|
47
|
+
? [...section.children]
|
|
48
|
+
: [
|
|
49
|
+
...(section.assessmentItemRefs ?? []).map((item) => ({ type: 'item', item })),
|
|
50
|
+
...(section.sections ?? []).map((child) => ({ type: 'section', section: child })),
|
|
51
|
+
];
|
|
52
|
+
}
|
|
53
|
+
/** Items in delivery order. Draws nothing: call it on an assessment the backend has drawn. */
|
|
54
|
+
export function deliveredItems(assessment) {
|
|
55
|
+
const out = [];
|
|
56
|
+
const visit = (section, testPart, path) => {
|
|
57
|
+
const sections = [...path, section];
|
|
58
|
+
for (const component of sectionComponents(section)) {
|
|
59
|
+
if (component.type === 'item')
|
|
60
|
+
out.push({ item: component.item, testPart, sections });
|
|
61
|
+
else
|
|
62
|
+
visit(component.section, testPart, sections);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
for (const testPart of assessment.testParts ?? []) {
|
|
66
|
+
for (const section of testPart.sections ?? [])
|
|
67
|
+
visit(section, testPart, []);
|
|
68
|
+
}
|
|
69
|
+
return out;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Draws selection and ordering for one session. Required children are always selected; selected
|
|
73
|
+
* children keep source order before ordering shuffles the non-fixed ones. A child selected more
|
|
74
|
+
* than once with replacement is cloned, its identifier and every descendant identifier suffixed
|
|
75
|
+
* `.2`, `.3` and so on. An invisible child section with keepTogether false is mixed into its
|
|
76
|
+
* parent's shuffle. The result carries no selection or ordering, so drawing it again changes
|
|
77
|
+
* nothing.
|
|
78
|
+
*/
|
|
79
|
+
export function drawAssessment(assessment, rng) {
|
|
80
|
+
const copy = structuredClone(assessment);
|
|
81
|
+
return {
|
|
82
|
+
...copy,
|
|
83
|
+
testParts: copy.testParts.map((testPart) => ({
|
|
84
|
+
...testPart,
|
|
85
|
+
sections: testPart.sections.map((section) => drawSection(section, rng)),
|
|
86
|
+
})),
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function drawSection(section, rng) {
|
|
90
|
+
let components = sectionComponents(section);
|
|
91
|
+
if (section.selection)
|
|
92
|
+
components = selectComponents(section, components, rng);
|
|
93
|
+
components = components.map((component) => component.type === 'section' ? { type: 'section', section: drawSection(component.section, rng) } : component);
|
|
94
|
+
if (section.ordering?.shuffle) {
|
|
95
|
+
const sourceOf = new Map();
|
|
96
|
+
const expanded = [];
|
|
97
|
+
for (const component of components) {
|
|
98
|
+
if (component.type === 'section' && !component.section.visible && component.section.keepTogether === false) {
|
|
99
|
+
for (const child of sectionComponents(component.section)) {
|
|
100
|
+
const fragment = withComponents({ ...component.section, fixed: isFixedSectionComponent(child) }, [child]);
|
|
101
|
+
sourceOf.set(fragment, component.section);
|
|
102
|
+
expanded.push({ type: 'section', section: fragment });
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
expanded.push(component);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
components = mergeAdjacentFragments(shuffleSectionComponents(expanded, rng), sourceOf);
|
|
110
|
+
}
|
|
111
|
+
const { selection: _selection, ordering: _ordering, ...rest } = section;
|
|
112
|
+
return withComponents(rest, components);
|
|
113
|
+
}
|
|
114
|
+
function selectComponents(section, components, rng) {
|
|
115
|
+
const count = Math.max(0, section.selection?.select ?? components.length);
|
|
116
|
+
const indexed = components.map((component, sourceIndex) => ({ component, sourceIndex }));
|
|
117
|
+
const required = indexed.filter(({ component }) => isRequiredSectionComponent(component));
|
|
118
|
+
if (count < required.length) {
|
|
119
|
+
throw new Error(`Section "${section.identifier}" selects ${count} components but contains ${required.length} required components.`);
|
|
120
|
+
}
|
|
121
|
+
let chosen;
|
|
122
|
+
if (section.selection?.withReplacement === true) {
|
|
123
|
+
if (indexed.length === 0 && count > 0) {
|
|
124
|
+
throw new Error(`Section "${section.identifier}" selects ${count} components but contains none.`);
|
|
125
|
+
}
|
|
126
|
+
const draws = Array.from({ length: count - required.length }, () => indexed[Math.min(indexed.length - 1, Math.floor(rng() * indexed.length))]);
|
|
127
|
+
chosen = [...required, ...draws];
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
if (count > indexed.length) {
|
|
131
|
+
throw new Error(`Section "${section.identifier}" selects ${count} components without replacement but only contains ${indexed.length}.`);
|
|
132
|
+
}
|
|
133
|
+
const optional = shuffleArray(indexed.filter(({ component }) => !isRequiredSectionComponent(component)), rng);
|
|
134
|
+
chosen = [...required, ...optional.slice(0, count - required.length)];
|
|
135
|
+
}
|
|
136
|
+
const seen = new Map();
|
|
137
|
+
return chosen
|
|
138
|
+
.sort((a, b) => a.sourceIndex - b.sourceIndex)
|
|
139
|
+
.map(({ component, sourceIndex }) => {
|
|
140
|
+
const occurrence = (seen.get(sourceIndex) ?? 0) + 1;
|
|
141
|
+
seen.set(sourceIndex, occurrence);
|
|
142
|
+
return occurrence === 1 ? component : renameComponent(component, `.${occurrence}`);
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
function renameComponent(component, suffix) {
|
|
146
|
+
const copy = structuredClone(component);
|
|
147
|
+
const renamed = new Set();
|
|
148
|
+
const renameItem = (item) => {
|
|
149
|
+
if (renamed.has(item))
|
|
150
|
+
return;
|
|
151
|
+
renamed.add(item);
|
|
152
|
+
item.identifier += suffix;
|
|
153
|
+
};
|
|
154
|
+
const renameSection = (section) => {
|
|
155
|
+
if (renamed.has(section))
|
|
156
|
+
return;
|
|
157
|
+
renamed.add(section);
|
|
158
|
+
section.identifier += suffix;
|
|
159
|
+
for (const item of section.assessmentItemRefs ?? [])
|
|
160
|
+
renameItem(item);
|
|
161
|
+
for (const child of section.sections ?? [])
|
|
162
|
+
renameSection(child);
|
|
163
|
+
for (const child of section.children ?? []) {
|
|
164
|
+
if (child.type === 'item')
|
|
165
|
+
renameItem(child.item);
|
|
166
|
+
else
|
|
167
|
+
renameSection(child.section);
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
if (copy.type === 'item')
|
|
171
|
+
renameItem(copy.item);
|
|
172
|
+
else
|
|
173
|
+
renameSection(copy.section);
|
|
174
|
+
return copy;
|
|
175
|
+
}
|
|
176
|
+
function withComponents(section, components) {
|
|
177
|
+
const sections = components.flatMap((c) => (c.type === 'section' ? [c.section] : []));
|
|
178
|
+
return {
|
|
179
|
+
...section,
|
|
180
|
+
children: components,
|
|
181
|
+
assessmentItemRefs: components.flatMap((c) => (c.type === 'item' ? [c.item] : [])),
|
|
182
|
+
sections: sections.length > 0 ? sections : undefined,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
function mergeAdjacentFragments(components, sourceOf) {
|
|
186
|
+
const out = [];
|
|
187
|
+
for (const component of components) {
|
|
188
|
+
const previous = out[out.length - 1];
|
|
189
|
+
const source = component.type === 'section' ? sourceOf.get(component.section) : undefined;
|
|
190
|
+
if (source && previous?.type === 'section' && sourceOf.get(previous.section) === source && component.type === 'section') {
|
|
191
|
+
const merged = withComponents(previous.section, [
|
|
192
|
+
...sectionComponents(previous.section),
|
|
193
|
+
...sectionComponents(component.section),
|
|
194
|
+
]);
|
|
195
|
+
sourceOf.set(merged, source);
|
|
196
|
+
out[out.length - 1] = { type: 'section', section: merged };
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
out.push(component);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return out;
|
|
203
|
+
}
|
|
204
|
+
/** Fisher-Yates shuffle using a provided RNG (returns a new array). */
|
|
205
|
+
export function shuffleArray(arr, rng) {
|
|
206
|
+
const out = [...arr];
|
|
207
|
+
for (let i = out.length - 1; i > 0; i--) {
|
|
208
|
+
const j = Math.floor(rng() * (i + 1));
|
|
209
|
+
[out[i], out[j]] = [out[j], out[i]];
|
|
210
|
+
}
|
|
211
|
+
return out;
|
|
212
|
+
}
|
|
213
|
+
function isRequiredSectionComponent(component) {
|
|
214
|
+
return component.type === 'item' ? component.item.required === true : component.section.required === true;
|
|
215
|
+
}
|
|
216
|
+
function isFixedSectionComponent(component) {
|
|
217
|
+
return component.type === 'item' ? component.item.fixed === true : component.section.fixed === true;
|
|
218
|
+
}
|
|
219
|
+
/** Shuffle non-fixed children while retaining fixed children at their post-selection positions. */
|
|
220
|
+
function shuffleSectionComponents(components, rng) {
|
|
221
|
+
const result = [...components];
|
|
222
|
+
const movableIndexes = components.flatMap((component, index) => (isFixedSectionComponent(component) ? [] : [index]));
|
|
223
|
+
const shuffled = shuffleArray(movableIndexes.map((index) => components[index]), rng);
|
|
224
|
+
for (let index = 0; index < movableIndexes.length; index++) {
|
|
225
|
+
result[movableIndexes[index]] = shuffled[index];
|
|
226
|
+
}
|
|
227
|
+
return result;
|
|
228
|
+
}
|
|
229
|
+
// ---------------------------------------------------------------------------
|
|
230
|
+
// Test-scope expressions
|
|
231
|
+
// ---------------------------------------------------------------------------
|
|
232
|
+
/** Canonical QTI 2.x element name for a QTI 2.x or QTI 3 (`qti-` kebab-case) element name. */
|
|
233
|
+
export function toCanonicalQtiName(name) {
|
|
234
|
+
const withoutPrefix = (name ?? '').replace(/^qti-/, '');
|
|
235
|
+
return withoutPrefix.replace(/-([a-z])/g, (_, char) => char.toUpperCase());
|
|
236
|
+
}
|
|
237
|
+
/** Reads QTI 2.x camelCase and QTI 3 `qti-` kebab-case element names alike. */
|
|
238
|
+
export const qtiElementNameMapper = {
|
|
239
|
+
version: 'qti2x-or-qti3',
|
|
240
|
+
toCanonical: (elementName) => toCanonicalQtiName(elementName).toLowerCase(),
|
|
241
|
+
toNative: (canonicalName) => canonicalName,
|
|
242
|
+
isValidElementName: () => true,
|
|
243
|
+
};
|
|
244
|
+
function toQtiValue(baseType, cardinality, value) {
|
|
245
|
+
if (value === null || value === undefined)
|
|
246
|
+
return qtiNull(baseType, cardinality);
|
|
247
|
+
return qtiValue(baseType, cardinality, value);
|
|
248
|
+
}
|
|
249
|
+
function declaration(identifier, value, defaultValue = value) {
|
|
250
|
+
return {
|
|
251
|
+
identifier,
|
|
252
|
+
baseType: value.baseType,
|
|
253
|
+
cardinality: value.cardinality ?? 'single',
|
|
254
|
+
defaultValue,
|
|
255
|
+
value: { ...value },
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Declarations for the test's outcome variables, set to `outcomes` where given and to their
|
|
260
|
+
* defaults otherwise. A numeric outcome with no declared default starts at 0.
|
|
261
|
+
*/
|
|
262
|
+
export function testOutcomeDeclarations(assessment, outcomes) {
|
|
263
|
+
const decls = {};
|
|
264
|
+
for (const d of assessment.outcomeDeclarations ?? []) {
|
|
265
|
+
const defaultValue = d.defaultValue !== undefined
|
|
266
|
+
? toQtiValue(d.baseType, d.cardinality, d.defaultValue)
|
|
267
|
+
: d.baseType === 'float' || d.baseType === 'integer'
|
|
268
|
+
? qtiValue(d.baseType, 'single', 0)
|
|
269
|
+
: qtiNull(d.baseType, d.cardinality);
|
|
270
|
+
const current = outcomes && d.identifier in outcomes
|
|
271
|
+
? toQtiValue(d.baseType, d.cardinality, outcomes[d.identifier])
|
|
272
|
+
: defaultValue;
|
|
273
|
+
decls[d.identifier] = {
|
|
274
|
+
identifier: d.identifier,
|
|
275
|
+
baseType: d.baseType,
|
|
276
|
+
cardinality: d.cardinality,
|
|
277
|
+
defaultValue,
|
|
278
|
+
value: { ...current },
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
return decls;
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Adds item variables as `itemIdentifier.VARIABLE`, the form test-scope expressions use, and the
|
|
285
|
+
* `unprefixed` variables under their own identifiers where the test declares no such outcome.
|
|
286
|
+
*/
|
|
287
|
+
export function addItemVariableDeclarations(decls, itemVariables, unprefixed = {}) {
|
|
288
|
+
for (const [itemIdentifier, variables] of Object.entries(itemVariables)) {
|
|
289
|
+
for (const [identifier, value] of Object.entries(variables)) {
|
|
290
|
+
const key = `${itemIdentifier}.${identifier}`;
|
|
291
|
+
if (!decls[key])
|
|
292
|
+
decls[key] = declaration(key, value);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
for (const [identifier, value] of Object.entries(unprefixed)) {
|
|
296
|
+
if (!decls[identifier])
|
|
297
|
+
decls[identifier] = declaration(identifier, value);
|
|
298
|
+
}
|
|
299
|
+
return decls;
|
|
300
|
+
}
|
|
301
|
+
/** Item variables with each variableMapping's source also readable as its target identifier. */
|
|
302
|
+
export function applyVariableMappings(variables, mappings) {
|
|
303
|
+
if (!mappings?.length)
|
|
304
|
+
return variables;
|
|
305
|
+
const out = { ...variables };
|
|
306
|
+
for (const { sourceIdentifier, targetIdentifier } of mappings) {
|
|
307
|
+
const value = variables[sourceIdentifier];
|
|
308
|
+
if (value)
|
|
309
|
+
out[targetIdentifier] = value;
|
|
310
|
+
}
|
|
311
|
+
return out;
|
|
312
|
+
}
|
|
313
|
+
/** Evaluates a serialized test-scope expression (QTI 2.x or 3) against `decls`. */
|
|
314
|
+
export function evaluateTestExpression(expressionXml, decls, rng) {
|
|
315
|
+
const root = parseXml(expressionXml).documentElement;
|
|
316
|
+
if (!root)
|
|
317
|
+
return qtiNull();
|
|
318
|
+
const expr = buildExpression(root, { scope: 'test', elementNameMapper: qtiElementNameMapper });
|
|
319
|
+
return evalExpr({ ctx: new DeclarationContext(structuredClone(decls)), ops: new OperatorRegistry(), rng }, expr);
|
|
320
|
+
}
|
|
321
|
+
/** True only when the expression evaluates to boolean true; NULL and errors read as false. */
|
|
322
|
+
export function evaluateTestCondition(expressionXml, decls, rng) {
|
|
323
|
+
try {
|
|
324
|
+
const out = evaluateTestExpression(expressionXml, decls, rng);
|
|
325
|
+
return out.kind === 'value' && out.value === true;
|
|
326
|
+
}
|
|
327
|
+
catch (err) {
|
|
328
|
+
console.warn('[assessment-player] test condition failed to evaluate:', err);
|
|
329
|
+
return false;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
/** Plain value of a QTI value, null for NULL, in the shape item template defaults take. */
|
|
333
|
+
export function plainQtiValue(value) {
|
|
334
|
+
return value.kind === 'value' ? value.value : null;
|
|
335
|
+
}
|
|
336
|
+
//# sourceMappingURL=testStructure.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"testStructure.js","sourceRoot":"","sources":["../../src/core/testStructure.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EACN,eAAe,EACf,kBAAkB,EAElB,QAAQ,EACR,gBAAgB,EAChB,QAAQ,EAER,OAAO,EACP,QAAQ,GACR,MAAM,yBAAyB,CAAC;AAcjC,gGAAgG;AAChG,MAAM,CAAC,MAAM,4BAA4B,GAAyC,MAAM,CAAC,MAAM,CAAC;IAC/F,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;CACxB,CAAC,CAAC;AAEH,MAAM,yBAAyB,GAAG,MAAM,CAAC,IAAI,CAAC,4BAA4B,CAA4C,CAAC;AAEvH,MAAM,kBAAkB,GAAG,yFAAyF,CAAC;AAErH,0DAA0D;AAC1D,MAAM,UAAU,iBAAiB,C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AI,GAAG,eAAe,CAAC,IAAI,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,CAAC,CAAC;IAC/F,OAAO,QAAQ,CAAC,EAAE,GAAG,EAAE,IAAI,kBAAkB,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,EAAE,IAAI,gBAAgB,EAAE,EAAE,GAAG,EAAE,EAAE,IAAI,CAAC,CAAC;AAClH,CAAC;AAED,8FAA8F;AAC9F,MAAM,UAAU,qBAAqB,CAAC,aAAqB,EAAE,KAAqB,EAAE,GAAiB;IACpG,IAAI,CAAC;QACJ,MAAM,GAAG,GAAG,sBAAsB,CAAC,aAAa,EAAE,KAAK,EAAE,GAAG,CAAC,CAAC;QAC9D,OAAO,GAAG,CAAC,IAAI,KAAK,OAAO,IAAI,GAAG,CAAC,KAAK,KAAK,IAAI,CAAC;IACnD,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACd,OAAO,CAAC,IAAI,CAAC,wDAAwD,EAAE,GAAG,CAAC,CAAC;QAC5E,OAAO,KAAK,CAAC;IACd,CAAC;AACF,CAAC;AAED,2FAA2F;AAC3F,MAAM,UAAU,aAAa,CAAC,KAAe;IAC5C,OAAO,KAAK,CAAC,IAAI,KAAK,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC;AACpD,CAAC","sourcesContent":["/**\n * QTI test structure shared by backends and the client: the itemSessionControl cascade, the\n * session-start draw of selection and ordering, delivery order, and test-scope expressions.\n */\n\nimport {\n\tbuildExpression,\n\tDeclarationContext,\n\ttype DeclarationMap,\n\tevalExpr,\n\tOperatorRegistry,\n\tparseXml,\n\ttype QtiValue,\n\tqtiNull,\n\tqtiValue,\n} from '@pie-qti/qti-processing';\nimport type {\n\tSecureAssessment,\n\tSecureItemRef,\n\tSecureItemSessionControl,\n\tSecureSection,\n\tSecureSectionComponent,\n\tSecureTestPart,\n\tTestOutcomeValue,\n\tTestOutcomeValues,\n} from '../integration/api-contract.js';\n\nexport type ResolvedItemSessionControl = Required<SecureItemSessionControl>;\n\n/** QTI itemSessionControl defaults, applied where no level of the cascade sets an attribute. */\nexport const DEFAULT_ITEM_SESSION_CONTROL: Readonly<ResolvedItemSessionControl> = Object.freeze({\n\tmaxAttempts: 1,\n\tshowFeedback: false,\n\tallowReview: true,\n\tshowSolution: false,\n\tallowComment: false,\n\tallowSkipping: true,\n\tvalidateResponses: false,\n});\n\nconst ITEM_SESSION_CONTROL_KEYS = Object.keys(DEFAULT_ITEM_SESSION_CONTROL) as Array<keyof ResolvedItemSessionControl>;\n\nconst ADAPTIVE_ITEM_ROOT = /<(?:[\\w-]+:)?(?:qti-assessment-item|assessmentItem)\\b[^>]*\\badaptive\\s*=\\s*[\"']true[\"']/;\n\n/** True when the item root declares `adaptive=\"true\"`. */\nexport function isAdaptiveItemXml(itemXml: string): boolean {\n\treturn ADAPTIVE_ITEM_ROOT.test(itemXml);\n}\n\n/**\n * Effective itemSessionControl for an item: the test part, each enclosing section from outer to\n * inner, then the item ref, each set attribute overriding the level above. Adaptive items allow\n * unlimited attempts whatever maxAttempts says.\n */\nexport function resolveItemSessionControl(\n\ttestPart: SecureTestPart,\n\tsections: readonly SecureSection[],\n\titem: SecureItemRef,\n): ResolvedItemSessionControl {\n\tconst out: ResolvedItemSessionControl = { ...DEFAULT_ITEM_SESSION_CONTROL };\n\tconst levels = [testPart.itemSessionControl, ...sections.map((s) => s.itemSessionControl), item.itemSessionControl];\n\tfor (const level of levels) {\n\t\tif (!level) continue;\n\t\tfor (const key of ITEM_SESSION_CONTROL_KEYS) {\n\t\t\tconst value = level[key];\n\t\t\tif (value === undefined || (typeof value === 'number' && !Number.isFinite(value))) continue;\n\t\t\t(out as Record<string, unknown>)[key] = value;\n\t\t}\n\t}\n\tif (isAdaptiveItemXml(item.itemXml)) out.maxAttempts = 0;\n\treturn out;\n}\n\n/** Direct delivery components of a section, in document order. */\nexport function sectionComponents(section: SecureSection): SecureSectionComponent[] {\n\treturn section.children\n\t\t? [...section.children]\n\t\t: [\n\t\t\t...(section.assessmentItemRefs ?? []).map((item) => ({ type: 'item' as const, item })),\n\t\t\t...(section.sections ?? []).map((child) => ({ type: 'section' as const, section: child })),\n\t\t];\n}\n\nexport interface DeliveredItem {\n\titem: SecureItemRef;\n\ttestPart: SecureTestPart;\n\t/** Enclosing sections, outermost first. */\n\tsections: SecureSection[];\n}\n\n/** Items in delivery order. Draws nothing: call it on an assessment the backend has drawn. */\nexport function deliveredItems(assessment: SecureAssessment): DeliveredItem[] {\n\tconst out: DeliveredItem[] = [];\n\tconst visit = (section: SecureSection, testPart: SecureTestPart, path: SecureSection[]): void => {\n\t\tconst sections = [...path, section];\n\t\tfor (const component of sectionComponents(section)) {\n\t\t\tif (component.type === 'item') out.push({ item: component.item, testPart, sections });\n\t\t\telse visit(component.section, testPart, sections);\n\t\t}\n\t};\n\tfor (const testPart of assessment.testParts ?? []) {\n\t\tfor (const section of testPart.sections ?? []) visit(section, testPart, []);\n\t}\n\treturn out;\n}\n\n/**\n * Draws selection and ordering for one session. Required children are always selected; selected\n * children keep source order before ordering shuffles the non-fixed ones. A child selected more\n * than once with replacement is cloned, its identifier and every descendant identifier suffixed\n * `.2`, `.3` and so on. An invisible child section with keepTogether false is mixed into its\n * parent's shuffle. The result carries no selection or ordering, so drawing it again changes\n * nothing.\n */\nexport function drawAssessment(assessment: SecureAssessment, rng: () => number): SecureAssessment {\n\tconst copy = structuredClone(assessment);\n\treturn {\n\t\t...copy,\n\t\ttestParts: copy.testParts.map((testPart) => ({\n\t\t\t...testPart,\n\t\t\tsections: testPart.sections.map((section) => drawSection(section, rng)),\n\t\t})),\n\t};\n}\n\nfunction drawSection(section: SecureSection, rng: () => number): SecureSection {\n\tlet components = sectionComponents(section);\n\tif (section.selection) components = selectComponents(section, components, rng);\n\tcomponents = components.map((component) =>\n\t\tcomponent.type === 'section' ? { type: 'section', section: drawSection(component.section, rng) } : component\n\t);\n\n\tif (section.ordering?.shuffle) {\n\t\tconst sourceOf = new Map<SecureSection, SecureSection>();\n\t\tconst expanded: SecureSectionComponent[] = [];\n\t\tfor (const component of components) {\n\t\t\tif (component.type === 'section' && !component.section.visible && component.section.keepTogether === false) {\n\t\t\t\tfor (const child of sectionComponents(component.section)) {\n\t\t\t\t\tconst fragment = withComponents({ ...component.section, fixed: isFixedSectionComponent(child) }, [child]);\n\t\t\t\t\tsourceOf.set(fragment, component.section);\n\t\t\t\t\texpanded.push({ type: 'section', section: fragment });\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\texpanded.push(component);\n\t\t\t}\n\t\t}\n\t\tcomponents = mergeAdjacentFragments(shuffleSectionComponents(expanded, rng), sourceOf);\n\t}\n\n\tconst { selection: _selection, ordering: _ordering, ...rest } = section;\n\treturn withComponents(rest, components);\n}\n\nfunction selectComponents(\n\tsection: SecureSection,\n\tcomponents: SecureSectionComponent[],\n\trng: () => number,\n): SecureSectionComponent[] {\n\tconst count = Math.max(0, section.selection?.select ?? components.length);\n\tconst indexed = components.map((component, sourceIndex) => ({ component, sourceIndex }));\n\tconst required = indexed.filter(({ component }) => isRequiredSectionComponent(component));\n\tif (count < required.length) {\n\t\tthrow new Error(\n\t\t\t`Section \"${section.identifier}\" selects ${count} components but contains ${required.length} required components.`,\n\t\t);\n\t}\n\tlet chosen: typeof indexed;\n\tif (section.selection?.withReplacement === true) {\n\t\tif (indexed.length === 0 && count > 0) {\n\t\t\tthrow new Error(`Section \"${section.identifier}\" selects ${count} components but contains none.`);\n\t\t}\n\t\tconst draws = Array.from(\n\t\t\t{ length: count - required.length },\n\t\t\t() => indexed[Math.min(indexed.length - 1, Math.floor(rng() * indexed.length))]!,\n\t\t);\n\t\tchosen = [...required, ...draws];\n\t} else {\n\t\tif (count > indexed.length) {\n\t\t\tthrow new Error(\n\t\t\t\t`Section \"${section.identifier}\" selects ${count} components without replacement but only contains ${indexed.length}.`,\n\t\t\t);\n\t\t}\n\t\tconst optional = shuffleArray(indexed.filter(({ component }) => !isRequiredSectionComponent(component)), rng);\n\t\tchosen = [...required, ...optional.slice(0, count - required.length)];\n\t}\n\tconst seen = new Map<number, number>();\n\treturn chosen\n\t\t.sort((a, b) => a.sourceIndex - b.sourceIndex)\n\t\t.map(({ component, sourceIndex }) => {\n\t\t\tconst occurrence = (seen.get(sourceIndex) ?? 0) + 1;\n\t\t\tseen.set(sourceIndex, occurrence);\n\t\t\treturn occurrence === 1 ? component : renameComponent(component, `.${occurrence}`);\n\t\t});\n}\n\nfunction renameComponent(component: SecureSectionComponent, suffix: string): SecureSectionComponent {\n\tconst copy = structuredClone(component);\n\tconst renamed = new Set<object>();\n\tconst renameItem = (item: SecureItemRef): void => {\n\t\tif (renamed.has(item)) return;\n\t\trenamed.add(item);\n\t\titem.identifier += suffix;\n\t};\n\tconst renameSection = (section: SecureSection): void => {\n\t\tif (renamed.has(section)) return;\n\t\trenamed.add(section);\n\t\tsection.identifier += suffix;\n\t\tfor (const item of section.assessmentItemRefs ?? []) renameItem(item);\n\t\tfor (const child of section.sections ?? []) renameSection(child);\n\t\tfor (const child of section.children ?? []) {\n\t\t\tif (child.type === 'item') renameItem(child.item);\n\t\t\telse renameSection(child.section);\n\t\t}\n\t};\n\tif (copy.type === 'item') renameItem(copy.item);\n\telse renameSection(copy.section);\n\treturn copy;\n}\n\nfunction withComponents(section: SecureSection, components: SecureSectionComponent[]): SecureSection {\n\tconst sections = components.flatMap((c) => (c.type === 'section' ? [c.section] : []));\n\treturn {\n\t\t...section,\n\t\tchildren: components,\n\t\tassessmentItemRefs: components.flatMap((c) => (c.type === 'item' ? [c.item] : [])),\n\t\tsections: sections.length > 0 ? sections : undefined,\n\t};\n}\n\nfunction mergeAdjacentFragments(\n\tcomponents: SecureSectionComponent[],\n\tsourceOf: Map<SecureSection, SecureSection>,\n): SecureSectionComponent[] {\n\tconst out: SecureSectionComponent[] = [];\n\tfor (const component of components) {\n\t\tconst previous = out[out.length - 1];\n\t\tconst source = component.type === 'section' ? sourceOf.get(component.section) : undefined;\n\t\tif (source && previous?.type === 'section' && sourceOf.get(previous.section) === source && component.type === 'section') {\n\t\t\tconst merged = withComponents(previous.section, [\n\t\t\t\t...sectionComponents(previous.section),\n\t\t\t\t...sectionComponents(component.section),\n\t\t\t]);\n\t\t\tsourceOf.set(merged, source);\n\t\t\tout[out.length - 1] = { type: 'section', section: merged };\n\t\t} else {\n\t\t\tout.push(component);\n\t\t}\n\t}\n\treturn out;\n}\n\n/** Fisher-Yates shuffle using a provided RNG (returns a new array). */\nexport function shuffleArray<T>(arr: readonly T[], rng: () => number): T[] {\n\tconst out = [...arr];\n\tfor (let i = out.length - 1; i > 0; i--) {\n\t\tconst j = Math.floor(rng() * (i + 1));\n\t\t[out[i], out[j]] = [out[j]!, out[i]!];\n\t}\n\treturn out;\n}\n\nfunction isRequiredSectionComponent(component: SecureSectionComponent): boolean {\n\treturn component.type === 'item' ? component.item.required === true : component.section.required === true;\n}\n\nfunction isFixedSectionComponent(component: SecureSectionComponent): boolean {\n\treturn component.type === 'item' ? component.item.fixed === true : component.section.fixed === true;\n}\n\n/** Shuffle non-fixed children while retaining fixed children at their post-selection positions. */\nfunction shuffleSectionComponents(components: SecureSectionComponent[], rng: () => number): SecureSectionComponent[] {\n\tconst result = [...components];\n\tconst movableIndexes = components.flatMap((component, index) => (isFixedSectionComponent(component) ? [] : [index]));\n\tconst shuffled = shuffleArray(movableIndexes.map((index) => components[index]!), rng);\n\tfor (let index = 0; index < movableIndexes.length; index++) {\n\t\tresult[movableIndexes[index]!] = shuffled[index]!;\n\t}\n\treturn result;\n}\n\n// ---------------------------------------------------------------------------\n// Test-scope expressions\n// ---------------------------------------------------------------------------\n\n/** Canonical QTI 2.x element name for a QTI 2.x or QTI 3 (`qti-` kebab-case) element name. */\nexport function toCanonicalQtiName(name: string | null | undefined): string {\n\tconst withoutPrefix = (name ?? '').replace(/^qti-/, '');\n\treturn withoutPrefix.replace(/-([a-z])/g, (_, char: string) => char.toUpperCase());\n}\n\n/** Reads QTI 2.x camelCase and QTI 3 `qti-` kebab-case element names alike. */\nexport const qtiElementNameMapper = {\n\tversion: 'qti2x-or-qti3',\n\ttoCanonical: (elementName: string) => toCanonicalQtiName(elementName).toLowerCase(),\n\ttoNative: (canonicalName: string) => canonicalName,\n\tisValidElementName: () => true,\n};\n\nfunction toQtiValue(baseType: string, cardinality: string, value: unknown): QtiValue {\n\tif (value === null || value === undefined) return qtiNull(baseType as never, cardinality as never);\n\treturn qtiValue(baseType as never, cardinality as never, value);\n}\n\nfunction declaration(identifier: string, value: QtiValue, defaultValue: QtiValue = value): DeclarationMap[string] {\n\treturn {\n\t\tidentifier,\n\t\tbaseType: value.baseType,\n\t\tcardinality: value.cardinality ?? 'single',\n\t\tdefaultValue,\n\t\tvalue: { ...value },\n\t};\n}\n\n/**\n * Declarations for the test's outcome variables, set to `outcomes` where given and to their\n * defaults otherwise. A numeric outcome with no declared default starts at 0.\n */\nexport function testOutcomeDeclarations(\n\tassessment: SecureAssessment,\n\toutcomes?: TestOutcomeValues | null,\n): DeclarationMap {\n\tconst decls: DeclarationMap = {};\n\tfor (const d of assessment.outcomeDeclarations ?? []) {\n\t\tconst defaultValue =\n\t\t\td.defaultValue !== undefined\n\t\t\t\t? toQtiValue(d.baseType, d.cardinality, d.defaultValue)\n\t\t\t\t: d.baseType === 'float' || d.baseType === 'integer'\n\t\t\t\t\t? qtiValue(d.baseType, 'single', 0)\n\t\t\t\t\t: qtiNull(d.baseType as never, d.cardinality as never);\n\t\tconst current = outcomes && d.identifier in outcomes\n\t\t\t? toQtiValue(d.baseType, d.cardinality, outcomes[d.identifier] as TestOutcomeValue)\n\t\t\t: defaultValue;\n\t\tdecls[d.identifier] = {\n\t\t\tidentifier: d.identifier,\n\t\t\tbaseType: d.baseType as never,\n\t\t\tcardinality: d.cardinality as never,\n\t\t\tdefaultValue,\n\t\t\tvalue: { ...current },\n\t\t};\n\t}\n\treturn decls;\n}\n\n/**\n * Adds item variables as `itemIdentifier.VARIABLE`, the form test-scope expressions use, and the\n * `unprefixed` variables under their own identifiers where the test declares no such outcome.\n */\nexport function addItemVariableDeclarations(\n\tdecls: DeclarationMap,\n\titemVariables: Record<string, Record<string, QtiValue>>,\n\tunprefixed: Record<string, QtiValue> = {},\n): DeclarationMap {\n\tfor (const [itemIdentifier, variables] of Object.entries(itemVariables)) {\n\t\tfor (const [identifier, value] of Object.entries(variables)) {\n\t\t\tconst key = `${itemIdentifier}.${identifier}`;\n\t\t\tif (!decls[key]) decls[key] = declaration(key, value);\n\t\t}\n\t}\n\tfor (const [identifier, value] of Object.entries(unprefixed)) {\n\t\tif (!decls[identifier]) decls[identifier] = declaration(identifier, value);\n\t}\n\treturn decls;\n}\n\n/** Item variables with each variableMapping's source also readable as its target identifier. */\nexport function applyVariableMappings(\n\tvariables: Record<string, QtiValue>,\n\tmappings: SecureItemRef['variableMappings'],\n): Record<string, QtiValue> {\n\tif (!mappings?.length) return variables;\n\tconst out = { ...variables };\n\tfor (const { sourceIdentifier, targetIdentifier } of mappings) {\n\t\tconst value = variables[sourceIdentifier];\n\t\tif (value) out[targetIdentifier] = value;\n\t}\n\treturn out;\n}\n\n/** Evaluates a serialized test-scope expression (QTI 2.x or 3) against `decls`. */\nexport function evaluateTestExpression(expressionXml: string, decls: DeclarationMap, rng: () => number): QtiValue {\n\tconst root = parseXml(expressionXml).documentElement;\n\tif (!root) return qtiNull();\n\tconst expr = buildExpression(root, { scope: 'test', elementNameMapper: qtiElementNameMapper });\n\treturn evalExpr({ ctx: new DeclarationContext(structuredClone(decls)), ops: new OperatorRegistry(), rng }, expr);\n}\n\n/** True only when the expression evaluates to boolean true; NULL and errors read as false. */\nexport function evaluateTestCondition(expressionXml: string, decls: DeclarationMap, rng: () => number): boolean {\n\ttry {\n\t\tconst out = evaluateTestExpression(expressionXml, decls, rng);\n\t\treturn out.kind === 'value' && out.value === true;\n\t} catch (err) {\n\t\tconsole.warn('[assessment-player] test condition failed to evaluate:', err);\n\t\treturn false;\n\t}\n}\n\n/** Plain value of a QTI value, null for NULL, in the shape item template defaults take. */\nexport function plainQtiValue(value: QtiValue): unknown {\n\treturn value.kind === 'value' ? value.value : null;\n}\n"]}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export type { BackendAssessmentPlayerConfig } from './core/AssessmentPlayer.js';
|
|
2
2
|
export type { NavigationMode } from './core/index.js';
|
|
3
3
|
export { AssessmentPlayer, NavigationManager } from './core/index.js';
|
|
4
|
+
export { toCanonicalQtiName } from './core/testStructure.js';
|
|
4
5
|
export * from './integration/index.js';
|
|
5
|
-
export type { AssessmentResults, ItemRef, ItemResult, NavigationState, QTIRole } from './types/index.js';
|
|
6
|
+
export type { AssessmentResults, ItemRef, ItemResult, NavigationState, QTIRole, VisibleTestFeedback, } from './types/index.js';
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// Core classes
|
|
2
2
|
export { AssessmentPlayer, NavigationManager } from './core/index.js';
|
|
3
|
+
// Shared QTI XML scan primitives used by the adapter and the element discovery parser.
|
|
4
|
+
export { toCanonicalQtiName } from './core/testStructure.js';
|
|
3
5
|
// Backend integration API + reference adapter
|
|
4
6
|
export * from './integration/index.js';
|
|
5
7
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,eAAe;AAIf,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACtE,8CAA8C;AAC9C,cAAc,wBAAwB,CAAC","sourcesContent":["// Core classes\n\nexport type { BackendAssessmentPlayerConfig } from './core/AssessmentPlayer.js';\nexport type { NavigationMode } from './core/index.js';\nexport { AssessmentPlayer, NavigationManager } from './core/index.js';\n// Backend integration API + reference adapter\nexport * from './integration/index.js';\n// Public UI/data types\nexport type {
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,eAAe;AAIf,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACtE,uFAAuF;AACvF,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAC7D,8CAA8C;AAC9C,cAAc,wBAAwB,CAAC","sourcesContent":["// Core classes\n\nexport type { BackendAssessmentPlayerConfig } from './core/AssessmentPlayer.js';\nexport type { NavigationMode } from './core/index.js';\nexport { AssessmentPlayer, NavigationManager } from './core/index.js';\n// Shared QTI XML scan primitives used by the adapter and the element discovery parser.\nexport { toCanonicalQtiName } from './core/testStructure.js';\n// Backend integration API + reference adapter\nexport * from './integration/index.js';\n// Public UI/data types\nexport type {\n\tAssessmentResults,\n\tItemRef,\n\tItemResult,\n\tNavigationState,\n\tQTIRole,\n\tVisibleTestFeedback,\n} from './types/index.js';\n"]}
|
|
@@ -39,8 +39,14 @@ import type { BackendAdapter, FinalizeAssessmentRequest, FinalizeAssessmentRespo
|
|
|
39
39
|
* });
|
|
40
40
|
* ```
|
|
41
41
|
*/
|
|
42
|
+
export interface ReferenceBackendAdapterOptions {
|
|
43
|
+
/** RNG for the session-start draw of selection and ordering. Defaults to Math.random. */
|
|
44
|
+
rng?: () => number;
|
|
45
|
+
}
|
|
42
46
|
export declare class ReferenceBackendAdapter implements BackendAdapter {
|
|
43
47
|
private registeredAssessments;
|
|
48
|
+
private readonly rng;
|
|
49
|
+
constructor(options?: ReferenceBackendAdapterOptions);
|
|
44
50
|
/**
|
|
45
51
|
* Register/override a client-provided assessment structure for a given assessmentId.
|
|
46
52
|
*
|
|
@@ -67,9 +73,12 @@ export declare class ReferenceBackendAdapter implements BackendAdapter {
|
|
|
67
73
|
* Finalize assessment and compute final scores
|
|
68
74
|
*/
|
|
69
75
|
finalizeAssessment(request: FinalizeAssessmentRequest): Promise<FinalizeAssessmentResponse>;
|
|
76
|
+
/** Outcome values after the submissions so far; undefined before the first submission. */
|
|
77
|
+
private currentOutcomes;
|
|
70
78
|
/**
|
|
71
79
|
* Execute test-level outcome processing (T9/T1).
|
|
72
|
-
* Returns
|
|
80
|
+
* Returns every declared outcome's value (null for NULL), or null if no outcomeProcessing is
|
|
81
|
+
* defined.
|
|
73
82
|
*/
|
|
74
83
|
private runOutcomeProcessing;
|
|
75
84
|
/**
|
|
@@ -91,8 +100,6 @@ export declare class ReferenceBackendAdapter implements BackendAdapter {
|
|
|
91
100
|
private generateSessionId;
|
|
92
101
|
private loadSession;
|
|
93
102
|
private saveSession;
|
|
94
|
-
private findItemXml;
|
|
95
|
-
private findItemRef;
|
|
96
103
|
/**
|
|
97
104
|
* Create demo assessment for testing
|
|
98
105
|
*
|
|
@@ -118,13 +125,54 @@ export declare class ReferenceBackendAdapter implements BackendAdapter {
|
|
|
118
125
|
itemXmlMap?: Record<string, string>;
|
|
119
126
|
fileResolver?: (href: string) => Promise<string>;
|
|
120
127
|
baseUrl?: string;
|
|
128
|
+
/**
|
|
129
|
+
* Shared catalog files the package manifest lists (resource type
|
|
130
|
+
* `imsqti_catalog_xmlv3p0`): href, XML, or both. Their entries back
|
|
131
|
+
* every item's `data-catalog-idref` terms; item-level catalogs win
|
|
132
|
+
* identifier collisions (QTI 3.0 §6.3).
|
|
133
|
+
*/
|
|
134
|
+
sharedCatalogs?: ReadonlyArray<{
|
|
135
|
+
href?: string;
|
|
136
|
+
xml?: string;
|
|
137
|
+
}>;
|
|
121
138
|
sectionReferenceLimits?: {
|
|
122
139
|
/** Maximum total inline and external assessmentSection nesting. Defaults to 32. */
|
|
123
140
|
maxDepth?: number;
|
|
124
141
|
};
|
|
125
142
|
}): Promise<SecureAssessment>;
|
|
126
|
-
|
|
127
|
-
|
|
143
|
+
/**
|
|
144
|
+
* Context for branch rules and preconditions: test outcomes, every submitted item's variables as
|
|
145
|
+
* `itemIdentifier.VARIABLE`, and `current` unprefixed where the test declares no such outcome.
|
|
146
|
+
*/
|
|
147
|
+
private navigationDeclarations;
|
|
148
|
+
/**
|
|
149
|
+
* Next item after `delivered[index]`. Branch rules come first: the item's, then each enclosing
|
|
150
|
+
* section's it ends (inner to outer), then the test part's when it ends that too. Item and
|
|
151
|
+
* section rules apply in linear navigation only. With no matching rule, navigation falls through
|
|
152
|
+
* to the following item. Preconditions then skip forward from the target. EXIT_TEST means the
|
|
153
|
+
* remaining items are all skipped; undefined means the current item was the last.
|
|
154
|
+
*/
|
|
155
|
+
private nextItemIdentifier;
|
|
156
|
+
private branchTarget;
|
|
157
|
+
/** Index a branch target resolves to, forward of `index`; delivered.length ends the test. */
|
|
158
|
+
private resolveBranchTarget;
|
|
159
|
+
/**
|
|
160
|
+
* First item from `start` whose preconditions hold. A test part's are checked on entering it in
|
|
161
|
+
* any navigation mode; a newly entered section's (outermost first) and the item's only in linear
|
|
162
|
+
* navigation. A failing part or section skips everything inside it.
|
|
163
|
+
*/
|
|
164
|
+
private firstEligibleItem;
|
|
165
|
+
private isLinear;
|
|
166
|
+
/** Delivered item refs in order, each with the identifiers of the sections containing it (outermost first). */
|
|
167
|
+
private getItemRefsInOrder;
|
|
168
|
+
/**
|
|
169
|
+
* Test-level view of every delivered item. Items not yet submitted carry their declared defaults
|
|
170
|
+
* and count as not presented; sessions stored before item test states existed fall back to the
|
|
171
|
+
* submitted scores, leaving correctness to SCORE/MAXSCORE inference.
|
|
172
|
+
*/
|
|
173
|
+
private buildTestItems;
|
|
174
|
+
/** Variables recoverable from a scoring result alone: numbers as float, everything else as string. */
|
|
175
|
+
private scoredItemVariables;
|
|
128
176
|
/**
|
|
129
177
|
* Generate cache key for item bank query
|
|
130
178
|
*/
|