@pie-qti/assessment-player 0.1.4 → 0.1.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.
@@ -8,7 +8,7 @@
8
8
  * - Apply backend navigation decisions + session state
9
9
  */
10
10
 
11
- import type { QTIRole } from '@pie-qti/item-player';
11
+ import type { QTIRole, PnpProfile } from '@pie-qti/item-player';
12
12
  import { Player } from '@pie-qti/item-player';
13
13
  import type {
14
14
  AssessmentRubricBlock,
@@ -37,6 +37,8 @@ export interface BackendAssessmentPlayerConfig {
37
37
  backend: BackendAdapter;
38
38
  initSession: InitSessionRequest;
39
39
  role?: QTIRole;
40
+ /** Optional RNG for deterministic shuffling in selection/ordering. Defaults to Math.random. */
41
+ rng?: () => number;
40
42
  /**
41
43
  * Optional hint for item renderers that support multiple extended text editors.
42
44
  * (Plumbed through the shell; the item player remains authoritative for behavior.)
@@ -58,6 +60,13 @@ export interface BackendAssessmentPlayerConfig {
58
60
  showProgress?: boolean;
59
61
  // Time management
60
62
  timeWarningThreshold?: number;
63
+ /**
64
+ * QTI 3.0 §6.2 Personal Needs and Preferences profile.
65
+ * Extended time (pnp.content.extendedTime) is applied to assessment timeLimits.
66
+ * The full profile is passed down to each item player for component-level features
67
+ * (color scheme, elimination tool).
68
+ */
69
+ pnp?: PnpProfile;
61
70
  // Callbacks
62
71
  onItemChange?: (itemIndex: number, totalItems: number) => void;
63
72
  onSectionChange?: (sectionIndex: number, totalSections: number) => void;
@@ -134,6 +143,7 @@ export class AssessmentPlayer {
134
143
  this.navigationManager = new NavigationManager(navigationMode, this.items.length);
135
144
 
136
145
  // Item session control is a UI hint; backend remains authoritative.
146
+ // Start with testPart-level defaults; section-level overrides are applied per item in navigateTo().
137
147
  const itemSessionControl = this.assessment.testParts?.[0]?.itemSessionControl;
138
148
  this.sessionController = new ItemSessionController(itemSessionControl);
139
149
 
@@ -147,6 +157,7 @@ export class AssessmentPlayer {
147
157
  this.timeManager = new TimeManager({
148
158
  assessmentTimeLimits: this.assessment.timeLimits,
149
159
  warningThreshold: config.timeWarningThreshold || 60,
160
+ extendedTime: config.pnp?.content?.extendedTime,
150
161
  onWarning: (remainingSeconds) => this.notifyTimeWarning(remainingSeconds),
151
162
  onExpired: () => this.notifyTimeExpired(),
152
163
  onTick: (remainingSeconds, elapsedSeconds) => this.notifyTimeTick(remainingSeconds, elapsedSeconds),
@@ -161,6 +172,29 @@ export class AssessmentPlayer {
161
172
  }
162
173
  }
163
174
 
175
+ /**
176
+ * Compute effective itemSessionControl for a given item using three-level fallback:
177
+ * section-level (highest priority) → testPart-level → defaults.
178
+ * Section-level values override testPart-level when present.
179
+ */
180
+ private getEffectiveItemSessionControl(flat: FlatItem): SecureSection['itemSessionControl'] {
181
+ const testPartControl = flat.testPart.itemSessionControl;
182
+ const sectionControl = flat.section.itemSessionControl;
183
+
184
+ if (!sectionControl) return testPartControl;
185
+ if (!testPartControl) return sectionControl;
186
+
187
+ return {
188
+ maxAttempts: sectionControl.maxAttempts ?? testPartControl.maxAttempts,
189
+ showFeedback: sectionControl.showFeedback ?? testPartControl.showFeedback,
190
+ allowReview: sectionControl.allowReview ?? testPartControl.allowReview,
191
+ showSolution: sectionControl.showSolution ?? testPartControl.showSolution,
192
+ allowComment: sectionControl.allowComment ?? testPartControl.allowComment,
193
+ allowSkipping: sectionControl.allowSkipping ?? testPartControl.allowSkipping,
194
+ validateResponses: sectionControl.validateResponses ?? testPartControl.validateResponses,
195
+ };
196
+ }
197
+
164
198
  private hasAnyResponse(responses: Record<string, unknown>): boolean {
165
199
  for (const v of Object.values(responses)) {
166
200
  if (v == null) continue;
@@ -183,10 +217,24 @@ export class AssessmentPlayer {
183
217
  const testParts = assessment.testParts || [];
184
218
  let idx = 0;
185
219
  let secIdx = 0;
220
+ const rng = this.config.rng ?? Math.random;
186
221
 
187
222
  for (const tp of testParts) {
188
223
  for (const section of tp.sections || []) {
189
- for (const item of section.assessmentItemRefs || []) {
224
+ let refs = [...(section.assessmentItemRefs || [])];
225
+
226
+ // QTI ordering: shuffle items within the section when requested.
227
+ // The server may have already shuffled; if not (e.g. reference adapter), do it client-side.
228
+ if (section.ordering?.shuffle) {
229
+ refs = shuffleArray(refs, rng);
230
+ }
231
+
232
+ // QTI selection: take only `select` items from the (possibly shuffled) list.
233
+ if (section.selection && section.selection.select > 0 && section.selection.select < refs.length) {
234
+ refs = refs.slice(0, section.selection.select);
235
+ }
236
+
237
+ for (const item of refs) {
190
238
  out.push({
191
239
  identifier: item.identifier,
192
240
  item,
@@ -367,6 +415,12 @@ export class AssessmentPlayer {
367
415
  const q = this.items[index]!;
368
416
  this.state.currentItemIdentifier = q.identifier;
369
417
 
418
+ // Apply three-level itemSessionControl fallback for this item's section (S1).
419
+ const effectiveControl = this.getEffectiveItemSessionControl(q);
420
+ if (effectiveControl) {
421
+ this.sessionController.updateSettings(effectiveControl);
422
+ }
423
+
370
424
  // Restore responses
371
425
  this.responses = { ...(this.state.itemResponses[q.identifier] || {}) };
372
426
 
@@ -463,11 +517,36 @@ export class AssessmentPlayer {
463
517
  this.sessionController.markAnswered(q.identifier, this.hasAnyResponse(this.responses));
464
518
  this.navigationManager.markVisited(this.currentItemIndex);
465
519
 
466
- // Apply backend branching decision if provided
520
+ // Apply backend branching decision if provided.
521
+ // Special QTI targets EXIT_TEST / EXIT_TESTPART / EXIT_SECTION trigger finalization.
467
522
  if (res.nextItemIdentifier) {
468
- const nextIdx = this.items.findIndex((x) => x.identifier === res.nextItemIdentifier);
469
- if (nextIdx >= 0) {
470
- await this.navigateTo(nextIdx);
523
+ const special = res.nextItemIdentifier;
524
+ if (special === 'EXIT_TEST') {
525
+ // Finalize immediately — no further navigation
526
+ for (const l of this.completeListeners) l();
527
+ this.config.onComplete?.();
528
+ } else if (special === 'EXIT_TESTPART' || special === 'EXIT_SECTION') {
529
+ // Advance past all remaining items in the current testPart or section
530
+ const currentItem = this.items[this.currentItemIndex];
531
+ const isSection = special === 'EXIT_SECTION';
532
+ const nextIdx = this.items.findIndex((item, i) =>
533
+ i > this.currentItemIndex &&
534
+ (isSection
535
+ ? item.section.identifier !== currentItem?.section.identifier
536
+ : item.testPart.identifier !== currentItem?.testPart.identifier)
537
+ );
538
+ if (nextIdx >= 0) {
539
+ await this.navigateTo(nextIdx);
540
+ } else {
541
+ // No further items — signal completion
542
+ for (const l of this.completeListeners) l();
543
+ this.config.onComplete?.();
544
+ }
545
+ } else {
546
+ const nextIdx = this.items.findIndex((x) => x.identifier === special);
547
+ if (nextIdx >= 0) {
548
+ await this.navigateTo(nextIdx);
549
+ }
471
550
  }
472
551
  }
473
552
 
@@ -554,9 +633,26 @@ export class AssessmentPlayer {
554
633
  responses: this.state.itemResponses[r.itemIdentifier] || {},
555
634
  }));
556
635
 
557
- this.visibleFeedback = finalized.feedback
558
- ? [{ identifier: 'testFeedback', content: finalized.feedback, access: 'atEnd' }]
559
- : [];
636
+ // Evaluate testFeedback visibility using QTI string equality:
637
+ // show when outcomeIdentifier's value equals feedback.identifier (for showHide='show'),
638
+ // or does NOT equal it (for showHide='hide').
639
+ const structuredFeedback = this.assessment.testFeedback ?? [];
640
+ const outcomes = finalized.outcomes ?? {};
641
+ if (structuredFeedback.length > 0) {
642
+ this.visibleFeedback = structuredFeedback
643
+ .filter((fb) => fb.access === 'atEnd')
644
+ .filter((fb) => {
645
+ const outcomeValue = String(outcomes[fb.outcomeIdentifier] ?? '');
646
+ const matches = outcomeValue === fb.identifier;
647
+ return fb.showHide === 'show' ? matches : !matches;
648
+ })
649
+ .map((fb) => ({ identifier: fb.identifier, content: fb.content, access: fb.access }));
650
+ } else {
651
+ // Legacy: backend returns a plain feedback string
652
+ this.visibleFeedback = finalized.feedback
653
+ ? [{ identifier: 'testFeedback', content: finalized.feedback, access: 'atEnd' }]
654
+ : [];
655
+ }
560
656
 
561
657
  this.notifyComplete();
562
658
  if (this.config.onComplete) this.config.onComplete();
@@ -725,3 +821,13 @@ export class AssessmentPlayer {
725
821
  };
726
822
  }
727
823
  }
824
+
825
+ /** Fisher-Yates shuffle using a provided RNG (returns a new array). */
826
+ function shuffleArray<T>(arr: T[], rng: () => number): T[] {
827
+ const out = [...arr];
828
+ for (let i = out.length - 1; i > 0; i--) {
829
+ const j = Math.floor(rng() * (i + 1));
830
+ [out[i], out[j]] = [out[j], out[i]];
831
+ }
832
+ return out;
833
+ }
@@ -25,6 +25,12 @@ export interface TimeManagerConfig {
25
25
  onExpired?: () => void;
26
26
  /** Callback for time updates (fires every second) */
27
27
  onTick?: (remainingSeconds: number, elapsedSeconds: number) => void;
28
+ /**
29
+ * QTI 3.0 PNP extended time accommodation.
30
+ * When active, multiplies assessmentTimeLimits.maxTime by the given multiplier.
31
+ * multiplier: Infinity removes the time limit entirely.
32
+ */
33
+ extendedTime?: { active: boolean; multiplier: number };
28
34
  }
29
35
 
30
36
  export interface TimeTrackingState {
@@ -63,10 +69,21 @@ export class TimeManager {
63
69
  isPaused: false,
64
70
  };
65
71
 
66
- // Initialize timer if time limit exists
72
+ // Initialize timer if time limit exists, applying PNP extended-time multiplier if set.
67
73
  if (config.assessmentTimeLimits?.maxTime) {
68
- this.maxTimeSeconds = config.assessmentTimeLimits.maxTime;
69
- this.startTimer();
74
+ const base = config.assessmentTimeLimits.maxTime;
75
+ const et = config.extendedTime;
76
+ if (et?.active && et.multiplier === Infinity) {
77
+ // multiplier: Infinity → unlimited time (no countdown)
78
+ this.maxTimeSeconds = null;
79
+ } else if (et?.active && et.multiplier > 0) {
80
+ this.maxTimeSeconds = base * et.multiplier;
81
+ } else {
82
+ this.maxTimeSeconds = base;
83
+ }
84
+ if (this.maxTimeSeconds !== null) {
85
+ this.startTimer();
86
+ }
70
87
  }
71
88
  }
72
89