@pie-qti/item-player 1.0.0-beta.0 → 1.0.0-beta.2

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.
@@ -1,5 +1,6 @@
1
1
  import { buildOutcomeProcessingAst, buildResponseProcessingAst, buildTemplateProcessingAst, coerceBaseValue, DeclarationContext, evalExpr, execProgram, findDescendants, findFirstDescendant, getAttr, OperatorRegistry, qtiNull, qtiValue, toBoolean, toNumber, toStringValue, } from '@pie-qti/qti-processing';
2
2
  import { prepareContentMarkup } from '@pie-qti/qti-common/accessibility';
3
+ import { DefaultI18nProvider } from '@pie-qti/i18n';
3
4
  import { Qti2xElementNameMapper, Qti2xAttributeNameMapper, Qti3ElementNameMapper, Qti3AttributeNameMapper, detectQtiVersion, isShownByIdentifier, } from '@pie-qti/qti-common';
4
5
  import { parseAssessmentItemDocument } from '../document/AssessmentItemDocument.js';
5
6
  import { printVariable, readPrintedVariableOptions } from '../document/printedVariable.js';
@@ -563,18 +564,11 @@ export class Player {
563
564
  return getAttr(el, canonicalName);
564
565
  }
565
566
  /**
566
- * Create a simple fallback i18n provider when none is provided
567
- * This avoids a hard dependency on @pie-qti/i18n
567
+ * The provider used when the host passes none: English, from the catalog
568
+ * `@pie-qti/i18n` bundles eagerly.
568
569
  */
569
570
  createDefaultI18nProvider() {
570
- return {
571
- getLocale: () => 'en-US',
572
- setLocale: () => { },
573
- t: (key) => key, // Fallback to key
574
- plural: (key) => key,
575
- formatNumber: (value) => value.toString(),
576
- formatDate: (date) => date.toISOString(),
577
- };
571
+ return new DefaultI18nProvider('en-US');
578
572
  }
579
573
  /**
580
574
  * Detects the QTI version from the assessmentItem element.
@@ -700,7 +694,7 @@ export class Player {
700
694
  const responses = Object.values(this.decls).filter((d) => d.__kind === 'response');
701
695
  if (responses.length === 0 || responses.some((d) => d.correctResponse?.kind !== 'value'))
702
696
  return undefined;
703
- const env = { ctx: this.ctx, ops: this.ops, rng: this.rng, customOperators: this.config.customOperators };
697
+ const env = this.processingEnv();
704
698
  return responses.every((d) => toBoolean(evalExpr(env, {
705
699
  kind: 'expr.match',
706
700
  id: `correct-${d.identifier}`,
@@ -1072,36 +1066,31 @@ export class Player {
1072
1066
  // ----------------------------------------------------------------------------
1073
1067
  // Execution
1074
1068
  // ----------------------------------------------------------------------------
1075
- execTemplateProcessing() {
1076
- this.interactionDataCache = null;
1077
- if (!this.templateProcessingProgram)
1078
- return;
1079
- execProgram({
1069
+ /** The evaluator's environment, which reads responses in the item's content language. */
1070
+ processingEnv() {
1071
+ return {
1080
1072
  ctx: this.ctx,
1081
1073
  ops: this.ops,
1082
1074
  rng: this.rng,
1075
+ language: this.getContentLanguage(),
1083
1076
  customOperators: this.config.customOperators,
1084
- }, this.templateProcessingProgram);
1077
+ };
1078
+ }
1079
+ execTemplateProcessing() {
1080
+ this.interactionDataCache = null;
1081
+ if (!this.templateProcessingProgram)
1082
+ return;
1083
+ execProgram(this.processingEnv(), this.templateProcessingProgram);
1085
1084
  }
1086
1085
  execResponseProcessingProgram() {
1087
1086
  if (!this.responseProcessingProgram)
1088
1087
  return;
1089
- execProgram({
1090
- ctx: this.ctx,
1091
- ops: this.ops,
1092
- rng: this.rng,
1093
- customOperators: this.config.customOperators,
1094
- }, this.responseProcessingProgram);
1088
+ execProgram(this.processingEnv(), this.responseProcessingProgram);
1095
1089
  }
1096
1090
  execOutcomeProcessingProgram() {
1097
1091
  if (!this.outcomeProcessingProgram)
1098
1092
  return;
1099
- execProgram({
1100
- ctx: this.ctx,
1101
- ops: this.ops,
1102
- rng: this.rng,
1103
- customOperators: this.config.customOperators,
1104
- }, this.outcomeProcessingProgram);
1093
+ execProgram(this.processingEnv(), this.outcomeProcessingProgram);
1105
1094
  }
1106
1095
  execResponseProcessingTemplate(templateUrl) {
1107
1096
  const name = templateUrl.split('/').pop()?.toLowerCase().replace(/\.xml$/, '');
@@ -1116,7 +1105,7 @@ export class Player {
1116
1105
  a: { kind: 'expr.variable', id: 'fixed-template-response', identifier: 'RESPONSE' },
1117
1106
  b: { kind: 'expr.correct', id: 'fixed-template-correct', identifier: 'RESPONSE' },
1118
1107
  };
1119
- return toBoolean(evalExpr({ ctx: this.ctx, ops: this.ops, rng: this.rng, customOperators: this.config.customOperators }, expression));
1108
+ return toBoolean(evalExpr(this.processingEnv(), expression));
1120
1109
  };
