@playcademy/sandbox 0.6.1-beta.8 → 0.6.1-beta.9

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.
Files changed (3) hide show
  1. package/dist/cli.js +1656 -566
  2. package/dist/server.js +1656 -566
  3. package/package.json +1 -1
package/dist/cli.js CHANGED
@@ -1084,7 +1084,7 @@ var package_default;
1084
1084
  var init_package = __esm(() => {
1085
1085
  package_default = {
1086
1086
  name: "@playcademy/sandbox",
1087
- version: "0.6.1-beta.8",
1087
+ version: "0.6.1-beta.9",
1088
1088
  description: "Local development server for Playcademy game development",
1089
1089
  type: "module",
1090
1090
  exports: {
@@ -11587,7 +11587,7 @@ var init_table6 = __esm(() => {
11587
11587
  });
11588
11588
 
11589
11589
  // ../data/src/domains/timeback/table.ts
11590
- var gameTimebackIntegrationStatusEnum, gameTimebackIntegrations, gameTimebackAssessmentTests, gameTimebackMetricDiscrepancyVerifications, gameTimebackActivityCompletions;
11590
+ var gameTimebackIntegrationStatusEnum, gameTimebackAssessmentPurposeEnum, gameTimebackAssessmentStatusEnum, gameTimebackIntegrations, gameTimebackAssessmentTests, gameTimebackMetricDiscrepancyVerifications, gameTimebackActivityCompletions;
11591
11591
  var init_table7 = __esm(() => {
11592
11592
  init_drizzle_orm();
11593
11593
  init_pg_core();
@@ -11597,6 +11597,15 @@ var init_table7 = __esm(() => {
11597
11597
  "active",
11598
11598
  "deactivated"
11599
11599
  ]);
11600
+ gameTimebackAssessmentPurposeEnum = pgEnum("game_timeback_assessment_purpose", [
11601
+ "end_of_course",
11602
+ "diagnostic"
11603
+ ]);
11604
+ gameTimebackAssessmentStatusEnum = pgEnum("game_timeback_assessment_status", [
11605
+ "draft",
11606
+ "live",
11607
+ "archived"
11608
+ ]);
11600
11609
  gameTimebackIntegrations = pgTable("game_timeback_integrations", {
11601
11610
  id: uuid("id").primaryKey().defaultRandom(),
11602
11611
  gameId: uuid("game_id").notNull().references(() => games.id, { onDelete: "cascade" }),
@@ -11617,10 +11626,11 @@ var init_table7 = __esm(() => {
11617
11626
  id: uuid("id").primaryKey().defaultRandom(),
11618
11627
  integrationId: uuid("integration_id").notNull().references(() => gameTimebackIntegrations.id, { onDelete: "cascade" }),
11619
11628
  qtiTestIdentifier: text("qti_test_identifier").notNull(),
11620
- bankResourceId: text("bank_resource_id"),
11621
- bankActive: boolean("bank_active").notNull().default(false),
11622
- sortOrder: integer("sort_order").notNull().default(0),
11623
- createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow()
11629
+ purpose: gameTimebackAssessmentPurposeEnum("purpose").notNull().default("end_of_course"),
11630
+ status: gameTimebackAssessmentStatusEnum("status").notNull().default("draft"),
11631
+ sortOrder: integer("sort_order"),
11632
+ createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
11633
+ updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow()
11624
11634
  }, (table3) => [
11625
11635
  uniqueIndex("game_timeback_assessment_tests_integration_qti_idx").on(table3.integrationId, table3.qtiTestIdentifier)
11626
11636
  ]);
@@ -11672,6 +11682,8 @@ __export(exports_tables_index, {
11672
11682
  gameTimebackIntegrations: () => gameTimebackIntegrations,
11673
11683
  gameTimebackIntegrationStatusEnum: () => gameTimebackIntegrationStatusEnum,
11674
11684
  gameTimebackAssessmentTests: () => gameTimebackAssessmentTests,
11685
+ gameTimebackAssessmentStatusEnum: () => gameTimebackAssessmentStatusEnum,
11686
+ gameTimebackAssessmentPurposeEnum: () => gameTimebackAssessmentPurposeEnum,
11675
11687
  gameTimebackActivityCompletions: () => gameTimebackActivityCompletions,
11676
11688
  gameScoresRelations: () => gameScoresRelations,
11677
11689
  gameScores: () => gameScores,
@@ -30101,6 +30113,25 @@ function sleep(ms) {
30101
30113
  }
30102
30114
  return new Promise((resolve) => setTimeout(resolve, ms));
30103
30115
  }
30116
+ async function runWithConcurrency(items, concurrency, worker) {
30117
+ if (items.length === 0) {
30118
+ return [];
30119
+ }
30120
+ const effectiveConcurrency = Math.max(1, Math.min(concurrency, items.length));
30121
+ const results = Array.from({ length: items.length });
30122
+ let nextIndex = 0;
30123
+ await Promise.all(Array.from({ length: effectiveConcurrency }, async () => {
30124
+ while (true) {
30125
+ const currentIndex = nextIndex;
30126
+ nextIndex++;
30127
+ if (currentIndex >= items.length) {
30128
+ return;
30129
+ }
30130
+ results[currentIndex] = await worker(items[currentIndex]);
30131
+ }
30132
+ }));
30133
+ return results;
30134
+ }
30104
30135
 
30105
30136
  // ../timeback/dist/types.js
30106
30137
  function isObject(value) {
@@ -31635,7 +31666,7 @@ class KVBackupService {
31635
31666
  "app.kv_backup.dry_run": dryRun,
31636
31667
  "app.kv_backup.game_slug": options.gameSlug
31637
31668
  });
31638
- const results = await KVBackupService.runWithConcurrency(targets, namespaceConcurrency, (target) => this.backupNamespace(target, {
31669
+ const results = await runWithConcurrency(targets, namespaceConcurrency, (target) => this.backupNamespace(target, {
31639
31670
  bucketName,
31640
31671
  stage,
31641
31672
  runId,
@@ -31701,7 +31732,7 @@ class KVBackupService {
31701
31732
  }
31702
31733
  static async fetchNamespaceEntries(cloudflare2, namespaceId, keyConcurrency = DEFAULT_KEY_CONCURRENCY) {
31703
31734
  const keys = await KVBackupService.withRetries(`List KV keys for namespace ${namespaceId}`, () => cloudflare2.kv.listKeys(namespaceId));
31704
- return KVBackupService.runWithConcurrency(keys, keyConcurrency, async (key) => {
31735
+ return runWithConcurrency(keys, keyConcurrency, async (key) => {
31705
31736
  const safeLabel = KVBackupService.redactKeyForLog(key.name);
31706
31737
  const value = await KVBackupService.withRetries(`Fetch KV value ${safeLabel}`, () => cloudflare2.kv.getValue(namespaceId, key.name));
31707
31738
  const metadata2 = KVBackupService.parseMetadata(key.metadata);
@@ -31878,25 +31909,6 @@ class KVBackupService {
31878
31909
  }
31879
31910
  throw lastError instanceof Error ? lastError : new Error(String(lastError));
31880
31911
  }
31881
- static async runWithConcurrency(items, concurrency, worker) {
31882
- if (items.length === 0) {
31883
- return [];
31884
- }
31885
- const effectiveConcurrency = Math.max(1, Math.min(concurrency, items.length));
31886
- const results = Array.from({ length: items.length });
31887
- let nextIndex = 0;
31888
- await Promise.all(Array.from({ length: effectiveConcurrency }, async () => {
31889
- while (true) {
31890
- const currentIndex = nextIndex;
31891
- nextIndex++;
31892
- if (currentIndex >= items.length) {
31893
- return;
31894
- }
31895
- results[currentIndex] = await worker(items[currentIndex]);
31896
- }
31897
- }));
31898
- return results;
31899
- }
31900
31912
  }
31901
31913
  var BACKUP_SCHEMA_VERSION = 1, DEFAULT_NAMESPACE_CONCURRENCY = 2, DEFAULT_KEY_CONCURRENCY = 10, DEFAULT_RETRY_ATTEMPTS = 3, RETRY_BASE_DELAY_MS = 500;
31902
31914
  var init_kv_backup_service = __esm(() => {
@@ -34203,9 +34215,8 @@ function isValidAdminAttributionDate(value) {
34203
34215
  const date3 = new Date(Date.UTC(year, month - 1, day, 12, 0, 0));
34204
34216
  return date3.getUTCFullYear() === year && date3.getUTCMonth() + 1 === month && date3.getUTCDate() === day;
34205
34217
  }
34206
- var TIMEBACK_GRADES, TIMEBACK_SUBJECTS2, TimebackGradeSchema, TimebackSubjectSchema, CourseGoalsSchema, UpdateGameTimebackIntegrationRequestSchema, CreateGameTimebackIntegrationRequestSchema, TimebackActivityDataSchema, EndActivityRequestSchema, GameRunMetricsSchema, GameCourseMetricsSchema, GameMetricsResponseSchema, AdvanceCourseRequestSchema, UnenrollCourseRequestSchema, HeartbeatRequestSchema, PopulateStudentRequestSchema, DerivedPlatformCourseConfigSchema, TimebackBaseConfigSchema, PlatformTimebackSetupRequestSchema, AdminTimebackMutationBaseSchema, AdminAttributionDateSchema, ADMIN_GRANT_XP_MIN = -1e5, ADMIN_GRANT_XP_MAX = 1e5, ADMIN_GRANT_XP_AMOUNT_RANGE_MESSAGE, GrantTimebackXpRequestSchema, AdjustTimebackTimeRequestSchema, AdjustTimebackMasteryRequestSchema, ReconcileMasteryForConfigChangeSchema, EnrollStudentRequestSchema, UnenrollStudentRequestSchema, ReactivateEnrollmentRequestSchema, VerifyTimebackMetricDiscrepancyRequestSchema, InsertAssessmentTestSchema, CreateAssessmentRequestSchema, ReorderAssessmentsRequestSchema, ReorderQuestionsRequestSchema;
34218
+ var TIMEBACK_GRADES, TIMEBACK_SUBJECTS2, TimebackGradeSchema, TimebackSubjectSchema, CourseGoalsSchema, UpdateGameTimebackIntegrationRequestSchema, CreateGameTimebackIntegrationRequestSchema, TimebackActivityDataSchema, EndActivityRequestSchema, GameRunMetricsSchema, GameCourseMetricsSchema, GameMetricsResponseSchema, AdvanceCourseRequestSchema, UnenrollCourseRequestSchema, HeartbeatRequestSchema, PopulateStudentRequestSchema, DerivedPlatformCourseConfigSchema, TimebackBaseConfigSchema, PlatformTimebackSetupRequestSchema, AdminTimebackMutationBaseSchema, AdminAttributionDateSchema, ADMIN_GRANT_XP_MIN = -1e5, ADMIN_GRANT_XP_MAX = 1e5, ADMIN_GRANT_XP_AMOUNT_RANGE_MESSAGE, GrantTimebackXpRequestSchema, AdjustTimebackTimeRequestSchema, AdjustTimebackMasteryRequestSchema, ReconcileMasteryForConfigChangeSchema, EnrollStudentRequestSchema, UnenrollStudentRequestSchema, ReactivateEnrollmentRequestSchema, VerifyTimebackMetricDiscrepancyRequestSchema, AssessmentPurposeSchema, AssessmentStatusSchema, CreateAssessmentRequestSchema, UpdateAssessmentRequestSchema, CopyAssessmentRequestSchema, ReorderAssessmentsRequestSchema, ReorderQuestionsRequestSchema;
34207
34219
  var init_schemas4 = __esm(() => {
34208
- init_drizzle_zod();
34209
34220
  init_esm();
34210
34221
  init_table7();
34211
34222
  TIMEBACK_GRADES = [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
@@ -34458,15 +34469,26 @@ var init_schemas4 = __esm(() => {
34458
34469
  runId: exports_external.string().uuid(),
34459
34470
  activityId: exports_external.string().min(1).optional()
34460
34471
  });
34461
- InsertAssessmentTestSchema = createInsertSchema(gameTimebackAssessmentTests).omit({
34462
- id: true,
34463
- createdAt: true
34464
- });
34472
+ AssessmentPurposeSchema = exports_external.enum(gameTimebackAssessmentPurposeEnum.enumValues);
34473
+ AssessmentStatusSchema = exports_external.enum(gameTimebackAssessmentStatusEnum.enumValues);
34465
34474
  CreateAssessmentRequestSchema = exports_external.object({
34466
- title: exports_external.string().min(1, "Assessment title is required")
34475
+ title: exports_external.string().min(1, "Assessment title is required"),
34476
+ purpose: AssessmentPurposeSchema
34477
+ });
34478
+ UpdateAssessmentRequestSchema = exports_external.object({
34479
+ title: exports_external.string().trim().min(1, "Assessment title is required").optional(),
34480
+ purpose: AssessmentPurposeSchema.optional(),
34481
+ status: AssessmentStatusSchema.optional()
34482
+ }).refine((input) => input.title !== undefined || input.purpose !== undefined || input.status !== undefined, {
34483
+ message: "Title, purpose, or status is required"
34484
+ });
34485
+ CopyAssessmentRequestSchema = exports_external.object({
34486
+ testIdentifier: exports_external.string().trim().min(1, "Assessment identifier is required"),
34487
+ purpose: AssessmentPurposeSchema
34467
34488
  });
34468
34489
  ReorderAssessmentsRequestSchema = exports_external.object({
34469
- identifiers: exports_external.array(exports_external.string().min(1)).min(1, "At least one identifier is required")
34490
+ purpose: AssessmentPurposeSchema,
34491
+ testIdentifiers: exports_external.array(exports_external.string().trim().min(1, "Assessment identifier is required")).min(1, "At least one assessment is required")
34470
34492
  });
34471
34493
  ReorderQuestionsRequestSchema = exports_external.object({
34472
34494
  items: exports_external.array(exports_external.object({
@@ -53965,10 +53987,10 @@ var init_dist2 = __esm(async () => {
53965
53987
  };
53966
53988
  });
53967
53989
 
53968
- // ../../node_modules/.bun/@timeback+qti@0.3.1+1fb4c65d43e298b9/node_modules/@timeback/qti/dist/chunk-6jf1natv.js
53990
+ // ../../node_modules/.bun/@timeback+qti@0.4.1-beta.20260717234944+1fb4c65d43e298b9/node_modules/@timeback/qti/dist/chunk-6jf1natv.js
53969
53991
  var init_chunk_6jf1natv = () => {};
53970
53992
 
53971
- // ../../node_modules/.bun/@timeback+qti@0.3.1+1fb4c65d43e298b9/node_modules/@timeback/qti/dist/chunk-dd0pbbq3.js
53993
+ // ../../node_modules/.bun/@timeback+qti@0.4.1-beta.20260717234944+1fb4c65d43e298b9/node_modules/@timeback/qti/dist/chunk-dd0pbbq3.js
53972
53994
  var exports_chunk_dd0pbbq3 = {};
53973
53995
  __export(exports_chunk_dd0pbbq3, {
53974
53996
  types: () => types6,
@@ -54379,7 +54401,7 @@ var init_chunk_dd0pbbq3 = __esm(() => {
54379
54401
  util_default2 = { TextEncoder: TextEncoder3, TextDecoder: TextDecoder3, promisify: promisify2, log: log9, inherits: inherits2, _extend: _extend2, callbackifyOnRejected: callbackifyOnRejected2, callbackify: callbackify2 };
54380
54402
  });
54381
54403
 
54382
- // ../../node_modules/.bun/@timeback+qti@0.3.1+1fb4c65d43e298b9/node_modules/@timeback/qti/dist/chunk-m54fefpq.js
54404
+ // ../../node_modules/.bun/@timeback+qti@0.4.1-beta.20260717234944+1fb4c65d43e298b9/node_modules/@timeback/qti/dist/chunk-pt7w63g2.js
54383
54405
  function createInputValidationError2(message, issues) {
54384
54406
  return new InputValidationError2(message, issues);
54385
54407
  }
@@ -55430,7 +55452,7 @@ var ApiError3, UnauthorizedError3, ForbiddenError2, NotFoundError3, ValidationEr
55430
55452
  console[method](label, style);
55431
55453
  }
55432
55454
  }, LOG_LEVELS2, GLOBAL_LOGGING_CONFIG_KEY2, BEYONDAI_PATHS2, LEARNWITHAI_PATHS2, PLATFORM_PATHS2, DEFAULT_PROVIDER_REGISTRY2, MODE_TRANSPORT2, MODE_PROVIDER2, MODE_ENV_CONFIG2, MODE_EXPLICIT_CONFIG2, MODE_ENV_FALLBACK_PLATFORM2, MODE_ENV_FALLBACK_EXPLICIT2, MODE_TOKEN_PROVIDER2, MODES2, MAX_RETRIES2 = 3, RETRY_STATUS_CODES2, INITIAL_RETRY_DELAY_MS2 = 1000, DEFAULT_LIMIT2 = 100, DEFAULT_MAX_ITEMS2 = 1e4, Paginator6;
55433
- var init_chunk_m54fefpq = __esm(async () => {
55455
+ var init_chunk_pt7w63g2 = __esm(async () => {
55434
55456
  init_chunk_6jf1natv();
55435
55457
  ApiError3 = class ApiError3 extends Error {
55436
55458
  statusCode;
@@ -70576,15 +70598,14 @@ var init_v42 = __esm(() => {
70576
70598
  init_classic2();
70577
70599
  });
70578
70600
 
70579
- // ../../node_modules/.bun/@timeback+qti@0.3.1+1fb4c65d43e298b9/node_modules/@timeback/qti/dist/index.js
70601
+ // ../../node_modules/.bun/@timeback+qti@0.4.1-beta.20260717234944+1fb4c65d43e298b9/node_modules/@timeback/qti/dist/index.js
70580
70602
  function resolveToProvider23(config4, registry3 = DEFAULT_PROVIDER_REGISTRY2) {
70581
70603
  return resolveToProvider10(config4, QTI_ENV_VARS2, registry3);
70582
70604
  }
70583
70605
  function translateParams2(params) {
70584
70606
  const {
70585
70607
  orderBy,
70586
- filter: _,
70587
- fields: __,
70608
+ fields: _,
70588
70609
  ...rest
70589
70610
  } = params;
70590
70611
  return {
@@ -70592,6 +70613,23 @@ function translateParams2(params) {
70592
70613
  order: orderBy
70593
70614
  };
70594
70615
  }
70616
+ function buildListQueryParams(params) {
70617
+ const queryParams = {};
70618
+ const filter = params.where ? whereToFilter2(params.where) : undefined;
70619
+ if (filter !== undefined)
70620
+ queryParams.filter = filter;
70621
+ if (params.query !== undefined)
70622
+ queryParams.query = params.query;
70623
+ if (params.page !== undefined)
70624
+ queryParams.page = params.page;
70625
+ if (params.limit !== undefined)
70626
+ queryParams.limit = params.limit;
70627
+ if (params.sort)
70628
+ queryParams.sort = params.sort;
70629
+ if (params.order)
70630
+ queryParams.order = params.order;
70631
+ return queryParams;
70632
+ }
70595
70633
 
70596
70634
  class AssessmentItemsResource2 {
70597
70635
  transport;
@@ -70600,19 +70638,8 @@ class AssessmentItemsResource2 {
70600
70638
  }
70601
70639
  list(params = {}) {
70602
70640
  validatePageListParams2(params);
70603
- const queryParams = {};
70604
- if (params.query !== undefined)
70605
- queryParams.query = params.query;
70606
- if (params.page !== undefined)
70607
- queryParams.page = params.page;
70608
- if (params.limit !== undefined)
70609
- queryParams.limit = params.limit;
70610
- if (params.sort)
70611
- queryParams.sort = params.sort;
70612
- if (params.order)
70613
- queryParams.order = params.order;
70614
70641
  return this.transport.request("/assessment-items", {
70615
- params: queryParams
70642
+ params: buildListQueryParams(params)
70616
70643
  });
70617
70644
  }
70618
70645
  stream(params = {}) {
@@ -70735,20 +70762,9 @@ class TestPartSectionsHelper2 {
70735
70762
  }
70736
70763
  list(params = {}) {
70737
70764
  validatePageListParams2(params);
70738
- const queryParams = {};
70739
- if (params.query !== undefined)
70740
- queryParams.query = params.query;
70741
- if (params.page !== undefined)
70742
- queryParams.page = params.page;
70743
- if (params.limit !== undefined)
70744
- queryParams.limit = params.limit;
70745
- if (params.sort)
70746
- queryParams.sort = params.sort;
70747
- if (params.order)
70748
- queryParams.order = params.order;
70749
70765
  const path = `/assessment-tests/${encodeURIComponent(this.testId)}/test-parts/${encodeURIComponent(this.testPartId)}/sections`;
70750
70766
  return this.transport.request(path, {
70751
- params: queryParams
70767
+ params: buildListQueryParams(params)
70752
70768
  });
70753
70769
  }
70754
70770
  get(identifier) {
@@ -70795,19 +70811,10 @@ class AssessmentTestPartsHelper2 {
70795
70811
  }
70796
70812
  list(params = {}) {
70797
70813
  validatePageListParams2(params);
70798
- const queryParams = {};
70799
- if (params.query !== undefined)
70800
- queryParams.query = params.query;
70801
- if (params.page !== undefined)
70802
- queryParams.page = params.page;
70803
- if (params.limit !== undefined)
70804
- queryParams.limit = params.limit;
70805
- if (params.sort)
70806
- queryParams.sort = params.sort;
70807
- if (params.order)
70808
- queryParams.order = params.order;
70809
70814
  const path = `/assessment-tests/${encodeURIComponent(this.testId)}/test-parts`;
70810
- return this.transport.request(path, { params: queryParams });
70815
+ return this.transport.request(path, {
70816
+ params: buildListQueryParams(params)
70817
+ });
70811
70818
  }
70812
70819
  get(identifier) {
70813
70820
  validateNonEmptyString2(identifier, "testPartId");
@@ -70850,19 +70857,8 @@ class AssessmentTestsResource2 {
70850
70857
  }
70851
70858
  list(params = {}) {
70852
70859
  validatePageListParams2(params);
70853
- const queryParams = {};
70854
- if (params.query !== undefined)
70855
- queryParams.query = params.query;
70856
- if (params.page !== undefined)
70857
- queryParams.page = params.page;
70858
- if (params.limit !== undefined)
70859
- queryParams.limit = params.limit;
70860
- if (params.sort)
70861
- queryParams.sort = params.sort;
70862
- if (params.order)
70863
- queryParams.order = params.order;
70864
70860
  return this.transport.request("/assessment-tests", {
70865
- params: queryParams
70861
+ params: buildListQueryParams(params)
70866
70862
  });
70867
70863
  }
70868
70864
  stream(params = {}) {
@@ -70968,19 +70964,8 @@ class StimuliResource2 {
70968
70964
  }
70969
70965
  list(params = {}) {
70970
70966
  validatePageListParams2(params);
70971
- const queryParams = {};
70972
- if (params.query !== undefined)
70973
- queryParams.query = params.query;
70974
- if (params.page !== undefined)
70975
- queryParams.page = params.page;
70976
- if (params.limit !== undefined)
70977
- queryParams.limit = params.limit;
70978
- if (params.sort)
70979
- queryParams.sort = params.sort;
70980
- if (params.order)
70981
- queryParams.order = params.order;
70982
70967
  return this.transport.request("/stimuli", {
70983
- params: queryParams
70968
+ params: buildListQueryParams(params)
70984
70969
  });
70985
70970
  }
70986
70971
  stream(params = {}) {
@@ -71112,7 +71097,7 @@ var init_dist3 = __esm(async () => {
71112
71097
  init_v42();
71113
71098
  init_v42();
71114
71099
  init_v42();
71115
- await init_chunk_m54fefpq();
71100
+ await init_chunk_pt7w63g2();
71116
71101
  QTI_ENV_VARS2 = {
71117
71102
  baseUrl: ["TIMEBACK_API_BASE_URL", "TIMEBACK_BASE_URL", "QTI_BASE_URL"],
71118
71103
  clientId: ["TIMEBACK_API_CLIENT_ID", "TIMEBACK_CLIENT_ID", "QTI_CLIENT_ID"],
@@ -77953,13 +77938,6 @@ function deriveSourcedIds(courseId) {
77953
77938
  componentResource: `${courseId}-cr`
77954
77939
  };
77955
77940
  }
77956
- function deriveAssessmentBankIds(courseId) {
77957
- return {
77958
- component: `${courseId}-assessment-bank-component`,
77959
- resource: `${courseId}-assessment-bank-resource`,
77960
- componentResource: `${courseId}-assessment-bank-cr`
77961
- };
77962
- }
77963
77941
  function validateProgressData(progressData) {
77964
77942
  if (!progressData.subject) {
77965
77943
  throw new ConfigurationError("subject", "Subject is required for Caliper events. Provide it in progressData.subject");
@@ -79248,59 +79226,22 @@ class CourseAssessments {
79248
79226
  constructor(core3) {
79249
79227
  this.core = core3;
79250
79228
  }
79251
- async ensureBank(input) {
79252
- const oneroster = this.core.api.oneroster;
79253
- const bankIds = deriveAssessmentBankIds(input.courseId);
79254
- try {
79255
- await oneroster.courses.createComponent({
79256
- sourcedId: bankIds.component,
79257
- status: "active",
79258
- title: "Test Out",
79259
- course: { sourcedId: input.courseId },
79260
- sortOrder: 9999,
79261
- metadata: { lessonType: "test-out" }
79262
- });
79263
- } catch (error88) {
79264
- if (!isAlreadyExists(error88)) {
79265
- throw error88;
79266
- }
79267
- }
79268
- try {
79269
- await oneroster.resources.create({
79270
- sourcedId: bankIds.resource,
79271
- status: "active",
79272
- title: "Assessment Bank",
79273
- vendorResourceId: "",
79274
- vendorId: undefined,
79275
- metadata: {
79276
- type: "assessment-bank",
79277
- resources: [],
79278
- lessonType: "test-out",
79279
- subject: input.subject,
79280
- grade: String(input.grade)
79281
- }
79282
- });
79283
- } catch (error88) {
79284
- if (!isAlreadyExists(error88)) {
79285
- throw error88;
79286
- }
79287
- }
79288
- try {
79289
- await oneroster.courses.createComponentResource({
79290
- sourcedId: bankIds.componentResource,
79291
- status: "active",
79292
- title: "Test Out",
79293
- resource: { sourcedId: bankIds.resource },
79294
- courseComponent: { sourcedId: bankIds.component },
79295
- sortOrder: 9999,
79296
- metadata: { lessonType: "test-out" },
79297
- lessonType: "test-out"
79298
- });
79299
- } catch (error88) {
79300
- if (!isAlreadyExists(error88)) {
79301
- throw error88;
79229
+ createItemFromXml(input) {
79230
+ return this.core.qti.assessmentItems.createFromXml({
79231
+ format: "xml",
79232
+ xml: input.xml,
79233
+ ...input.metadata ? { metadata: input.metadata } : {}
79234
+ });
79235
+ }
79236
+ updateItemFromXml(identifier, input) {
79237
+ return this.core.qti.getTransport().request(`/assessment-items/${encodeURIComponent(identifier)}`, {
79238
+ method: "PUT",
79239
+ body: {
79240
+ format: "xml",
79241
+ xml: input.xml,
79242
+ ...input.metadata ? { metadata: input.metadata } : {}
79302
79243
  }
79303
- }
79244
+ });
79304
79245
  }
79305
79246
  async createTestScaffold(input) {
79306
79247
  const partId = `${input.identifier}-part1`;
@@ -79335,18 +79276,6 @@ class CourseAssessments {
79335
79276
  }
79336
79277
  }
79337
79278
  }
79338
- async teardownTest(qtiTestIdentifier) {
79339
- const qti = this.core.qti;
79340
- const partId = `${qtiTestIdentifier}-part1`;
79341
- const sectionId = `${qtiTestIdentifier}-section1`;
79342
- const questions = await qti.assessmentTests.getQuestions(qtiTestIdentifier);
79343
- const sectionItems = qti.assessmentTests.testParts(qtiTestIdentifier).sections(partId).items(sectionId);
79344
- for (const q of questions.questions ?? []) {
79345
- await sectionItems.remove(q.reference.identifier);
79346
- await qti.assessmentItems.delete(q.reference.identifier);
79347
- }
79348
- await qti.assessmentTests.delete(qtiTestIdentifier);
79349
- }
79350
79279
  }
79351
79280
  function buildCoursePayload(config4) {
79352
79281
  return {
@@ -79576,9 +79505,9 @@ function createCourseNamespace(core3) {
79576
79505
  cleanup: (courseId) => integration.cleanup(courseId),
79577
79506
  deactivateCourse: (courseId) => integration.updateCourseStatus(courseId, "tobedeleted"),
79578
79507
  reactivateCourse: (courseId) => integration.updateCourseStatus(courseId, "active"),
79579
- ensureAssessmentBank: (input) => assessments.ensureBank(input),
79580
79508
  createAssessmentTest: (input) => assessments.createTestScaffold(input),
79581
- teardownAssessmentTest: (qtiTestIdentifier) => assessments.teardownTest(qtiTestIdentifier)
79509
+ createAssessmentItemXml: (input) => assessments.createItemFromXml(input),
79510
+ updateAssessmentItemXml: (identifier, input) => assessments.updateItemFromXml(identifier, input)
79582
79511
  };
79583
79512
  }
79584
79513
  function recordTimebackClientSummary(outcome) {
@@ -80061,13 +79990,6 @@ function deriveSourcedIds2(courseId) {
80061
79990
  componentResource: `${courseId}-cr`
80062
79991
  };
80063
79992
  }
80064
- function deriveAssessmentBankIds2(courseId) {
80065
- return {
80066
- component: `${courseId}-assessment-bank-component`,
80067
- resource: `${courseId}-assessment-bank-resource`,
80068
- componentResource: `${courseId}-assessment-bank-cr`
80069
- };
80070
- }
80071
79993
  var CACHE_DEFAULTS4, RESOURCE_DEFAULTS4;
80072
79994
  var init_utils6 = __esm(() => {
80073
79995
  init_src();
@@ -80102,6 +80024,96 @@ var init_utils6 = __esm(() => {
80102
80024
  });
80103
80025
 
80104
80026
  // ../utils/src/timeback.ts
80027
+ function playcademySupportedQtiInteractionType(value) {
80028
+ if (typeof value !== "string") {
80029
+ return;
80030
+ }
80031
+ const normalized = value.trim().toLowerCase().replaceAll("_", "-");
80032
+ return PLAYCADEMY_SUPPORTED_QTI_INTERACTION_TYPE_SET.has(normalized) ? normalized : undefined;
80033
+ }
80034
+ function parseNumericTextEntry(input) {
80035
+ const answer = typeof input.answer === "string" ? input.answer.trim() : "";
80036
+ const comparison = input.comparison;
80037
+ if (input.baseType !== "integer" && input.baseType !== "float") {
80038
+ return { success: false, message: "Numeric answers must be integer or float responses" };
80039
+ }
80040
+ if (!answer || (input.baseType === "integer" ? !INTEGER_ANSWER_PATTERN.test(answer) : !FLOAT_ANSWER_PATTERN.test(answer) || !Number.isFinite(Number(answer)))) {
80041
+ return { success: false, message: `The correct answer is not a valid ${input.baseType}` };
80042
+ }
80043
+ if (!comparison || typeof comparison !== "object" || !("kind" in comparison)) {
80044
+ return { success: false, message: "Numeric comparison is required" };
80045
+ }
80046
+ if (comparison.kind === "equal") {
80047
+ return {
80048
+ success: true,
80049
+ value: { baseType: input.baseType, answer, comparison: { kind: "equal" } }
80050
+ };
80051
+ }
80052
+ if (input.baseType === "integer") {
80053
+ return { success: false, message: "Integer answers cannot use decimal-place rounding" };
80054
+ }
80055
+ const figures = "figures" in comparison ? comparison.figures : undefined;
80056
+ const roundingMode = "roundingMode" in comparison ? comparison.roundingMode : undefined;
80057
+ if (comparison.kind !== "equal-rounded" || roundingMode !== "decimalPlaces" || !Number.isInteger(figures) || figures < 0 || figures > 15) {
80058
+ return { success: false, message: "Decimal places must be a whole number from 0 to 15" };
80059
+ }
80060
+ return {
80061
+ success: true,
80062
+ value: {
80063
+ baseType: "float",
80064
+ answer,
80065
+ comparison: { kind: "equal-rounded", roundingMode, figures }
80066
+ }
80067
+ };
80068
+ }
80069
+ function escapeXml(value) {
80070
+ return value.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll('"', "&quot;").replaceAll("'", "&apos;");
80071
+ }
80072
+ function hottextMaxChoices(selectableCount, multiple) {
80073
+ return multiple ? selectableCount : 1;
80074
+ }
80075
+ function numericTextEntryXml(input) {
80076
+ const comparison = input.numeric.comparison.kind === "equal-rounded" ? `<qti-equal-rounded rounding-mode="decimalPlaces" figures="${input.numeric.comparison.figures}">` : "<qti-equal>";
80077
+ const closeComparison = input.numeric.comparison.kind === "equal-rounded" ? "</qti-equal-rounded>" : "</qti-equal>";
80078
+ return `<?xml version="1.0" encoding="UTF-8"?>
80079
+ <qti-assessment-item xmlns="http://www.imsglobal.org/xsd/imsqtiasi_v3p0" identifier="${escapeXml(input.identifier)}" title="${escapeXml(input.title)}" adaptive="false" time-dependent="false">
80080
+ <qti-response-declaration identifier="RESPONSE" cardinality="single" base-type="${input.numeric.baseType}">
80081
+ <qti-correct-response><qti-value>${escapeXml(input.numeric.answer)}</qti-value></qti-correct-response>
80082
+ </qti-response-declaration>
80083
+ <qti-outcome-declaration identifier="SCORE" cardinality="single" base-type="float">
80084
+ <qti-default-value><qti-value>0</qti-value></qti-default-value>
80085
+ </qti-outcome-declaration>
80086
+ <qti-item-body>
80087
+ <p>${escapeXml(input.prompt)} <qti-text-entry-interaction response-identifier="RESPONSE" expected-length="15" /></p>
80088
+ </qti-item-body>
80089
+ <qti-response-processing>
80090
+ <qti-response-condition>
80091
+ <qti-response-if>
80092
+ ${comparison}<qti-variable identifier="RESPONSE" /><qti-correct identifier="RESPONSE" />${closeComparison}
80093
+ <qti-set-outcome-value identifier="SCORE"><qti-base-value base-type="float">1</qti-base-value></qti-set-outcome-value>
80094
+ </qti-response-if>
80095
+ </qti-response-condition>
80096
+ </qti-response-processing>
80097
+ </qti-assessment-item>`;
80098
+ }
80099
+ function countQtiTestItems(test) {
80100
+ let itemCount = 0;
80101
+ for (const part of test["qti-test-part"] ?? []) {
80102
+ for (const section of part["qti-assessment-section"] ?? []) {
80103
+ itemCount += section["qti-assessment-item-ref"]?.length ?? 0;
80104
+ }
80105
+ }
80106
+ return itemCount;
80107
+ }
80108
+ function isQtiItemOwnedByTest(item, testIdentifier) {
80109
+ const hasOwnershipMetadata = Boolean(item.metadata && (("ownerSystem" in item.metadata) || (PLAYCADEMY_QTI_OWNER_TEST_IDENTIFIER_KEY in item.metadata)));
80110
+ if (hasOwnershipMetadata) {
80111
+ return item.metadata?.ownerSystem === PLAYCADEMY_QTI_OWNER_SYSTEM && item.metadata?.[PLAYCADEMY_QTI_OWNER_TEST_IDENTIFIER_KEY] === testIdentifier;
80112
+ }
80113
+ const identifierPrefix = `${testIdentifier}-q`;
80114
+ const identifierSuffix = item.identifier?.startsWith(identifierPrefix) ? item.identifier.slice(identifierPrefix.length) : undefined;
80115
+ return identifierSuffix !== undefined && /^[0-9a-f]{8}$/i.test(identifierSuffix);
80116
+ }
80105
80117
  function formatGradeLabel(grade) {
80106
80118
  if (grade === null || grade === undefined) {
80107
80119
  return "N/A";
@@ -80143,7 +80155,7 @@ function parseTimebackDiscrepancyQueueMetrics(values) {
80143
80155
  }
80144
80156
  return metrics;
80145
80157
  }
80146
- var TIMEBACK_DISCREPANCY_QUEUE_WINDOWS, TIMEBACK_DISCREPANCY_QUEUE_METRICS, DEFAULT_TIMEBACK_DISCREPANCY_QUEUE_WINDOW = "this-week", TIMEBACK_DISCREPANCY_QUEUE_WINDOW_VALUES, TIMEBACK_DISCREPANCY_QUEUE_METRIC_VALUES;
80158
+ var TIMEBACK_DISCREPANCY_QUEUE_WINDOWS, TIMEBACK_DISCREPANCY_QUEUE_METRICS, PLAYCADEMY_QTI_OWNER_SYSTEM = "playcademy", PLAYCADEMY_QTI_OWNER_TEST_IDENTIFIER_KEY = "ownerTestIdentifier", PLAYCADEMY_QTI_EDITOR_MODE_KEY = "playcademyEditorMode", PLAYCADEMY_SUPPORTED_QTI_INTERACTION_TYPES, PLAYCADEMY_SUPPORTED_QTI_INTERACTION_TYPE_SET, INTEGER_ANSWER_PATTERN, FLOAT_ANSWER_PATTERN, INLINE_CHOICE_BLANK = "_____", DEFAULT_TIMEBACK_DISCREPANCY_QUEUE_WINDOW = "this-week", TIMEBACK_DISCREPANCY_QUEUE_WINDOW_VALUES, TIMEBACK_DISCREPANCY_QUEUE_METRIC_VALUES;
80147
80159
  var init_timeback3 = __esm(() => {
80148
80160
  TIMEBACK_DISCREPANCY_QUEUE_WINDOWS = [
80149
80161
  "today",
@@ -80154,6 +80166,17 @@ var init_timeback3 = __esm(() => {
80154
80166
  "custom"
80155
80167
  ];
80156
80168
  TIMEBACK_DISCREPANCY_QUEUE_METRICS = ["xp", "mastery", "time", "score"];
80169
+ PLAYCADEMY_SUPPORTED_QTI_INTERACTION_TYPES = [
80170
+ "choice",
80171
+ "inline-choice",
80172
+ "text-entry",
80173
+ "order",
80174
+ "match",
80175
+ "hottext"
80176
+ ];
80177
+ PLAYCADEMY_SUPPORTED_QTI_INTERACTION_TYPE_SET = new Set(PLAYCADEMY_SUPPORTED_QTI_INTERACTION_TYPES);
80178
+ INTEGER_ANSWER_PATTERN = /^[+-]?\d+$/;
80179
+ FLOAT_ANSWER_PATTERN = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?$/i;
80157
80180
  TIMEBACK_DISCREPANCY_QUEUE_WINDOW_VALUES = new Set(TIMEBACK_DISCREPANCY_QUEUE_WINDOWS);
80158
80181
  TIMEBACK_DISCREPANCY_QUEUE_METRIC_VALUES = new Set(TIMEBACK_DISCREPANCY_QUEUE_METRICS);
80159
80182
  });
@@ -81396,7 +81419,7 @@ class TimebackAdminService {
81396
81419
  }
81397
81420
  async getMasterableUnitsByCourse(courseIds) {
81398
81421
  const uniqueCourseIds = [...new Set(courseIds)];
81399
- const results = await TimebackAdminService.runWithConcurrency(uniqueCourseIds, TimebackAdminService.MASTERABLE_UNITS_CONCURRENCY, async (courseId) => [courseId, await this.getMasterableUnits(courseId)]);
81422
+ const results = await runWithConcurrency(uniqueCourseIds, TimebackAdminService.MASTERABLE_UNITS_CONCURRENCY, async (courseId) => [courseId, await this.getMasterableUnits(courseId)]);
81400
81423
  return new Map(results);
81401
81424
  }
81402
81425
  deriveGameSensorUrl(game2) {
@@ -81577,7 +81600,7 @@ class TimebackAdminService {
81577
81600
  async loadEnrollmentAnalyticsSummaries(enrollmentIds) {
81578
81601
  const client = this.requireClient();
81579
81602
  const uniqueEnrollmentIds = [...new Set(enrollmentIds)];
81580
- const results = await TimebackAdminService.runWithConcurrency(uniqueEnrollmentIds, TimebackAdminService.ANALYTICS_CONCURRENCY, async (enrollmentId) => {
81603
+ const results = await runWithConcurrency(uniqueEnrollmentIds, TimebackAdminService.ANALYTICS_CONCURRENCY, async (enrollmentId) => {
81581
81604
  try {
81582
81605
  const analytics = await client.api.edubridge.analytics.getEnrollmentFacts({
81583
81606
  enrollmentId,
@@ -81671,7 +81694,7 @@ class TimebackAdminService {
81671
81694
  if (options.runOwnersById.size === 0) {
81672
81695
  return { events: [], runIds: new Set };
81673
81696
  }
81674
- const hydratedRuns = await TimebackAdminService.runWithConcurrency([...options.runOwnersById.entries()], TimebackAdminService.DISCREPANCY_QUEUE_RUN_HYDRATION_CONCURRENCY, async ([runId, studentId]) => {
81697
+ const hydratedRuns = await runWithConcurrency([...options.runOwnersById.entries()], TimebackAdminService.DISCREPANCY_QUEUE_RUN_HYDRATION_CONCURRENCY, async ([runId, studentId]) => {
81675
81698
  try {
81676
81699
  return {
81677
81700
  runId,
@@ -81704,7 +81727,7 @@ class TimebackAdminService {
81704
81727
  };
81705
81728
  }
81706
81729
  async buildMetricDiscrepancyQueueCandidates(user, options) {
81707
- const comparisonResults = await TimebackAdminService.runWithConcurrency(options.activityGroups, TimebackAdminService.DISCREPANCY_QUEUE_COMPARISON_CONCURRENCY, async (group) => {
81730
+ const comparisonResults = await runWithConcurrency(options.activityGroups, TimebackAdminService.DISCREPANCY_QUEUE_COMPARISON_CONCURRENCY, async (group) => {
81708
81731
  try {
81709
81732
  return {
81710
81733
  group,
@@ -82704,7 +82727,7 @@ class TimebackAdminService {
82704
82727
  const action = context2.newMasterableUnits < context2.oldMasterableUnits ? "complete" : "revoke";
82705
82728
  const failed = [];
82706
82729
  let processed = 0;
82707
- await TimebackAdminService.runWithConcurrency(context2.affectedStudentIds, 8, async (studentId) => {
82730
+ await runWithConcurrency(context2.affectedStudentIds, 8, async (studentId) => {
82708
82731
  try {
82709
82732
  await upsertMasteryCompletionEntry({
82710
82733
  client,
@@ -82925,28 +82948,9 @@ class TimebackAdminService {
82925
82948
  }
82926
82949
  async getCompletionStatusByCourse(client, courseIds, studentId) {
82927
82950
  const uniqueCourseIds = [...new Set(courseIds)];
82928
- const results = await TimebackAdminService.runWithConcurrency(uniqueCourseIds, TimebackAdminService.ANALYTICS_CONCURRENCY, async (courseId) => [courseId, await this.getCompletionStatus(client, courseId, studentId)]);
82951
+ const results = await runWithConcurrency(uniqueCourseIds, TimebackAdminService.ANALYTICS_CONCURRENCY, async (courseId) => [courseId, await this.getCompletionStatus(client, courseId, studentId)]);
82929
82952
  return new Map(results);
82930
82953
  }
82931
- static async runWithConcurrency(items, concurrency, worker) {
82932
- if (items.length === 0) {
82933
- return [];
82934
- }
82935
- const effectiveConcurrency = Math.max(1, Math.min(concurrency, items.length));
82936
- const results = Array.from({ length: items.length });
82937
- let nextIndex = 0;
82938
- await Promise.all(Array.from({ length: effectiveConcurrency }, async () => {
82939
- while (true) {
82940
- const currentIndex = nextIndex;
82941
- nextIndex++;
82942
- if (currentIndex >= items.length) {
82943
- return;
82944
- }
82945
- results[currentIndex] = await worker(items[currentIndex]);
82946
- }
82947
- }));
82948
- return results;
82949
- }
82950
82954
  }
82951
82955
  var init_timeback_admin_service = __esm(async () => {
82952
82956
  init_drizzle_orm();
@@ -82973,8 +82977,877 @@ var init_timeback_admin_service = __esm(async () => {
82973
82977
  ]);
82974
82978
  });
82975
82979
 
82980
+ // ../api-core/src/utils/timeback-assessment-rules.util.ts
82981
+ function validateAssessmentStatusTransition(current, next) {
82982
+ if (current === next) {
82983
+ return;
82984
+ }
82985
+ const allowed = current === "draft" && next === "live" || current === "live" && next === "archived" || current === "archived" && next === "live";
82986
+ if (!allowed) {
82987
+ throw new ValidationError(`Assessment status cannot change from ${current} to ${next}`);
82988
+ }
82989
+ }
82990
+ function isAssessmentPublicationTransition(current, next) {
82991
+ return current !== "live" && next === "live";
82992
+ }
82993
+ function assertDraftAssessment(row) {
82994
+ if (row.status !== "draft") {
82995
+ throw new ValidationError("Only draft assessments can change QTI content or question membership");
82996
+ }
82997
+ }
82998
+ function assertAllAssessmentAssociationsDraft(rows) {
82999
+ if (rows.some((row) => row.status !== "draft")) {
83000
+ throw new ValidationError("QTI content cannot change while any associated assessment is live or archived");
83001
+ }
83002
+ }
83003
+ function assertAssessmentHasQuestions(questions) {
83004
+ if (questions.length === 0) {
83005
+ throw new ValidationError("An assessment must contain at least one question to publish");
83006
+ }
83007
+ }
83008
+ function planAssessmentRemoval(status) {
83009
+ if (status === "draft") {
83010
+ return { kind: "delete", action: "discarded", operation: "discard_draft" };
83011
+ }
83012
+ if (status === "live") {
83013
+ return { kind: "archive", action: "archived", operation: "archive" };
83014
+ }
83015
+ return { kind: "none", action: "archived" };
83016
+ }
83017
+ function buildAssessmentAssociationUpdates(row, input) {
83018
+ const updates = {};
83019
+ if (input.purpose !== undefined) {
83020
+ updates.purpose = input.purpose;
83021
+ if (row.status === "live" && input.purpose !== row.purpose) {
83022
+ updates.sortOrder = null;
83023
+ }
83024
+ }
83025
+ if (input.status !== undefined) {
83026
+ updates.status = input.status;
83027
+ if (input.status === "archived") {
83028
+ updates.sortOrder = null;
83029
+ }
83030
+ }
83031
+ return updates;
83032
+ }
83033
+ function validateUniqueAssessmentIdentifiers(testIdentifiers) {
83034
+ if (new Set(testIdentifiers).size !== testIdentifiers.length) {
83035
+ throw new ValidationError("Assessment order must contain unique identifiers");
83036
+ }
83037
+ }
83038
+ function assertAssessmentOrderUpdateSucceeded(updatedRow) {
83039
+ if (!updatedRow) {
83040
+ throw new ValidationError("Assessment order changed while it was being saved. Refresh and try again.");
83041
+ }
83042
+ return updatedRow;
83043
+ }
83044
+ function lockOrderAssessmentRows(rows) {
83045
+ return rows.toSorted((left, right) => left.id.localeCompare(right.id));
83046
+ }
83047
+ function orderLiveAssessmentRows(liveRows, purpose, testIdentifiers) {
83048
+ const rowsByIdentifier = new Map(liveRows.map((row) => [row.qtiTestIdentifier, row]));
83049
+ if (liveRows.length !== testIdentifiers.length || testIdentifiers.some((identifier) => !rowsByIdentifier.has(identifier))) {
83050
+ throw new ValidationError(`Assessment order must include every live ${purpose} assessment exactly once`);
83051
+ }
83052
+ return testIdentifiers.map((identifier) => rowsByIdentifier.get(identifier));
83053
+ }
83054
+ var init_timeback_assessment_rules_util = __esm(() => {
83055
+ init_errors();
83056
+ });
83057
+
83058
+ // ../api-core/src/utils/timeback-qti-authoring.util.ts
83059
+ function recordValue(value) {
83060
+ return value && typeof value === "object" && !Array.isArray(value) ? value : undefined;
83061
+ }
83062
+ function qtiAuthoringSnapshot(input) {
83063
+ if (!recordValue(input.interaction)) {
83064
+ return;
83065
+ }
83066
+ return Object.fromEntries(QTI_AUTHORING_FIELDS.flatMap((field) => input[field] === undefined ? [] : [[field, input[field]]]));
83067
+ }
83068
+ function buildOwnedQtiQuestionMetadata(input, ownership, existingMetadata = {}) {
83069
+ const inputMetadata = recordValue(input.metadata) ?? {};
83070
+ const metadata2 = {
83071
+ ...existingMetadata,
83072
+ ...inputMetadata
83073
+ };
83074
+ const existingAuthoring = recordValue(existingMetadata[PLAYCADEMY_QTI_AUTHORING_METADATA_KEY]);
83075
+ Reflect.deleteProperty(metadata2, PLAYCADEMY_QTI_AUTHORING_METADATA_KEY);
83076
+ Reflect.deleteProperty(metadata2, PLAYCADEMY_QTI_EDITOR_MODE_KEY);
83077
+ const authoring = qtiAuthoringSnapshot(input);
83078
+ const shouldReplaceAuthoring = "interaction" in input || "numericTextEntry" in input;
83079
+ const persistedAuthoring = authoring ?? (shouldReplaceAuthoring ? undefined : existingAuthoring);
83080
+ return {
83081
+ ...metadata2,
83082
+ ownerSystem: PLAYCADEMY_QTI_OWNER_SYSTEM,
83083
+ ownerGameSlug: ownership.gameSlug,
83084
+ [PLAYCADEMY_QTI_OWNER_TEST_IDENTIFIER_KEY]: ownership.testIdentifier,
83085
+ ...persistedAuthoring ? { [PLAYCADEMY_QTI_AUTHORING_METADATA_KEY]: persistedAuthoring } : {}
83086
+ };
83087
+ }
83088
+ function restoreQtiQuestionAuthoringData(item) {
83089
+ const authoring = recordValue(item.metadata?.[PLAYCADEMY_QTI_AUTHORING_METADATA_KEY]);
83090
+ if (!authoring) {
83091
+ return item;
83092
+ }
83093
+ const restored = Object.fromEntries(QTI_AUTHORING_FIELDS.flatMap((field) => authoring[field] === undefined ? [] : [[field, authoring[field]]]));
83094
+ return { ...item, ...restored };
83095
+ }
83096
+ function mergeQtiQuestionItem(item, fallbackItem, itemIdentifier, metadata2) {
83097
+ return restoreQtiQuestionAuthoringData({
83098
+ ...fallbackItem,
83099
+ ...item,
83100
+ identifier: itemIdentifier,
83101
+ title: item.title || fallbackItem?.title || itemIdentifier,
83102
+ type: item.type ?? fallbackItem?.type,
83103
+ rawXml: item.rawXml ?? fallbackItem?.rawXml,
83104
+ interaction: item.interaction ?? fallbackItem?.interaction,
83105
+ responseDeclarations: item.responseDeclarations ?? fallbackItem?.responseDeclarations,
83106
+ metadata: {
83107
+ ...fallbackItem?.metadata,
83108
+ ...item.metadata,
83109
+ ...metadata2
83110
+ }
83111
+ });
83112
+ }
83113
+ function newPlaycademyQuestionIdentifier(testIdentifier) {
83114
+ return `${testIdentifier}-q${crypto.randomUUID().slice(0, 8)}`;
83115
+ }
83116
+ function parseQtiQuestionCreationInput(input) {
83117
+ if (!input || typeof input !== "object" || Array.isArray(input)) {
83118
+ throw new ValidationError("Question creation input must be an object");
83119
+ }
83120
+ const rawInput = input;
83121
+ if (rawInput.mode === undefined) {
83122
+ return { kind: "authoring", input: rawInput };
83123
+ }
83124
+ if (rawInput.mode !== "copy") {
83125
+ throw new ValidationError("Question creation mode is invalid");
83126
+ }
83127
+ const sourceItemIdentifier = typeof rawInput.sourceItemIdentifier === "string" ? rawInput.sourceItemIdentifier.trim() : "";
83128
+ if (!sourceItemIdentifier) {
83129
+ throw new ValidationError("A source question identifier is required to create a copy");
83130
+ }
83131
+ const unexpectedField = Object.keys(rawInput).find((field) => field !== "mode" && field !== "sourceItemIdentifier");
83132
+ if (unexpectedField) {
83133
+ throw new ValidationError(`Question copy input contains an unexpected “${unexpectedField}” field`);
83134
+ }
83135
+ return { kind: "copy", sourceItemIdentifier };
83136
+ }
83137
+ function nonEmptyMetadataString(value) {
83138
+ return typeof value === "string" && value.trim() ? value.trim() : undefined;
83139
+ }
83140
+ function qtiQuestionOwnerTestIdentifier(item) {
83141
+ if (item.metadata?.ownerSystem !== PLAYCADEMY_QTI_OWNER_SYSTEM) {
83142
+ return;
83143
+ }
83144
+ return nonEmptyMetadataString(item.metadata[PLAYCADEMY_QTI_OWNER_TEST_IDENTIFIER_KEY]);
83145
+ }
83146
+ function qtiTestReferencesItem(test, itemIdentifier) {
83147
+ return (test["qti-test-part"] ?? []).some((part) => (part["qti-assessment-section"] ?? []).some((section) => (section["qti-assessment-item-ref"] ?? []).some((reference) => reference.identifier === itemIdentifier)));
83148
+ }
83149
+ function resolveQtiQuestionOwnerGameSlug(item, ownerTest) {
83150
+ if (item.metadata?.ownerSystem !== PLAYCADEMY_QTI_OWNER_SYSTEM) {
83151
+ return;
83152
+ }
83153
+ if ("ownerGameSlug" in item.metadata) {
83154
+ return nonEmptyMetadataString(item.metadata.ownerGameSlug);
83155
+ }
83156
+ const ownerTestIdentifier = qtiQuestionOwnerTestIdentifier(item);
83157
+ if (!ownerTestIdentifier || !ownerTest || ownerTest.identifier !== ownerTestIdentifier || !qtiTestReferencesItem(ownerTest, item.identifier) || ownerTest.metadata?.ownerSystem !== PLAYCADEMY_QTI_OWNER_SYSTEM) {
83158
+ return;
83159
+ }
83160
+ return nonEmptyMetadataString(ownerTest.metadata.ownerGameSlug);
83161
+ }
83162
+ function isQtiQuestionOwnedByGame(item, gameSlug, ownerTest) {
83163
+ return resolveQtiQuestionOwnerGameSlug(item, ownerTest) === gameSlug;
83164
+ }
83165
+ function isQtiQuestionOwnedByContext(item, ownership, ownerTest) {
83166
+ const declaredOwnerTest = qtiQuestionOwnerTestIdentifier(item);
83167
+ if (declaredOwnerTest) {
83168
+ return declaredOwnerTest === ownership.testIdentifier && isQtiQuestionOwnedByGame(item, ownership.gameSlug, ownerTest);
83169
+ }
83170
+ return isQtiItemOwnedByTest(item, ownership.testIdentifier);
83171
+ }
83172
+ function normalizedQtiInteractionType(value) {
83173
+ if (typeof value !== "string") {
83174
+ return;
83175
+ }
83176
+ const normalized = value.trim().toLowerCase().replaceAll("_", "-");
83177
+ return normalized || undefined;
83178
+ }
83179
+ function qtiXmlInteractionTypes(xml) {
83180
+ const markup = xml.replaceAll(/<!--[\s\S]*?-->/g, "").replaceAll(/<!\[CDATA\[[\s\S]*?\]\]>/g, "");
83181
+ const interactionPattern = /<(?!\/)(?:[A-Za-z_][\w.-]*:)?qti-([a-z][\w-]*)-interaction\b[^>]*>/gi;
83182
+ return [...markup.matchAll(interactionPattern)].map((match) => match[1].toLowerCase());
83183
+ }
83184
+ function supportedQtiQuestionInteractionType(question) {
83185
+ const interaction = recordValue(question.interaction);
83186
+ const structuredTypes = [
83187
+ normalizedQtiInteractionType(question.type),
83188
+ normalizedQtiInteractionType(interaction?.type)
83189
+ ].filter((type) => Boolean(type));
83190
+ const distinctStructuredTypes = [...new Set(structuredTypes)];
83191
+ if (distinctStructuredTypes.length > 1) {
83192
+ return;
83193
+ }
83194
+ const rawXml = typeof question.rawXml === "string" ? question.rawXml : undefined;
83195
+ if (rawXml) {
83196
+ const xmlTypes = qtiXmlInteractionTypes(rawXml);
83197
+ if (xmlTypes.length !== 1) {
83198
+ return;
83199
+ }
83200
+ const [xmlType] = xmlTypes;
83201
+ const [structuredType2] = distinctStructuredTypes;
83202
+ if (structuredType2 && structuredType2 !== xmlType) {
83203
+ return;
83204
+ }
83205
+ return playcademySupportedQtiInteractionType(xmlType);
83206
+ }
83207
+ const [structuredType] = distinctStructuredTypes;
83208
+ return playcademySupportedQtiInteractionType(structuredType);
83209
+ }
83210
+ function assertSupportedQtiQuestionInteraction(question) {
83211
+ if (!supportedQtiQuestionInteractionType(question)) {
83212
+ throw new ValidationError("This QTI interaction type is not supported by the Playcademy question editor.");
83213
+ }
83214
+ }
83215
+ function invalidQtiCopyXml() {
83216
+ throw new ValidationError("The source question does not contain valid QTI item XML");
83217
+ }
83218
+ function markupEnd(xml, start2, allowInternalSubset) {
83219
+ let quote = null;
83220
+ let subsetDepth = 0;
83221
+ for (let index2 = start2;index2 < xml.length; index2 += 1) {
83222
+ const character = xml[index2];
83223
+ if (quote) {
83224
+ if (character === quote) {
83225
+ quote = null;
83226
+ }
83227
+ } else if (character === '"' || character === "'") {
83228
+ quote = character;
83229
+ } else if (allowInternalSubset && character === "[") {
83230
+ subsetDepth += 1;
83231
+ } else if (allowInternalSubset && character === "]") {
83232
+ subsetDepth = Math.max(0, subsetDepth - 1);
83233
+ } else if (character === ">" && subsetDepth === 0) {
83234
+ return index2;
83235
+ }
83236
+ }
83237
+ return invalidQtiCopyXml();
83238
+ }
83239
+ function skipXmlWhitespace(xml, start2, end = xml.length) {
83240
+ let cursor2 = start2;
83241
+ while (cursor2 < end && /\s/.test(xml[cursor2] ?? "")) {
83242
+ cursor2 += 1;
83243
+ }
83244
+ return cursor2;
83245
+ }
83246
+ function xmlPreambleEnd(xml, cursor2) {
83247
+ if (xml.startsWith("<?", cursor2)) {
83248
+ const end = xml.indexOf("?>", cursor2 + 2);
83249
+ if (end === -1) {
83250
+ return invalidQtiCopyXml();
83251
+ }
83252
+ return end + 2;
83253
+ }
83254
+ if (xml.startsWith("<!--", cursor2)) {
83255
+ const end = xml.indexOf("-->", cursor2 + 4);
83256
+ if (end === -1) {
83257
+ return invalidQtiCopyXml();
83258
+ }
83259
+ return end + 3;
83260
+ }
83261
+ if (/^<!DOCTYPE\b/i.test(xml.slice(cursor2))) {
83262
+ return markupEnd(xml, cursor2 + 2, true) + 1;
83263
+ }
83264
+ return null;
83265
+ }
83266
+ function qtiRootAttributes(xml, start2, end) {
83267
+ const attributes = [];
83268
+ let cursor2 = start2;
83269
+ while (cursor2 < end) {
83270
+ cursor2 = skipXmlWhitespace(xml, cursor2, end);
83271
+ if (xml[cursor2] === "/") {
83272
+ cursor2 += 1;
83273
+ } else if (cursor2 < end) {
83274
+ const nameMatch = /^[A-Za-z_][\w.:-]*/.exec(xml.slice(cursor2, end));
83275
+ if (!nameMatch) {
83276
+ return invalidQtiCopyXml();
83277
+ }
83278
+ const name3 = nameMatch[0];
83279
+ cursor2 = skipXmlWhitespace(xml, cursor2 + name3.length, end);
83280
+ if (xml[cursor2] !== "=") {
83281
+ return invalidQtiCopyXml();
83282
+ }
83283
+ cursor2 = skipXmlWhitespace(xml, cursor2 + 1, end);
83284
+ const quote = xml[cursor2];
83285
+ if (quote !== '"' && quote !== "'") {
83286
+ return invalidQtiCopyXml();
83287
+ }
83288
+ const valueStart = cursor2 + 1;
83289
+ const valueEnd = xml.indexOf(quote, valueStart);
83290
+ if (valueEnd === -1 || valueEnd > end) {
83291
+ return invalidQtiCopyXml();
83292
+ }
83293
+ attributes.push({ name: name3, quote, valueStart, valueEnd });
83294
+ cursor2 = valueEnd + 1;
83295
+ }
83296
+ }
83297
+ return attributes;
83298
+ }
83299
+ function qtiItemStartTag(xml) {
83300
+ let cursor2 = xml.charCodeAt(0) === 65279 ? 1 : 0;
83301
+ while (cursor2 < xml.length) {
83302
+ cursor2 = skipXmlWhitespace(xml, cursor2);
83303
+ const preambleEnd = xmlPreambleEnd(xml, cursor2);
83304
+ if (preambleEnd !== null) {
83305
+ cursor2 = preambleEnd;
83306
+ } else {
83307
+ const root = /^<([A-Za-z_][\w.:-]*)/.exec(xml.slice(cursor2));
83308
+ if (!root || root[1].split(":").at(-1) !== "qti-assessment-item") {
83309
+ return invalidQtiCopyXml();
83310
+ }
83311
+ const attributesStart = cursor2 + root[0].length;
83312
+ const end = markupEnd(xml, attributesStart, false);
83313
+ const attributes = qtiRootAttributes(xml, attributesStart, end);
83314
+ let insertionPoint = skipXmlWhitespaceBackward(xml, end);
83315
+ if (xml[insertionPoint - 1] === "/") {
83316
+ insertionPoint -= 1;
83317
+ }
83318
+ return { attributes, insertionPoint };
83319
+ }
83320
+ }
83321
+ return invalidQtiCopyXml();
83322
+ }
83323
+ function skipXmlWhitespaceBackward(xml, start2) {
83324
+ let cursor2 = start2;
83325
+ while (/\s/.test(xml[cursor2 - 1] ?? "")) {
83326
+ cursor2 -= 1;
83327
+ }
83328
+ return cursor2;
83329
+ }
83330
+ function escapeXmlAttribute(value, quote) {
83331
+ return value.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(quote, quote === '"' ? "&quot;" : "&apos;");
83332
+ }
83333
+ function rewriteQtiItemIdentity(xml, identifier, title) {
83334
+ if (!xml.trim() || !identifier.trim() || !title.trim()) {
83335
+ return invalidQtiCopyXml();
83336
+ }
83337
+ const root = qtiItemStartTag(xml);
83338
+ const replacements = [];
83339
+ const targetAttributes = new Map([
83340
+ ["identifier", identifier],
83341
+ ["title", title]
83342
+ ]);
83343
+ for (const [name3, value] of targetAttributes) {
83344
+ const matches = root.attributes.filter((attribute2) => attribute2.name === name3);
83345
+ if (matches.length > 1) {
83346
+ return invalidQtiCopyXml();
83347
+ }
83348
+ const attribute = matches[0];
83349
+ if (attribute) {
83350
+ replacements.push({
83351
+ start: attribute.valueStart,
83352
+ end: attribute.valueEnd,
83353
+ value: escapeXmlAttribute(value, attribute.quote)
83354
+ });
83355
+ } else {
83356
+ replacements.push({
83357
+ start: root.insertionPoint,
83358
+ end: root.insertionPoint,
83359
+ value: ` ${name3}="${escapeXmlAttribute(value, '"')}"`
83360
+ });
83361
+ }
83362
+ }
83363
+ return replacements.toSorted((left, right) => right.start - left.start).reduce((rewritten, replacement) => `${rewritten.slice(0, replacement.start)}${replacement.value}${rewritten.slice(replacement.end)}`, xml);
83364
+ }
83365
+ function buildQtiQuestionCopyInput(input) {
83366
+ assertSupportedQtiQuestionInteraction(input.source);
83367
+ const title = input.source.title?.trim() || input.source.identifier;
83368
+ if (!input.source.rawXml) {
83369
+ return invalidQtiCopyXml();
83370
+ }
83371
+ const sourceMetadata = { ...input.source.metadata };
83372
+ if (sourceMetadata.ownerSystem !== PLAYCADEMY_QTI_OWNER_SYSTEM) {
83373
+ Reflect.deleteProperty(sourceMetadata, PLAYCADEMY_QTI_AUTHORING_METADATA_KEY);
83374
+ }
83375
+ const metadata2 = buildOwnedQtiQuestionMetadata({
83376
+ metadata: {
83377
+ copiedFromItemIdentifier: input.source.identifier,
83378
+ integrationId: input.integrationId
83379
+ }
83380
+ }, {
83381
+ gameSlug: input.gameSlug,
83382
+ testIdentifier: input.targetTestIdentifier
83383
+ }, sourceMetadata);
83384
+ return {
83385
+ identifier: input.targetIdentifier,
83386
+ title,
83387
+ xml: rewriteQtiItemIdentity(input.source.rawXml, input.targetIdentifier, title),
83388
+ metadata: metadata2
83389
+ };
83390
+ }
83391
+ function assertQtiQuestionEditable(item, ownership, ownerTest) {
83392
+ if (!isQtiQuestionOwnedByContext(item, ownership, ownerTest)) {
83393
+ throw new ValidationError("Shared question references are read-only. Copy the question to create an editable independent item.");
83394
+ }
83395
+ }
83396
+ function buildNumericQuestionXml(input, identifier, title) {
83397
+ if (input.format === "xml") {
83398
+ throw new ValidationError("Raw question XML is not accepted at this API boundary");
83399
+ }
83400
+ const candidate = input.numericTextEntry;
83401
+ if (candidate === undefined) {
83402
+ return null;
83403
+ }
83404
+ if (!candidate || typeof candidate !== "object" || Array.isArray(candidate)) {
83405
+ throw new ValidationError("Numeric text-entry data is invalid");
83406
+ }
83407
+ const prompt = "prompt" in candidate ? candidate.prompt : undefined;
83408
+ const numeric3 = parseNumericTextEntry({
83409
+ baseType: "baseType" in candidate ? candidate.baseType : undefined,
83410
+ answer: "answer" in candidate ? candidate.answer : undefined,
83411
+ comparison: "comparison" in candidate ? candidate.comparison : undefined
83412
+ });
83413
+ if (!numeric3.success) {
83414
+ throw new ValidationError(numeric3.message);
83415
+ }
83416
+ if (!title || typeof prompt !== "string" || !prompt.trim()) {
83417
+ throw new ValidationError("Numeric questions require a title and prompt");
83418
+ }
83419
+ return numericTextEntryXml({ identifier, title, prompt: prompt.trim(), numeric: numeric3.value });
83420
+ }
83421
+ function buildExactMatchQuestionXml(input) {
83422
+ const responseIdentifier = escapeXml(input.responseIdentifier);
83423
+ const correctValues = input.correctIdentifiers.map((identifier) => ` <qti-value>${escapeXml(identifier)}</qti-value>`).join(`
83424
+ `);
83425
+ return `<?xml version="1.0" encoding="UTF-8"?>
83426
+ <qti-assessment-item xmlns="http://www.imsglobal.org/xsd/imsqtiasi_v3p0" identifier="${escapeXml(input.identifier)}" title="${escapeXml(input.title)}" adaptive="false" time-dependent="false">
83427
+ <qti-response-declaration identifier="${responseIdentifier}" cardinality="${input.cardinality}" base-type="identifier">
83428
+ <qti-correct-response>
83429
+ ${correctValues}
83430
+ </qti-correct-response>
83431
+ </qti-response-declaration>
83432
+ <qti-outcome-declaration identifier="FEEDBACK" cardinality="single" base-type="identifier" />
83433
+ <qti-outcome-declaration identifier="SCORE" cardinality="single" base-type="float">
83434
+ <qti-default-value><qti-value>0</qti-value></qti-default-value>
83435
+ </qti-outcome-declaration>
83436
+ <qti-item-body>
83437
+ ${input.itemBody}
83438
+ </qti-item-body>
83439
+ <qti-response-processing>
83440
+ <qti-response-condition>
83441
+ <qti-response-if>
83442
+ <qti-match><qti-variable identifier="${responseIdentifier}" /><qti-correct identifier="${responseIdentifier}" /></qti-match>
83443
+ <qti-set-outcome-value identifier="FEEDBACK"><qti-base-value base-type="identifier">CORRECT</qti-base-value></qti-set-outcome-value>
83444
+ <qti-set-outcome-value identifier="SCORE"><qti-base-value base-type="float">1</qti-base-value></qti-set-outcome-value>
83445
+ </qti-response-if>
83446
+ <qti-response-else>
83447
+ <qti-set-outcome-value identifier="FEEDBACK"><qti-base-value base-type="identifier">INCORRECT</qti-base-value></qti-set-outcome-value>
83448
+ <qti-set-outcome-value identifier="SCORE"><qti-base-value base-type="float">0</qti-base-value></qti-set-outcome-value>
83449
+ </qti-response-else>
83450
+ </qti-response-condition>
83451
+ </qti-response-processing>
83452
+ </qti-assessment-item>`;
83453
+ }
83454
+ function parseInlineChoices(value) {
83455
+ if (!Array.isArray(value) || value.length < 2) {
83456
+ throw new ValidationError("Inline-choice questions require at least two choices");
83457
+ }
83458
+ const choices = value.map((choice) => {
83459
+ const record3 = recordValue(choice);
83460
+ const identifier = record3?.identifier;
83461
+ const content = record3?.content;
83462
+ if (typeof identifier !== "string" || !identifier.trim() || typeof content !== "string" || !content.trim()) {
83463
+ throw new ValidationError("Inline-choice options are invalid");
83464
+ }
83465
+ return { identifier: identifier.trim(), content: content.trim() };
83466
+ });
83467
+ if (new Set(choices.map((choice) => choice.identifier)).size !== choices.length) {
83468
+ throw new ValidationError("Inline-choice option identifiers must be unique");
83469
+ }
83470
+ return choices;
83471
+ }
83472
+ function inlineChoiceCorrectIdentifier(input, responseIdentifier, choiceIdentifiers) {
83473
+ const declarations = Array.isArray(input.responseDeclarations) ? input.responseDeclarations : [];
83474
+ const declaration = declarations.map(recordValue).find((candidate) => candidate?.identifier === responseIdentifier);
83475
+ const correctResponse = recordValue(declaration?.correctResponse);
83476
+ const rawValues = correctResponse?.value;
83477
+ const values = Array.isArray(rawValues) ? rawValues.filter((value) => typeof value === "string") : [];
83478
+ if (declaration?.cardinality !== "single" || declaration.baseType !== "identifier") {
83479
+ throw new ValidationError("Inline-choice questions require a single-identifier response declaration");
83480
+ }
83481
+ if (values.length !== 1 || !choiceIdentifiers.has(values[0])) {
83482
+ throw new ValidationError("Inline-choice questions require one valid correct option");
83483
+ }
83484
+ return values[0];
83485
+ }
83486
+ function buildInlineChoiceQuestionXml(input, identifier, title) {
83487
+ const interaction = recordValue(input.interaction);
83488
+ const interactionType = normalizedQtiInteractionType(interaction?.type ?? input.type);
83489
+ if (interactionType !== "inline-choice") {
83490
+ return null;
83491
+ }
83492
+ const responseIdentifier = interaction?.responseIdentifier;
83493
+ const structure = recordValue(interaction?.questionStructure);
83494
+ const prompt = structure?.prompt;
83495
+ if (!structure || typeof responseIdentifier !== "string" || !responseIdentifier.trim()) {
83496
+ throw new ValidationError("Inline-choice question data is invalid");
83497
+ }
83498
+ if (!title || typeof prompt !== "string" || !prompt.trim()) {
83499
+ throw new ValidationError("Inline-choice questions require a title and prompt");
83500
+ }
83501
+ const normalizedPrompt = prompt.trim();
83502
+ const promptParts = normalizedPrompt.split(INLINE_CHOICE_BLANK);
83503
+ if (promptParts.length !== 2) {
83504
+ throw new ValidationError("Inline-choice questions require exactly one blank");
83505
+ }
83506
+ const choices = parseInlineChoices(structure.inlineChoices);
83507
+ const choiceIdentifiers = new Set(choices.map((choice) => choice.identifier));
83508
+ const correctIdentifier = inlineChoiceCorrectIdentifier(input, responseIdentifier, choiceIdentifiers);
83509
+ const choicesXml = choices.map((choice) => ` <qti-inline-choice identifier="${escapeXml(choice.identifier)}">${escapeXml(choice.content)}</qti-inline-choice>`).join(`
83510
+ `);
83511
+ const [before = "", after = ""] = promptParts;
83512
+ const itemBody = ` <p>${escapeXml(before)}<qti-inline-choice-interaction response-identifier="${escapeXml(responseIdentifier)}">
83513
+ ${choicesXml}
83514
+ </qti-inline-choice-interaction>${escapeXml(after)}</p>`;
83515
+ return buildExactMatchQuestionXml({
83516
+ identifier,
83517
+ title,
83518
+ responseIdentifier,
83519
+ cardinality: "single",
83520
+ correctIdentifiers: [correctIdentifier],
83521
+ itemBody
83522
+ });
83523
+ }
83524
+ function parseHottextSegments(value) {
83525
+ if (!Array.isArray(value) || value.length === 0) {
83526
+ throw new ValidationError("Hottext questions require passage segments");
83527
+ }
83528
+ const segments = value.map((segment) => {
83529
+ const record3 = recordValue(segment);
83530
+ const identifier = record3?.identifier;
83531
+ const content = record3?.content;
83532
+ const mode = record3?.mode;
83533
+ if (typeof identifier !== "string" || typeof content !== "string" || !content.trim() || !["plain", "option", "correct"].includes(String(mode))) {
83534
+ throw new ValidationError("Hottext passage segments are invalid");
83535
+ }
83536
+ return {
83537
+ identifier: identifier.trim(),
83538
+ content: content.trim(),
83539
+ mode
83540
+ };
83541
+ });
83542
+ const selectable = segments.filter((segment) => segment.mode !== "plain");
83543
+ const correct = selectable.filter((segment) => segment.mode === "correct");
83544
+ const identifiers = selectable.map((segment) => segment.identifier);
83545
+ if (selectable.length < 2) {
83546
+ throw new ValidationError("Hottext questions require at least two selectable phrases");
83547
+ }
83548
+ if (identifiers.some((identifier) => !identifier)) {
83549
+ throw new ValidationError("Hottext selectable phrases require identifiers");
83550
+ }
83551
+ if (new Set(identifiers).size !== identifiers.length) {
83552
+ throw new ValidationError("Hottext selectable phrase identifiers must be unique");
83553
+ }
83554
+ if (correct.length === 0) {
83555
+ throw new ValidationError("Hottext questions require a correct phrase");
83556
+ }
83557
+ return { segments, selectable, correct };
83558
+ }
83559
+ function buildHottextQuestionXml(input, identifier, title) {
83560
+ const candidate = input.hottext;
83561
+ if (candidate === undefined) {
83562
+ return null;
83563
+ }
83564
+ const hottext = recordValue(candidate);
83565
+ if (!hottext) {
83566
+ throw new ValidationError("Hottext question data is invalid");
83567
+ }
83568
+ const prompt = hottext.prompt;
83569
+ if (!title || typeof prompt !== "string" || !prompt.trim()) {
83570
+ throw new ValidationError("Hottext questions require a title and prompt");
83571
+ }
83572
+ const { segments, selectable, correct } = parseHottextSegments(hottext.segments);
83573
+ const multiple = correct.length > 1;
83574
+ const maxChoices = hottextMaxChoices(selectable.length, multiple);
83575
+ const passage = segments.map((segment) => segment.mode === "plain" ? escapeXml(segment.content) : `<qti-hottext identifier="${escapeXml(segment.identifier)}">${escapeXml(segment.content)}</qti-hottext>`).join(" ");
83576
+ const itemBody = ` <qti-hottext-interaction response-identifier="RESPONSE" max-choices="${maxChoices}">
83577
+ <qti-prompt>${escapeXml(prompt.trim())}</qti-prompt>
83578
+ <p>${passage}</p>
83579
+ </qti-hottext-interaction>`;
83580
+ return buildExactMatchQuestionXml({
83581
+ identifier,
83582
+ title,
83583
+ responseIdentifier: "RESPONSE",
83584
+ cardinality: multiple ? "multiple" : "single",
83585
+ correctIdentifiers: correct.map((segment) => segment.identifier),
83586
+ itemBody
83587
+ });
83588
+ }
83589
+ function buildQuestionXml(input, identifier, title) {
83590
+ for (const build2 of QUESTION_XML_BUILDERS) {
83591
+ const xml = build2(input, identifier, title);
83592
+ if (xml !== null) {
83593
+ return xml;
83594
+ }
83595
+ }
83596
+ return null;
83597
+ }
83598
+ function buildCreatedQtiQuestionReference(input) {
83599
+ return {
83600
+ ownership: "owned",
83601
+ reference: {
83602
+ identifier: input.itemIdentifier,
83603
+ href: input.href,
83604
+ testPart: input.partIdentifier,
83605
+ section: input.sectionIdentifier
83606
+ },
83607
+ question: mergeQtiQuestionItem(input.item, input.fallbackItem, input.itemIdentifier, input.metadata)
83608
+ };
83609
+ }
83610
+ function buildHydratedQtiQuestionReference(input) {
83611
+ const identifier = input.reference.reference.identifier;
83612
+ const ownerGameSlug = resolveQtiQuestionOwnerGameSlug(input.item, input.ownerTest);
83613
+ const authoritativeItem = ownerGameSlug ? {
83614
+ ...input.item,
83615
+ metadata: { ...input.item.metadata, ownerGameSlug }
83616
+ } : input.item;
83617
+ const question = mergeQtiQuestionItem(authoritativeItem, input.reference.question, identifier);
83618
+ return {
83619
+ ...input.reference,
83620
+ ownership: isQtiQuestionOwnedByContext(question, { gameSlug: input.gameSlug, testIdentifier: input.testIdentifier }, input.ownerTest) ? "owned" : "shared",
83621
+ question
83622
+ };
83623
+ }
83624
+ function isQtiTestOwnedByGame(test, gameSlug) {
83625
+ return test.metadata?.ownerSystem === PLAYCADEMY_QTI_OWNER_SYSTEM && test.metadata.ownerGameSlug === gameSlug;
83626
+ }
83627
+ function assertQtiTestOwnedByGame(test, gameSlug) {
83628
+ if (!isQtiTestOwnedByGame(test, gameSlug)) {
83629
+ throw new ValidationError("Shared assessment references are read-only. Copy the assessment to create an editable independent test.");
83630
+ }
83631
+ }
83632
+ function qtiTestParts(test) {
83633
+ const parts2 = test["qti-test-part"];
83634
+ if (!Array.isArray(parts2) || parts2.length === 0) {
83635
+ throw new ValidationError(`Assessment ${test.identifier} has no test parts`);
83636
+ }
83637
+ for (const [partIndex, part] of parts2.entries()) {
83638
+ if (!part || typeof part !== "object" || Array.isArray(part)) {
83639
+ throw new ValidationError(`Assessment ${test.identifier} contains an invalid test part at position ${partIndex + 1}`);
83640
+ }
83641
+ const sections = part["qti-assessment-section"];
83642
+ if (!Array.isArray(sections)) {
83643
+ throw new ValidationError(`Assessment ${test.identifier} test part ${partIndex + 1} has no section list`);
83644
+ }
83645
+ }
83646
+ return parts2;
83647
+ }
83648
+ function qtiTestOptionalAttributes(test) {
83649
+ return {
83650
+ ...test.qtiVersion ? { qtiVersion: test.qtiVersion } : {},
83651
+ ...test.timeLimit !== undefined ? { timeLimit: test.timeLimit } : {},
83652
+ ...test.maxAttempts !== undefined ? { maxAttempts: test.maxAttempts } : {},
83653
+ ...test.toolsEnabled !== undefined ? { toolsEnabled: test.toolsEnabled } : {}
83654
+ };
83655
+ }
83656
+ function buildQtiAssessmentItemCopyPlan(test, targetTestIdentifier, hrefForIdentifier, createIdentifier = newPlaycademyQuestionIdentifier) {
83657
+ const sourceIdentifiers = [];
83658
+ const seen = new Set;
83659
+ for (const part of qtiTestParts(test)) {
83660
+ for (const section of part["qti-assessment-section"]) {
83661
+ for (const reference of section["qti-assessment-item-ref"] ?? []) {
83662
+ if (!seen.has(reference.identifier)) {
83663
+ seen.add(reference.identifier);
83664
+ sourceIdentifiers.push(reference.identifier);
83665
+ }
83666
+ }
83667
+ }
83668
+ }
83669
+ return sourceIdentifiers.map((sourceIdentifier) => {
83670
+ const targetIdentifier = createIdentifier(targetTestIdentifier);
83671
+ return {
83672
+ sourceIdentifier,
83673
+ targetIdentifier,
83674
+ href: hrefForIdentifier(targetIdentifier)
83675
+ };
83676
+ });
83677
+ }
83678
+ function buildQtiTestStructureInput(test, targetTestIdentifier, itemCopies) {
83679
+ const outcomeDeclarations = test["qti-outcome-declaration"];
83680
+ if (outcomeDeclarations !== undefined && !Array.isArray(outcomeDeclarations)) {
83681
+ throw new ValidationError(`Assessment ${test.identifier} has an invalid outcome declaration list`);
83682
+ }
83683
+ return {
83684
+ "qti-test-part": qtiTestParts(test).map((part, partIndex) => ({
83685
+ identifier: targetTestIdentifier ? `${targetTestIdentifier}-part${partIndex + 1}` : part.identifier,
83686
+ navigationMode: part.navigationMode,
83687
+ submissionMode: part.submissionMode,
83688
+ "qti-assessment-section": part["qti-assessment-section"].map((section, sectionIndex) => {
83689
+ let sectionIdentifier = section.identifier;
83690
+ if (targetTestIdentifier) {
83691
+ sectionIdentifier = partIndex === 0 && sectionIndex === 0 ? `${targetTestIdentifier}-section1` : `${targetTestIdentifier}-part${partIndex + 1}-section${sectionIndex + 1}`;
83692
+ }
83693
+ return {
83694
+ identifier: sectionIdentifier,
83695
+ title: section.title,
83696
+ visible: section.visible ?? true,
83697
+ ...section.required !== undefined ? { required: section.required } : {},
83698
+ ...section.fixed !== undefined ? { fixed: section.fixed } : {},
83699
+ sequence: section.sequence ?? sectionIndex + 1,
83700
+ ...section["qti-assessment-item-ref"] ? {
83701
+ "qti-assessment-item-ref": section["qti-assessment-item-ref"].map((item, itemIndex) => {
83702
+ const copy = itemCopies?.get(item.identifier);
83703
+ if (itemCopies && !copy) {
83704
+ throw new ValidationError(`Assessment copy is missing question ${item.identifier}`);
83705
+ }
83706
+ return {
83707
+ identifier: copy?.targetIdentifier ?? item.identifier,
83708
+ href: copy?.href ?? item.href,
83709
+ sequence: item.sequence ?? itemIndex + 1
83710
+ };
83711
+ })
83712
+ } : {}
83713
+ };
83714
+ })
83715
+ })),
83716
+ ...outcomeDeclarations ? {
83717
+ "qti-outcome-declaration": outcomeDeclarations.map((declaration) => ({
83718
+ identifier: declaration.identifier,
83719
+ ...declaration.cardinality !== undefined ? { cardinality: declaration.cardinality } : {},
83720
+ baseType: declaration.baseType,
83721
+ ...declaration.normalMaximum !== undefined ? { normalMaximum: declaration.normalMaximum } : {},
83722
+ ...declaration.normalMinimum !== undefined ? { normalMinimum: declaration.normalMinimum } : {},
83723
+ ...declaration.defaultValue ? {
83724
+ defaultValue: declaration.defaultValue.value !== undefined ? { value: declaration.defaultValue.value } : {}
83725
+ } : {}
83726
+ }))
83727
+ } : {}
83728
+ };
83729
+ }
83730
+ function buildQtiTestUpdateInput(test, title) {
83731
+ return {
83732
+ title,
83733
+ ...qtiTestOptionalAttributes(test),
83734
+ ...test.metadata ? { metadata: test.metadata } : {},
83735
+ ...buildQtiTestStructureInput(test)
83736
+ };
83737
+ }
83738
+ function buildQtiTestCopyInput(source, targetTestIdentifier, metadata2, itemCopies) {
83739
+ const copiesBySourceIdentifier = new Map(itemCopies.map((copy) => [copy.sourceIdentifier, copy]));
83740
+ return {
83741
+ identifier: targetTestIdentifier,
83742
+ title: `${source.title} (copy)`,
83743
+ ...qtiTestOptionalAttributes(source),
83744
+ metadata: metadata2,
83745
+ ...buildQtiTestStructureInput(source, targetTestIdentifier, copiesBySourceIdentifier)
83746
+ };
83747
+ }
83748
+ function validateUniqueQuestionIdentifiers(itemIdentifiers) {
83749
+ if (itemIdentifiers.length === 0 || new Set(itemIdentifiers).size !== itemIdentifiers.length) {
83750
+ throw new ValidationError("Question order must contain unique question identifiers");
83751
+ }
83752
+ }
83753
+ function resolveQtiQuestionSection(test, qtiTestIdentifier, itemIdentifier, expectedItemIdentifiers) {
83754
+ let selectedPart;
83755
+ let selectedSection;
83756
+ for (const part of qtiTestParts(test)) {
83757
+ for (const section of part["qti-assessment-section"]) {
83758
+ if (!itemIdentifier || section["qti-assessment-item-ref"]?.some((item) => item.identifier === itemIdentifier)) {
83759
+ selectedPart = part;
83760
+ selectedSection = section;
83761
+ break;
83762
+ }
83763
+ }
83764
+ if (selectedSection) {
83765
+ break;
83766
+ }
83767
+ }
83768
+ if (!selectedPart || !selectedSection) {
83769
+ throw new ValidationError(itemIdentifier ? `Question ${itemIdentifier} is not referenced by assessment ${qtiTestIdentifier}` : `Assessment ${qtiTestIdentifier} has no section for questions`);
83770
+ }
83771
+ if (expectedItemIdentifiers) {
83772
+ const sectionIdentifiers = selectedSection["qti-assessment-item-ref"]?.map((item) => item.identifier) ?? [];
83773
+ const expectedIdentifiers = new Set(expectedItemIdentifiers);
83774
+ if (expectedIdentifiers.size !== sectionIdentifiers.length || sectionIdentifiers.some((identifier) => !expectedIdentifiers.has(identifier))) {
83775
+ throw new ValidationError("Questions can only be reordered within one complete assessment section");
83776
+ }
83777
+ }
83778
+ return {
83779
+ partIdentifier: selectedPart.identifier,
83780
+ sectionIdentifier: selectedSection.identifier
83781
+ };
83782
+ }
83783
+ var PLAYCADEMY_QTI_AUTHORING_METADATA_KEY = "playcademyAuthoring", QTI_AUTHORING_FIELDS, QUESTION_XML_BUILDERS;
83784
+ var init_timeback_qti_authoring_util = __esm(() => {
83785
+ init_timeback3();
83786
+ init_errors();
83787
+ QTI_AUTHORING_FIELDS = [
83788
+ "type",
83789
+ "qtiVersion",
83790
+ "timeDependent",
83791
+ "adaptive",
83792
+ "preInteraction",
83793
+ "interaction",
83794
+ "postInteraction",
83795
+ "responseDeclarations",
83796
+ "outcomeDeclarations",
83797
+ "responseProcessing",
83798
+ "modalFeedback",
83799
+ "feedbackInline",
83800
+ "feedbackBlock",
83801
+ "rubrics",
83802
+ "stimulus",
83803
+ "content"
83804
+ ];
83805
+ QUESTION_XML_BUILDERS = [
83806
+ buildNumericQuestionXml,
83807
+ buildHottextQuestionXml,
83808
+ buildInlineChoiceQuestionXml
83809
+ ];
83810
+ });
83811
+
83812
+ // ../api-core/src/utils/timeback-qti-library.util.ts
83813
+ function newPlaycademyTestIdentifier() {
83814
+ return `${PLAYCADEMY_QTI_SOURCE_PREFIX}${crypto.randomUUID()}`;
83815
+ }
83816
+ function playcademySourceWhere() {
83817
+ return {
83818
+ identifier: {
83819
+ gte: PLAYCADEMY_QTI_SOURCE_PREFIX,
83820
+ lt: PLAYCADEMY_QTI_SOURCE_UPPER_BOUND
83821
+ }
83822
+ };
83823
+ }
83824
+ function buildQtiLibraryListPlan(params) {
83825
+ const query = params.query?.trim() || undefined;
83826
+ if (!params.source && !query) {
83827
+ throw new Error("An exact QTI source identifier is required for a global lookup");
83828
+ }
83829
+ let where;
83830
+ if (params.source) {
83831
+ where = playcademySourceWhere();
83832
+ } else if (query) {
83833
+ where = { identifier: query };
83834
+ }
83835
+ return {
83836
+ kind: "direct",
83837
+ params: {
83838
+ ...where ? { where } : {},
83839
+ ...params.source && query ? { query } : {},
83840
+ page: params.page,
83841
+ limit: params.limit,
83842
+ ...params.source ? { sort: "identifier", order: "asc" } : {}
83843
+ }
83844
+ };
83845
+ }
83846
+ var PLAYCADEMY_QTI_SOURCE_PREFIX = "playcademy-test-", PLAYCADEMY_QTI_SOURCE_UPPER_BOUND = "playcademy-test.";
83847
+
82976
83848
  // ../api-core/src/services/timeback-assessments.service.ts
82977
83849
  class TimebackAssessmentsService {
83850
+ static QTI_HYDRATION_CONCURRENCY = 8;
82978
83851
  deps;
82979
83852
  constructor(deps) {
82980
83853
  this.deps = deps;
@@ -82991,33 +83864,19 @@ class TimebackAssessmentsService {
82991
83864
  }
82992
83865
  async listAssessments(integrationId) {
82993
83866
  const client = this.requireClient();
82994
- await this.requireIntegration(integrationId);
83867
+ const ownership = await this.requireQtiTestOwnershipContext(integrationId);
82995
83868
  const rows = await this.deps.db.query.gameTimebackAssessmentTests.findMany({
82996
- where: eq(gameTimebackAssessmentTests.integrationId, integrationId),
82997
- orderBy: asc(gameTimebackAssessmentTests.sortOrder)
83869
+ where: eq(gameTimebackAssessmentTests.integrationId, integrationId)
82998
83870
  });
82999
- if (rows.length === 0) {
83000
- return [];
83001
- }
83002
- const assessments = await Promise.all(rows.map(async (row) => {
83871
+ const assessments = await runWithConcurrency(rows, TimebackAssessmentsService.QTI_HYDRATION_CONCURRENCY, async (row) => {
83003
83872
  try {
83004
83873
  const test = await client.qtiApi.assessmentTests.get(row.qtiTestIdentifier);
83005
- let itemCount = 0;
83006
- for (const part of test["qti-test-part"] ?? []) {
83007
- for (const section of part["qti-assessment-section"] ?? []) {
83008
- itemCount += section["qti-assessment-item-ref"]?.length ?? 0;
83009
- }
83010
- }
83011
83874
  return {
83012
- id: row.id,
83013
- integrationId: row.integrationId,
83014
- qtiTestIdentifier: row.qtiTestIdentifier,
83015
- bankResourceId: row.bankResourceId,
83016
- bankActive: row.bankActive,
83017
- sortOrder: row.sortOrder,
83875
+ ...this.associationSummary(row),
83018
83876
  title: test.title,
83019
- questionCount: itemCount,
83020
- isActive: row.bankActive
83877
+ questionCount: countQtiTestItems(test),
83878
+ available: true,
83879
+ editable: isQtiTestOwnedByGame(test, ownership.gameSlug)
83021
83880
  };
83022
83881
  } catch (error88) {
83023
83882
  addEvent("assessment.qti_fetch_failed", {
@@ -83026,319 +83885,420 @@ class TimebackAssessmentsService {
83026
83885
  "app.error.message": errorMessage(error88)
83027
83886
  });
83028
83887
  return {
83029
- id: row.id,
83030
- integrationId: row.integrationId,
83031
- qtiTestIdentifier: row.qtiTestIdentifier,
83032
- bankResourceId: row.bankResourceId,
83033
- bankActive: row.bankActive,
83034
- sortOrder: row.sortOrder,
83888
+ ...this.associationSummary(row),
83035
83889
  title: row.qtiTestIdentifier,
83036
83890
  questionCount: 0,
83037
- isActive: row.bankActive
83891
+ available: false,
83892
+ editable: false
83038
83893
  };
83039
83894
  }
83040
- }));
83041
- return assessments;
83895
+ });
83896
+ return assessments.toSorted((a, b) => a.title.localeCompare(b.title));
83042
83897
  }
83043
83898
  async createAssessment(integrationId, input) {
83044
83899
  const client = this.requireClient();
83045
- const integration = await this.requireIntegration(integrationId);
83046
- await client.course.ensureAssessmentBank({
83047
- courseId: integration.courseId,
83048
- subject: integration.subject,
83049
- grade: integration.grade
83050
- });
83900
+ const ownership = await this.requireQtiTestOwnershipContext(integrationId);
83901
+ const { integration } = ownership;
83051
83902
  await client.course.createAssessmentTest({
83052
83903
  identifier: input.qtiTestIdentifier,
83053
83904
  title: input.title,
83054
83905
  metadata: {
83906
+ ownerSystem: PLAYCADEMY_QTI_OWNER_SYSTEM,
83907
+ ownerGameSlug: ownership.gameSlug,
83055
83908
  integrationId,
83056
83909
  subject: integration.subject,
83057
83910
  grade: String(integration.grade)
83058
83911
  }
83059
83912
  });
83060
- const maxSortOrder = await this.getMaxSortOrder(integrationId);
83061
- const [row] = await this.deps.db.insert(gameTimebackAssessmentTests).values({
83062
- integrationId,
83063
- qtiTestIdentifier: input.qtiTestIdentifier,
83064
- sortOrder: maxSortOrder + 1
83065
- }).returning();
83066
- setAttribute("app.assessment.operation", "create");
83067
- return row;
83068
- }
83069
- async deleteAssessment(integrationId, qtiTestIdentifier) {
83070
- const client = this.requireClient();
83071
- const row = await this.requireAssessmentRow(integrationId, qtiTestIdentifier);
83072
- if (row.bankActive) {
83073
- await this.deactivateAssessment(integrationId, qtiTestIdentifier);
83074
- }
83075
83913
  try {
83076
- await client.course.teardownAssessmentTest(qtiTestIdentifier);
83914
+ const [row] = await this.deps.db.insert(gameTimebackAssessmentTests).values({
83915
+ integrationId,
83916
+ qtiTestIdentifier: input.qtiTestIdentifier,
83917
+ purpose: input.purpose,
83918
+ status: "draft"
83919
+ }).returning();
83920
+ setAttribute("app.assessment.operation", "create");
83921
+ return row;
83077
83922
  } catch (error88) {
83078
- addEvent("assessment.qti_cleanup_partial", {
83079
- "app.assessment.qti_test_identifier": qtiTestIdentifier,
83080
- "exception.type": errorType(error88),
83081
- "app.error.message": errorMessage(error88)
83082
- });
83923
+ let committedRow;
83924
+ try {
83925
+ committedRow = await this.deps.db.query.gameTimebackAssessmentTests.findFirst({
83926
+ where: and(eq(gameTimebackAssessmentTests.integrationId, integrationId), eq(gameTimebackAssessmentTests.qtiTestIdentifier, input.qtiTestIdentifier))
83927
+ });
83928
+ } catch (verificationError) {
83929
+ addEvent("assessment.qti_test_create_commit_verification_failed", {
83930
+ "app.assessment.qti_test_identifier": input.qtiTestIdentifier,
83931
+ "exception.type": errorType(verificationError),
83932
+ "app.error.message": errorMessage(verificationError)
83933
+ });
83934
+ throw error88;
83935
+ }
83936
+ if (committedRow) {
83937
+ setAttribute("app.assessment.operation", "create");
83938
+ return committedRow;
83939
+ }
83940
+ try {
83941
+ await client.qtiApi.assessmentTests.delete(input.qtiTestIdentifier);
83942
+ } catch (cleanupError) {
83943
+ if (!isApiError(cleanupError) || cleanupError.statusCode !== 404) {
83944
+ addEvent("assessment.qti_test_create_cleanup_failed", {
83945
+ "app.assessment.qti_test_identifier": input.qtiTestIdentifier,
83946
+ "exception.type": errorType(cleanupError),
83947
+ "app.error.message": errorMessage(cleanupError)
83948
+ });
83949
+ }
83950
+ }
83951
+ throw error88;
83083
83952
  }
83084
- await this.deps.db.delete(gameTimebackAssessmentTests).where(eq(gameTimebackAssessmentTests.id, row.id));
83085
- setAttribute("app.assessment.operation", "delete");
83086
83953
  }
83087
- async reorderAssessments(integrationId, identifiers) {
83088
- await this.requireIntegration(integrationId);
83089
- await Promise.all(identifiers.map((identifier, i2) => this.deps.db.update(gameTimebackAssessmentTests).set({ sortOrder: i2 }).where(and(eq(gameTimebackAssessmentTests.integrationId, integrationId), eq(gameTimebackAssessmentTests.qtiTestIdentifier, identifier)))));
83090
- }
83091
- async listQuestions(integrationId, qtiTestIdentifier) {
83092
- const client = this.requireClient();
83093
- await this.requireAssessmentRow(integrationId, qtiTestIdentifier);
83094
- return await client.qtiApi.assessmentTests.getQuestions(qtiTestIdentifier);
83095
- }
83096
- async createQuestion(integrationId, qtiTestIdentifier, input) {
83097
- const client = this.requireClient();
83098
- await this.requireAssessmentRow(integrationId, qtiTestIdentifier);
83099
- const itemIdentifier = input.identifier;
83100
- if (typeof itemIdentifier !== "string" || !itemIdentifier) {
83101
- throw new ValidationError("Question identifier is required");
83102
- }
83103
- const item = await client.qtiApi.assessmentItems.create(input);
83104
- await this.qtiSectionItems(client, qtiTestIdentifier).add({
83105
- identifier: itemIdentifier,
83106
- href: `${client.getQtiBaseUrl()}/assessment-items/${itemIdentifier}`
83954
+ async updateAssessment(integrationId, qtiTestIdentifier, input) {
83955
+ return this.withLockedAssessmentAssociations(integrationId, qtiTestIdentifier, async (row, tx, associations) => {
83956
+ const updates = buildAssessmentAssociationUpdates(row, input);
83957
+ if (input.status !== undefined) {
83958
+ validateAssessmentStatusTransition(row.status, input.status);
83959
+ if (isAssessmentPublicationTransition(row.status, input.status)) {
83960
+ await this.validateAssessmentHasQuestions(qtiTestIdentifier);
83961
+ }
83962
+ }
83963
+ if (input.title !== undefined) {
83964
+ assertDraftAssessment(row);
83965
+ assertAllAssessmentAssociationsDraft(associations);
83966
+ const client = this.requireClient();
83967
+ const test = await client.qtiApi.assessmentTests.get(qtiTestIdentifier);
83968
+ const ownership = await this.requireQtiTestOwnershipContext(integrationId, tx);
83969
+ assertQtiTestOwnedByGame(test, ownership.gameSlug);
83970
+ await client.qtiApi.assessmentTests.update(qtiTestIdentifier, buildQtiTestUpdateInput(test, input.title));
83971
+ }
83972
+ let updated = row;
83973
+ if (Object.keys(updates).length > 0) {
83974
+ const [updatedRow] = await tx.update(gameTimebackAssessmentTests).set({ ...updates, updatedAt: new Date }).where(eq(gameTimebackAssessmentTests.id, row.id)).returning();
83975
+ updated = updatedRow ?? row;
83976
+ }
83977
+ setAttribute("app.assessment.operation", input.title !== undefined ? "update_test" : "update_association");
83978
+ return updated;
83107
83979
  });
83108
- return item;
83109
83980
  }
83110
- async updateQuestion(integrationId, qtiTestIdentifier, itemIdentifier, input) {
83111
- const client = this.requireClient();
83112
- await this.requireAssessmentRow(integrationId, qtiTestIdentifier);
83113
- return client.qtiApi.assessmentItems.update(itemIdentifier, input);
83114
- }
83115
- async deleteQuestion(integrationId, qtiTestIdentifier, itemIdentifier) {
83116
- const client = this.requireClient();
83117
- await this.requireAssessmentRow(integrationId, qtiTestIdentifier);
83118
- await this.qtiSectionItems(client, qtiTestIdentifier).remove(itemIdentifier);
83119
- await client.qtiApi.assessmentItems.delete(itemIdentifier);
83981
+ async removeAssessment(integrationId, qtiTestIdentifier) {
83982
+ return this.withLockedAssessmentAssociations(integrationId, qtiTestIdentifier, async (row, tx) => {
83983
+ const plan = planAssessmentRemoval(row.status);
83984
+ if (plan.kind === "delete") {
83985
+ await tx.delete(gameTimebackAssessmentTests).where(eq(gameTimebackAssessmentTests.id, row.id));
83986
+ } else if (plan.kind === "archive") {
83987
+ await tx.update(gameTimebackAssessmentTests).set({ status: "archived", sortOrder: null, updatedAt: new Date }).where(eq(gameTimebackAssessmentTests.id, row.id));
83988
+ }
83989
+ if (plan.kind !== "none") {
83990
+ setAttribute("app.assessment.operation", plan.operation);
83991
+ }
83992
+ return { action: plan.action };
83993
+ });
83120
83994
  }
83121
- async reorderQuestions(integrationId, qtiTestIdentifier, items) {
83122
- const client = this.requireClient();
83123
- await this.requireAssessmentRow(integrationId, qtiTestIdentifier);
83124
- const qtiBaseUrl = client.getQtiBaseUrl();
83125
- return this.qtiSectionItems(client, qtiTestIdentifier).reorder({
83126
- items: items.map((item) => ({
83127
- identifier: item.identifier,
83128
- href: item.href ?? `${qtiBaseUrl}/assessment-items/${item.identifier}`,
83129
- sequence: item.sequence
83130
- }))
83995
+ async reorderAssessments(integrationId, purpose, testIdentifiers) {
83996
+ await this.requireIntegration(integrationId);
83997
+ validateUniqueAssessmentIdentifiers(testIdentifiers);
83998
+ const updatedAt = new Date;
83999
+ await this.deps.db.transaction(async (tx) => {
84000
+ const liveRows = await tx.query.gameTimebackAssessmentTests.findMany({
84001
+ where: and(eq(gameTimebackAssessmentTests.integrationId, integrationId), eq(gameTimebackAssessmentTests.purpose, purpose), eq(gameTimebackAssessmentTests.status, "live"))
84002
+ });
84003
+ const orderedRows = orderLiveAssessmentRows(liveRows, purpose, testIdentifiers);
84004
+ const positionByRowId = new Map(orderedRows.map((row, index2) => [row.id, index2 + 1]));
84005
+ for (const row of lockOrderAssessmentRows(orderedRows)) {
84006
+ const testIdentifier = row.qtiTestIdentifier;
84007
+ const [updatedRow] = await tx.update(gameTimebackAssessmentTests).set({ sortOrder: positionByRowId.get(row.id), updatedAt }).where(and(eq(gameTimebackAssessmentTests.id, row.id), eq(gameTimebackAssessmentTests.integrationId, integrationId), eq(gameTimebackAssessmentTests.qtiTestIdentifier, testIdentifier), eq(gameTimebackAssessmentTests.purpose, purpose), eq(gameTimebackAssessmentTests.status, "live"))).returning({ id: gameTimebackAssessmentTests.id });
84008
+ assertAssessmentOrderUpdateSucceeded(updatedRow);
84009
+ }
83131
84010
  });
84011
+ setAttribute("app.assessment.operation", "reorder_live_assessments");
83132
84012
  }
83133
- async activateAssessment(integrationId, qtiTestIdentifier) {
84013
+ async listQuestions(integrationId, qtiTestIdentifier) {
83134
84014
  const client = this.requireClient();
83135
- const integration = await this.requireIntegration(integrationId);
83136
- const row = await this.requireAssessmentRow(integrationId, qtiTestIdentifier);
83137
- if (row.bankActive) {
83138
- throw new ValidationError("Assessment is already active");
83139
- }
83140
- const bankIds = deriveAssessmentBankIds2(integration.courseId);
83141
- const qtiTestUrl = `${client.getQtiBaseUrl()}/assessment-tests/${qtiTestIdentifier}`;
83142
- await client.course.ensureAssessmentBank({
83143
- courseId: integration.courseId,
83144
- subject: integration.subject,
83145
- grade: integration.grade
83146
- });
83147
- const parentResource = await client.api.oneroster.resources.get(bankIds.resource);
83148
- const currentResources = parentResource.metadata?.resources ?? [];
83149
- let childResourceId = row.bankResourceId;
83150
- if (childResourceId) {
84015
+ await this.requireAssessmentRow(integrationId, qtiTestIdentifier);
84016
+ const { gameSlug } = await this.requireQtiTestOwnershipContext(integrationId);
84017
+ const result = await client.qtiApi.assessmentTests.getQuestions(qtiTestIdentifier);
84018
+ const ownerTests = new Map;
84019
+ const questions = await runWithConcurrency(result.questions, TimebackAssessmentsService.QTI_HYDRATION_CONCURRENCY, async (reference) => {
84020
+ let item;
83151
84021
  try {
83152
- await client.api.oneroster.resources.update(childResourceId, {
83153
- status: "active",
83154
- title: `Assessment Bank Test: ${qtiTestIdentifier}`,
83155
- vendorResourceId: "",
83156
- metadata: {
83157
- type: "qti",
83158
- subType: "qti-test",
83159
- url: qtiTestUrl
83160
- }
83161
- });
83162
- if (!currentResources.includes(childResourceId)) {
83163
- await client.api.oneroster.resources.update(bankIds.resource, {
83164
- status: "active",
83165
- title: parentResource.title,
83166
- vendorResourceId: parentResource.vendorResourceId,
83167
- vendorId: parentResource.vendorId,
83168
- metadata: {
83169
- ...parentResource.metadata,
83170
- resources: [...currentResources, childResourceId]
83171
- }
83172
- });
83173
- }
84022
+ item = await client.qtiApi.assessmentItems.get(reference.reference.identifier);
83174
84023
  } catch (error88) {
83175
- addEvent("assessment.child_resource_reactivation_failed", {
83176
- "app.assessment.child_resource_id": childResourceId ?? "",
84024
+ addEvent("assessment.qti_question_hydration_failed", {
84025
+ "app.assessment.qti_test_identifier": qtiTestIdentifier,
84026
+ "app.assessment.qti_item_identifier": reference.reference.identifier,
83177
84027
  "exception.type": errorType(error88),
83178
84028
  "app.error.message": errorMessage(error88)
83179
84029
  });
83180
- childResourceId = null;
84030
+ return reference;
83181
84031
  }
83182
- }
83183
- if (!childResourceId) {
83184
- for (const resourceId of currentResources) {
83185
- try {
83186
- const resource = await client.api.oneroster.resources.get(resourceId);
83187
- const resourceUrl = resource.metadata?.url;
83188
- if (resourceUrl === qtiTestUrl) {
83189
- childResourceId = resourceId;
83190
- addEvent("assessment.child_resource_reused", {
83191
- "app.assessment.qti_test_identifier": qtiTestIdentifier,
83192
- "app.assessment.child_resource_id": resourceId
83193
- });
83194
- break;
83195
- }
83196
- } catch {}
83197
- }
83198
- if (!childResourceId) {
83199
- const childResult = await client.api.oneroster.resources.create({
83200
- status: "active",
83201
- title: `Assessment Bank Test: ${qtiTestIdentifier}`,
83202
- vendorResourceId: "",
83203
- vendorId: undefined,
83204
- metadata: {
83205
- type: "qti",
83206
- subType: "qti-test",
83207
- url: qtiTestUrl
83208
- }
83209
- });
83210
- childResourceId = childResult.sourcedIdPairs.allocatedSourcedId;
83211
- await client.api.oneroster.resources.update(bankIds.resource, {
83212
- status: "active",
83213
- title: parentResource.title,
83214
- vendorResourceId: parentResource.vendorResourceId,
83215
- vendorId: parentResource.vendorId,
83216
- metadata: {
83217
- ...parentResource.metadata,
83218
- resources: [...currentResources, childResourceId]
83219
- }
84032
+ let ownerTest;
84033
+ try {
84034
+ ownerTest = await this.qtiQuestionOwnerTest(client, item, ownerTests);
84035
+ } catch (error88) {
84036
+ addEvent("assessment.qti_question_owner_test_fetch_failed", {
84037
+ "app.assessment.qti_test_identifier": qtiTestIdentifier,
84038
+ "app.assessment.qti_item_identifier": reference.reference.identifier,
84039
+ "exception.type": errorType(error88),
84040
+ "app.error.message": errorMessage(error88)
83220
84041
  });
83221
84042
  }
83222
- }
83223
- await this.deps.db.update(gameTimebackAssessmentTests).set({ bankResourceId: childResourceId, bankActive: true }).where(eq(gameTimebackAssessmentTests.id, row.id));
83224
- setAttributes({
83225
- "app.assessment.operation": "activate",
83226
- "app.assessment.child_resource_id": childResourceId ?? ""
84043
+ return buildHydratedQtiQuestionReference({
84044
+ reference,
84045
+ item,
84046
+ gameSlug,
84047
+ ownerTest,
84048
+ testIdentifier: qtiTestIdentifier
84049
+ });
83227
84050
  });
84051
+ return { ...result, questions };
83228
84052
  }
83229
- async deactivateAssessment(integrationId, qtiTestIdentifier) {
84053
+ async listQuestionLibrary(integrationId, params) {
83230
84054
  const client = this.requireClient();
83231
- const integration = await this.requireIntegration(integrationId);
83232
- const row = await this.requireAssessmentRow(integrationId, qtiTestIdentifier);
83233
- if (!row.bankActive) {
83234
- throw new ValidationError("Assessment is already in draft");
83235
- }
83236
- if (!row.bankResourceId) {
83237
- throw new ValidationError("Assessment has no bank resource — activate it first");
83238
- }
83239
- const childResourceId = row.bankResourceId;
83240
- const bankIds = deriveAssessmentBankIds2(integration.courseId);
83241
- try {
83242
- const parentResource = await client.api.oneroster.resources.get(bankIds.resource);
83243
- const currentResources = parentResource.metadata?.resources ?? [];
83244
- const filtered = currentResources.filter((id) => id !== childResourceId);
83245
- if (filtered.length !== currentResources.length) {
83246
- await client.api.oneroster.resources.update(bankIds.resource, {
83247
- status: "active",
83248
- title: parentResource.title,
83249
- vendorResourceId: parentResource.vendorResourceId,
83250
- vendorId: parentResource.vendorId,
83251
- metadata: {
83252
- ...parentResource.metadata,
83253
- resources: filtered
83254
- }
83255
- });
83256
- }
83257
- } catch (error88) {
83258
- addEvent("assessment.parent_resource_update_failed", {
83259
- "app.assessment.bank_resource_id": bankIds.resource,
83260
- "exception.type": errorType(error88),
83261
- "app.error.message": errorMessage(error88)
83262
- });
83263
- }
83264
- try {
83265
- await client.api.oneroster.resources.delete(childResourceId);
83266
- } catch (error88) {
83267
- addEvent("assessment.child_resource_delete_failed", {
83268
- "app.assessment.child_resource_id": childResourceId,
83269
- "exception.type": errorType(error88),
83270
- "app.error.message": errorMessage(error88)
83271
- });
83272
- }
83273
- await this.deps.db.update(gameTimebackAssessmentTests).set({ bankActive: false }).where(eq(gameTimebackAssessmentTests.id, row.id));
83274
- setAttribute("app.assessment.operation", "deactivate");
83275
- }
83276
- isAssessmentActive(row) {
83277
- return row.bankActive;
84055
+ await this.requireIntegration(integrationId);
84056
+ return this.listQtiLibrary(params, async (listParams) => await client.qtiApi.assessmentItems.list(listParams));
83278
84057
  }
83279
- async getBankStatus(integrationId) {
83280
- const integration = await this.requireIntegration(integrationId);
83281
- const bankIds = deriveAssessmentBankIds2(integration.courseId);
83282
- const rows = await this.deps.db.query.gameTimebackAssessmentTests.findMany({
83283
- where: eq(gameTimebackAssessmentTests.integrationId, integrationId)
83284
- });
83285
- const activeCount = rows.filter((r) => r.bankActive).length;
83286
- return {
83287
- bankIds,
83288
- totalAssessments: rows.length,
83289
- activeAssessments: activeCount,
83290
- draftAssessments: rows.length - activeCount
83291
- };
84058
+ async listTestLibrary(integrationId, params) {
84059
+ const client = this.requireClient();
84060
+ await this.requireIntegration(integrationId);
84061
+ return this.listQtiLibrary(params, async (listParams) => await client.qtiApi.assessmentTests.list(listParams));
83292
84062
  }
83293
- async destroyBank(integrationId) {
84063
+ async copyAssessment(integrationId, sourceTestIdentifier, targetTestIdentifier, purpose) {
83294
84064
  const client = this.requireClient();
83295
- const integration = await this.requireIntegration(integrationId);
83296
- const bankIds = deriveAssessmentBankIds2(integration.courseId);
83297
- const activeRows = await this.deps.db.query.gameTimebackAssessmentTests.findMany({
83298
- where: and(eq(gameTimebackAssessmentTests.integrationId, integrationId))
84065
+ const ownership = await this.requireQtiTestOwnershipContext(integrationId);
84066
+ const { integration } = ownership;
84067
+ const source = await client.qtiApi.assessmentTests.get(sourceTestIdentifier);
84068
+ const itemPlan = buildQtiAssessmentItemCopyPlan(source, targetTestIdentifier, (identifier) => this.qtiItemHref(client, identifier));
84069
+ const itemCopies = await runWithConcurrency(itemPlan, TimebackAssessmentsService.QTI_HYDRATION_CONCURRENCY, async (plan) => {
84070
+ const sourceItem = await client.qtiApi.assessmentItems.get(plan.sourceIdentifier);
84071
+ return {
84072
+ plan,
84073
+ input: buildQtiQuestionCopyInput({
84074
+ source: sourceItem,
84075
+ targetIdentifier: plan.targetIdentifier,
84076
+ targetTestIdentifier,
84077
+ gameSlug: ownership.gameSlug,
84078
+ integrationId
84079
+ })
84080
+ };
83299
84081
  });
84082
+ const testInput = buildQtiTestCopyInput(source, targetTestIdentifier, {
84083
+ ...source.metadata,
84084
+ ownerSystem: PLAYCADEMY_QTI_OWNER_SYSTEM,
84085
+ ownerGameSlug: ownership.gameSlug,
84086
+ integrationId,
84087
+ subject: integration.subject,
84088
+ grade: String(integration.grade),
84089
+ copiedFromTestIdentifier: sourceTestIdentifier
84090
+ }, itemPlan);
84091
+ const attemptedItemIdentifiers = [];
84092
+ let testCreationAttempted = false;
84093
+ let associationCreationAttempted = false;
83300
84094
  try {
83301
- await client.api.oneroster.courses.deleteComponentResource(bankIds.componentResource);
84095
+ for (const copy of itemCopies) {
84096
+ attemptedItemIdentifiers.push(copy.plan.targetIdentifier);
84097
+ await client.course.createAssessmentItemXml({
84098
+ xml: copy.input.xml,
84099
+ metadata: copy.input.metadata
84100
+ });
84101
+ }
84102
+ testCreationAttempted = true;
84103
+ await client.qtiApi.assessmentTests.create(testInput);
84104
+ associationCreationAttempted = true;
84105
+ const [row] = await this.deps.db.insert(gameTimebackAssessmentTests).values({
84106
+ integrationId,
84107
+ qtiTestIdentifier: targetTestIdentifier,
84108
+ purpose,
84109
+ status: "draft"
84110
+ }).returning();
84111
+ setAttribute("app.assessment.operation", "copy_test");
84112
+ return row;
83302
84113
  } catch (error88) {
83303
- addEvent("assessment.component_resource_delete_failed", {
83304
- "app.assessment.component_resource_id": bankIds.componentResource,
83305
- "exception.type": errorType(error88),
83306
- "app.error.message": errorMessage(error88)
83307
- });
83308
- }
83309
- for (const row of activeRows) {
83310
- if (row.bankResourceId) {
84114
+ if (associationCreationAttempted) {
84115
+ let committedRow;
83311
84116
  try {
83312
- await client.api.oneroster.resources.delete(row.bankResourceId);
83313
- } catch (error88) {
83314
- addEvent("assessment.child_resource_delete_failed", {
83315
- "app.assessment.child_resource_id": row.bankResourceId,
83316
- "exception.type": errorType(error88),
83317
- "app.error.message": errorMessage(error88)
84117
+ committedRow = await this.deps.db.query.gameTimebackAssessmentTests.findFirst({
84118
+ where: and(eq(gameTimebackAssessmentTests.integrationId, integrationId), eq(gameTimebackAssessmentTests.qtiTestIdentifier, targetTestIdentifier))
83318
84119
  });
84120
+ } catch (verificationError) {
84121
+ addEvent("assessment.qti_test_copy_commit_verification_failed", {
84122
+ "app.assessment.qti_test_identifier": targetTestIdentifier,
84123
+ "exception.type": errorType(verificationError),
84124
+ "app.error.message": errorMessage(verificationError)
84125
+ });
84126
+ throw error88;
84127
+ }
84128
+ if (committedRow) {
84129
+ setAttribute("app.assessment.operation", "copy_test");
84130
+ return committedRow;
83319
84131
  }
83320
84132
  }
84133
+ await this.cleanupQtiAssessmentCopy(client, testCreationAttempted ? targetTestIdentifier : undefined, attemptedItemIdentifiers);
84134
+ throw error88;
83321
84135
  }
83322
- try {
83323
- await client.api.oneroster.resources.delete(bankIds.resource);
83324
- } catch (error88) {
83325
- addEvent("assessment.parent_resource_delete_failed", {
83326
- "app.assessment.bank_resource_id": bankIds.resource,
83327
- "exception.type": errorType(error88),
83328
- "app.error.message": errorMessage(error88)
84136
+ }
84137
+ async createQuestion(integrationId, qtiTestIdentifier, input) {
84138
+ return this.withLockedAssessmentAssociations(integrationId, qtiTestIdentifier, async (row, tx, associations) => {
84139
+ const client = this.requireClient();
84140
+ assertAllAssessmentAssociationsDraft(associations);
84141
+ const ownership = await this.requireOwnedDraftAssessment(integrationId, qtiTestIdentifier, row, tx);
84142
+ const creation = parseQtiQuestionCreationInput(input);
84143
+ const itemIdentifier = newPlaycademyQuestionIdentifier(qtiTestIdentifier);
84144
+ let title;
84145
+ let metadata2;
84146
+ let xml;
84147
+ let structuredInput;
84148
+ let fallbackItem;
84149
+ if (creation.kind === "copy") {
84150
+ const source = await client.qtiApi.assessmentItems.get(creation.sourceItemIdentifier);
84151
+ const copy = buildQtiQuestionCopyInput({
84152
+ source,
84153
+ targetIdentifier: itemIdentifier,
84154
+ targetTestIdentifier: qtiTestIdentifier,
84155
+ gameSlug: ownership.gameSlug,
84156
+ integrationId
84157
+ });
84158
+ title = copy.title;
84159
+ metadata2 = copy.metadata;
84160
+ xml = copy.xml;
84161
+ fallbackItem = {
84162
+ ...source,
84163
+ identifier: itemIdentifier,
84164
+ title,
84165
+ rawXml: xml,
84166
+ metadata: metadata2
84167
+ };
84168
+ } else {
84169
+ structuredInput = creation.input;
84170
+ assertSupportedQtiQuestionInteraction(structuredInput);
84171
+ title = typeof structuredInput.title === "string" ? structuredInput.title.trim() : "";
84172
+ metadata2 = buildOwnedQtiQuestionMetadata(structuredInput, {
84173
+ gameSlug: ownership.gameSlug,
84174
+ testIdentifier: qtiTestIdentifier
84175
+ });
84176
+ xml = buildQuestionXml(structuredInput, itemIdentifier, title);
84177
+ fallbackItem = {
84178
+ ...structuredInput,
84179
+ identifier: itemIdentifier,
84180
+ title,
84181
+ ...xml ? { rawXml: xml } : {}
84182
+ };
84183
+ }
84184
+ const section = await this.qtiSectionItems(client, qtiTestIdentifier, undefined, undefined, ownership.test);
84185
+ const href = this.qtiItemHref(client, itemIdentifier);
84186
+ let item;
84187
+ let itemCreationAttempted = false;
84188
+ let referenceCreationAttempted = false;
84189
+ try {
84190
+ itemCreationAttempted = true;
84191
+ item = xml ? await client.course.createAssessmentItemXml({ xml, metadata: metadata2 }) : await client.qtiApi.assessmentItems.create({
84192
+ ...structuredInput,
84193
+ identifier: itemIdentifier,
84194
+ metadata: metadata2
84195
+ });
84196
+ referenceCreationAttempted = true;
84197
+ await section.items.add({
84198
+ identifier: itemIdentifier,
84199
+ href
84200
+ });
84201
+ } catch (error88) {
84202
+ let referenceRemoved = !referenceCreationAttempted;
84203
+ const creationStatus = isApiError(error88) ? error88.statusCode : undefined;
84204
+ const itemCreationDefinitelyRejected = !referenceCreationAttempted && typeof creationStatus === "number" && creationStatus >= 400 && creationStatus < 500 && creationStatus !== 408;
84205
+ if (referenceCreationAttempted) {
84206
+ try {
84207
+ await section.items.remove(itemIdentifier);
84208
+ referenceRemoved = true;
84209
+ } catch (cleanupError) {
84210
+ referenceRemoved = isApiError(cleanupError) && cleanupError.statusCode === 404;
84211
+ addEvent("assessment.qti_question_reference_cleanup_failed", {
84212
+ "app.assessment.qti_test_identifier": qtiTestIdentifier,
84213
+ "app.assessment.qti_item_identifier": itemIdentifier,
84214
+ "exception.type": errorType(cleanupError),
84215
+ "app.error.message": errorMessage(cleanupError)
84216
+ });
84217
+ }
84218
+ }
84219
+ if (itemCreationAttempted && !itemCreationDefinitelyRejected && referenceRemoved) {
84220
+ try {
84221
+ await client.qtiApi.assessmentItems.delete(itemIdentifier);
84222
+ } catch (cleanupError) {
84223
+ addEvent("assessment.qti_question_cleanup_failed", {
84224
+ "app.assessment.qti_test_identifier": qtiTestIdentifier,
84225
+ "app.assessment.qti_item_identifier": itemIdentifier,
84226
+ "exception.type": errorType(cleanupError),
84227
+ "app.error.message": errorMessage(cleanupError)
84228
+ });
84229
+ }
84230
+ }
84231
+ throw error88;
84232
+ }
84233
+ setAttribute("app.assessment.operation", creation.kind === "copy" ? "copy_question" : "create_question");
84234
+ return buildCreatedQtiQuestionReference({
84235
+ item,
84236
+ fallbackItem,
84237
+ itemIdentifier,
84238
+ href,
84239
+ partIdentifier: section.partIdentifier,
84240
+ sectionIdentifier: section.sectionIdentifier,
84241
+ metadata: metadata2
83329
84242
  });
83330
- }
83331
- try {
83332
- await client.api.oneroster.courses.deleteComponent(bankIds.component);
83333
- } catch (error88) {
83334
- addEvent("assessment.course_component_delete_failed", {
83335
- "app.assessment.component_id": bankIds.component,
83336
- "exception.type": errorType(error88),
83337
- "app.error.message": errorMessage(error88)
84243
+ });
84244
+ }
84245
+ async updateQuestion(integrationId, qtiTestIdentifier, itemIdentifier, input) {
84246
+ return this.withLockedAssessmentAssociations(integrationId, qtiTestIdentifier, async (row, tx, associations) => {
84247
+ const client = this.requireClient();
84248
+ assertAllAssessmentAssociationsDraft(associations);
84249
+ const ownership = await this.requireOwnedDraftAssessment(integrationId, qtiTestIdentifier, row, tx);
84250
+ const [, item] = await Promise.all([
84251
+ this.qtiSectionItems(client, qtiTestIdentifier, itemIdentifier, undefined, ownership.test),
84252
+ client.qtiApi.assessmentItems.get(itemIdentifier)
84253
+ ]);
84254
+ const ownerTest = await this.qtiQuestionOwnerTest(client, item, undefined, ownership.test);
84255
+ const replacesInteraction = "type" in input || "interaction" in input || "numericTextEntry" in input;
84256
+ assertQtiQuestionEditable(item, { gameSlug: ownership.gameSlug, testIdentifier: qtiTestIdentifier }, ownerTest);
84257
+ assertSupportedQtiQuestionInteraction(replacesInteraction ? input : item);
84258
+ let title = typeof input.title === "string" ? input.title.trim() : "";
84259
+ if (!title) {
84260
+ title = item.title;
84261
+ }
84262
+ const metadata2 = buildOwnedQtiQuestionMetadata(input, { gameSlug: ownership.gameSlug, testIdentifier: qtiTestIdentifier }, item.metadata);
84263
+ const xml = buildQuestionXml(input, itemIdentifier, title);
84264
+ const updated = xml ? await client.course.updateAssessmentItemXml(itemIdentifier, { xml, metadata: metadata2 }) : await client.qtiApi.assessmentItems.update(itemIdentifier, {
84265
+ ...input,
84266
+ title,
84267
+ metadata: metadata2
84268
+ });
84269
+ setAttribute("app.assessment.operation", "update_question");
84270
+ return updated;
84271
+ });
84272
+ }
84273
+ async removeQuestion(integrationId, qtiTestIdentifier, itemIdentifier) {
84274
+ return this.withLockedAssessmentAssociations(integrationId, qtiTestIdentifier, async (row, tx, associations) => {
84275
+ const client = this.requireClient();
84276
+ assertAllAssessmentAssociationsDraft(associations);
84277
+ const ownership = await this.requireOwnedDraftAssessment(integrationId, qtiTestIdentifier, row, tx);
84278
+ const section = await this.qtiSectionItems(client, qtiTestIdentifier, itemIdentifier, undefined, ownership.test);
84279
+ await section.items.remove(itemIdentifier);
84280
+ setAttribute("app.assessment.operation", "remove_question");
84281
+ });
84282
+ }
84283
+ async reorderQuestions(integrationId, qtiTestIdentifier, items) {
84284
+ return this.withLockedAssessmentAssociations(integrationId, qtiTestIdentifier, async (row, tx, associations) => {
84285
+ const client = this.requireClient();
84286
+ assertAllAssessmentAssociationsDraft(associations);
84287
+ const ownership = await this.requireOwnedDraftAssessment(integrationId, qtiTestIdentifier, row, tx);
84288
+ const identifiers = items.map((item) => item.identifier);
84289
+ validateUniqueQuestionIdentifiers(identifiers);
84290
+ const firstIdentifier = identifiers[0];
84291
+ const section = await this.qtiSectionItems(client, qtiTestIdentifier, firstIdentifier, identifiers, ownership.test);
84292
+ const result = await section.items.reorder({
84293
+ items: items.map((item) => ({
84294
+ identifier: item.identifier,
84295
+ href: item.href ?? this.qtiItemHref(client, item.identifier),
84296
+ sequence: item.sequence
84297
+ }))
83338
84298
  });
83339
- }
83340
- await this.deps.db.update(gameTimebackAssessmentTests).set({ bankResourceId: null, bankActive: false }).where(eq(gameTimebackAssessmentTests.integrationId, integrationId));
83341
- setAttribute("app.assessment.operation", "destroy_bank");
84299
+ setAttribute("app.assessment.operation", "reorder_questions");
84300
+ return result;
84301
+ });
83342
84302
  }
83343
84303
  requireClient() {
83344
84304
  if (!this.deps.timeback) {
@@ -83346,8 +84306,18 @@ class TimebackAssessmentsService {
83346
84306
  }
83347
84307
  return this.deps.timeback;
83348
84308
  }
83349
- async requireIntegration(integrationId) {
83350
- const integration = await this.deps.db.query.gameTimebackIntegrations.findFirst({
84309
+ async withLockedAssessmentAssociations(integrationId, qtiTestIdentifier, action) {
84310
+ return this.deps.db.transaction(async (tx) => {
84311
+ const rows = await tx.select().from(gameTimebackAssessmentTests).where(eq(gameTimebackAssessmentTests.qtiTestIdentifier, qtiTestIdentifier)).orderBy(gameTimebackAssessmentTests.id).for("update");
84312
+ const row = rows.find((candidate) => candidate.integrationId === integrationId);
84313
+ if (!row) {
84314
+ throw new NotFoundError(`Assessment not found: ${qtiTestIdentifier}`);
84315
+ }
84316
+ return action(row, tx, rows);
84317
+ });
84318
+ }
84319
+ async requireIntegration(integrationId, db2 = this.deps.db) {
84320
+ const integration = await db2.query.gameTimebackIntegrations.findFirst({
83351
84321
  where: and(eq(gameTimebackIntegrations.id, integrationId), isActiveGameTimebackIntegrationStatus())
83352
84322
  });
83353
84323
  if (!integration) {
@@ -83355,6 +84325,17 @@ class TimebackAssessmentsService {
83355
84325
  }
83356
84326
  return integration;
83357
84327
  }
84328
+ async requireQtiTestOwnershipContext(integrationId, db2 = this.deps.db) {
84329
+ const integration = await this.requireIntegration(integrationId, db2);
84330
+ const game2 = await db2.query.games.findFirst({
84331
+ where: eq(games.id, integration.gameId),
84332
+ columns: { slug: true }
84333
+ });
84334
+ if (!game2) {
84335
+ throw new NotFoundError(`Game not found for integration: ${integrationId}`);
84336
+ }
84337
+ return { integration, gameSlug: game2.slug };
84338
+ }
83358
84339
  async requireAssessmentRow(integrationId, qtiTestIdentifier) {
83359
84340
  const row = await this.deps.db.query.gameTimebackAssessmentTests.findFirst({
83360
84341
  where: and(eq(gameTimebackAssessmentTests.integrationId, integrationId), eq(gameTimebackAssessmentTests.qtiTestIdentifier, qtiTestIdentifier))
@@ -83364,27 +84345,125 @@ class TimebackAssessmentsService {
83364
84345
  }
83365
84346
  return row;
83366
84347
  }
83367
- qtiSectionItems(client, qtiTestIdentifier) {
83368
- return client.qtiApi.assessmentTests.testParts(qtiTestIdentifier).sections(`${qtiTestIdentifier}-part1`).items(`${qtiTestIdentifier}-section1`);
84348
+ async requireDraftAssessmentRow(integrationId, qtiTestIdentifier, lockedRow) {
84349
+ const row = lockedRow ?? await this.requireAssessmentRow(integrationId, qtiTestIdentifier);
84350
+ assertDraftAssessment(row);
84351
+ return row;
83369
84352
  }
83370
- async getMaxSortOrder(integrationId) {
83371
- const rows = await this.deps.db.query.gameTimebackAssessmentTests.findMany({
83372
- where: eq(gameTimebackAssessmentTests.integrationId, integrationId),
83373
- columns: { sortOrder: true }
84353
+ async requireOwnedDraftAssessment(integrationId, qtiTestIdentifier, lockedRow, db2 = this.deps.db) {
84354
+ const client = this.requireClient();
84355
+ await this.requireDraftAssessmentRow(integrationId, qtiTestIdentifier, lockedRow);
84356
+ const test = await client.qtiApi.assessmentTests.get(qtiTestIdentifier);
84357
+ const ownership = await this.requireQtiTestOwnershipContext(integrationId, db2);
84358
+ assertQtiTestOwnedByGame(test, ownership.gameSlug);
84359
+ return { ...ownership, test };
84360
+ }
84361
+ async qtiQuestionOwnerTest(client, item, cache, knownTest) {
84362
+ if (item.metadata && "ownerGameSlug" in item.metadata) {
84363
+ return;
84364
+ }
84365
+ const ownerTestIdentifier = qtiQuestionOwnerTestIdentifier(item);
84366
+ if (!ownerTestIdentifier) {
84367
+ return;
84368
+ }
84369
+ if (knownTest?.identifier === ownerTestIdentifier) {
84370
+ return knownTest;
84371
+ }
84372
+ function loadOwnerTest(identifier) {
84373
+ return client.qtiApi.assessmentTests.get(identifier);
84374
+ }
84375
+ if (!cache) {
84376
+ return loadOwnerTest(ownerTestIdentifier);
84377
+ }
84378
+ let ownerTest = cache.get(ownerTestIdentifier);
84379
+ if (!ownerTest) {
84380
+ ownerTest = loadOwnerTest(ownerTestIdentifier);
84381
+ cache.set(ownerTestIdentifier, ownerTest);
84382
+ }
84383
+ return ownerTest;
84384
+ }
84385
+ async assertQtiTestQuestionsSupported(client, qtiTestIdentifier, questions) {
84386
+ const result = questions ?? await client.qtiApi.assessmentTests.getQuestions(qtiTestIdentifier);
84387
+ await runWithConcurrency(result.questions, TimebackAssessmentsService.QTI_HYDRATION_CONCURRENCY, async (reference) => {
84388
+ const item = await client.qtiApi.assessmentItems.get(reference.reference.identifier);
84389
+ assertSupportedQtiQuestionInteraction(item);
83374
84390
  });
83375
- if (rows.length === 0) {
83376
- return -1;
84391
+ }
84392
+ async qtiSectionItems(client, qtiTestIdentifier, itemIdentifier, expectedItemIdentifiers, knownTest) {
84393
+ const test = knownTest ?? await client.qtiApi.assessmentTests.get(qtiTestIdentifier);
84394
+ const section = resolveQtiQuestionSection(test, qtiTestIdentifier, itemIdentifier, expectedItemIdentifiers);
84395
+ return {
84396
+ ...section,
84397
+ items: client.qtiApi.assessmentTests.testParts(qtiTestIdentifier).sections(section.partIdentifier).items(section.sectionIdentifier)
84398
+ };
84399
+ }
84400
+ qtiItemHref(client, itemIdentifier) {
84401
+ return `${client.getQtiBaseUrl()}/assessment-items/${itemIdentifier}`;
84402
+ }
84403
+ async cleanupQtiAssessmentCopy(client, testIdentifier, itemIdentifiers) {
84404
+ let testDeleted = !testIdentifier;
84405
+ if (testIdentifier) {
84406
+ try {
84407
+ await client.qtiApi.assessmentTests.delete(testIdentifier);
84408
+ testDeleted = true;
84409
+ } catch (error88) {
84410
+ testDeleted = isApiError(error88) && error88.statusCode === 404;
84411
+ addEvent("assessment.qti_test_copy_cleanup_failed", {
84412
+ "app.assessment.qti_test_identifier": testIdentifier,
84413
+ "exception.type": errorType(error88),
84414
+ "app.error.message": errorMessage(error88)
84415
+ });
84416
+ }
84417
+ }
84418
+ if (!testDeleted) {
84419
+ return;
83377
84420
  }
83378
- return Math.max(...rows.map((r) => r.sortOrder));
84421
+ for (const itemIdentifier of itemIdentifiers.toReversed()) {
84422
+ try {
84423
+ await client.qtiApi.assessmentItems.delete(itemIdentifier);
84424
+ } catch (error88) {
84425
+ addEvent("assessment.qti_question_cleanup_failed", {
84426
+ ...testIdentifier ? { "app.assessment.qti_test_identifier": testIdentifier } : {},
84427
+ "app.assessment.qti_item_identifier": itemIdentifier,
84428
+ "exception.type": errorType(error88),
84429
+ "app.error.message": errorMessage(error88)
84430
+ });
84431
+ }
84432
+ }
84433
+ }
84434
+ associationSummary(row) {
84435
+ return {
84436
+ id: row.id,
84437
+ integrationId: row.integrationId,
84438
+ qtiTestIdentifier: row.qtiTestIdentifier,
84439
+ purpose: row.purpose,
84440
+ status: row.status,
84441
+ sortOrder: row.sortOrder,
84442
+ createdAt: row.createdAt,
84443
+ updatedAt: row.updatedAt
84444
+ };
84445
+ }
84446
+ async listQtiLibrary(params, list) {
84447
+ const plan = buildQtiLibraryListPlan(params);
84448
+ return list(plan.params);
84449
+ }
84450
+ async validateAssessmentHasQuestions(qtiTestIdentifier) {
84451
+ const client = this.requireClient();
84452
+ const result = await client.qtiApi.assessmentTests.getQuestions(qtiTestIdentifier);
84453
+ assertAssessmentHasQuestions(result.questions);
84454
+ await this.assertQtiTestQuestionsSupported(client, qtiTestIdentifier, result);
83379
84455
  }
83380
84456
  }
83381
- var init_timeback_assessments_service = __esm(() => {
84457
+ var init_timeback_assessments_service = __esm(async () => {
83382
84458
  init_drizzle_orm();
83383
84459
  init_helpers_index();
83384
84460
  init_tables_index();
83385
84461
  init_spans();
83386
- init_utils6();
84462
+ init_timeback3();
83387
84463
  init_errors();
84464
+ init_timeback_assessment_rules_util();
84465
+ init_timeback_qti_authoring_util();
84466
+ await init_errors8();
83388
84467
  });
83389
84468
 
83390
84469
  // ../api-core/src/utils/timeback-create-integration.util.ts
@@ -85477,10 +86556,10 @@ var init_platform2 = __esm(async () => {
85477
86556
  init_kv_service();
85478
86557
  init_secrets_service();
85479
86558
  init_seed_service();
85480
- init_timeback_assessments_service();
85481
86559
  init_upload_service();
85482
86560
  await __promiseAll([
85483
86561
  init_timeback_admin_service(),
86562
+ init_timeback_assessments_service(),
85484
86563
  init_timeback_service()
85485
86564
  ]);
85486
86565
  });
@@ -142099,16 +143178,6 @@ function requireAnonymous(handler) {
142099
143178
  return handler(ctx);
142100
143179
  };
142101
143180
  }
142102
- function requireAdmin(handler) {
142103
- return async (ctx) => {
142104
- assertAuthenticatedRequest(ctx);
142105
- rejectDashboardWorkerKey(ctx);
142106
- if (ctx.user.role !== "admin") {
142107
- throw ApiError.forbidden("Admin access required");
142108
- }
142109
- return handler(ctx);
142110
- };
142111
- }
142112
143181
  function requireDeveloper(handler) {
142113
143182
  return async (ctx) => {
142114
143183
  assertAuthenticatedRequest(ctx);
@@ -143272,7 +144341,27 @@ var init_session_controller = __esm(() => {
143272
144341
  });
143273
144342
 
143274
144343
  // ../api-core/src/controllers/timeback.controller.ts
143275
- var populateStudent, getUser, getUserEnrollments, getUserById, setupIntegration, getIntegrations, getRemovedIntegrations, createIntegration, updateIntegration, deactivateCourse, reactivateCourse, getIntegrationConfig, verifyIntegration, getConfig, deleteIntegrations, endActivity, heartbeat, advanceCourse, unenrollCourse, getStudentXp, getStudentMastery, getStudentHighestGradeMastered, getRoster, getStudentOverview, getGameMetrics, getStudentActivity, getGradeLevelTestResults, getGradeLevelTestReview, getActivityDetail, listMetricDiscrepancies, verifyMetricDiscrepancy, grantXp, adjustTime, adjustMastery, reconcileMasteryForConfigChange, searchStudents, enrollStudent, unenrollStudent, reactivateEnrollment, listAssessments, createAssessment, deleteAssessment, reorderAssessments, reorderQuestions, activateAssessment, deactivateAssessment, listQuestions, createQuestion, updateQuestion, deleteQuestion, getAssessmentBankStatus, destroyAssessmentBank, timeback2;
144344
+ function parseQtiLibraryParams(searchParams) {
144345
+ const requestedPage = Number(searchParams.get("page"));
144346
+ const requestedLimit = Number(searchParams.get("limit"));
144347
+ const page = Number.isInteger(requestedPage) && requestedPage > 0 ? requestedPage : 1;
144348
+ const limit = Number.isInteger(requestedLimit) && requestedLimit > 0 ? Math.min(requestedLimit, 50) : 20;
144349
+ const query = searchParams.get("q")?.trim() || undefined;
144350
+ const requestedSource = searchParams.get("source");
144351
+ if (requestedSource && requestedSource !== "playcademy") {
144352
+ throw ApiError.badRequest("Unsupported QTI library source");
144353
+ }
144354
+ if (requestedSource !== "playcademy" && !query) {
144355
+ throw ApiError.badRequest("An exact QTI source identifier is required for a global lookup");
144356
+ }
144357
+ return {
144358
+ query,
144359
+ source: requestedSource === "playcademy" ? "playcademy" : undefined,
144360
+ page,
144361
+ limit
144362
+ };
144363
+ }
144364
+ var populateStudent, getUser, getUserEnrollments, getUserById, setupIntegration, getIntegrations, getRemovedIntegrations, createIntegration, updateIntegration, deactivateCourse, reactivateCourse, getIntegrationConfig, verifyIntegration, getConfig, deleteIntegrations, endActivity, heartbeat, advanceCourse, unenrollCourse, getStudentXp, getStudentMastery, getStudentHighestGradeMastered, getRoster, getStudentOverview, getGameMetrics, getStudentActivity, getGradeLevelTestResults, getGradeLevelTestReview, getActivityDetail, listMetricDiscrepancies, verifyMetricDiscrepancy, grantXp, adjustTime, adjustMastery, reconcileMasteryForConfigChange, searchStudents, enrollStudent, unenrollStudent, reactivateEnrollment, listAssessments, createAssessment, updateAssessment, reorderAssessments, reorderQuestions, removeAssessment, listQuestions, listQuestionLibrary, listTestLibrary, copyAssessment, createQuestion, updateQuestion, removeQuestion, timeback2;
143276
144365
  var init_timeback_controller = __esm(() => {
143277
144366
  init_esm();
143278
144367
  init_schemas_index();
@@ -143787,7 +144876,7 @@ var init_timeback_controller = __esm(() => {
143787
144876
  const body2 = await parseRequestBody(ctx.request, ReactivateEnrollmentRequestSchema);
143788
144877
  return ctx.services.timebackAdmin.reactivateEnrollment(body2, ctx.user);
143789
144878
  });
143790
- listAssessments = requireGameManagementAccess(async (ctx) => {
144879
+ listAssessments = requireDeveloper(async (ctx) => {
143791
144880
  const { gameId, courseId } = ctx.params;
143792
144881
  if (!gameId || !courseId) {
143793
144882
  throw ApiError.badRequest("Missing gameId or courseId parameter");
@@ -143795,40 +144884,40 @@ var init_timeback_controller = __esm(() => {
143795
144884
  const integrationId = await ctx.services.timebackAssessments.resolveIntegrationId(gameId, courseId, ctx.user);
143796
144885
  return ctx.services.timebackAssessments.listAssessments(integrationId);
143797
144886
  });
143798
- createAssessment = requireGameManagementAccess(async (ctx) => {
144887
+ createAssessment = requireDeveloper(async (ctx) => {
143799
144888
  const { gameId, courseId } = ctx.params;
143800
144889
  if (!gameId || !courseId) {
143801
144890
  throw ApiError.badRequest("Missing gameId or courseId parameter");
143802
144891
  }
143803
144892
  const body2 = await parseRequestBody(ctx.request, CreateAssessmentRequestSchema);
143804
144893
  const integrationId = await ctx.services.timebackAssessments.resolveIntegrationId(gameId, courseId, ctx.user);
143805
- const shortId = crypto.randomUUID().slice(0, 8);
143806
- const qtiTestIdentifier = `assessment-${shortId}`;
144894
+ const qtiTestIdentifier = newPlaycademyTestIdentifier();
143807
144895
  return ctx.services.timebackAssessments.createAssessment(integrationId, {
143808
144896
  title: body2.title,
144897
+ purpose: body2.purpose,
143809
144898
  qtiTestIdentifier
143810
144899
  });
143811
144900
  });
143812
- deleteAssessment = requireAdmin(async (ctx) => {
144901
+ updateAssessment = requireDeveloper(async (ctx) => {
143813
144902
  const { gameId, courseId, testIdentifier } = ctx.params;
143814
144903
  if (!gameId || !courseId || !testIdentifier) {
143815
144904
  throw ApiError.badRequest("Missing gameId, courseId, or testIdentifier parameter");
143816
144905
  }
143817
144906
  const integrationId = await ctx.services.timebackAssessments.resolveIntegrationId(gameId, courseId, ctx.user);
143818
- await ctx.services.timebackAssessments.deleteAssessment(integrationId, testIdentifier);
143819
- return { success: true };
144907
+ const body2 = await parseRequestBody(ctx.request, UpdateAssessmentRequestSchema);
144908
+ return ctx.services.timebackAssessments.updateAssessment(integrationId, testIdentifier, body2);
143820
144909
  });
143821
- reorderAssessments = requireGameManagementAccess(async (ctx) => {
144910
+ reorderAssessments = requireDeveloper(async (ctx) => {
143822
144911
  const { gameId, courseId } = ctx.params;
143823
144912
  if (!gameId || !courseId) {
143824
144913
  throw ApiError.badRequest("Missing gameId or courseId parameter");
143825
144914
  }
143826
144915
  const body2 = await parseRequestBody(ctx.request, ReorderAssessmentsRequestSchema);
143827
144916
  const integrationId = await ctx.services.timebackAssessments.resolveIntegrationId(gameId, courseId, ctx.user);
143828
- await ctx.services.timebackAssessments.reorderAssessments(integrationId, body2.identifiers);
144917
+ await ctx.services.timebackAssessments.reorderAssessments(integrationId, body2.purpose, body2.testIdentifiers);
143829
144918
  return { success: true };
143830
144919
  });
143831
- reorderQuestions = requireGameManagementAccess(async (ctx) => {
144920
+ reorderQuestions = requireDeveloper(async (ctx) => {
143832
144921
  const { gameId, courseId, testIdentifier } = ctx.params;
143833
144922
  if (!gameId || !courseId || !testIdentifier) {
143834
144923
  throw ApiError.badRequest("Missing gameId, courseId, or testIdentifier parameter");
@@ -143838,33 +144927,51 @@ var init_timeback_controller = __esm(() => {
143838
144927
  await ctx.services.timebackAssessments.reorderQuestions(integrationId, testIdentifier, body2.items);
143839
144928
  return { success: true };
143840
144929
  });
143841
- activateAssessment = requireGameManagementAccess(async (ctx) => {
144930
+ removeAssessment = requireDeveloper(async (ctx) => {
143842
144931
  const { gameId, courseId, testIdentifier } = ctx.params;
143843
144932
  if (!gameId || !courseId || !testIdentifier) {
143844
144933
  throw ApiError.badRequest("Missing gameId, courseId, or testIdentifier parameter");
143845
144934
  }
143846
144935
  const integrationId = await ctx.services.timebackAssessments.resolveIntegrationId(gameId, courseId, ctx.user);
143847
- await ctx.services.timebackAssessments.activateAssessment(integrationId, testIdentifier);
143848
- return { success: true };
144936
+ return ctx.services.timebackAssessments.removeAssessment(integrationId, testIdentifier);
143849
144937
  });
143850
- deactivateAssessment = requireGameManagementAccess(async (ctx) => {
144938
+ listQuestions = requireDeveloper(async (ctx) => {
143851
144939
  const { gameId, courseId, testIdentifier } = ctx.params;
143852
144940
  if (!gameId || !courseId || !testIdentifier) {
143853
144941
  throw ApiError.badRequest("Missing gameId, courseId, or testIdentifier parameter");
143854
144942
  }
143855
144943
  const integrationId = await ctx.services.timebackAssessments.resolveIntegrationId(gameId, courseId, ctx.user);
143856
- await ctx.services.timebackAssessments.deactivateAssessment(integrationId, testIdentifier);
143857
- return { success: true };
144944
+ return ctx.services.timebackAssessments.listQuestions(integrationId, testIdentifier);
143858
144945
  });
143859
- listQuestions = requireGameManagementAccess(async (ctx) => {
143860
- const { gameId, courseId, testIdentifier } = ctx.params;
143861
- if (!gameId || !courseId || !testIdentifier) {
143862
- throw ApiError.badRequest("Missing gameId, courseId, or testIdentifier parameter");
144946
+ listQuestionLibrary = requireDeveloper(async (ctx) => {
144947
+ const { gameId, courseId } = ctx.params;
144948
+ if (!gameId || !courseId) {
144949
+ throw ApiError.badRequest("Missing gameId or courseId parameter");
143863
144950
  }
144951
+ const params = parseQtiLibraryParams(ctx.url.searchParams);
143864
144952
  const integrationId = await ctx.services.timebackAssessments.resolveIntegrationId(gameId, courseId, ctx.user);
143865
- return ctx.services.timebackAssessments.listQuestions(integrationId, testIdentifier);
144953
+ return ctx.services.timebackAssessments.listQuestionLibrary(integrationId, params);
144954
+ });
144955
+ listTestLibrary = requireDeveloper(async (ctx) => {
144956
+ const { gameId, courseId } = ctx.params;
144957
+ if (!gameId || !courseId) {
144958
+ throw ApiError.badRequest("Missing gameId or courseId parameter");
144959
+ }
144960
+ const params = parseQtiLibraryParams(ctx.url.searchParams);
144961
+ const integrationId = await ctx.services.timebackAssessments.resolveIntegrationId(gameId, courseId, ctx.user);
144962
+ return ctx.services.timebackAssessments.listTestLibrary(integrationId, params);
143866
144963
  });
143867
- createQuestion = requireGameManagementAccess(async (ctx) => {
144964
+ copyAssessment = requireDeveloper(async (ctx) => {
144965
+ const { gameId, courseId } = ctx.params;
144966
+ if (!gameId || !courseId) {
144967
+ throw ApiError.badRequest("Missing gameId or courseId parameter");
144968
+ }
144969
+ const body2 = await parseRequestBody(ctx.request, CopyAssessmentRequestSchema);
144970
+ const integrationId = await ctx.services.timebackAssessments.resolveIntegrationId(gameId, courseId, ctx.user);
144971
+ const targetTestIdentifier = newPlaycademyTestIdentifier();
144972
+ return ctx.services.timebackAssessments.copyAssessment(integrationId, body2.testIdentifier, targetTestIdentifier, body2.purpose);
144973
+ });
144974
+ createQuestion = requireDeveloper(async (ctx) => {
143868
144975
  const { gameId, courseId, testIdentifier } = ctx.params;
143869
144976
  if (!gameId || !courseId || !testIdentifier) {
143870
144977
  throw ApiError.badRequest("Missing gameId, courseId, or testIdentifier parameter");
@@ -143873,7 +144980,7 @@ var init_timeback_controller = __esm(() => {
143873
144980
  const body2 = await ctx.request.json();
143874
144981
  return ctx.services.timebackAssessments.createQuestion(integrationId, testIdentifier, body2);
143875
144982
  });
143876
- updateQuestion = requireGameManagementAccess(async (ctx) => {
144983
+ updateQuestion = requireDeveloper(async (ctx) => {
143877
144984
  const { gameId, courseId, testIdentifier, itemIdentifier } = ctx.params;
143878
144985
  if (!gameId || !courseId || !testIdentifier || !itemIdentifier) {
143879
144986
  throw ApiError.badRequest("Missing gameId, courseId, testIdentifier, or itemIdentifier parameter");
@@ -143882,30 +144989,13 @@ var init_timeback_controller = __esm(() => {
143882
144989
  const body2 = await ctx.request.json();
143883
144990
  return ctx.services.timebackAssessments.updateQuestion(integrationId, testIdentifier, itemIdentifier, body2);
143884
144991
  });
143885
- deleteQuestion = requireGameManagementAccess(async (ctx) => {
144992
+ removeQuestion = requireDeveloper(async (ctx) => {
143886
144993
  const { gameId, courseId, testIdentifier, itemIdentifier } = ctx.params;
143887
144994
  if (!gameId || !courseId || !testIdentifier || !itemIdentifier) {
143888
144995
  throw ApiError.badRequest("Missing gameId, courseId, testIdentifier, or itemIdentifier parameter");
143889
144996
  }
143890
144997
  const integrationId = await ctx.services.timebackAssessments.resolveIntegrationId(gameId, courseId, ctx.user);
143891
- await ctx.services.timebackAssessments.deleteQuestion(integrationId, testIdentifier, itemIdentifier);
143892
- return { success: true };
143893
- });
143894
- getAssessmentBankStatus = requireGameManagementAccess(async (ctx) => {
143895
- const { gameId, courseId } = ctx.params;
143896
- if (!gameId || !courseId) {
143897
- throw ApiError.badRequest("Missing gameId or courseId parameter");
143898
- }
143899
- const integrationId = await ctx.services.timebackAssessments.resolveIntegrationId(gameId, courseId, ctx.user);
143900
- return ctx.services.timebackAssessments.getBankStatus(integrationId);
143901
- });
143902
- destroyAssessmentBank = requireAdmin(async (ctx) => {
143903
- const { gameId, courseId } = ctx.params;
143904
- if (!gameId || !courseId) {
143905
- throw ApiError.badRequest("Missing gameId or courseId parameter");
143906
- }
143907
- const integrationId = await ctx.services.timebackAssessments.resolveIntegrationId(gameId, courseId, ctx.user);
143908
- await ctx.services.timebackAssessments.destroyBank(integrationId);
144998
+ await ctx.services.timebackAssessments.removeQuestion(integrationId, testIdentifier, itemIdentifier);
143909
144999
  return { success: true };
143910
145000
  });
143911
145001
  timeback2 = defineControllerNames("timeback", {
@@ -143950,17 +145040,17 @@ var init_timeback_controller = __esm(() => {
143950
145040
  reactivateEnrollment,
143951
145041
  listAssessments,
143952
145042
  createAssessment,
143953
- deleteAssessment,
145043
+ updateAssessment,
143954
145044
  reorderAssessments,
145045
+ removeAssessment,
143955
145046
  reorderQuestions,
143956
- activateAssessment,
143957
- deactivateAssessment,
143958
145047
  listQuestions,
145048
+ listTestLibrary,
145049
+ copyAssessment,
145050
+ listQuestionLibrary,
143959
145051
  createQuestion,
143960
145052
  updateQuestion,
143961
- deleteQuestion,
143962
- getAssessmentBankStatus,
143963
- destroyAssessmentBank
145053
+ removeQuestion
143964
145054
  });
143965
145055
  });
143966
145056