@pie-qti/assessment-player 0.1.10 → 0.1.12
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 +22 -82
- package/dist/core/AssessmentPlayer.d.ts +0 -1
- package/dist/core/AssessmentPlayer.js +1 -0
- package/dist/core/AssessmentPlayer.js.map +1 -0
- package/dist/core/AssessmentSessionCoordinator.d.ts +0 -1
- package/dist/core/AssessmentSessionCoordinator.js +1 -0
- package/dist/core/AssessmentSessionCoordinator.js.map +1 -0
- package/dist/core/ItemSessionController.d.ts +0 -1
- package/dist/core/ItemSessionController.js +1 -0
- package/dist/core/ItemSessionController.js.map +1 -0
- package/dist/core/NavigationManager.d.ts +0 -1
- package/dist/core/NavigationManager.js +1 -0
- package/dist/core/NavigationManager.js.map +1 -0
- package/dist/core/StatePersistenceManager.d.ts +0 -1
- package/dist/core/StatePersistenceManager.js +1 -0
- package/dist/core/StatePersistenceManager.js.map +1 -0
- package/dist/core/TimeManager.d.ts +0 -1
- package/dist/core/TimeManager.js +1 -0
- package/dist/core/TimeManager.js.map +1 -0
- package/dist/core/index.d.ts +0 -1
- package/dist/core/index.js +1 -0
- package/dist/core/index.js.map +1 -0
- package/dist/index.d.ts +0 -1
- package/dist/index.js +1 -3
- package/dist/index.js.map +1 -0
- package/dist/integration/ReferenceBackendAdapter.d.ts +0 -1
- package/dist/integration/ReferenceBackendAdapter.js +1 -0
- package/dist/integration/ReferenceBackendAdapter.js.map +1 -0
- package/dist/integration/api-contract.d.ts +0 -1
- package/dist/integration/api-contract.js +1 -0
- package/dist/integration/api-contract.js.map +1 -0
- package/dist/integration/assessment-item-scorer.d.ts +0 -1
- package/dist/integration/assessment-item-scorer.js +1 -0
- package/dist/integration/assessment-item-scorer.js.map +1 -0
- package/dist/integration/index.d.ts +0 -1
- package/dist/integration/index.js +1 -0
- package/dist/integration/index.js.map +1 -0
- package/dist/types/index.d.ts +0 -1
- package/dist/types/index.js +1 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/PerformanceMonitor.d.ts +0 -1
- package/dist/utils/PerformanceMonitor.js +1 -0
- package/dist/utils/PerformanceMonitor.js.map +1 -0
- package/package.json +8 -37
- package/dist/core/AssessmentPlayer.d.ts.map +0 -1
- package/dist/core/AssessmentSessionCoordinator.d.ts.map +0 -1
- package/dist/core/ItemSessionController.d.ts.map +0 -1
- package/dist/core/NavigationManager.d.ts.map +0 -1
- package/dist/core/StatePersistenceManager.d.ts.map +0 -1
- package/dist/core/TimeManager.d.ts.map +0 -1
- package/dist/core/index.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/integration/ReferenceBackendAdapter.d.ts.map +0 -1
- package/dist/integration/api-contract.d.ts.map +0 -1
- package/dist/integration/assessment-item-scorer.d.ts.map +0 -1
- package/dist/integration/index.d.ts.map +0 -1
- package/dist/types/index.d.ts.map +0 -1
- package/dist/utils/PerformanceMonitor.d.ts.map +0 -1
- package/src/ambient.d.ts +0 -55
- package/src/components/AccessibilityAnnouncer.svelte +0 -113
- package/src/components/AssessmentHeader.svelte +0 -76
- package/src/components/AssessmentShell.svelte +0 -468
- package/src/components/AssessmentTimer.svelte +0 -251
- package/src/components/ItemRenderer.svelte +0 -156
- package/src/components/ItemSessionInfo.svelte +0 -241
- package/src/components/NavigationBar.svelte +0 -146
- package/src/components/RubricDisplay.svelte +0 -185
- package/src/components/SectionMenu.svelte +0 -273
- package/src/components/SplitPaneResizer.svelte +0 -288
- package/src/components/TestFeedback.svelte +0 -147
- package/src/components/index.ts +0 -19
- package/src/core/AssessmentPlayer.ts +0 -952
- package/src/core/AssessmentSessionCoordinator.ts +0 -274
- package/src/core/ItemSessionController.ts +0 -287
- package/src/core/NavigationManager.ts +0 -126
- package/src/core/StatePersistenceManager.ts +0 -344
- package/src/core/TimeManager.ts +0 -306
- package/src/core/index.ts +0 -15
- package/src/index.ts +0 -13
- package/src/integration/ReferenceBackendAdapter.ts +0 -1382
- package/src/integration/api-contract.ts +0 -480
- package/src/integration/assessment-item-scorer.ts +0 -58
- package/src/integration/index.ts +0 -77
- package/src/svelte-shims.d.ts +0 -7
- package/src/types/index.ts +0 -59
- package/src/utils/PerformanceMonitor.ts +0 -222
|
@@ -1,952 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* AssessmentPlayer (backend-authoritative)
|
|
3
|
-
*
|
|
4
|
-
* Client responsibilities:
|
|
5
|
-
* - Render items using @pie-qti/item-player
|
|
6
|
-
* - Collect candidate responses
|
|
7
|
-
* - Submit responses to BackendAdapter
|
|
8
|
-
* - Apply backend navigation decisions + session state
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import type { QTIRole, PnpProfile, SerializedItemSessionState } from '@pie-qti/item-player';
|
|
12
|
-
import { Player } from '@pie-qti/item-player';
|
|
13
|
-
import type {
|
|
14
|
-
AssessmentRubricBlock,
|
|
15
|
-
AssessmentSessionState,
|
|
16
|
-
AssessmentScoringResult,
|
|
17
|
-
BackendAdapter,
|
|
18
|
-
FinalizeAssessmentResponse,
|
|
19
|
-
InitSessionRequest,
|
|
20
|
-
InitSessionResponse,
|
|
21
|
-
SecureAssessment,
|
|
22
|
-
SecureItemRef,
|
|
23
|
-
SecureSection,
|
|
24
|
-
SecureTestPart,
|
|
25
|
-
SessionId,
|
|
26
|
-
} from '../integration/api-contract.js';
|
|
27
|
-
import type {
|
|
28
|
-
AssessmentResults,
|
|
29
|
-
ItemResult,
|
|
30
|
-
NavigationState,
|
|
31
|
-
TimeLimits,
|
|
32
|
-
} from '../types/index.js';
|
|
33
|
-
import { AssessmentSessionCoordinator } from './AssessmentSessionCoordinator.js';
|
|
34
|
-
import { NavigationManager } from './NavigationManager.js';
|
|
35
|
-
import { TimeManager } from './TimeManager.js';
|
|
36
|
-
|
|
37
|
-
export interface BackendAssessmentPlayerConfig {
|
|
38
|
-
backend: BackendAdapter;
|
|
39
|
-
initSession: InitSessionRequest;
|
|
40
|
-
role?: QTIRole;
|
|
41
|
-
/** Optional RNG for deterministic shuffling in selection/ordering. Defaults to Math.random. */
|
|
42
|
-
rng?: () => number;
|
|
43
|
-
/**
|
|
44
|
-
* Optional hint for item renderers that support multiple extended text editors.
|
|
45
|
-
* (Plumbed through the shell; the item player remains authoritative for behavior.)
|
|
46
|
-
*/
|
|
47
|
-
extendedTextEditor?: string;
|
|
48
|
-
/**
|
|
49
|
-
* Optional i18n provider for internationalization.
|
|
50
|
-
* If provided, this will be shared across all item players in the assessment.
|
|
51
|
-
*/
|
|
52
|
-
i18nProvider?: any; // Will be I18nProvider from @pie-qti/i18n
|
|
53
|
-
/**
|
|
54
|
-
* Security configuration for URL policy and content restrictions.
|
|
55
|
-
* If provided, this will be passed to all item players in the assessment.
|
|
56
|
-
*/
|
|
57
|
-
security?: any; // Will be PlayerSecurityConfig from @pie-qti/item-player
|
|
58
|
-
/**
|
|
59
|
-
* Demo/reference-only escape hatch. When false (default), rich item-session
|
|
60
|
-
* variables stay client-local and are not submitted because template variables
|
|
61
|
-
* can contain answer keys. Production backends should own this state server-side.
|
|
62
|
-
*/
|
|
63
|
-
sendItemSessionToBackend?: boolean;
|
|
64
|
-
// UI options
|
|
65
|
-
showSections?: boolean;
|
|
66
|
-
allowSectionNavigation?: boolean;
|
|
67
|
-
showProgress?: boolean;
|
|
68
|
-
// Time management
|
|
69
|
-
timeWarningThreshold?: number;
|
|
70
|
-
/**
|
|
71
|
-
* QTI 3.0 §6.2 Personal Needs and Preferences profile.
|
|
72
|
-
* Extended time (pnp.content.extendedTime) is applied to assessment timeLimits.
|
|
73
|
-
* The full profile is passed down to each item player for component-level features
|
|
74
|
-
* (color scheme, elimination tool).
|
|
75
|
-
*/
|
|
76
|
-
pnp?: PnpProfile;
|
|
77
|
-
// Callbacks
|
|
78
|
-
onItemChange?: (itemIndex: number, totalItems: number) => void;
|
|
79
|
-
onSectionChange?: (sectionIndex: number, totalSections: number) => void;
|
|
80
|
-
onResponseChange?: (responses: Record<string, unknown>) => void;
|
|
81
|
-
onComplete?: () => void;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
type FlatItem = {
|
|
85
|
-
identifier: string;
|
|
86
|
-
item: SecureItemRef;
|
|
87
|
-
section: SecureSection;
|
|
88
|
-
testPart: SecureTestPart;
|
|
89
|
-
index: number;
|
|
90
|
-
sectionIndex: number;
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
type CurrentItemView = {
|
|
94
|
-
identifier: string;
|
|
95
|
-
title?: string;
|
|
96
|
-
required?: boolean;
|
|
97
|
-
itemXml: string;
|
|
98
|
-
deliveryContext?: SecureItemRef['deliveryContext'];
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
export interface EffectiveItemTimeLimits {
|
|
102
|
-
timeLimits?: TimeLimits;
|
|
103
|
-
source?: 'item' | 'section' | 'testPart' | 'assessment';
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
type ItemSessionCapablePlayer = Player & {
|
|
107
|
-
suspendAttempt(): { sessionState: SerializedItemSessionState; duration: number };
|
|
108
|
-
endAttempt(options?: { countAttempt?: boolean; validateResponses?: boolean }): {
|
|
109
|
-
sessionState: SerializedItemSessionState;
|
|
110
|
-
duration: number;
|
|
111
|
-
validation?: { valid: boolean; issues: Array<{ message: string }> };
|
|
112
|
-
};
|
|
113
|
-
restoreItemSession(state: SerializedItemSessionState): void;
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
export class AssessmentPlayer {
|
|
117
|
-
public static async create(config: BackendAssessmentPlayerConfig): Promise<AssessmentPlayer> {
|
|
118
|
-
const init = await config.backend.initSession(config.initSession);
|
|
119
|
-
return new AssessmentPlayer(config, init);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
private backend: BackendAdapter;
|
|
123
|
-
private sessionId: SessionId;
|
|
124
|
-
private assessment: SecureAssessment;
|
|
125
|
-
private i18nProvider: any; // I18nProvider from @pie-qti/i18n
|
|
126
|
-
|
|
127
|
-
private navigationManager: NavigationManager;
|
|
128
|
-
private sessionCoordinator!: AssessmentSessionCoordinator;
|
|
129
|
-
private timeManager: TimeManager | null = null;
|
|
130
|
-
|
|
131
|
-
private items: FlatItem[] = [];
|
|
132
|
-
private currentItemIndex = -1;
|
|
133
|
-
private currentItemPlayer: Player | null = null;
|
|
134
|
-
private visibleFeedback: Array<{ identifier: string; content: string; access: string }> = [];
|
|
135
|
-
|
|
136
|
-
// Event listeners
|
|
137
|
-
private itemChangeListeners = new Set<(index: number, total: number) => void>();
|
|
138
|
-
private sectionChangeListeners = new Set<(index: number, total: number) => void>();
|
|
139
|
-
private responseChangeListeners = new Set<(responses: Record<string, unknown>) => void>();
|
|
140
|
-
private completeListeners = new Set<() => void>();
|
|
141
|
-
private timeWarningListeners = new Set<(remainingSeconds: number) => void>();
|
|
142
|
-
private timeExpiredListeners = new Set<() => void>();
|
|
143
|
-
private timeTickListeners = new Set<(remainingSeconds: number, elapsedSeconds: number) => void>();
|
|
144
|
-
|
|
145
|
-
private constructor(private config: BackendAssessmentPlayerConfig, init: InitSessionResponse) {
|
|
146
|
-
this.backend = config.backend;
|
|
147
|
-
this.sessionId = init.sessionId;
|
|
148
|
-
this.assessment = init.assessment;
|
|
149
|
-
this.i18nProvider = config.i18nProvider ?? this.createDefaultI18nProvider();
|
|
150
|
-
const initialState =
|
|
151
|
-
init.restoredState ??
|
|
152
|
-
({
|
|
153
|
-
currentItemIdentifier: '',
|
|
154
|
-
visitedItems: [],
|
|
155
|
-
itemResponses: {},
|
|
156
|
-
itemScores: {},
|
|
157
|
-
itemSessions: {},
|
|
158
|
-
timing: { startedAt: Date.now(), itemTimes: {}, totalTime: 0 },
|
|
159
|
-
} as AssessmentSessionState);
|
|
160
|
-
|
|
161
|
-
this.items = this.flattenItems(this.assessment);
|
|
162
|
-
|
|
163
|
-
const navigationMode = this.assessment.navigationMode || 'nonlinear';
|
|
164
|
-
this.navigationManager = new NavigationManager(navigationMode, this.items.length);
|
|
165
|
-
|
|
166
|
-
// Item session control is a UI hint; backend remains authoritative.
|
|
167
|
-
// Start with testPart-level defaults; section-level overrides are applied per item in navigateTo().
|
|
168
|
-
const itemSessionControl = this.assessment.testParts?.[0]?.itemSessionControl;
|
|
169
|
-
this.sessionCoordinator = new AssessmentSessionCoordinator({
|
|
170
|
-
state: initialState,
|
|
171
|
-
itemIdentifiers: this.items.map((q) => q.identifier),
|
|
172
|
-
itemSessionControl,
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
this.timeManager = new TimeManager({
|
|
176
|
-
assessmentTimeLimits: this.assessment.timeLimits,
|
|
177
|
-
warningThreshold: config.timeWarningThreshold || 60,
|
|
178
|
-
extendedTime: config.pnp?.content?.extendedTime,
|
|
179
|
-
onWarning: (remainingSeconds) => this.notifyTimeWarning(remainingSeconds),
|
|
180
|
-
onExpired: () => this.notifyTimeExpired(),
|
|
181
|
-
onTick: (remainingSeconds, elapsedSeconds) => this.notifyTimeTick(remainingSeconds, elapsedSeconds),
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
// Restore to backend-provided current item if present
|
|
185
|
-
const startId = this.state.currentItemIdentifier;
|
|
186
|
-
const idx = startId ? this.items.findIndex((q) => q.identifier === startId) : -1;
|
|
187
|
-
if (idx >= 0) {
|
|
188
|
-
this.navigateTo(idx, { restoring: true }).catch(() => {});
|
|
189
|
-
} else if (this.items.length > 0) {
|
|
190
|
-
this.navigateTo(0, { restoring: true }).catch(() => {});
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
private get state(): AssessmentSessionState {
|
|
195
|
-
return this.sessionCoordinator.state;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
private set state(state: AssessmentSessionState) {
|
|
199
|
-
this.sessionCoordinator.restoreState(state);
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* Compute effective itemSessionControl for a given item using three-level fallback:
|
|
204
|
-
* section-level (highest priority) → testPart-level → defaults.
|
|
205
|
-
* Section-level values override testPart-level when present.
|
|
206
|
-
*/
|
|
207
|
-
private getEffectiveItemSessionControl(flat: FlatItem): SecureSection['itemSessionControl'] {
|
|
208
|
-
const testPartControl = flat.testPart.itemSessionControl;
|
|
209
|
-
const sectionControl = flat.section.itemSessionControl;
|
|
210
|
-
|
|
211
|
-
if (!sectionControl) return testPartControl;
|
|
212
|
-
if (!testPartControl) return sectionControl;
|
|
213
|
-
|
|
214
|
-
return {
|
|
215
|
-
maxAttempts: sectionControl.maxAttempts ?? testPartControl.maxAttempts,
|
|
216
|
-
showFeedback: sectionControl.showFeedback ?? testPartControl.showFeedback,
|
|
217
|
-
allowReview: sectionControl.allowReview ?? testPartControl.allowReview,
|
|
218
|
-
showSolution: sectionControl.showSolution ?? testPartControl.showSolution,
|
|
219
|
-
allowComment: sectionControl.allowComment ?? testPartControl.allowComment,
|
|
220
|
-
allowSkipping: sectionControl.allowSkipping ?? testPartControl.allowSkipping,
|
|
221
|
-
validateResponses: sectionControl.validateResponses ?? testPartControl.validateResponses,
|
|
222
|
-
};
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
private getEffectiveItemTimeLimits(flat: FlatItem): EffectiveItemTimeLimits {
|
|
226
|
-
const scopes: Array<{ source: EffectiveItemTimeLimits['source']; limits?: TimeLimits }> = [
|
|
227
|
-
{ source: 'assessment', limits: this.assessment.timeLimits },
|
|
228
|
-
{ source: 'testPart', limits: flat.testPart.timeLimits },
|
|
229
|
-
{ source: 'section', limits: flat.section.timeLimits },
|
|
230
|
-
{ source: 'item', limits: flat.item.timeLimits },
|
|
231
|
-
];
|
|
232
|
-
let source: EffectiveItemTimeLimits['source'];
|
|
233
|
-
let minTime: number | undefined;
|
|
234
|
-
let maxTime: number | undefined;
|
|
235
|
-
let allowLateSubmission: boolean | undefined;
|
|
236
|
-
for (const scope of scopes) {
|
|
237
|
-
if (!scope.limits) continue;
|
|
238
|
-
if (scope.limits.minTime !== undefined) {
|
|
239
|
-
minTime = minTime === undefined ? scope.limits.minTime : Math.max(minTime, scope.limits.minTime);
|
|
240
|
-
}
|
|
241
|
-
if (scope.limits.maxTime !== undefined) {
|
|
242
|
-
const extended = this.config.pnp?.content?.extendedTime;
|
|
243
|
-
const scopedMaxTime = extended?.active
|
|
244
|
-
? extended.multiplier === Infinity
|
|
245
|
-
? undefined
|
|
246
|
-
: scope.limits.maxTime * extended.multiplier
|
|
247
|
-
: scope.limits.maxTime;
|
|
248
|
-
if (scopedMaxTime !== undefined && (maxTime === undefined || scopedMaxTime < maxTime)) {
|
|
249
|
-
maxTime = scopedMaxTime;
|
|
250
|
-
source = scope.source;
|
|
251
|
-
allowLateSubmission = scope.limits.allowLateSubmission;
|
|
252
|
-
}
|
|
253
|
-
} else if (scope.limits.allowLateSubmission !== undefined && maxTime === undefined) {
|
|
254
|
-
allowLateSubmission = scope.limits.allowLateSubmission;
|
|
255
|
-
source = scope.source;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
if (maxTime === undefined && minTime === undefined && allowLateSubmission === undefined) {
|
|
260
|
-
return {};
|
|
261
|
-
}
|
|
262
|
-
return { source, timeLimits: { minTime, maxTime, allowLateSubmission } };
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
public getCurrentEffectiveTimeLimits(): EffectiveItemTimeLimits {
|
|
266
|
-
const q = this.items[this.currentItemIndex];
|
|
267
|
-
return q ? this.getEffectiveItemTimeLimits(q) : {};
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
private getSubmitTimingEvidence(q: FlatItem, elapsedMs: number): import('../integration/api-contract.js').SubmitTimingEvidence | undefined {
|
|
271
|
-
const effective = this.getEffectiveItemTimeLimits(q);
|
|
272
|
-
const limitSeconds = effective.timeLimits?.maxTime;
|
|
273
|
-
if (!effective.source || limitSeconds === undefined) return undefined;
|
|
274
|
-
if (effective.source !== 'item' && effective.source !== 'assessment') {
|
|
275
|
-
return undefined;
|
|
276
|
-
}
|
|
277
|
-
const scopedElapsedMs = effective.source === 'assessment'
|
|
278
|
-
? (this.timeManager?.getElapsedSeconds() ?? 0) * 1000
|
|
279
|
-
: elapsedMs;
|
|
280
|
-
return {
|
|
281
|
-
scope: effective.source,
|
|
282
|
-
elapsedMs: scopedElapsedMs,
|
|
283
|
-
limitSeconds,
|
|
284
|
-
expired: scopedElapsedMs >= limitSeconds * 1000,
|
|
285
|
-
allowLateSubmission: effective.timeLimits?.allowLateSubmission,
|
|
286
|
-
};
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
private flattenItems(assessment: SecureAssessment): FlatItem[] {
|
|
290
|
-
const out: FlatItem[] = [];
|
|
291
|
-
const testParts = assessment.testParts || [];
|
|
292
|
-
let idx = 0;
|
|
293
|
-
let secIdx = 0;
|
|
294
|
-
const rng = this.config.rng ?? Math.random;
|
|
295
|
-
|
|
296
|
-
for (const tp of testParts) {
|
|
297
|
-
for (const section of tp.sections || []) {
|
|
298
|
-
let refs = [...(section.assessmentItemRefs || [])];
|
|
299
|
-
|
|
300
|
-
// QTI ordering: shuffle items within the section when requested.
|
|
301
|
-
// The server may have already shuffled; if not (e.g. reference adapter), do it client-side.
|
|
302
|
-
if (section.ordering?.shuffle) {
|
|
303
|
-
refs = shuffleArray(refs, rng);
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
// QTI selection: take only `select` items from the (possibly shuffled) list.
|
|
307
|
-
if (section.selection && section.selection.select > 0 && section.selection.select < refs.length) {
|
|
308
|
-
refs = refs.slice(0, section.selection.select);
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
for (const item of refs) {
|
|
312
|
-
out.push({
|
|
313
|
-
identifier: item.identifier,
|
|
314
|
-
item,
|
|
315
|
-
section,
|
|
316
|
-
testPart: tp,
|
|
317
|
-
index: idx++,
|
|
318
|
-
sectionIndex: secIdx,
|
|
319
|
-
});
|
|
320
|
-
}
|
|
321
|
-
secIdx++;
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
return out;
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
// ---------------------------------------------------------------------------
|
|
328
|
-
// Public API used by components
|
|
329
|
-
// ---------------------------------------------------------------------------
|
|
330
|
-
|
|
331
|
-
public getAllSections(): Array<{ id: string; title?: string; visible: boolean; index: number }> {
|
|
332
|
-
const out: Array<{ id: string; title?: string; visible: boolean; index: number }> = [];
|
|
333
|
-
let idx = 0;
|
|
334
|
-
for (const tp of this.assessment.testParts || []) {
|
|
335
|
-
for (const s of tp.sections || []) {
|
|
336
|
-
out.push({ id: s.identifier, title: s.title, visible: s.visible, index: idx++ });
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
return out;
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
public getNavigationState(): NavigationState {
|
|
343
|
-
const total = this.items.length;
|
|
344
|
-
const canPrevious = this.canPrevious();
|
|
345
|
-
const canNext = this.canNext();
|
|
346
|
-
const q = this.items[this.currentItemIndex];
|
|
347
|
-
const totalSections = this.getAllSections().length;
|
|
348
|
-
return {
|
|
349
|
-
currentIndex: this.currentItemIndex,
|
|
350
|
-
totalItems: total,
|
|
351
|
-
canNext,
|
|
352
|
-
canPrevious,
|
|
353
|
-
isLoading: false,
|
|
354
|
-
currentSection: q
|
|
355
|
-
? { id: q.section.identifier, title: q.section.title, index: q.sectionIndex }
|
|
356
|
-
: undefined,
|
|
357
|
-
totalSections,
|
|
358
|
-
};
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
public getCurrentItem(): CurrentItemView | null {
|
|
362
|
-
const q = this.items[this.currentItemIndex];
|
|
363
|
-
if (!q) return null;
|
|
364
|
-
return {
|
|
365
|
-
identifier: q.item.identifier,
|
|
366
|
-
title: q.section.title,
|
|
367
|
-
required: q.item.required,
|
|
368
|
-
itemXml: q.item.itemXml,
|
|
369
|
-
deliveryContext: q.item.deliveryContext,
|
|
370
|
-
};
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
public getCurrentRubricBlocks(): AssessmentRubricBlock[] {
|
|
374
|
-
const q = this.items[this.currentItemIndex];
|
|
375
|
-
if (!q) return [];
|
|
376
|
-
return q.section.rubricBlocks || q.testPart.rubricBlocks || [];
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
public getVisibleFeedback(): Array<{ identifier: string; content: string; access: string }> {
|
|
380
|
-
return this.visibleFeedback;
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
public updateResponse(responseId: string, value: unknown): void {
|
|
384
|
-
this.sessionCoordinator.updateActiveResponse(responseId, value);
|
|
385
|
-
this.notifyResponseChange();
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
/**
|
|
389
|
-
* Update a response for a specific item.
|
|
390
|
-
* Useful when response events are observed outside the current-item renderer and we
|
|
391
|
-
* want to be explicit about which item receives the response (some assessments reuse
|
|
392
|
-
* the same responseIdentifier like "RESPONSE" across items).
|
|
393
|
-
*/
|
|
394
|
-
public updateResponseForItem(itemIdentifier: string, responseId: string, value: unknown): void {
|
|
395
|
-
const next = this.sessionCoordinator.updateResponseForItem(itemIdentifier, responseId, value);
|
|
396
|
-
|
|
397
|
-
// If this is the active item, keep the live response state + UI hints in sync.
|
|
398
|
-
const active = this.items[this.currentItemIndex];
|
|
399
|
-
if (active?.identifier === itemIdentifier) {
|
|
400
|
-
this.currentItemPlayer?.setResponses(next as any);
|
|
401
|
-
this.notifyResponseChange();
|
|
402
|
-
}
|
|
403
|
-
}
|
|
404
|
-
|
|
405
|
-
/**
|
|
406
|
-
* Get current unsaved responses for the active item.
|
|
407
|
-
* (Useful for host integrations / web components.)
|
|
408
|
-
*/
|
|
409
|
-
public getResponses(): Record<string, unknown> {
|
|
410
|
-
return this.sessionCoordinator.getActiveResponses();
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
public saveCurrentItemSession(): SerializedItemSessionState | null {
|
|
414
|
-
const q = this.items[this.currentItemIndex];
|
|
415
|
-
if (!q || !this.currentItemPlayer) return null;
|
|
416
|
-
const responses = this.sessionCoordinator.getActiveResponses();
|
|
417
|
-
this.currentItemPlayer.setResponses(responses as any);
|
|
418
|
-
const result = (this.currentItemPlayer as ItemSessionCapablePlayer).suspendAttempt();
|
|
419
|
-
this.sessionCoordinator.saveItemSession(q.identifier, responses, result.sessionState, result.duration);
|
|
420
|
-
return result.sessionState;
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
private endItemSessionForSubmit(
|
|
424
|
-
q: FlatItem,
|
|
425
|
-
responses: Record<string, unknown>,
|
|
426
|
-
itemSession?: SerializedItemSessionState
|
|
427
|
-
): SerializedItemSessionState {
|
|
428
|
-
const player = new Player({
|
|
429
|
-
itemXml: q.item.itemXml,
|
|
430
|
-
role: q.item.role,
|
|
431
|
-
i18nProvider: this.i18nProvider,
|
|
432
|
-
pnp: this.config.pnp,
|
|
433
|
-
security: this.config.security,
|
|
434
|
-
deliveryContext: q.item.deliveryContext,
|
|
435
|
-
}) as ItemSessionCapablePlayer;
|
|
436
|
-
if (itemSession) {
|
|
437
|
-
player.restoreItemSession(itemSession);
|
|
438
|
-
}
|
|
439
|
-
player.setResponses(responses as any);
|
|
440
|
-
return player.endAttempt().sessionState;
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
/**
|
|
444
|
-
* Get the current session state snapshot (client-side view).
|
|
445
|
-
* Note: backend remains authoritative; this is intended for simple save/resume.
|
|
446
|
-
*/
|
|
447
|
-
public getState(options: { includeItemSessions?: boolean } = {}): AssessmentSessionState {
|
|
448
|
-
return this.sessionCoordinator.snapshot(options);
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
/**
|
|
452
|
-
* Restore a previously saved session state.
|
|
453
|
-
*/
|
|
454
|
-
public async restoreState(state: AssessmentSessionState): Promise<void> {
|
|
455
|
-
this.state = state;
|
|
456
|
-
|
|
457
|
-
// Restore visited state for UI navigation hints.
|
|
458
|
-
const visitedIdx = (state.visitedItems || [])
|
|
459
|
-
.map((id) => this.items.findIndex((q) => q.identifier === id))
|
|
460
|
-
.filter((i) => i >= 0);
|
|
461
|
-
this.navigationManager.restoreState(visitedIdx);
|
|
462
|
-
|
|
463
|
-
const currentId = state.currentItemIdentifier;
|
|
464
|
-
const idx = currentId ? this.items.findIndex((q) => q.identifier === currentId) : -1;
|
|
465
|
-
if (idx >= 0) {
|
|
466
|
-
await this.navigateTo(idx, { restoring: true });
|
|
467
|
-
} else if (this.items.length > 0) {
|
|
468
|
-
await this.navigateTo(0, { restoring: true });
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
public async navigateTo(index: number, options: { restoring?: boolean } = {}): Promise<void> {
|
|
473
|
-
if (index < 0 || index >= this.items.length) throw new Error(`Invalid item index: ${index}`);
|
|
474
|
-
|
|
475
|
-
// Enforce navigation rules (UI hints, still backend-authoritative for real deployments).
|
|
476
|
-
if (!options.restoring && this.currentItemIndex >= 0 && !this.navigationManager.canNavigateTo(index, this.currentItemIndex)) {
|
|
477
|
-
if (this.navigationManager.getMode() === 'linear' && index > this.currentItemIndex + 1) {
|
|
478
|
-
throw new Error('In linear navigation mode, you can only move to the next question.');
|
|
479
|
-
}
|
|
480
|
-
throw new Error('Navigation is not allowed.');
|
|
481
|
-
}
|
|
482
|
-
const target = this.items[index];
|
|
483
|
-
if (!options.restoring && target && !this.sessionCoordinator.canReview(target.identifier)) {
|
|
484
|
-
throw new Error('You cannot go back to previous questions in this assessment.');
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
const currentItem = this.items[this.currentItemIndex];
|
|
488
|
-
const currentSessionStatus = currentItem ? this.state.itemSessions?.[currentItem.identifier]?.lifecycleStatus : undefined;
|
|
489
|
-
if (
|
|
490
|
-
!options.restoring &&
|
|
491
|
-
this.currentItemIndex >= 0 &&
|
|
492
|
-
this.currentItemIndex !== index &&
|
|
493
|
-
currentSessionStatus !== 'closed'
|
|
494
|
-
) {
|
|
495
|
-
this.saveCurrentItemSession();
|
|
496
|
-
}
|
|
497
|
-
if (currentItem && this.currentItemIndex !== index) {
|
|
498
|
-
this.timeManager?.endItem(currentItem.identifier);
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
this.currentItemIndex = index;
|
|
502
|
-
const q = this.items[index]!;
|
|
503
|
-
this.state.currentItemIdentifier = q.identifier;
|
|
504
|
-
this.timeManager?.startSection(q.section.identifier);
|
|
505
|
-
this.timeManager?.startItem(q.identifier);
|
|
506
|
-
|
|
507
|
-
// Apply three-level itemSessionControl fallback for this item's section (S1).
|
|
508
|
-
const effectiveControl = this.getEffectiveItemSessionControl(q);
|
|
509
|
-
if (effectiveControl) {
|
|
510
|
-
this.sessionCoordinator.updateItemSessionControl(effectiveControl);
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
// Restore responses
|
|
514
|
-
const responses = this.sessionCoordinator.activateItem(q.identifier);
|
|
515
|
-
|
|
516
|
-
// Create item player
|
|
517
|
-
this.currentItemPlayer?.destroy();
|
|
518
|
-
this.currentItemPlayer = new Player({
|
|
519
|
-
itemXml: q.item.itemXml,
|
|
520
|
-
role: q.item.role,
|
|
521
|
-
i18nProvider: this.i18nProvider,
|
|
522
|
-
pnp: this.config.pnp,
|
|
523
|
-
security: this.config.security,
|
|
524
|
-
deliveryContext: q.item.deliveryContext,
|
|
525
|
-
});
|
|
526
|
-
const restoredItemSession = this.state.itemSessions?.[q.identifier];
|
|
527
|
-
if (restoredItemSession) {
|
|
528
|
-
(this.currentItemPlayer as ItemSessionCapablePlayer).restoreItemSession(restoredItemSession);
|
|
529
|
-
} else {
|
|
530
|
-
this.currentItemPlayer.setResponses(responses as any);
|
|
531
|
-
}
|
|
532
|
-
|
|
533
|
-
this.notifyItemChange();
|
|
534
|
-
this.notifySectionChange();
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
public async navigateToSection(sectionId: string): Promise<void> {
|
|
538
|
-
const idx = this.items.findIndex((q) => q.section.identifier === sectionId);
|
|
539
|
-
if (idx >= 0) await this.navigateTo(idx);
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
public async next(): Promise<void> {
|
|
543
|
-
const state = this.getNavigationState();
|
|
544
|
-
if (!state.canNext) return;
|
|
545
|
-
|
|
546
|
-
const q = this.items[this.currentItemIndex];
|
|
547
|
-
if (q) {
|
|
548
|
-
const responses = this.sessionCoordinator.getActiveResponses();
|
|
549
|
-
this.sessionCoordinator.markAnswered(q.identifier, responses);
|
|
550
|
-
|
|
551
|
-
const navAway = this.sessionCoordinator.canNavigateAway(q.identifier, responses);
|
|
552
|
-
if (!navAway.allowed) {
|
|
553
|
-
throw new Error(navAway.reason || 'You must answer this question before continuing.');
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
// In individual submission mode, submit current item before moving forward.
|
|
557
|
-
if (this.assessment.submissionMode === 'individual') {
|
|
558
|
-
const before = this.currentItemIndex;
|
|
559
|
-
await this.submitCurrentItem();
|
|
560
|
-
// submitCurrentItem may branch/navigate; if it did, we're done.
|
|
561
|
-
if (this.currentItemIndex !== before) return;
|
|
562
|
-
}
|
|
563
|
-
}
|
|
564
|
-
|
|
565
|
-
await this.navigateTo(this.currentItemIndex + 1);
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
public async previous(): Promise<void> {
|
|
569
|
-
const state = this.getNavigationState();
|
|
570
|
-
if (!state.canPrevious) return;
|
|
571
|
-
await this.navigateTo(this.currentItemIndex - 1);
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
/** Submit current item (server-side scoring) */
|
|
575
|
-
public async submitCurrentItem(): Promise<ItemResult> {
|
|
576
|
-
const q = this.items[this.currentItemIndex];
|
|
577
|
-
if (!q) throw new Error('No current item');
|
|
578
|
-
|
|
579
|
-
const submittedAt = Date.now();
|
|
580
|
-
let itemSession: SerializedItemSessionState | undefined;
|
|
581
|
-
const responses = this.sessionCoordinator.getActiveResponses();
|
|
582
|
-
if (this.currentItemPlayer) {
|
|
583
|
-
this.currentItemPlayer.setResponses(responses as any);
|
|
584
|
-
const attempt = (this.currentItemPlayer as ItemSessionCapablePlayer).endAttempt({
|
|
585
|
-
validateResponses: this.sessionCoordinator.mustValidateResponses(),
|
|
586
|
-
});
|
|
587
|
-
if (attempt.validation && !attempt.validation.valid) {
|
|
588
|
-
throw new Error(attempt.validation.issues[0]?.message || 'Submit failed: invalid response');
|
|
589
|
-
}
|
|
590
|
-
itemSession = attempt.sessionState;
|
|
591
|
-
this.sessionCoordinator.setItemSession(q.identifier, itemSession);
|
|
592
|
-
}
|
|
593
|
-
const res = await this.backend.submitResponses({
|
|
594
|
-
sessionId: this.sessionId,
|
|
595
|
-
itemIdentifier: q.identifier,
|
|
596
|
-
responses: responses as any,
|
|
597
|
-
submittedAt,
|
|
598
|
-
timeSpent: itemSession?.duration,
|
|
599
|
-
timing: this.getSubmitTimingEvidence(q, itemSession?.duration ?? 0),
|
|
600
|
-
itemSession: this.config.sendItemSessionToBackend ? itemSession : undefined,
|
|
601
|
-
});
|
|
602
|
-
|
|
603
|
-
if (!res.success || !res.result) {
|
|
604
|
-
throw new Error(res.error || 'Submit failed');
|
|
605
|
-
}
|
|
606
|
-
|
|
607
|
-
if (res.updatedState) {
|
|
608
|
-
this.sessionCoordinator.replaceStatePreservingItemSessions(res.updatedState);
|
|
609
|
-
} else {
|
|
610
|
-
// Minimal state update when backend doesn't return full state.
|
|
611
|
-
this.state.itemScores = this.state.itemScores || {};
|
|
612
|
-
this.state.itemScores[q.identifier] = res.result;
|
|
613
|
-
this.sessionCoordinator.markVisited(q.identifier);
|
|
614
|
-
this.state.itemResponses[q.identifier] = responses as any;
|
|
615
|
-
if (itemSession) {
|
|
616
|
-
this.sessionCoordinator.setItemSession(q.identifier, itemSession);
|
|
617
|
-
}
|
|
618
|
-
}
|
|
619
|
-
|
|
620
|
-
const itemResult: ItemResult = {
|
|
621
|
-
itemIdentifier: q.identifier,
|
|
622
|
-
score: res.result.score,
|
|
623
|
-
maxScore: res.result.maxScore,
|
|
624
|
-
responses: { ...responses },
|
|
625
|
-
};
|
|
626
|
-
this.sessionCoordinator.setItemResult(itemResult);
|
|
627
|
-
|
|
628
|
-
// Track item session rules (review/attempts) and visited items for navigation hints.
|
|
629
|
-
try {
|
|
630
|
-
this.sessionCoordinator.recordAttempt(q.identifier);
|
|
631
|
-
} catch {
|
|
632
|
-
// ignore
|
|
633
|
-
}
|
|
634
|
-
this.sessionCoordinator.markAnswered(q.identifier, responses);
|
|
635
|
-
this.navigationManager.markVisited(this.currentItemIndex);
|
|
636
|
-
|
|
637
|
-
// Apply backend branching decision if provided.
|
|
638
|
-
// Special QTI targets EXIT_TEST / EXIT_TESTPART / EXIT_SECTION trigger finalization.
|
|
639
|
-
if (res.nextItemIdentifier) {
|
|
640
|
-
const special = res.nextItemIdentifier;
|
|
641
|
-
if (special === 'EXIT_TEST') {
|
|
642
|
-
// Finalize immediately — no further navigation
|
|
643
|
-
for (const l of this.completeListeners) l();
|
|
644
|
-
this.config.onComplete?.();
|
|
645
|
-
} else if (special === 'EXIT_TESTPART' || special === 'EXIT_SECTION') {
|
|
646
|
-
// Advance past all remaining items in the current testPart or section
|
|
647
|
-
const currentItem = this.items[this.currentItemIndex];
|
|
648
|
-
const isSection = special === 'EXIT_SECTION';
|
|
649
|
-
const nextIdx = this.items.findIndex((item, i) =>
|
|
650
|
-
i > this.currentItemIndex &&
|
|
651
|
-
(isSection
|
|
652
|
-
? item.section.identifier !== currentItem?.section.identifier
|
|
653
|
-
: item.testPart.identifier !== currentItem?.testPart.identifier)
|
|
654
|
-
);
|
|
655
|
-
if (nextIdx >= 0) {
|
|
656
|
-
await this.navigateTo(nextIdx);
|
|
657
|
-
} else {
|
|
658
|
-
// No further items — signal completion
|
|
659
|
-
for (const l of this.completeListeners) l();
|
|
660
|
-
this.config.onComplete?.();
|
|
661
|
-
}
|
|
662
|
-
} else {
|
|
663
|
-
const nextIdx = this.items.findIndex((x) => x.identifier === special);
|
|
664
|
-
if (nextIdx >= 0) {
|
|
665
|
-
await this.navigateTo(nextIdx);
|
|
666
|
-
}
|
|
667
|
-
}
|
|
668
|
-
}
|
|
669
|
-
|
|
670
|
-
return itemResult;
|
|
671
|
-
}
|
|
672
|
-
|
|
673
|
-
private canPrevious(): boolean {
|
|
674
|
-
if (this.currentItemIndex <= 0) return false;
|
|
675
|
-
const prev = this.items[this.currentItemIndex - 1];
|
|
676
|
-
if (!prev) return false;
|
|
677
|
-
if (!this.navigationManager.canNavigateTo(this.currentItemIndex - 1, this.currentItemIndex)) return false;
|
|
678
|
-
return this.sessionCoordinator.canReview(prev.identifier);
|
|
679
|
-
}
|
|
680
|
-
|
|
681
|
-
private canNext(): boolean {
|
|
682
|
-
if (this.currentItemIndex < 0) return false;
|
|
683
|
-
const nextIdx = this.currentItemIndex + 1;
|
|
684
|
-
if (nextIdx >= this.items.length) return false;
|
|
685
|
-
if (!this.navigationManager.canNavigateTo(nextIdx, this.currentItemIndex)) return false;
|
|
686
|
-
// Note: itemSessionControl constraints on leaving current item are enforced in next().
|
|
687
|
-
return true;
|
|
688
|
-
}
|
|
689
|
-
|
|
690
|
-
/** Submit entire assessment (finalize on backend) */
|
|
691
|
-
public async submit(): Promise<AssessmentResults> {
|
|
692
|
-
// For simultaneous submission, ensure all items are submitted before finalize so
|
|
693
|
-
// the backend can compute a complete test score.
|
|
694
|
-
if (this.assessment.submissionMode === 'simultaneous') {
|
|
695
|
-
this.saveCurrentItemSession();
|
|
696
|
-
// Preserve client-collected responses across backend state updates.
|
|
697
|
-
// Some backend adapters return an updatedState snapshot that may only include
|
|
698
|
-
// responses for items that have been submitted so far, which would otherwise
|
|
699
|
-
// wipe out responses for later items during this loop.
|
|
700
|
-
const allItemResponses = { ...(this.state.itemResponses || {}) } as any;
|
|
701
|
-
const allItemSessions = { ...(this.state.itemSessions || {}) };
|
|
702
|
-
|
|
703
|
-
for (const q of this.items) {
|
|
704
|
-
if (this.sessionCoordinator.hasItemResult(q.identifier)) continue;
|
|
705
|
-
const submittedAt = Date.now();
|
|
706
|
-
const responsesForItem = (allItemResponses?.[q.identifier] || {}) as any;
|
|
707
|
-
const itemSession = this.endItemSessionForSubmit(q, responsesForItem, allItemSessions[q.identifier]);
|
|
708
|
-
allItemSessions[q.identifier] = itemSession;
|
|
709
|
-
const res = await this.backend.submitResponses({
|
|
710
|
-
sessionId: this.sessionId,
|
|
711
|
-
itemIdentifier: q.identifier,
|
|
712
|
-
responses: responsesForItem,
|
|
713
|
-
submittedAt,
|
|
714
|
-
timeSpent: itemSession?.duration,
|
|
715
|
-
timing: this.getSubmitTimingEvidence(q, itemSession?.duration ?? 0),
|
|
716
|
-
itemSession: this.config.sendItemSessionToBackend ? itemSession : undefined,
|
|
717
|
-
});
|
|
718
|
-
if (!res.success || !res.result) {
|
|
719
|
-
throw new Error(res.error || `Submit failed for item ${q.identifier}`);
|
|
720
|
-
}
|
|
721
|
-
if (res.updatedState) {
|
|
722
|
-
this.sessionCoordinator.replaceStatePreservingResponsesAndSessions(
|
|
723
|
-
res.updatedState,
|
|
724
|
-
allItemResponses,
|
|
725
|
-
allItemSessions
|
|
726
|
-
);
|
|
727
|
-
} else {
|
|
728
|
-
this.state.itemScores = this.state.itemScores || {};
|
|
729
|
-
this.state.itemScores[q.identifier] = res.result;
|
|
730
|
-
this.sessionCoordinator.markVisited(q.identifier);
|
|
731
|
-
this.state.itemResponses[q.identifier] = responsesForItem;
|
|
732
|
-
if (itemSession) {
|
|
733
|
-
this.sessionCoordinator.setItemSession(q.identifier, itemSession);
|
|
734
|
-
}
|
|
735
|
-
}
|
|
736
|
-
this.sessionCoordinator.setItemResult({
|
|
737
|
-
itemIdentifier: q.identifier,
|
|
738
|
-
score: res.result.score,
|
|
739
|
-
maxScore: res.result.maxScore,
|
|
740
|
-
responses: this.state.itemResponses[q.identifier] || {},
|
|
741
|
-
});
|
|
742
|
-
}
|
|
743
|
-
} else {
|
|
744
|
-
// Ensure current item is submitted (individual submission mode)
|
|
745
|
-
const q = this.items[this.currentItemIndex];
|
|
746
|
-
if (q && !this.sessionCoordinator.hasItemResult(q.identifier)) {
|
|
747
|
-
await this.submitCurrentItem();
|
|
748
|
-
}
|
|
749
|
-
}
|
|
750
|
-
|
|
751
|
-
const finalized: FinalizeAssessmentResponse = await this.backend.finalizeAssessment({
|
|
752
|
-
sessionId: this.sessionId,
|
|
753
|
-
});
|
|
754
|
-
if (!finalized.success) throw new Error('Finalize failed');
|
|
755
|
-
|
|
756
|
-
// Map backend itemScores to ItemResult[]
|
|
757
|
-
const itemScores = finalized.itemScores || {};
|
|
758
|
-
const itemResults: ItemResult[] = Object.values(itemScores).map((r: AssessmentScoringResult) => ({
|
|
759
|
-
itemIdentifier: r.itemIdentifier,
|
|
760
|
-
score: r.score,
|
|
761
|
-
maxScore: r.maxScore,
|
|
762
|
-
responses: this.state.itemResponses[r.itemIdentifier] || {},
|
|
763
|
-
}));
|
|
764
|
-
|
|
765
|
-
// Evaluate testFeedback visibility using QTI string equality:
|
|
766
|
-
// show when outcomeIdentifier's value equals feedback.identifier (for showHide='show'),
|
|
767
|
-
// or does NOT equal it (for showHide='hide').
|
|
768
|
-
const structuredFeedback = this.assessment.testFeedback ?? [];
|
|
769
|
-
const outcomes = finalized.outcomes ?? {};
|
|
770
|
-
if (structuredFeedback.length > 0) {
|
|
771
|
-
this.visibleFeedback = structuredFeedback
|
|
772
|
-
.filter((fb) => fb.access === 'atEnd')
|
|
773
|
-
.filter((fb) => {
|
|
774
|
-
const outcomeValue = String(outcomes[fb.outcomeIdentifier] ?? '');
|
|
775
|
-
const matches = outcomeValue === fb.identifier;
|
|
776
|
-
return fb.showHide === 'show' ? matches : !matches;
|
|
777
|
-
})
|
|
778
|
-
.map((fb) => ({ identifier: fb.identifier, content: fb.content, access: fb.access }));
|
|
779
|
-
} else {
|
|
780
|
-
// Legacy: backend returns a plain feedback string
|
|
781
|
-
this.visibleFeedback = finalized.feedback
|
|
782
|
-
? [{ identifier: 'testFeedback', content: finalized.feedback, access: 'atEnd' }]
|
|
783
|
-
: [];
|
|
784
|
-
}
|
|
785
|
-
|
|
786
|
-
this.notifyComplete();
|
|
787
|
-
if (this.config.onComplete) this.config.onComplete();
|
|
788
|
-
|
|
789
|
-
return {
|
|
790
|
-
totalScore: finalized.totalScore,
|
|
791
|
-
maxScore: finalized.maxScore,
|
|
792
|
-
itemResults,
|
|
793
|
-
completedAt: new Date(finalized.finalizedAt),
|
|
794
|
-
};
|
|
795
|
-
}
|
|
796
|
-
|
|
797
|
-
// ---------------------------------------------------------------------------
|
|
798
|
-
// Session info + time helpers
|
|
799
|
-
// ---------------------------------------------------------------------------
|
|
800
|
-
|
|
801
|
-
public getItemSessionInfo(_itemIdentifier?: string): {
|
|
802
|
-
canSubmit: boolean;
|
|
803
|
-
remainingAttempts: number | null;
|
|
804
|
-
attemptCount: number;
|
|
805
|
-
showFeedback: boolean;
|
|
806
|
-
showSolution: boolean;
|
|
807
|
-
canReview: boolean;
|
|
808
|
-
canSkip: boolean;
|
|
809
|
-
} | null {
|
|
810
|
-
const q = this.items[this.currentItemIndex];
|
|
811
|
-
if (!q) return null;
|
|
812
|
-
const id = q.identifier;
|
|
813
|
-
|
|
814
|
-
return this.sessionCoordinator.getItemSessionInfo(id);
|
|
815
|
-
}
|
|
816
|
-
|
|
817
|
-
public getRemainingTime(): number {
|
|
818
|
-
// TimeManager exposes seconds getters.
|
|
819
|
-
return this.timeManager?.getRemainingSeconds() ?? 0;
|
|
820
|
-
}
|
|
821
|
-
|
|
822
|
-
public getElapsedTime(): number {
|
|
823
|
-
return this.timeManager?.getElapsedSeconds() ?? 0;
|
|
824
|
-
}
|
|
825
|
-
|
|
826
|
-
public isTimeExpired(): boolean {
|
|
827
|
-
return this.timeManager?.isExpired() ?? false;
|
|
828
|
-
}
|
|
829
|
-
|
|
830
|
-
// ---------------------------------------------------------------------------
|
|
831
|
-
// Event subscriptions
|
|
832
|
-
// ---------------------------------------------------------------------------
|
|
833
|
-
|
|
834
|
-
public onItemChange(listener: (index: number, total: number) => void): () => void {
|
|
835
|
-
this.itemChangeListeners.add(listener);
|
|
836
|
-
return () => this.itemChangeListeners.delete(listener);
|
|
837
|
-
}
|
|
838
|
-
|
|
839
|
-
public onSectionChange(listener: (index: number, total: number) => void): () => void {
|
|
840
|
-
this.sectionChangeListeners.add(listener);
|
|
841
|
-
return () => this.sectionChangeListeners.delete(listener);
|
|
842
|
-
}
|
|
843
|
-
|
|
844
|
-
public onResponseChange(listener: (responses: Record<string, unknown>) => void): () => void {
|
|
845
|
-
this.responseChangeListeners.add(listener);
|
|
846
|
-
return () => this.responseChangeListeners.delete(listener);
|
|
847
|
-
}
|
|
848
|
-
|
|
849
|
-
public onComplete(listener: () => void): () => void {
|
|
850
|
-
this.completeListeners.add(listener);
|
|
851
|
-
return () => this.completeListeners.delete(listener);
|
|
852
|
-
}
|
|
853
|
-
|
|
854
|
-
public onTimeWarning(listener: (remainingSeconds: number) => void): () => void {
|
|
855
|
-
this.timeWarningListeners.add(listener);
|
|
856
|
-
return () => this.timeWarningListeners.delete(listener);
|
|
857
|
-
}
|
|
858
|
-
|
|
859
|
-
public onTimeExpired(listener: () => void): () => void {
|
|
860
|
-
this.timeExpiredListeners.add(listener);
|
|
861
|
-
return () => this.timeExpiredListeners.delete(listener);
|
|
862
|
-
}
|
|
863
|
-
|
|
864
|
-
public onTimeTick(listener: (remainingSeconds: number, elapsedSeconds: number) => void): () => void {
|
|
865
|
-
this.timeTickListeners.add(listener);
|
|
866
|
-
return () => this.timeTickListeners.delete(listener);
|
|
867
|
-
}
|
|
868
|
-
|
|
869
|
-
public destroy(): void {
|
|
870
|
-
this.timeManager?.destroy();
|
|
871
|
-
this.currentItemPlayer?.destroy();
|
|
872
|
-
this.itemChangeListeners.clear();
|
|
873
|
-
this.sectionChangeListeners.clear();
|
|
874
|
-
this.responseChangeListeners.clear();
|
|
875
|
-
this.completeListeners.clear();
|
|
876
|
-
this.timeWarningListeners.clear();
|
|
877
|
-
this.timeExpiredListeners.clear();
|
|
878
|
-
this.timeTickListeners.clear();
|
|
879
|
-
this.currentItemPlayer = null;
|
|
880
|
-
}
|
|
881
|
-
|
|
882
|
-
// ---------------------------------------------------------------------------
|
|
883
|
-
// Internal notify helpers
|
|
884
|
-
// ---------------------------------------------------------------------------
|
|
885
|
-
|
|
886
|
-
private notifyItemChange(): void {
|
|
887
|
-
for (const l of this.itemChangeListeners) l(this.currentItemIndex, this.items.length);
|
|
888
|
-
this.config.onItemChange?.(this.currentItemIndex, this.items.length);
|
|
889
|
-
}
|
|
890
|
-
|
|
891
|
-
private notifySectionChange(): void {
|
|
892
|
-
const q = this.items[this.currentItemIndex];
|
|
893
|
-
const sectionIndex = q?.sectionIndex ?? 0;
|
|
894
|
-
const totalSections = this.getAllSections().length;
|
|
895
|
-
for (const l of this.sectionChangeListeners) l(sectionIndex, totalSections);
|
|
896
|
-
this.config.onSectionChange?.(sectionIndex, totalSections);
|
|
897
|
-
}
|
|
898
|
-
|
|
899
|
-
private notifyResponseChange(): void {
|
|
900
|
-
const responses = this.sessionCoordinator.getActiveResponses();
|
|
901
|
-
for (const l of this.responseChangeListeners) l(responses);
|
|
902
|
-
this.config.onResponseChange?.(responses);
|
|
903
|
-
}
|
|
904
|
-
|
|
905
|
-
private notifyComplete(): void {
|
|
906
|
-
for (const l of this.completeListeners) l();
|
|
907
|
-
}
|
|
908
|
-
|
|
909
|
-
private notifyTimeWarning(remainingSeconds: number): void {
|
|
910
|
-
for (const l of this.timeWarningListeners) l(remainingSeconds);
|
|
911
|
-
}
|
|
912
|
-
|
|
913
|
-
private notifyTimeExpired(): void {
|
|
914
|
-
for (const l of this.timeExpiredListeners) l();
|
|
915
|
-
}
|
|
916
|
-
|
|
917
|
-
private notifyTimeTick(remainingSeconds: number, elapsedSeconds: number): void {
|
|
918
|
-
for (const l of this.timeTickListeners) l(remainingSeconds, elapsedSeconds);
|
|
919
|
-
}
|
|
920
|
-
|
|
921
|
-
/**
|
|
922
|
-
* Get the i18n provider instance
|
|
923
|
-
* @returns I18nProvider instance
|
|
924
|
-
*/
|
|
925
|
-
public getI18nProvider(): any {
|
|
926
|
-
return this.i18nProvider;
|
|
927
|
-
}
|
|
928
|
-
|
|
929
|
-
/**
|
|
930
|
-
* Create a simple fallback i18n provider when none is provided
|
|
931
|
-
*/
|
|
932
|
-
private createDefaultI18nProvider(): any {
|
|
933
|
-
return {
|
|
934
|
-
getLocale: () => 'en-US',
|
|
935
|
-
setLocale: () => {},
|
|
936
|
-
t: (key: string) => key, // Fallback to key
|
|
937
|
-
plural: (key: string) => key,
|
|
938
|
-
formatNumber: (value: number) => value.toString(),
|
|
939
|
-
formatDate: (date: Date) => date.toISOString(),
|
|
940
|
-
};
|
|
941
|
-
}
|
|
942
|
-
}
|
|
943
|
-
|
|
944
|
-
/** Fisher-Yates shuffle using a provided RNG (returns a new array). */
|
|
945
|
-
function shuffleArray<T>(arr: T[], rng: () => number): T[] {
|
|
946
|
-
const out = [...arr];
|
|
947
|
-
for (let i = out.length - 1; i > 0; i--) {
|
|
948
|
-
const j = Math.floor(rng() * (i + 1));
|
|
949
|
-
[out[i], out[j]] = [out[j], out[i]];
|
|
950
|
-
}
|
|
951
|
-
return out;
|
|
952
|
-
}
|