1121
1110
  const setScore = (value) => {
1122
1111
  this.ctx.setValue('SCORE', qtiValue('float', 'single', value));
@@ -1128,7 +1117,7 @@ export class Player {
1128
1117
  const response = this.ctx.getValue('RESPONSE');
1129
1118
  if (response.kind === 'null')
1130
1119
  return 0;
1131
- const mapped = evalExpr({ ctx: this.ctx, ops: this.ops, rng: this.rng, customOperators: this.config.customOperators }, {
1120
+ const mapped = evalExpr(this.processingEnv(), {
1132
1121
  kind: point ? 'expr.mapResponsePoint' : 'expr.mapResponse',
1133
1122
  id: point ? 'fixed-template-map-response-point' : 'fixed-template-map-response',
1134
1123
  identifier: 'RESPONSE',
@@ -1530,7 +1519,7 @@ export class Player {
1530
1519
  const fieldBaseType = this.getAttrMapped(valueElement, 'baseType');
1531
1520
  if (!fieldIdentifier || !fieldBaseType)
1532
1521
  continue;
1533
- record[fieldIdentifier] = coerceBaseValue(fieldBaseType, valueElement.textContent || '');
1522
+ record[fieldIdentifier] = coerceBaseValue(fieldBaseType, valueElement.textContent || '', this.getContentLanguage());
1534
1523
  }
1535
1524
  return Object.keys(record).length > 0
1536
1525
  ? qtiValue(undefined, 'record', record)
@@ -1541,11 +1530,11 @@ export class Player {
1541
1530
  const values = valueElements.map((value) => (value.textContent || '').trim());
1542
1531
  if (cardinality === 'multiple' || cardinality === 'ordered') {
1543
1532
  return qtiValue(baseType, cardinality, values.map((t) => {
1544
- const v = coerceBaseValue(baseType, t);
1533
+ const v = coerceBaseValue(baseType, t, this.getContentLanguage());
1545
1534
  return v.kind === 'value' ? v.value : null;
1546
1535
  }));
1547
1536
  }
1548
- return coerceBaseValue(baseType, values[0] ?? '');
1537
+ return coerceBaseValue(baseType, values[0] ?? '', this.getContentLanguage());
1549
1538
  }
1550
1539
  parseMapping(mappingEl, baseType) {
1551
1540
  const defaultValue = Number(this.getAttrMapped(mappingEl, 'defaultValue') || 0);
@@ -1633,10 +1622,10 @@ export class Player {
1633
1622
  .filter((s) => s.length > 0)
1634
1623
  .map((t) => {
1635
1624
  if (extendedTextBase && (baseType === 'integer' || baseType === 'float')) {
1636
- const numeric = createExtendedTextNumericRecord(t, extendedTextBase);
1625
+ const numeric = createExtendedTextNumericRecord(t, extendedTextBase, this.getContentLanguage());
1637
1626
  return baseType === 'integer' ? numeric.integerValue : numeric.floatValue;
1638
1627
  }
1639
- const v = coerceBaseValue(baseType, t);
1628
+ const v = coerceBaseValue(baseType, t, this.getContentLanguage());
1640
1629
  return v.kind === 'value' ? v.value : null;
1641
1630
  })
1642
1631
  .filter((v) => v !== null);
@@ -1647,11 +1636,11 @@ export class Player {
1647
1636
  if (t.length === 0)
1648
1637
  return qtiNull(baseType, cardinality);
1649
1638
  if (extendedTextBase && (baseType === 'integer' || baseType === 'float')) {
1650
- const numeric = createExtendedTextNumericRecord(t, extendedTextBase);
1639
+ const numeric = createExtendedTextNumericRecord(t, extendedTextBase, this.getContentLanguage());
1651
1640
  const value = baseType === 'integer' ? numeric.integerValue : numeric.floatValue;
1652
1641
  return value === null ? qtiNull(baseType, 'single') : qtiValue(baseType, 'single', value);
1653
1642
  }
1654
- return coerceBaseValue(baseType, t);
1643
+ return coerceBaseValue(baseType, t, this.getContentLanguage());
1655
1644
  }
1656
1645
  getExtendedTextBase(responseIdentifier) {
1657
1646
  return this.itemDocument.getExtendedTextBase(responseIdentifier);
@@ -1671,7 +1660,7 @@ export class Player {
1671
1660
  }
1672
1661
  }
1673
1662
  else {
1674
- source = createExtendedTextNumericRecord(stringSource, base);
1663
+ source = createExtendedTextNumericRecord(stringSource, base, this.getContentLanguage());
1675
1664
  }
1676
1665
  }
1677
1666
  if (!source || typeof source !== 'object' || Array.isArray(source)) {
@@ -1704,7 +1693,7 @@ export class Player {
1704
1693
  fields[fieldIdentifier] = qtiValue('file', 'single', fieldValue);
1705
1694
  }
1706
1695
  else {
1707
- fields[fieldIdentifier] = coerceBaseValue(fieldBaseType, String(fieldValue));
1696
+ fields[fieldIdentifier] = coerceBaseValue(fieldBaseType, String(fieldValue), this.getContentLanguage());
1708
1697
  }
1709
1698
  }
1710
1699
  return Object.keys(fields).length > 0