@pie-qti/assessment-player 0.1.1
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/.turbo/turbo-build.log +1 -0
- package/BACKEND-INTEGRATION.md +620 -0
- package/CHANGELOG.md +11 -0
- package/EXTENSIBILITY.md +551 -0
- package/README.md +1137 -0
- package/dist/core/AssessmentPlayer.d.ts +151 -0
- package/dist/core/AssessmentPlayer.d.ts.map +1 -0
- package/dist/core/AssessmentPlayer.js +595 -0
- package/dist/core/ItemSessionController.d.ts +119 -0
- package/dist/core/ItemSessionController.d.ts.map +1 -0
- package/dist/core/ItemSessionController.js +227 -0
- package/dist/core/NavigationManager.d.ts +59 -0
- package/dist/core/NavigationManager.d.ts.map +1 -0
- package/dist/core/NavigationManager.js +104 -0
- package/dist/core/StatePersistenceManager.d.ts +126 -0
- package/dist/core/StatePersistenceManager.d.ts.map +1 -0
- package/dist/core/StatePersistenceManager.js +267 -0
- package/dist/core/TimeManager.d.ts +123 -0
- package/dist/core/TimeManager.d.ts.map +1 -0
- package/dist/core/TimeManager.js +217 -0
- package/dist/core/index.d.ts +10 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/index.js +5 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/integration/ReferenceBackendAdapter.d.ts +128 -0
- package/dist/integration/ReferenceBackendAdapter.d.ts.map +1 -0
- package/dist/integration/ReferenceBackendAdapter.js +735 -0
- package/dist/integration/api-contract.d.ts +319 -0
- package/dist/integration/api-contract.d.ts.map +1 -0
- package/dist/integration/api-contract.js +19 -0
- package/dist/integration/index.d.ts +47 -0
- package/dist/integration/index.d.ts.map +1 -0
- package/dist/integration/index.js +46 -0
- package/dist/types/index.d.ts +51 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +7 -0
- package/dist/utils/PerformanceMonitor.d.ts +115 -0
- package/dist/utils/PerformanceMonitor.d.ts.map +1 -0
- package/dist/utils/PerformanceMonitor.js +171 -0
- package/package.json +82 -0
- package/src/ambient.d.ts +55 -0
- package/src/components/AccessibilityAnnouncer.svelte +102 -0
- package/src/components/AssessmentHeader.svelte +76 -0
- package/src/components/AssessmentShell.svelte +485 -0
- package/src/components/AssessmentTimer.svelte +228 -0
- package/src/components/ItemRenderer.svelte +147 -0
- package/src/components/ItemSessionInfo.svelte +241 -0
- package/src/components/NavigationBar.svelte +142 -0
- package/src/components/RubricDisplay.svelte +185 -0
- package/src/components/SectionMenu.svelte +206 -0
- package/src/components/SplitPaneResizer.svelte +288 -0
- package/src/components/TestFeedback.svelte +147 -0
- package/src/components/index.ts +19 -0
- package/src/core/AssessmentPlayer.ts +720 -0
- package/src/core/ItemSessionController.ts +287 -0
- package/src/core/NavigationManager.ts +126 -0
- package/src/core/StatePersistenceManager.ts +344 -0
- package/src/core/TimeManager.ts +289 -0
- package/src/core/index.ts +15 -0
- package/src/index.ts +13 -0
- package/src/integration/ReferenceBackendAdapter.ts +861 -0
- package/src/integration/api-contract.ts +377 -0
- package/src/integration/index.ts +70 -0
- package/src/svelte-shims.d.ts +7 -0
- package/src/types/index.ts +56 -0
- package/src/utils/PerformanceMonitor.ts +222 -0
- package/svelte.config.js +8 -0
- package/tests/ReferenceBackendAdapter.spec.ts +309 -0
- package/tests/setup.ts +103 -0
- package/tests/smoke.test.ts +7 -0
- package/tsconfig.json +20 -0
- package/tsconfig.svelte.json +10 -0
|
@@ -0,0 +1,735 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reference Backend Adapter - localStorage Implementation
|
|
3
|
+
*
|
|
4
|
+
* This is a REFERENCE IMPLEMENTATION demonstrating the BackendAdapter contract.
|
|
5
|
+
* It uses localStorage for persistence and performs scoring CLIENT-SIDE.
|
|
6
|
+
*
|
|
7
|
+
* ## Security Warning
|
|
8
|
+
*
|
|
9
|
+
* This implementation is INSECURE for production use because:
|
|
10
|
+
* - Scoring is done client-side (can be manipulated)
|
|
11
|
+
* - QTI XML with correct answers is stored in client
|
|
12
|
+
* - No authentication or authorization checks
|
|
13
|
+
*
|
|
14
|
+
* ## Purpose
|
|
15
|
+
*
|
|
16
|
+
* This adapter serves as:
|
|
17
|
+
* 1. Reference implementation showing the API contract
|
|
18
|
+
* 2. Development/demo tool for offline testing
|
|
19
|
+
* 3. Template for building secure backend adapters
|
|
20
|
+
*
|
|
21
|
+
* ## Production Use
|
|
22
|
+
*
|
|
23
|
+
* Replace this with a real backend adapter that:
|
|
24
|
+
* - Stores QTI XML server-side
|
|
25
|
+
* - Performs scoring server-side
|
|
26
|
+
* - Validates all requests with authentication
|
|
27
|
+
* - Filters sensitive data based on role
|
|
28
|
+
*/
|
|
29
|
+
import { buildExpression, DeclarationContext, evalExpr, OperatorRegistry, parseXml, qtiNull, qtiValue, } from '@pie-qti/qti-processing';
|
|
30
|
+
import { Player } from '@pie-qti/item-player';
|
|
31
|
+
/**
|
|
32
|
+
* Storage key prefix for localStorage
|
|
33
|
+
*/
|
|
34
|
+
const STORAGE_PREFIX = 'qti_session_';
|
|
35
|
+
/**
|
|
36
|
+
* Cache key for item bank queries
|
|
37
|
+
*/
|
|
38
|
+
const BANK_CACHE_PREFIX = 'qti_bank_cache_';
|
|
39
|
+
/**
|
|
40
|
+
* Reference Backend Adapter using localStorage
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* const adapter = new ReferenceBackendAdapter();
|
|
45
|
+
* const response = await adapter.initSession({
|
|
46
|
+
* assessmentId: 'test-001',
|
|
47
|
+
* candidateId: 'student-123'
|
|
48
|
+
* });
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export class ReferenceBackendAdapter {
|
|
52
|
+
constructor() {
|
|
53
|
+
this.registeredAssessments = new Map();
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Register/override a client-provided assessment structure for a given assessmentId.
|
|
57
|
+
*
|
|
58
|
+
* This is useful for demos or web-components that supply assessment XML directly.
|
|
59
|
+
* Security: this remains a reference/demo adapter; production backends should not
|
|
60
|
+
* accept client-authored assessments.
|
|
61
|
+
*/
|
|
62
|
+
registerAssessment(assessmentId, assessment) {
|
|
63
|
+
this.registeredAssessments.set(assessmentId, assessment);
|
|
64
|
+
}
|
|
65
|
+
getAssessment(assessmentId) {
|
|
66
|
+
return this.registeredAssessments.get(assessmentId) ?? this.createDemoAssessment(assessmentId);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Initialize a new assessment session
|
|
70
|
+
*/
|
|
71
|
+
async initSession(request) {
|
|
72
|
+
// Check if resuming existing session
|
|
73
|
+
if (request.resumeSessionId) {
|
|
74
|
+
const stored = this.loadSession(request.resumeSessionId);
|
|
75
|
+
if (stored) {
|
|
76
|
+
return {
|
|
77
|
+
sessionId: stored.sessionId,
|
|
78
|
+
assessment: stored.assessment,
|
|
79
|
+
restoredState: stored.state,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
// Create new session
|
|
84
|
+
const sessionId = this.generateSessionId(request.assessmentId, request.candidateId);
|
|
85
|
+
// In a real backend, you would:
|
|
86
|
+
// 1. Fetch assessment from database
|
|
87
|
+
// 2. Filter QTI XML based on role
|
|
88
|
+
// 3. Remove responseProcessing for candidates
|
|
89
|
+
//
|
|
90
|
+
// For reference implementation, we'll use demo data
|
|
91
|
+
const assessment = this.getAssessment(request.assessmentId);
|
|
92
|
+
const state = {
|
|
93
|
+
currentItemIdentifier: assessment.testParts[0].sections[0].items[0].identifier,
|
|
94
|
+
visitedItems: [],
|
|
95
|
+
itemResponses: {},
|
|
96
|
+
itemScores: {},
|
|
97
|
+
timing: {
|
|
98
|
+
startedAt: Date.now(),
|
|
99
|
+
itemTimes: {},
|
|
100
|
+
totalTime: 0,
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
const session = {
|
|
104
|
+
sessionId,
|
|
105
|
+
assessmentId: request.assessmentId,
|
|
106
|
+
candidateId: request.candidateId,
|
|
107
|
+
assessment,
|
|
108
|
+
state,
|
|
109
|
+
createdAt: Date.now(),
|
|
110
|
+
finalized: false,
|
|
111
|
+
};
|
|
112
|
+
this.saveSession(session);
|
|
113
|
+
return {
|
|
114
|
+
sessionId,
|
|
115
|
+
assessment,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Submit responses and get scoring result
|
|
120
|
+
*/
|
|
121
|
+
async submitResponses(request) {
|
|
122
|
+
const session = this.loadSession(request.sessionId);
|
|
123
|
+
if (!session) {
|
|
124
|
+
return {
|
|
125
|
+
success: false,
|
|
126
|
+
error: 'Session not found',
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
if (session.finalized) {
|
|
130
|
+
return {
|
|
131
|
+
success: false,
|
|
132
|
+
error: 'Session already finalized',
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
// Find the item XML
|
|
136
|
+
const itemXml = this.findItemXml(session.assessment, request.itemIdentifier);
|
|
137
|
+
if (!itemXml) {
|
|
138
|
+
return {
|
|
139
|
+
success: false,
|
|
140
|
+
error: 'Item not found',
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
// **SECURITY WARNING**: Client-side scoring is INSECURE
|
|
144
|
+
// In production, this MUST be done server-side
|
|
145
|
+
const result = this.scoreItem(itemXml, request.responses);
|
|
146
|
+
// Store responses and result
|
|
147
|
+
session.state.itemResponses[request.itemIdentifier] = request.responses;
|
|
148
|
+
if (!session.state.itemScores) {
|
|
149
|
+
session.state.itemScores = {};
|
|
150
|
+
}
|
|
151
|
+
session.state.itemScores[request.itemIdentifier] = result;
|
|
152
|
+
// Update timing
|
|
153
|
+
if (request.timeSpent) {
|
|
154
|
+
session.state.timing.itemTimes[request.itemIdentifier] = request.timeSpent;
|
|
155
|
+
}
|
|
156
|
+
// Add to visited items if not already there
|
|
157
|
+
if (!session.state.visitedItems.includes(request.itemIdentifier)) {
|
|
158
|
+
session.state.visitedItems.push(request.itemIdentifier);
|
|
159
|
+
}
|
|
160
|
+
this.saveSession(session);
|
|
161
|
+
const order = this.getItemOrder(session.assessment);
|
|
162
|
+
const idx = order.indexOf(request.itemIdentifier);
|
|
163
|
+
let nextItemIdentifier = idx >= 0 ? order[idx + 1] : undefined;
|
|
164
|
+
// Demo branching using qti-processing (backend-authoritative). If SCORE > 0, go to item2.
|
|
165
|
+
if (request.itemIdentifier === 'item1') {
|
|
166
|
+
const condXml = `<gt><variable identifier="SCORE" /><baseValue baseType="float">0</baseValue></gt>`;
|
|
167
|
+
if (this.evaluateConditionXml(condXml, { SCORE: result.score })) {
|
|
168
|
+
nextItemIdentifier = 'item2';
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
nextItemIdentifier = undefined;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
return {
|
|
175
|
+
success: true,
|
|
176
|
+
result,
|
|
177
|
+
nextItemIdentifier,
|
|
178
|
+
updatedState: session.state,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Save session state (for auto-save)
|
|
183
|
+
*/
|
|
184
|
+
async saveState(request) {
|
|
185
|
+
const session = this.loadSession(request.sessionId);
|
|
186
|
+
if (!session) {
|
|
187
|
+
return {
|
|
188
|
+
success: false,
|
|
189
|
+
savedAt: Date.now(),
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
// Update state
|
|
193
|
+
session.state = request.state;
|
|
194
|
+
this.saveSession(session);
|
|
195
|
+
return {
|
|
196
|
+
success: true,
|
|
197
|
+
savedAt: Date.now(),
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Finalize assessment and compute final scores
|
|
202
|
+
*/
|
|
203
|
+
async finalizeAssessment(request) {
|
|
204
|
+
const session = this.loadSession(request.sessionId);
|
|
205
|
+
if (!session) {
|
|
206
|
+
return {
|
|
207
|
+
success: false,
|
|
208
|
+
totalScore: 0,
|
|
209
|
+
maxScore: 0,
|
|
210
|
+
itemScores: {},
|
|
211
|
+
finalizedAt: Date.now(),
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
// Mark as finalized
|
|
215
|
+
session.finalized = true;
|
|
216
|
+
this.saveSession(session);
|
|
217
|
+
// Compute total score
|
|
218
|
+
const itemScores = session.state.itemScores || {};
|
|
219
|
+
const totalScore = Object.values(itemScores).reduce((sum, result) => sum + result.score, 0);
|
|
220
|
+
const maxScore = Object.values(itemScores).reduce((sum, result) => sum + result.maxScore, 0);
|
|
221
|
+
return {
|
|
222
|
+
success: true,
|
|
223
|
+
totalScore,
|
|
224
|
+
maxScore,
|
|
225
|
+
itemScores,
|
|
226
|
+
finalizedAt: Date.now(),
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Resume existing session (alias for initSession with resumeSessionId)
|
|
231
|
+
*/
|
|
232
|
+
async resumeSession(sessionId) {
|
|
233
|
+
return this.initSession({
|
|
234
|
+
assessmentId: '',
|
|
235
|
+
candidateId: '',
|
|
236
|
+
resumeSessionId: sessionId,
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Query item bank for random item selection
|
|
241
|
+
*
|
|
242
|
+
* This reference implementation:
|
|
243
|
+
* - Caches bank queries to avoid repeated calls
|
|
244
|
+
* - Implements retry logic with exponential backoff
|
|
245
|
+
* - Returns demo items (in production, fetch from real item bank)
|
|
246
|
+
*
|
|
247
|
+
* @param request - Item bank query request
|
|
248
|
+
* @returns Selected items from bank
|
|
249
|
+
*/
|
|
250
|
+
async queryItemBank(request) {
|
|
251
|
+
const { sessionId, bankId, count, withReplacement, filters } = request;
|
|
252
|
+
// Validate session exists
|
|
253
|
+
const session = this.loadSession(sessionId);
|
|
254
|
+
if (!session) {
|
|
255
|
+
throw new Error(`Session not found: ${sessionId}`);
|
|
256
|
+
}
|
|
257
|
+
// Check cache first
|
|
258
|
+
const cacheKey = this.getBankCacheKey(sessionId, bankId, count, withReplacement, filters);
|
|
259
|
+
const cached = this.loadBankCache(cacheKey);
|
|
260
|
+
if (cached) {
|
|
261
|
+
return {
|
|
262
|
+
items: cached.items,
|
|
263
|
+
selectedAt: cached.timestamp,
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
// Simulate network call with retry logic
|
|
267
|
+
const items = await this.fetchItemsFromBank(bankId, count, withReplacement, filters);
|
|
268
|
+
// Cache the result (5 minute TTL)
|
|
269
|
+
this.saveBankCache(cacheKey, {
|
|
270
|
+
items,
|
|
271
|
+
timestamp: Date.now(),
|
|
272
|
+
ttl: 5 * 60 * 1000, // 5 minutes
|
|
273
|
+
});
|
|
274
|
+
return {
|
|
275
|
+
items,
|
|
276
|
+
selectedAt: Date.now(),
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
// =========================================================================
|
|
280
|
+
// PRIVATE HELPERS
|
|
281
|
+
// =========================================================================
|
|
282
|
+
generateSessionId(assessmentId, candidateId) {
|
|
283
|
+
return `${assessmentId}:${candidateId}:${Date.now()}`;
|
|
284
|
+
}
|
|
285
|
+
loadSession(sessionId) {
|
|
286
|
+
try {
|
|
287
|
+
const key = STORAGE_PREFIX + sessionId;
|
|
288
|
+
const data = localStorage.getItem(key);
|
|
289
|
+
if (!data)
|
|
290
|
+
return null;
|
|
291
|
+
return JSON.parse(data);
|
|
292
|
+
}
|
|
293
|
+
catch {
|
|
294
|
+
return null;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
saveSession(session) {
|
|
298
|
+
try {
|
|
299
|
+
const key = STORAGE_PREFIX + session.sessionId;
|
|
300
|
+
localStorage.setItem(key, JSON.stringify(session));
|
|
301
|
+
}
|
|
302
|
+
catch (error) {
|
|
303
|
+
console.error('Failed to save session to localStorage:', error);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
findItemXml(assessment, itemIdentifier) {
|
|
307
|
+
for (const part of assessment.testParts) {
|
|
308
|
+
for (const section of part.sections) {
|
|
309
|
+
for (const item of section.items) {
|
|
310
|
+
if (item.identifier === itemIdentifier) {
|
|
311
|
+
return item.itemXml;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
return null;
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Score an item using the Player
|
|
320
|
+
*
|
|
321
|
+
* **SECURITY WARNING**: This is done CLIENT-SIDE for demo purposes only.
|
|
322
|
+
* Production implementations MUST perform scoring SERVER-SIDE.
|
|
323
|
+
*/
|
|
324
|
+
scoreItem(itemXml, responses) {
|
|
325
|
+
try {
|
|
326
|
+
// Extract identifier from XML using regex (more reliable than DOM parsing)
|
|
327
|
+
const identifierMatch = itemXml.match(/assessmentItem[^>]+identifier=["']([^"']+)["']/);
|
|
328
|
+
const itemIdentifier = identifierMatch?.[1] || 'unknown';
|
|
329
|
+
// Use a scorer-grade view of the item for accurate scoring.
|
|
330
|
+
// NOTE: This reference adapter is intentionally insecure and runs client-side.
|
|
331
|
+
// In production, scoring must happen server-side with secured item content.
|
|
332
|
+
const player = new Player({
|
|
333
|
+
itemXml,
|
|
334
|
+
role: 'scorer',
|
|
335
|
+
});
|
|
336
|
+
// Set responses using setResponses (plural)
|
|
337
|
+
player.setResponses(responses);
|
|
338
|
+
// Process and score
|
|
339
|
+
const result = player.processResponses();
|
|
340
|
+
return {
|
|
341
|
+
itemIdentifier,
|
|
342
|
+
score: result.score,
|
|
343
|
+
maxScore: result.maxScore,
|
|
344
|
+
completed: result.completed,
|
|
345
|
+
outcomeValues: result.outcomeValues,
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
catch (error) {
|
|
349
|
+
console.error('Scoring error:', error);
|
|
350
|
+
return {
|
|
351
|
+
itemIdentifier: 'unknown',
|
|
352
|
+
score: 0,
|
|
353
|
+
maxScore: 1,
|
|
354
|
+
completed: false,
|
|
355
|
+
outcomeValues: {},
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Create demo assessment for testing
|
|
361
|
+
*
|
|
362
|
+
* In production, this would fetch from database
|
|
363
|
+
*/
|
|
364
|
+
createDemoAssessment(assessmentId) {
|
|
365
|
+
// This is a minimal demo assessment
|
|
366
|
+
// In production, load from database and filter based on role
|
|
367
|
+
return {
|
|
368
|
+
identifier: assessmentId,
|
|
369
|
+
title: 'Demo Assessment',
|
|
370
|
+
navigationMode: 'nonlinear',
|
|
371
|
+
submissionMode: 'simultaneous',
|
|
372
|
+
testParts: [
|
|
373
|
+
{
|
|
374
|
+
identifier: 'part1',
|
|
375
|
+
rubrics: [],
|
|
376
|
+
itemSessionControl: {
|
|
377
|
+
maxAttempts: 1,
|
|
378
|
+
showFeedback: true,
|
|
379
|
+
allowReview: true,
|
|
380
|
+
showSolution: false,
|
|
381
|
+
allowComment: false,
|
|
382
|
+
allowSkipping: true,
|
|
383
|
+
validateResponses: true,
|
|
384
|
+
},
|
|
385
|
+
sections: [
|
|
386
|
+
{
|
|
387
|
+
identifier: 'section1',
|
|
388
|
+
title: 'Section 1',
|
|
389
|
+
visible: true,
|
|
390
|
+
items: [
|
|
391
|
+
{
|
|
392
|
+
identifier: 'item1',
|
|
393
|
+
role: 'candidate',
|
|
394
|
+
// In production, load from database
|
|
395
|
+
// For candidates, responseProcessing should be removed server-side
|
|
396
|
+
itemXml: `<assessmentItem identifier="item1" title="Sample Item" adaptive="false" timeDependent="false">
|
|
397
|
+
<responseDeclaration identifier="RESPONSE" cardinality="single" baseType="identifier">
|
|
398
|
+
<correctResponse>
|
|
399
|
+
<value>A</value>
|
|
400
|
+
</correctResponse>
|
|
401
|
+
</responseDeclaration>
|
|
402
|
+
<outcomeDeclaration identifier="SCORE" cardinality="single" baseType="float">
|
|
403
|
+
<defaultValue>
|
|
404
|
+
<value>0</value>
|
|
405
|
+
</defaultValue>
|
|
406
|
+
</outcomeDeclaration>
|
|
407
|
+
<itemBody>
|
|
408
|
+
<p>What is 2 + 2?</p>
|
|
409
|
+
<choiceInteraction responseIdentifier="RESPONSE" shuffle="false" maxChoices="1">
|
|
410
|
+
<simpleChoice identifier="A">4</simpleChoice>
|
|
411
|
+
<simpleChoice identifier="B">3</simpleChoice>
|
|
412
|
+
<simpleChoice identifier="C">5</simpleChoice>
|
|
413
|
+
</choiceInteraction>
|
|
414
|
+
</itemBody>
|
|
415
|
+
<responseProcessing>
|
|
416
|
+
<responseCondition>
|
|
417
|
+
<responseIf>
|
|
418
|
+
<match>
|
|
419
|
+
<variable identifier="RESPONSE" />
|
|
420
|
+
<correct identifier="RESPONSE" />
|
|
421
|
+
</match>
|
|
422
|
+
<setOutcomeValue identifier="SCORE">
|
|
423
|
+
<baseValue baseType="float">1</baseValue>
|
|
424
|
+
</setOutcomeValue>
|
|
425
|
+
</responseIf>
|
|
426
|
+
<responseElse>
|
|
427
|
+
<setOutcomeValue identifier="SCORE">
|
|
428
|
+
<baseValue baseType="float">0</baseValue>
|
|
429
|
+
</setOutcomeValue>
|
|
430
|
+
</responseElse>
|
|
431
|
+
</responseCondition>
|
|
432
|
+
</responseProcessing>
|
|
433
|
+
</assessmentItem>`,
|
|
434
|
+
},
|
|
435
|
+
{
|
|
436
|
+
identifier: 'item2',
|
|
437
|
+
role: 'candidate',
|
|
438
|
+
itemXml: `<assessmentItem identifier="item2" title="Follow-up Item" adaptive="false" timeDependent="false">
|
|
439
|
+
<responseDeclaration identifier="RESPONSE" cardinality="single" baseType="identifier">
|
|
440
|
+
<correctResponse>
|
|
441
|
+
<value>A</value>
|
|
442
|
+
</correctResponse>
|
|
443
|
+
</responseDeclaration>
|
|
444
|
+
<outcomeDeclaration identifier="SCORE" cardinality="single" baseType="float">
|
|
445
|
+
<defaultValue>
|
|
446
|
+
<value>0</value>
|
|
447
|
+
</defaultValue>
|
|
448
|
+
</outcomeDeclaration>
|
|
449
|
+
<itemBody>
|
|
450
|
+
<p>Demo follow-up item</p>
|
|
451
|
+
<choiceInteraction responseIdentifier="RESPONSE" shuffle="false" maxChoices="1">
|
|
452
|
+
<simpleChoice identifier="A">ok</simpleChoice>
|
|
453
|
+
<simpleChoice identifier="B">no</simpleChoice>
|
|
454
|
+
</choiceInteraction>
|
|
455
|
+
</itemBody>
|
|
456
|
+
<responseProcessing>
|
|
457
|
+
<responseCondition>
|
|
458
|
+
<responseIf>
|
|
459
|
+
<match>
|
|
460
|
+
<variable identifier="RESPONSE" />
|
|
461
|
+
<correct identifier="RESPONSE" />
|
|
462
|
+
</match>
|
|
463
|
+
<setOutcomeValue identifier="SCORE">
|
|
464
|
+
<baseValue baseType="float">1</baseValue>
|
|
465
|
+
</setOutcomeValue>
|
|
466
|
+
</responseIf>
|
|
467
|
+
</responseCondition>
|
|
468
|
+
</responseProcessing>
|
|
469
|
+
</assessmentItem>`,
|
|
470
|
+
},
|
|
471
|
+
],
|
|
472
|
+
},
|
|
473
|
+
],
|
|
474
|
+
},
|
|
475
|
+
],
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
evaluateConditionXml(conditionXml, vars) {
|
|
479
|
+
// conditionXml is an operator element like <gt>...</gt> (not wrapped in <expression>).
|
|
480
|
+
const doc = parseXml(conditionXml);
|
|
481
|
+
const root = doc.documentElement;
|
|
482
|
+
if (!root)
|
|
483
|
+
return false;
|
|
484
|
+
const decls = {};
|
|
485
|
+
for (const [k, raw] of Object.entries(vars)) {
|
|
486
|
+
let v = qtiNull();
|
|
487
|
+
if (typeof raw === 'number')
|
|
488
|
+
v = qtiValue('float', 'single', raw);
|
|
489
|
+
else if (typeof raw === 'boolean')
|
|
490
|
+
v = qtiValue('boolean', 'single', raw);
|
|
491
|
+
else if (typeof raw === 'string')
|
|
492
|
+
v = qtiValue('string', 'single', raw);
|
|
493
|
+
decls[k] = {
|
|
494
|
+
identifier: k,
|
|
495
|
+
baseType: v.kind === 'value' ? v.baseType : 'string',
|
|
496
|
+
cardinality: 'single',
|
|
497
|
+
defaultValue: qtiNull(),
|
|
498
|
+
value: v,
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
const ctx = new DeclarationContext(decls);
|
|
502
|
+
const ops = new OperatorRegistry();
|
|
503
|
+
const expr = buildExpression(root, { scope: 'test' });
|
|
504
|
+
const out = evalExpr({ ctx, ops, rng: Math.random }, expr);
|
|
505
|
+
return out.kind === 'value' && out.baseType === 'boolean' ? Boolean(out.value) : false;
|
|
506
|
+
}
|
|
507
|
+
getItemOrder(assessment) {
|
|
508
|
+
const out = [];
|
|
509
|
+
for (const part of assessment.testParts || []) {
|
|
510
|
+
for (const section of part.sections || []) {
|
|
511
|
+
for (const item of section.items || []) {
|
|
512
|
+
out.push(item.identifier);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
return out;
|
|
517
|
+
}
|
|
518
|
+
// =========================================================================
|
|
519
|
+
// ITEM BANK HELPERS
|
|
520
|
+
// =========================================================================
|
|
521
|
+
/**
|
|
522
|
+
* Generate cache key for item bank query
|
|
523
|
+
*/
|
|
524
|
+
getBankCacheKey(sessionId, bankId, count, withReplacement, filters) {
|
|
525
|
+
const filterKey = filters ? JSON.stringify(filters) : '';
|
|
526
|
+
return `${BANK_CACHE_PREFIX}${sessionId}:${bankId}:${count}:${withReplacement}:${filterKey}`;
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* Load item bank cache from localStorage
|
|
530
|
+
*/
|
|
531
|
+
loadBankCache(cacheKey) {
|
|
532
|
+
try {
|
|
533
|
+
const data = localStorage.getItem(cacheKey);
|
|
534
|
+
if (!data)
|
|
535
|
+
return null;
|
|
536
|
+
const entry = JSON.parse(data);
|
|
537
|
+
// Check if cache is expired
|
|
538
|
+
const now = Date.now();
|
|
539
|
+
if (now - entry.timestamp > entry.ttl) {
|
|
540
|
+
// Clean up expired cache
|
|
541
|
+
localStorage.removeItem(cacheKey);
|
|
542
|
+
return null;
|
|
543
|
+
}
|
|
544
|
+
return entry;
|
|
545
|
+
}
|
|
546
|
+
catch {
|
|
547
|
+
return null;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* Save item bank cache to localStorage
|
|
552
|
+
*/
|
|
553
|
+
saveBankCache(cacheKey, entry) {
|
|
554
|
+
try {
|
|
555
|
+
localStorage.setItem(cacheKey, JSON.stringify(entry));
|
|
556
|
+
}
|
|
557
|
+
catch (error) {
|
|
558
|
+
console.error('Failed to save bank cache to localStorage:', error);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
/**
|
|
562
|
+
* Fetch items from item bank with retry logic
|
|
563
|
+
*
|
|
564
|
+
* **DEMO IMPLEMENTATION**: Returns hardcoded items
|
|
565
|
+
* In production, this would make HTTP request to item bank service
|
|
566
|
+
*/
|
|
567
|
+
async fetchItemsFromBank(bankId, count, withReplacement, filters, retryCount = 0) {
|
|
568
|
+
const maxRetries = 3;
|
|
569
|
+
const baseDelay = 1000; // 1 second
|
|
570
|
+
try {
|
|
571
|
+
// Simulate network call delay
|
|
572
|
+
await this.delay(100);
|
|
573
|
+
// **DEMO**: Return hardcoded items
|
|
574
|
+
// In production, replace with:
|
|
575
|
+
// const response = await fetch(`/api/item-banks/${bankId}`, {
|
|
576
|
+
// method: 'POST',
|
|
577
|
+
// headers: { 'Content-Type': 'application/json' },
|
|
578
|
+
// body: JSON.stringify({ count, withReplacement, filters })
|
|
579
|
+
// });
|
|
580
|
+
// return response.json();
|
|
581
|
+
const demoItems = [
|
|
582
|
+
{
|
|
583
|
+
identifier: `bank-${bankId}-item1`,
|
|
584
|
+
role: 'candidate',
|
|
585
|
+
itemXml: `<assessmentItem identifier="bank-${bankId}-item1" title="Bank Item 1" adaptive="false" timeDependent="false">
|
|
586
|
+
<responseDeclaration identifier="RESPONSE" cardinality="single" baseType="identifier">
|
|
587
|
+
<correctResponse><value>A</value></correctResponse>
|
|
588
|
+
</responseDeclaration>
|
|
589
|
+
<outcomeDeclaration identifier="SCORE" cardinality="single" baseType="float">
|
|
590
|
+
<defaultValue><value>0</value></defaultValue>
|
|
591
|
+
</outcomeDeclaration>
|
|
592
|
+
<itemBody>
|
|
593
|
+
<p>Sample question from bank ${bankId}</p>
|
|
594
|
+
<choiceInteraction responseIdentifier="RESPONSE" shuffle="false" maxChoices="1">
|
|
595
|
+
<simpleChoice identifier="A">Option A</simpleChoice>
|
|
596
|
+
<simpleChoice identifier="B">Option B</simpleChoice>
|
|
597
|
+
</choiceInteraction>
|
|
598
|
+
</itemBody>
|
|
599
|
+
<responseProcessing>
|
|
600
|
+
<responseCondition>
|
|
601
|
+
<responseIf>
|
|
602
|
+
<match>
|
|
603
|
+
<variable identifier="RESPONSE" />
|
|
604
|
+
<correct identifier="RESPONSE" />
|
|
605
|
+
</match>
|
|
606
|
+
<setOutcomeValue identifier="SCORE">
|
|
607
|
+
<baseValue baseType="float">1</baseValue>
|
|
608
|
+
</setOutcomeValue>
|
|
609
|
+
</responseIf>
|
|
610
|
+
<responseElse>
|
|
611
|
+
<setOutcomeValue identifier="SCORE">
|
|
612
|
+
<baseValue baseType="float">0</baseValue>
|
|
613
|
+
</setOutcomeValue>
|
|
614
|
+
</responseElse>
|
|
615
|
+
</responseCondition>
|
|
616
|
+
</responseProcessing>
|
|
617
|
+
</assessmentItem>`,
|
|
618
|
+
},
|
|
619
|
+
{
|
|
620
|
+
identifier: `bank-${bankId}-item2`,
|
|
621
|
+
role: 'candidate',
|
|
622
|
+
itemXml: `<assessmentItem identifier="bank-${bankId}-item2" title="Bank Item 2" adaptive="false" timeDependent="false">
|
|
623
|
+
<responseDeclaration identifier="RESPONSE" cardinality="single" baseType="identifier">
|
|
624
|
+
<correctResponse><value>B</value></correctResponse>
|
|
625
|
+
</responseDeclaration>
|
|
626
|
+
<outcomeDeclaration identifier="SCORE" cardinality="single" baseType="float">
|
|
627
|
+
<defaultValue><value>0</value></defaultValue>
|
|
628
|
+
</outcomeDeclaration>
|
|
629
|
+
<itemBody>
|
|
630
|
+
<p>Another question from bank ${bankId}</p>
|
|
631
|
+
<choiceInteraction responseIdentifier="RESPONSE" shuffle="false" maxChoices="1">
|
|
632
|
+
<simpleChoice identifier="A">Option A</simpleChoice>
|
|
633
|
+
<simpleChoice identifier="B">Option B</simpleChoice>
|
|
634
|
+
</choiceInteraction>
|
|
635
|
+
</itemBody>
|
|
636
|
+
<responseProcessing>
|
|
637
|
+
<responseCondition>
|
|
638
|
+
<responseIf>
|
|
639
|
+
<match>
|
|
640
|
+
<variable identifier="RESPONSE" />
|
|
641
|
+
<correct identifier="RESPONSE" />
|
|
642
|
+
</match>
|
|
643
|
+
<setOutcomeValue identifier="SCORE">
|
|
644
|
+
<baseValue baseType="float">1</baseValue>
|
|
645
|
+
</setOutcomeValue>
|
|
646
|
+
</responseIf>
|
|
647
|
+
<responseElse>
|
|
648
|
+
<setOutcomeValue identifier="SCORE">
|
|
649
|
+
<baseValue baseType="float">0</baseValue>
|
|
650
|
+
</setOutcomeValue>
|
|
651
|
+
</responseElse>
|
|
652
|
+
</responseCondition>
|
|
653
|
+
</responseProcessing>
|
|
654
|
+
</assessmentItem>`,
|
|
655
|
+
},
|
|
656
|
+
{
|
|
657
|
+
identifier: `bank-${bankId}-item3`,
|
|
658
|
+
role: 'candidate',
|
|
659
|
+
itemXml: `<assessmentItem identifier="bank-${bankId}-item3" title="Bank Item 3" adaptive="false" timeDependent="false">
|
|
660
|
+
<responseDeclaration identifier="RESPONSE" cardinality="single" baseType="identifier">
|
|
661
|
+
<correctResponse><value>A</value></correctResponse>
|
|
662
|
+
</responseDeclaration>
|
|
663
|
+
<outcomeDeclaration identifier="SCORE" cardinality="single" baseType="float">
|
|
664
|
+
<defaultValue><value>0</value></defaultValue>
|
|
665
|
+
</outcomeDeclaration>
|
|
666
|
+
<itemBody>
|
|
667
|
+
<p>Third question from bank ${bankId}</p>
|
|
668
|
+
<choiceInteraction responseIdentifier="RESPONSE" shuffle="false" maxChoices="1">
|
|
669
|
+
<simpleChoice identifier="A">Option A</simpleChoice>
|
|
670
|
+
<simpleChoice identifier="B">Option B</simpleChoice>
|
|
671
|
+
</choiceInteraction>
|
|
672
|
+
</itemBody>
|
|
673
|
+
<responseProcessing>
|
|
674
|
+
<responseCondition>
|
|
675
|
+
<responseIf>
|
|
676
|
+
<match>
|
|
677
|
+
<variable identifier="RESPONSE" />
|
|
678
|
+
<correct identifier="RESPONSE" />
|
|
679
|
+
</match>
|
|
680
|
+
<setOutcomeValue identifier="SCORE">
|
|
681
|
+
<baseValue baseType="float">1</baseValue>
|
|
682
|
+
</setOutcomeValue>
|
|
683
|
+
</responseIf>
|
|
684
|
+
<responseElse>
|
|
685
|
+
<setOutcomeValue identifier="SCORE">
|
|
686
|
+
<baseValue baseType="float">0</baseValue>
|
|
687
|
+
</setOutcomeValue>
|
|
688
|
+
</responseElse>
|
|
689
|
+
</responseCondition>
|
|
690
|
+
</responseProcessing>
|
|
691
|
+
</assessmentItem>`,
|
|
692
|
+
},
|
|
693
|
+
];
|
|
694
|
+
// Apply filtering if specified (demo implementation)
|
|
695
|
+
let filteredItems = [...demoItems];
|
|
696
|
+
if (filters?.difficulty) {
|
|
697
|
+
// In production, server would filter by metadata
|
|
698
|
+
console.log(`Filtering by difficulty: ${filters.difficulty}`);
|
|
699
|
+
}
|
|
700
|
+
// Select requested number of items
|
|
701
|
+
if (withReplacement) {
|
|
702
|
+
// Allow duplicates
|
|
703
|
+
const selected = [];
|
|
704
|
+
for (let i = 0; i < count; i++) {
|
|
705
|
+
const randomIndex = Math.floor(Math.random() * filteredItems.length);
|
|
706
|
+
selected.push(filteredItems[randomIndex]);
|
|
707
|
+
}
|
|
708
|
+
return selected;
|
|
709
|
+
}
|
|
710
|
+
else {
|
|
711
|
+
// No duplicates - shuffle and take first N
|
|
712
|
+
const shuffled = [...filteredItems].sort(() => Math.random() - 0.5);
|
|
713
|
+
return shuffled.slice(0, Math.min(count, shuffled.length));
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
catch (error) {
|
|
717
|
+
// Implement exponential backoff retry
|
|
718
|
+
if (retryCount < maxRetries) {
|
|
719
|
+
const delay = baseDelay * Math.pow(2, retryCount);
|
|
720
|
+
console.warn(`Item bank query failed (attempt ${retryCount + 1}/${maxRetries + 1}). ` +
|
|
721
|
+
`Retrying in ${delay}ms...`, error);
|
|
722
|
+
await this.delay(delay);
|
|
723
|
+
return this.fetchItemsFromBank(bankId, count, withReplacement, filters, retryCount + 1);
|
|
724
|
+
}
|
|
725
|
+
// All retries exhausted
|
|
726
|
+
throw new Error(`Failed to load items from bank "${bankId}" after ${maxRetries + 1} attempts: ${error}`);
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
/**
|
|
730
|
+
* Delay helper for async operations
|
|
731
|
+
*/
|
|
732
|
+
delay(ms) {
|
|
733
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
734
|
+
}
|
|
735
|
+
}
|