@longsightgroup/qti3-core 0.9.3 → 0.9.5
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 +88 -8
- package/dist/adaptive-turn-materializer.d.ts +2 -0
- package/dist/adaptive-turn-materializer.d.ts.map +1 -1
- package/dist/adaptive-turn-materializer.js +25 -19
- package/dist/adaptive-turn-materializer.js.map +1 -1
- package/dist/adaptive-turn.d.ts +8 -1
- package/dist/adaptive-turn.d.ts.map +1 -1
- package/dist/adaptive-turn.js +15 -5
- package/dist/adaptive-turn.js.map +1 -1
- package/dist/delivery-preparation.d.ts +20 -0
- package/dist/delivery-preparation.d.ts.map +1 -0
- package/dist/delivery-preparation.js +71 -0
- package/dist/delivery-preparation.js.map +1 -0
- package/dist/delivery-redaction.d.ts +5 -0
- package/dist/delivery-redaction.d.ts.map +1 -1
- package/dist/delivery-redaction.js +26 -5
- package/dist/delivery-redaction.js.map +1 -1
- package/dist/delivery-security.js +2 -2
- package/dist/delivery-security.js.map +1 -1
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -1
- package/dist/item-scoring-expectation.d.ts +4 -0
- package/dist/item-scoring-expectation.d.ts.map +1 -0
- package/dist/item-scoring-expectation.js +6 -0
- package/dist/item-scoring-expectation.js.map +1 -0
- package/dist/item-submission-materialization.d.ts +25 -0
- package/dist/item-submission-materialization.d.ts.map +1 -0
- package/dist/item-submission-materialization.js +68 -0
- package/dist/item-submission-materialization.js.map +1 -0
- package/dist/qti-package-assets.d.ts +8 -0
- package/dist/qti-package-assets.d.ts.map +1 -0
- package/dist/qti-package-assets.js +173 -0
- package/dist/qti-package-assets.js.map +1 -0
- package/dist/qti-package-items.d.ts +14 -0
- package/dist/qti-package-items.d.ts.map +1 -0
- package/dist/qti-package-items.js +138 -0
- package/dist/qti-package-items.js.map +1 -0
- package/dist/qti-package-manifest.d.ts +13 -0
- package/dist/qti-package-manifest.d.ts.map +1 -0
- package/dist/qti-package-manifest.js +109 -0
- package/dist/qti-package-manifest.js.map +1 -0
- package/dist/qti-package-metadata.d.ts +8 -0
- package/dist/qti-package-metadata.d.ts.map +1 -0
- package/dist/qti-package-metadata.js +115 -0
- package/dist/qti-package-metadata.js.map +1 -0
- package/dist/qti-package-paths.d.ts +6 -0
- package/dist/qti-package-paths.d.ts.map +1 -0
- package/dist/qti-package-paths.js +32 -0
- package/dist/qti-package-paths.js.map +1 -0
- package/dist/qti-package-types.d.ts +98 -0
- package/dist/qti-package-types.d.ts.map +1 -0
- package/dist/qti-package-types.js +2 -0
- package/dist/qti-package-types.js.map +1 -0
- package/dist/qti-package-xml.d.ts +21 -0
- package/dist/qti-package-xml.d.ts.map +1 -0
- package/dist/qti-package-xml.js +68 -0
- package/dist/qti-package-xml.js.map +1 -0
- package/dist/qti-package-zip.d.ts +24 -0
- package/dist/qti-package-zip.d.ts.map +1 -0
- package/dist/qti-package-zip.js +185 -0
- package/dist/qti-package-zip.js.map +1 -0
- package/dist/qti-package.d.ts +7 -0
- package/dist/qti-package.d.ts.map +1 -0
- package/dist/qti-package.js +76 -0
- package/dist/qti-package.js.map +1 -0
- package/dist/response-input.d.ts +9 -0
- package/dist/response-input.d.ts.map +1 -0
- package/dist/response-input.js +9 -0
- package/dist/response-input.js.map +1 -0
- package/dist/response-validation-policy.d.ts +31 -0
- package/dist/response-validation-policy.d.ts.map +1 -0
- package/dist/response-validation-policy.js +166 -0
- package/dist/response-validation-policy.js.map +1 -0
- package/dist/response-validation.d.ts +23 -0
- package/dist/response-validation.d.ts.map +1 -0
- package/dist/response-validation.js +170 -0
- package/dist/response-validation.js.map +1 -0
- package/dist/scoring-disposition-policy.d.ts +8 -0
- package/dist/scoring-disposition-policy.d.ts.map +1 -0
- package/dist/scoring-disposition-policy.js +24 -0
- package/dist/scoring-disposition-policy.js.map +1 -0
- package/dist/template-presentation-materializer.d.ts +14 -0
- package/dist/template-presentation-materializer.d.ts.map +1 -0
- package/dist/template-presentation-materializer.js +143 -0
- package/dist/template-presentation-materializer.js.map +1 -0
- package/dist/trusted-item-session.d.ts +25 -8
- package/dist/trusted-item-session.d.ts.map +1 -1
- package/dist/trusted-item-session.js +51 -46
- package/dist/trusted-item-session.js.map +1 -1
- package/dist/trusted-session-materialization.d.ts +33 -0
- package/dist/trusted-session-materialization.d.ts.map +1 -0
- package/dist/trusted-session-materialization.js +26 -0
- package/dist/trusted-session-materialization.js.map +1 -0
- package/dist/validation-interactions.js +1 -0
- package/dist/validation-interactions.js.map +1 -1
- package/dist/validation-primitives.d.ts +1 -0
- package/dist/validation-primitives.d.ts.map +1 -1
- package/dist/validation-primitives.js +5 -0
- package/dist/validation-primitives.js.map +1 -1
- package/dist/variable-snapshot.d.ts +9 -0
- package/dist/variable-snapshot.d.ts.map +1 -0
- package/dist/variable-snapshot.js +9 -0
- package/dist/variable-snapshot.js.map +1 -0
- package/package.json +1 -1
- package/src/adaptive-turn-materializer.ts +29 -16
- package/src/adaptive-turn.ts +29 -6
- package/src/delivery-preparation.ts +121 -0
- package/src/delivery-redaction.ts +36 -5
- package/src/delivery-security.ts +2 -2
- package/src/index.ts +65 -0
- package/src/item-scoring-expectation.ts +9 -0
- package/src/item-submission-materialization.ts +120 -0
- package/src/qti-package-assets.ts +256 -0
- package/src/qti-package-items.ts +240 -0
- package/src/qti-package-manifest.ts +210 -0
- package/src/qti-package-metadata.ts +143 -0
- package/src/qti-package-paths.ts +56 -0
- package/src/qti-package-types.ts +117 -0
- package/src/qti-package-xml.ts +97 -0
- package/src/qti-package-zip.ts +325 -0
- package/src/qti-package.ts +176 -0
- package/src/response-input.ts +19 -0
- package/src/response-validation-policy.ts +226 -0
- package/src/response-validation.ts +298 -0
- package/src/scoring-disposition-policy.ts +37 -0
- package/src/template-presentation-materializer.ts +219 -0
- package/src/trusted-item-session.ts +80 -74
- package/src/trusted-session-materialization.ts +67 -0
- package/src/validation-interactions.ts +1 -0
- package/src/validation-primitives.ts +5 -0
- package/src/variable-snapshot.ts +20 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** One named response variable in record or array wire form. */
|
|
2
|
+
export interface QtiNamedResponseInput {
|
|
3
|
+
identifier: string;
|
|
4
|
+
value: unknown;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export type QtiNamedResponsesInput =
|
|
8
|
+
| Record<string, unknown>
|
|
9
|
+
| readonly QtiNamedResponseInput[]
|
|
10
|
+
| undefined;
|
|
11
|
+
|
|
12
|
+
/** Normalize record or array response payloads into a stable entry list. */
|
|
13
|
+
export function listNamedResponseInputs(
|
|
14
|
+
responses: QtiNamedResponsesInput,
|
|
15
|
+
): readonly QtiNamedResponseInput[] {
|
|
16
|
+
if (!responses) return [];
|
|
17
|
+
if (Array.isArray(responses)) return responses;
|
|
18
|
+
return Object.entries(responses).map(([identifier, value]) => ({ identifier, value }));
|
|
19
|
+
}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import type { QtiChoice, QtiDiagnostic, QtiInteraction, QtiValue } from "./types.js";
|
|
2
|
+
import { parseNonNegativeInteger } from "./validation-primitives.js";
|
|
3
|
+
import { qtiValueToIdentifierList } from "./value-format.js";
|
|
4
|
+
|
|
5
|
+
export interface QtiResponseValidationPolicy {
|
|
6
|
+
checkMinimum: boolean;
|
|
7
|
+
checkMaximum: boolean;
|
|
8
|
+
checkMatchMax: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function responseIsEmpty(value: QtiValue): boolean {
|
|
12
|
+
return value === null || value === "" || (Array.isArray(value) && value.length === 0);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function responseCount(value: QtiValue): number {
|
|
16
|
+
return responseIsEmpty(value) ? 0 : Array.isArray(value) ? value.length : 1;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Order and graphic order ignore max-choices unless min-choices is authored. */
|
|
20
|
+
export function orderSubsetLimitsActive(interaction: QtiInteraction): boolean {
|
|
21
|
+
return (
|
|
22
|
+
(interaction.type === "order" || interaction.type === "graphicOrder") &&
|
|
23
|
+
interaction.attributes["min-choices"] !== undefined
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function responseLimitAttribute(
|
|
28
|
+
interaction: QtiInteraction,
|
|
29
|
+
choiceKey: "min-choices" | "max-choices",
|
|
30
|
+
associationKey: "min-associations" | "max-associations",
|
|
31
|
+
): string | undefined {
|
|
32
|
+
if (interaction.type === "order" || interaction.type === "graphicOrder") {
|
|
33
|
+
return interaction.attributes[choiceKey];
|
|
34
|
+
}
|
|
35
|
+
return interaction.attributes[choiceKey] ?? interaction.attributes[associationKey];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function interactionRequiresResponse(interaction: QtiInteraction): boolean {
|
|
39
|
+
return interaction.attributes.required === "true";
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function minimumRequiredResponses(interaction: QtiInteraction | undefined): number {
|
|
43
|
+
if (!interaction) return 1;
|
|
44
|
+
if (interaction.type === "media") return minimumMediaPlays(interaction);
|
|
45
|
+
const explicit = responseLimitAttribute(interaction, "min-choices", "min-associations");
|
|
46
|
+
if (explicit === undefined) return interactionRequiresResponse(interaction) ? 1 : 0;
|
|
47
|
+
return parseNonNegativeInteger(explicit) ?? 1;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function maximumAllowedResponses(
|
|
51
|
+
interaction: QtiInteraction | undefined,
|
|
52
|
+
): number | undefined {
|
|
53
|
+
if (!interaction) return undefined;
|
|
54
|
+
if (interaction.type === "media") return maximumMediaPlays(interaction);
|
|
55
|
+
if (
|
|
56
|
+
(interaction.type === "order" || interaction.type === "graphicOrder") &&
|
|
57
|
+
!orderSubsetLimitsActive(interaction)
|
|
58
|
+
) {
|
|
59
|
+
return undefined;
|
|
60
|
+
}
|
|
61
|
+
const explicit = responseLimitAttribute(interaction, "max-choices", "max-associations");
|
|
62
|
+
if (explicit === undefined) return undefined;
|
|
63
|
+
const parsed = parseNonNegativeInteger(explicit);
|
|
64
|
+
return parsed === undefined || parsed <= 0 ? undefined : parsed;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function associationMaximumResponses(interaction: QtiInteraction): number | undefined {
|
|
68
|
+
return interaction.responseCardinality === "single" ? 1 : maximumAllowedResponses(interaction);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function mediaPlayCount(value: QtiValue): number {
|
|
72
|
+
const parsed = typeof value === "number" ? value : typeof value === "string" ? Number(value) : 0;
|
|
73
|
+
return Number.isInteger(parsed) && parsed > 0 ? parsed : 0;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function minimumMediaPlays(interaction: QtiInteraction): number {
|
|
77
|
+
const parsed = parseNonNegativeInteger(interaction.attributes["min-plays"]);
|
|
78
|
+
if (parsed !== undefined) return parsed;
|
|
79
|
+
return interactionRequiresResponse(interaction) ? 1 : 0;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function maximumMediaPlays(interaction: QtiInteraction): number | undefined {
|
|
83
|
+
const parsed = parseNonNegativeInteger(interaction.attributes["max-plays"]);
|
|
84
|
+
return parsed === undefined || parsed <= 0 ? undefined : parsed;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export { maximumMediaPlays, minimumMediaPlays };
|
|
88
|
+
|
|
89
|
+
export function responseValidationPolicy(
|
|
90
|
+
declaration: { readonly correctResponse: QtiValue | null },
|
|
91
|
+
interaction: QtiInteraction | undefined,
|
|
92
|
+
): QtiResponseValidationPolicy {
|
|
93
|
+
const authoredMinimum =
|
|
94
|
+
interaction === undefined
|
|
95
|
+
? undefined
|
|
96
|
+
: responseLimitAttribute(interaction, "min-choices", "min-associations");
|
|
97
|
+
const minimum = interaction === undefined ? undefined : minimumRequiredResponses(interaction);
|
|
98
|
+
const validatesMinimum =
|
|
99
|
+
declaration.correctResponse !== null ||
|
|
100
|
+
interaction?.type === "media" ||
|
|
101
|
+
authoredMinimum !== undefined ||
|
|
102
|
+
(minimum !== undefined && minimum > 0);
|
|
103
|
+
const maximum = maximumAllowedResponses(interaction);
|
|
104
|
+
if (
|
|
105
|
+
declaration.correctResponse === null &&
|
|
106
|
+
interaction?.type !== "media" &&
|
|
107
|
+
!validatesMinimum &&
|
|
108
|
+
maximum === undefined
|
|
109
|
+
) {
|
|
110
|
+
return { checkMinimum: false, checkMaximum: false, checkMatchMax: false };
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
checkMinimum: validatesMinimum,
|
|
114
|
+
checkMaximum: maximum !== undefined,
|
|
115
|
+
checkMatchMax: interaction !== undefined,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function requiredResponseDiagnostic(
|
|
120
|
+
responseIdentifier: string,
|
|
121
|
+
interaction: QtiInteraction | undefined,
|
|
122
|
+
minimum: number,
|
|
123
|
+
): QtiDiagnostic {
|
|
124
|
+
return {
|
|
125
|
+
code: "response.required",
|
|
126
|
+
severity: "error",
|
|
127
|
+
message:
|
|
128
|
+
interaction?.attributes["data-min-selections-message"] ??
|
|
129
|
+
(interaction?.type === "media"
|
|
130
|
+
? `${responseIdentifier} requires at least ${minimum} play${minimum === 1 ? "" : "s"}.`
|
|
131
|
+
: minimum === 1
|
|
132
|
+
? `${responseIdentifier} requires a response.`
|
|
133
|
+
: `${responseIdentifier} requires at least ${minimum} responses.`),
|
|
134
|
+
path: responseIdentifier,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function maximumResponseDiagnostic(
|
|
139
|
+
responseIdentifier: string,
|
|
140
|
+
interaction: QtiInteraction | undefined,
|
|
141
|
+
maximum: number,
|
|
142
|
+
): QtiDiagnostic {
|
|
143
|
+
return {
|
|
144
|
+
code: "response.maximum",
|
|
145
|
+
severity: "error",
|
|
146
|
+
message:
|
|
147
|
+
interaction?.attributes["data-max-selections-message"] ??
|
|
148
|
+
(interaction?.type === "media"
|
|
149
|
+
? `${responseIdentifier} allows at most ${maximum} play${maximum === 1 ? "" : "s"}.`
|
|
150
|
+
: `${responseIdentifier} allows at most ${maximum} response${maximum === 1 ? "" : "s"}.`),
|
|
151
|
+
path: responseIdentifier,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function parseUnlimitedMaximum(value: string | undefined): number | undefined {
|
|
156
|
+
if (value === undefined) return undefined;
|
|
157
|
+
const parsed = parseNonNegativeInteger(value);
|
|
158
|
+
return parsed === undefined || parsed <= 0 ? undefined : parsed;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function choiceMatchMaximum(choice: QtiChoice): number | undefined {
|
|
162
|
+
return parseUnlimitedMaximum(choice.attributes["match-max"]);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export type DirectedPairUseSide = "source" | "target" | "either";
|
|
166
|
+
|
|
167
|
+
export function directedPairChoiceUseCount(
|
|
168
|
+
choice: QtiChoice,
|
|
169
|
+
selectedPairs: string[],
|
|
170
|
+
side: DirectedPairUseSide = "either",
|
|
171
|
+
): number {
|
|
172
|
+
return selectedPairs.reduce((count, pair) => {
|
|
173
|
+
const [source = "", target = ""] = pair.split(/\s+/);
|
|
174
|
+
if (side === "source") return source === choice.identifier ? count + 1 : count;
|
|
175
|
+
if (side === "target") return target === choice.identifier ? count + 1 : count;
|
|
176
|
+
return count + (source === choice.identifier ? 1 : 0) + (target === choice.identifier ? 1 : 0);
|
|
177
|
+
}, 0);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function choiceMatchLimitExceeded(choice: QtiChoice, useCount: number): boolean {
|
|
181
|
+
const maximum = choiceMatchMaximum(choice);
|
|
182
|
+
return maximum !== undefined && useCount > maximum;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export function wouldExceedChoiceMatchMaximum(
|
|
186
|
+
choice: QtiChoice,
|
|
187
|
+
selectedPairs: string[],
|
|
188
|
+
side: DirectedPairUseSide = "either",
|
|
189
|
+
): boolean {
|
|
190
|
+
return choiceMatchLimitExceeded(
|
|
191
|
+
choice,
|
|
192
|
+
directedPairChoiceUseCount(choice, selectedPairs, side) + 1,
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function responseChoiceIdentifiers(response: QtiValue): string[] {
|
|
197
|
+
return qtiValueToIdentifierList(response).flatMap((value) => value.split(/\s+/).filter(Boolean));
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export function matchMaxDiagnostics(
|
|
201
|
+
responseIdentifier: string,
|
|
202
|
+
interaction: QtiInteraction,
|
|
203
|
+
response: QtiValue,
|
|
204
|
+
): QtiDiagnostic[] {
|
|
205
|
+
const identifiers = responseChoiceIdentifiers(response);
|
|
206
|
+
if (identifiers.length === 0) return [];
|
|
207
|
+
const counts = new Map<string, number>();
|
|
208
|
+
for (const identifier of identifiers) {
|
|
209
|
+
counts.set(identifier, (counts.get(identifier) ?? 0) + 1);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const diagnostics: QtiDiagnostic[] = [];
|
|
213
|
+
for (const choice of interaction.choices) {
|
|
214
|
+
const maximum = choiceMatchMaximum(choice);
|
|
215
|
+
if (maximum === undefined) continue;
|
|
216
|
+
const count = counts.get(choice.identifier) ?? 0;
|
|
217
|
+
if (count <= maximum) continue;
|
|
218
|
+
diagnostics.push({
|
|
219
|
+
code: "response.matchMax",
|
|
220
|
+
severity: "error",
|
|
221
|
+
message: `${choice.text || choice.identifier} may be used at most ${maximum} time${maximum === 1 ? "" : "s"}.`,
|
|
222
|
+
path: responseIdentifier,
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
return diagnostics;
|
|
226
|
+
}
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
QtiAssessmentItem,
|
|
3
|
+
QtiCardinality,
|
|
4
|
+
QtiDiagnostic,
|
|
5
|
+
QtiInteraction,
|
|
6
|
+
QtiResponseDeclaration,
|
|
7
|
+
QtiValue,
|
|
8
|
+
} from "./types.js";
|
|
9
|
+
import { assertNever } from "./assert-never.js";
|
|
10
|
+
import { isNullResponse, isRecordValue } from "./processing-values.js";
|
|
11
|
+
import { listNamedResponseInputs, type QtiNamedResponseInput } from "./response-input.js";
|
|
12
|
+
import {
|
|
13
|
+
matchMaxDiagnostics,
|
|
14
|
+
maximumAllowedResponses,
|
|
15
|
+
maximumResponseDiagnostic,
|
|
16
|
+
mediaPlayCount,
|
|
17
|
+
minimumRequiredResponses,
|
|
18
|
+
requiredResponseDiagnostic,
|
|
19
|
+
responseCount,
|
|
20
|
+
responseLimitAttribute,
|
|
21
|
+
responseValidationPolicy,
|
|
22
|
+
} from "./response-validation-policy.js";
|
|
23
|
+
import { readQtiJsonValue } from "./value-format.js";
|
|
24
|
+
|
|
25
|
+
export type { QtiNamedResponseInput as QtiResponseVariableInput } from "./response-input.js";
|
|
26
|
+
export type QtiResponseVariablesInput = Record<string, unknown> | readonly QtiNamedResponseInput[];
|
|
27
|
+
|
|
28
|
+
export type QtiResponseValidationDiagnosticCode =
|
|
29
|
+
| "response.required"
|
|
30
|
+
| "response.maximum"
|
|
31
|
+
| "response.matchMax"
|
|
32
|
+
| "response.cardinality"
|
|
33
|
+
| "response.undeclared"
|
|
34
|
+
| "response.identifier.required"
|
|
35
|
+
| "response.value.invalid";
|
|
36
|
+
|
|
37
|
+
export interface QtiResponseValidationDiagnostic extends QtiDiagnostic {
|
|
38
|
+
code: QtiResponseValidationDiagnosticCode;
|
|
39
|
+
identifier?: string | undefined;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface QtiResponseValidationResult {
|
|
43
|
+
ok: boolean;
|
|
44
|
+
diagnostics: QtiResponseValidationDiagnostic[];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface QtiResponseValidationInput {
|
|
48
|
+
item: QtiAssessmentItem;
|
|
49
|
+
responses: QtiResponseVariablesInput;
|
|
50
|
+
allowIncompleteResponses?: boolean | undefined;
|
|
51
|
+
allowedUndeclaredResponseIdentifiers?: readonly string[] | undefined;
|
|
52
|
+
responseIdentifiers?: Iterable<string> | undefined;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Validate submitted response variables against a parsed QTI assessment item. */
|
|
56
|
+
export function validateQtiResponseVariables(
|
|
57
|
+
input: QtiResponseValidationInput,
|
|
58
|
+
): QtiResponseValidationResult {
|
|
59
|
+
const diagnostics: QtiResponseValidationDiagnostic[] = [];
|
|
60
|
+
const declaredIdentifiers = new Set(
|
|
61
|
+
input.item.responseDeclarations.map((declaration) => declaration.identifier),
|
|
62
|
+
);
|
|
63
|
+
const allowedUndeclaredIdentifiers = new Set(input.allowedUndeclaredResponseIdentifiers ?? []);
|
|
64
|
+
const responses = ingestSubmittedResponses(
|
|
65
|
+
input.responses,
|
|
66
|
+
declaredIdentifiers,
|
|
67
|
+
allowedUndeclaredIdentifiers,
|
|
68
|
+
diagnostics,
|
|
69
|
+
);
|
|
70
|
+
const scopedResponseIdentifiers = input.responseIdentifiers
|
|
71
|
+
? new Set(input.responseIdentifiers)
|
|
72
|
+
: undefined;
|
|
73
|
+
const interactionsByResponse = indexInteractionsByResponse(input.item.interactions);
|
|
74
|
+
|
|
75
|
+
for (const declaration of input.item.responseDeclarations) {
|
|
76
|
+
if (
|
|
77
|
+
scopedResponseIdentifiers !== undefined &&
|
|
78
|
+
!scopedResponseIdentifiers.has(declaration.identifier)
|
|
79
|
+
) {
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const value = responses.get(declaration.identifier);
|
|
84
|
+
if (value !== undefined) {
|
|
85
|
+
validateResponseCardinality(declaration, value, diagnostics);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const interactions = interactionsByResponse.get(declaration.identifier);
|
|
89
|
+
if (interactions === undefined) {
|
|
90
|
+
validateDeclarationResponse(
|
|
91
|
+
declaration,
|
|
92
|
+
undefined,
|
|
93
|
+
value,
|
|
94
|
+
input.allowIncompleteResponses,
|
|
95
|
+
diagnostics,
|
|
96
|
+
);
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
for (const interaction of interactions) {
|
|
101
|
+
validateDeclarationResponse(
|
|
102
|
+
declaration,
|
|
103
|
+
interaction,
|
|
104
|
+
value,
|
|
105
|
+
input.allowIncompleteResponses,
|
|
106
|
+
diagnostics,
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
ok: diagnostics.every((diagnostic) => diagnostic.severity !== "error"),
|
|
113
|
+
diagnostics,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function ingestSubmittedResponses(
|
|
118
|
+
input: QtiResponseVariablesInput,
|
|
119
|
+
declaredIdentifiers: ReadonlySet<string>,
|
|
120
|
+
allowedUndeclaredIdentifiers: ReadonlySet<string>,
|
|
121
|
+
diagnostics: QtiResponseValidationDiagnostic[],
|
|
122
|
+
): Map<string, QtiValue> {
|
|
123
|
+
const responses = new Map<string, QtiValue>();
|
|
124
|
+
|
|
125
|
+
for (const entry of listNamedResponseInputs(input)) {
|
|
126
|
+
const identifier = entry.identifier.trim();
|
|
127
|
+
if (!identifier) {
|
|
128
|
+
diagnostics.push({
|
|
129
|
+
code: "response.identifier.required",
|
|
130
|
+
severity: "error",
|
|
131
|
+
message: "Response identifiers must be non-empty strings.",
|
|
132
|
+
});
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const value = readQtiJsonValue(entry.value);
|
|
137
|
+
if (value === undefined) {
|
|
138
|
+
diagnostics.push({
|
|
139
|
+
code: "response.value.invalid",
|
|
140
|
+
severity: "error",
|
|
141
|
+
identifier,
|
|
142
|
+
message: `Response ${identifier} is not a supported QTI value.`,
|
|
143
|
+
path: identifier,
|
|
144
|
+
});
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (!declaredIdentifiers.has(identifier)) {
|
|
149
|
+
if (allowedUndeclaredIdentifiers.has(identifier)) continue;
|
|
150
|
+
diagnostics.push({
|
|
151
|
+
code: "response.undeclared",
|
|
152
|
+
severity: "error",
|
|
153
|
+
identifier,
|
|
154
|
+
message: `Response ${identifier} is not declared by the assessment item.`,
|
|
155
|
+
path: identifier,
|
|
156
|
+
});
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
responses.set(identifier, value);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return responses;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function indexInteractionsByResponse(
|
|
167
|
+
interactions: readonly QtiInteraction[],
|
|
168
|
+
): Map<string, QtiInteraction[]> {
|
|
169
|
+
const indexed = new Map<string, QtiInteraction[]>();
|
|
170
|
+
for (const interaction of interactions) {
|
|
171
|
+
const identifier = interaction.responseIdentifier;
|
|
172
|
+
if (!identifier) continue;
|
|
173
|
+
const bucket = indexed.get(identifier);
|
|
174
|
+
if (bucket) {
|
|
175
|
+
bucket.push(interaction);
|
|
176
|
+
} else {
|
|
177
|
+
indexed.set(identifier, [interaction]);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return indexed;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function validateResponseCardinality(
|
|
184
|
+
declaration: QtiResponseDeclaration,
|
|
185
|
+
value: QtiValue,
|
|
186
|
+
diagnostics: QtiResponseValidationDiagnostic[],
|
|
187
|
+
): void {
|
|
188
|
+
if (value === null) return;
|
|
189
|
+
|
|
190
|
+
if (matchesCardinality(declaration.cardinality, value)) return;
|
|
191
|
+
diagnostics.push({
|
|
192
|
+
code: "response.cardinality",
|
|
193
|
+
severity: "error",
|
|
194
|
+
identifier: declaration.identifier,
|
|
195
|
+
message: `Response ${declaration.identifier} must match ${declaration.cardinality} cardinality.`,
|
|
196
|
+
path: declaration.identifier,
|
|
197
|
+
source: declaration.source,
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function matchesCardinality(cardinality: QtiCardinality, value: QtiValue): boolean {
|
|
202
|
+
switch (cardinality) {
|
|
203
|
+
case "single":
|
|
204
|
+
return !Array.isArray(value) && !isRecordValue(value);
|
|
205
|
+
case "multiple":
|
|
206
|
+
case "ordered":
|
|
207
|
+
return Array.isArray(value);
|
|
208
|
+
case "record":
|
|
209
|
+
return isRecordValue(value);
|
|
210
|
+
}
|
|
211
|
+
return assertNever(cardinality);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function validateDeclarationResponse(
|
|
215
|
+
declaration: QtiResponseDeclaration,
|
|
216
|
+
interaction: QtiInteraction | undefined,
|
|
217
|
+
value: QtiValue | undefined,
|
|
218
|
+
allowIncompleteResponses: boolean | undefined,
|
|
219
|
+
diagnostics: QtiResponseValidationDiagnostic[],
|
|
220
|
+
): void {
|
|
221
|
+
const policy = responseValidationPolicy(declaration, interaction);
|
|
222
|
+
if (!policy.checkMinimum && !policy.checkMaximum && !policy.checkMatchMax) return;
|
|
223
|
+
|
|
224
|
+
const effectiveValue = value ?? null;
|
|
225
|
+
const count =
|
|
226
|
+
interaction?.type === "media" ? mediaPlayCount(effectiveValue) : responseCount(effectiveValue);
|
|
227
|
+
|
|
228
|
+
if (policy.checkMinimum && !allowIncompleteResponses) {
|
|
229
|
+
const minimum = effectiveMinimumRequiredResponses(declaration, interaction);
|
|
230
|
+
if (count < minimum) {
|
|
231
|
+
diagnostics.push(
|
|
232
|
+
attachResponseIdentifier(
|
|
233
|
+
declaration.identifier,
|
|
234
|
+
requiredResponseDiagnostic(declaration.identifier, interaction, minimum),
|
|
235
|
+
),
|
|
236
|
+
);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (policy.checkMaximum && value !== undefined && !isNullResponse(value)) {
|
|
241
|
+
const maximum = maximumAllowedResponses(interaction);
|
|
242
|
+
if (maximum !== undefined && count > maximum) {
|
|
243
|
+
diagnostics.push(
|
|
244
|
+
attachResponseIdentifier(
|
|
245
|
+
declaration.identifier,
|
|
246
|
+
maximumResponseDiagnostic(declaration.identifier, interaction, maximum),
|
|
247
|
+
),
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
if (policy.checkMatchMax && interaction && value !== undefined && !isNullResponse(value)) {
|
|
253
|
+
diagnostics.push(
|
|
254
|
+
...matchMaxDiagnostics(declaration.identifier, interaction, value).map((diagnostic) =>
|
|
255
|
+
attachResponseIdentifier(declaration.identifier, diagnostic),
|
|
256
|
+
),
|
|
257
|
+
);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function effectiveMinimumRequiredResponses(
|
|
262
|
+
declaration: { readonly correctResponse: QtiValue | null },
|
|
263
|
+
interaction: QtiInteraction | undefined,
|
|
264
|
+
): number {
|
|
265
|
+
const minimum = minimumRequiredResponses(interaction);
|
|
266
|
+
if (declaration.correctResponse === null || hasAuthoredMinimum(interaction)) return minimum;
|
|
267
|
+
return Math.max(minimum, 1);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function hasAuthoredMinimum(interaction: QtiInteraction | undefined): boolean {
|
|
271
|
+
if (!interaction) return false;
|
|
272
|
+
if (interaction.type === "media") return interaction.attributes["min-plays"] !== undefined;
|
|
273
|
+
return responseLimitAttribute(interaction, "min-choices", "min-associations") !== undefined;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function attachResponseIdentifier(
|
|
277
|
+
identifier: string,
|
|
278
|
+
diagnostic: QtiDiagnostic,
|
|
279
|
+
): QtiResponseValidationDiagnostic {
|
|
280
|
+
if (!isResponseValidationDiagnosticCode(diagnostic.code)) {
|
|
281
|
+
throw new Error(`Unexpected response validation code: ${diagnostic.code}`);
|
|
282
|
+
}
|
|
283
|
+
return { ...diagnostic, identifier, code: diagnostic.code };
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function isResponseValidationDiagnosticCode(
|
|
287
|
+
code: string,
|
|
288
|
+
): code is QtiResponseValidationDiagnosticCode {
|
|
289
|
+
return (
|
|
290
|
+
code === "response.required" ||
|
|
291
|
+
code === "response.maximum" ||
|
|
292
|
+
code === "response.matchMax" ||
|
|
293
|
+
code === "response.cardinality" ||
|
|
294
|
+
code === "response.undeclared" ||
|
|
295
|
+
code === "response.identifier.required" ||
|
|
296
|
+
code === "response.value.invalid"
|
|
297
|
+
);
|
|
298
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { QtiAssessmentItem } from "./types.js";
|
|
2
|
+
import { itemExpectsAutomatedScore } from "./item-scoring-expectation.js";
|
|
3
|
+
|
|
4
|
+
export type QtiItemSubmissionScoringDisposition =
|
|
5
|
+
| "scored"
|
|
6
|
+
| "manual-scoring-required"
|
|
7
|
+
| "unscored-reference"
|
|
8
|
+
| "invalid";
|
|
9
|
+
|
|
10
|
+
const MANUALLY_SCORED_INTERACTION_TYPES = new Set<string>([
|
|
11
|
+
"custom",
|
|
12
|
+
"drawing",
|
|
13
|
+
"extendedText",
|
|
14
|
+
"portableCustom",
|
|
15
|
+
"upload",
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
/** Interaction types that typically require human review even without response processing. */
|
|
19
|
+
export function itemHasManuallyScoredInteractions(item: QtiAssessmentItem): boolean {
|
|
20
|
+
return item.interactions.some((interaction) =>
|
|
21
|
+
MANUALLY_SCORED_INTERACTION_TYPES.has(interaction.type),
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function itemNeedsScoringFollowUpWhenUnscored(item: QtiAssessmentItem): boolean {
|
|
26
|
+
return itemExpectsAutomatedScore(item) || itemHasManuallyScoredInteractions(item);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Default generic disposition taxonomy shipped by qti3-core for submission materialization. */
|
|
30
|
+
export function classifyQtiItemScoringDisposition(
|
|
31
|
+
item: QtiAssessmentItem,
|
|
32
|
+
score: number | null,
|
|
33
|
+
): Exclude<QtiItemSubmissionScoringDisposition, "invalid"> {
|
|
34
|
+
if (score !== null) return "scored";
|
|
35
|
+
if (itemNeedsScoringFollowUpWhenUnscored(item)) return "manual-scoring-required";
|
|
36
|
+
return "unscored-reference";
|
|
37
|
+
}
|