@patch-adams/core 1.4.28 → 1.4.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -1412,6 +1412,10 @@ function generateLrsBridgeCode(options) {
1412
1412
  log('Looking up email for employee ID:', employeeId);
1413
1413
 
1414
1414
  fetchEmployeeEmail(employeeId, function(employeeData) {
1415
+ console.info('[PA-LRS] Employee lookup result for ' + employeeId + ': ' +
1416
+ (employeeData ? 'name=' + employeeData.name + ', email=' + employeeData.email +
1417
+ ', bravaisUserId=' + employeeData.bravaisUserId + ', tenantUrl=' + employeeData.tenantUrl
1418
+ : 'NO DATA'));
1415
1419
  if (employeeData && employeeData.email) {
1416
1420
  actor.mbox = 'mailto:' + employeeData.email;
1417
1421
  log('Enhanced actor with email:', employeeData.email);
@@ -4117,14 +4121,126 @@ function generateLrsBridgeCode(options) {
4117
4121
  // Launch events will NOT be sent until this completes
4118
4122
  extractActorAsync(function(actor) {
4119
4123
  log('Actor updated after async fetch:', actor);
4120
- actorReady = true;
4121
- // Log final actor state for diagnostics
4122
- if (window.console && window.console.info) {
4123
- console.info('[PA-LRS] Actor resolved: name=' + (actor ? actor.name : 'none') +
4124
- ', mbox=' + (actor ? actor.mbox : 'none') +
4125
- ', account=' + (actor && actor.account ? actor.account.name : 'none'));
4124
+
4125
+ // Check if actor is still "Unknown Learner" - SCORM API might not be available yet
4126
+ // The SCORM API is often injected by scorm-calls.js AFTER <head> init completes
4127
+ var isUnknown = !actor || actor.name === 'Unknown Learner' ||
4128
+ !actor.account || actor.account.name === 'unknown';
4129
+
4130
+ if (isUnknown) {
4131
+ log('Actor is Unknown Learner - SCORM API likely not available yet, polling for it...');
4132
+ var scormPollCount = 0;
4133
+ var scormMaxPolls = 30; // wait up to 30 seconds
4134
+ var scormResolved = false;
4135
+
4136
+ // Helper: search all possible locations for SCORM API
4137
+ function findScormApiAnywhere() {
4138
+ // 1. Search parent frame hierarchy (standard approach)
4139
+ var api2004 = findAPIInFrameHierarchy('API_1484_11', 10);
4140
+ if (api2004) return { api: api2004.api, type: '2004', source: 'parent-chain level ' + api2004.level };
4141
+ var api12 = findAPIInFrameHierarchy('API', 10);
4142
+ if (api12) return { api: api12.api, type: '1.2', source: 'parent-chain level ' + api12.level };
4143
+
4144
+ // 2. Check window.opener and its parent hierarchy (LMS opens content in popup)
4145
+ try {
4146
+ if (window.opener && window.opener !== window) {
4147
+ // Direct check on opener
4148
+ if (window.opener.API_1484_11) return { api: window.opener.API_1484_11, type: '2004', source: 'window.opener' };
4149
+ if (window.opener.API) return { api: window.opener.API, type: '1.2', source: 'window.opener' };
4150
+ // Check opener's parent chain
4151
+ var openerWin = window.opener;
4152
+ var openerLevel = 0;
4153
+ while (openerLevel < 10) {
4154
+ try {
4155
+ if (openerWin.API_1484_11) return { api: openerWin.API_1484_11, type: '2004', source: 'opener-chain level ' + openerLevel };
4156
+ if (openerWin.API) return { api: openerWin.API, type: '1.2', source: 'opener-chain level ' + openerLevel };
4157
+ if (openerWin.parent && openerWin.parent !== openerWin) { openerWin = openerWin.parent; openerLevel++; }
4158
+ else break;
4159
+ } catch(e) { break; }
4160
+ }
4161
+ }
4162
+ } catch(e) { /* cross-origin opener */ }
4163
+
4164
+ // 3. Check for global LMS functions (Bravais/Xyleme mock API)
4165
+ try {
4166
+ if (typeof window.LMSInitialize === 'function') {
4167
+ return { api: { LMSInitialize: window.LMSInitialize, LMSGetValue: window.LMSGetValue, LMSSetValue: window.LMSSetValue, LMSCommit: window.LMSCommit, LMSFinish: window.LMSFinish, LMSGetLastError: window.LMSGetLastError, LMSGetErrorString: window.LMSGetErrorString }, type: '1.2', source: 'global-functions' };
4168
+ }
4169
+ } catch(e) {}
4170
+ try {
4171
+ if (window.parent && window.parent !== window && typeof window.parent.LMSInitialize === 'function') {
4172
+ return { api: { LMSInitialize: window.parent.LMSInitialize, LMSGetValue: window.parent.LMSGetValue, LMSSetValue: window.parent.LMSSetValue, LMSCommit: window.parent.LMSCommit, LMSFinish: window.parent.LMSFinish, LMSGetLastError: window.parent.LMSGetLastError, LMSGetErrorString: window.parent.LMSGetErrorString }, type: '1.2', source: 'parent-global-functions' };
4173
+ }
4174
+ } catch(e) {}
4175
+
4176
+ return null;
4177
+ }
4178
+
4179
+ function onScormApiFound(result, pollNum) {
4180
+ if (scormResolved) return;
4181
+ scormResolved = true;
4182
+
4183
+ LRS.scormApi = result.api;
4184
+ LRS.scormApiFound = true;
4185
+ LRS.scormApiType = result.type;
4186
+
4187
+ // Read learner_id for diagnostics
4188
+ var scormLearnerId = 'n/a', scormLearnerName = 'n/a';
4189
+ try {
4190
+ if (result.type === '2004') {
4191
+ scormLearnerId = result.api.GetValue('cmi.learner_id');
4192
+ scormLearnerName = result.api.GetValue('cmi.learner_name');
4193
+ } else {
4194
+ scormLearnerId = result.api.LMSGetValue('cmi.core.student_id');
4195
+ scormLearnerName = result.api.LMSGetValue('cmi.core.student_name');
4196
+ }
4197
+ } catch(e) { scormLearnerId = 'error'; scormLearnerName = 'error'; }
4198
+ console.info('[PA-LRS] SCORM API found (poll ' + pollNum + ', source=' + result.source +
4199
+ ', type=' + result.type + '): learner_id=' + scormLearnerId + ', learner_name=' + scormLearnerName);
4200
+
4201
+ // refreshActor re-reads learner_id from SCORM and runs employee lookup
4202
+ LRS.refreshActor(function(refreshedActor) {
4203
+ if (window.console && window.console.info) {
4204
+ console.info('[PA-LRS] Actor resolved (after SCORM wait, poll ' + pollNum + '): name=' +
4205
+ (refreshedActor ? refreshedActor.name : 'none') +
4206
+ ', mbox=' + (refreshedActor ? refreshedActor.mbox : 'none') +
4207
+ ', account=' + (refreshedActor && refreshedActor.account ? refreshedActor.account.name : 'none'));
4208
+ }
4209
+ actorReady = true;
4210
+ tryLaunchEvents();
4211
+ });
4212
+ }
4213
+
4214
+ var scormPollTimer = setInterval(function() {
4215
+ if (scormResolved) { clearInterval(scormPollTimer); return; }
4216
+ scormPollCount++;
4217
+
4218
+ var result = findScormApiAnywhere();
4219
+
4220
+ if (result) {
4221
+ clearInterval(scormPollTimer);
4222
+ onScormApiFound(result, scormPollCount);
4223
+ } else if (scormPollCount >= scormMaxPolls) {
4224
+ clearInterval(scormPollTimer);
4225
+ log('SCORM API not found after ' + scormMaxPolls + 's, proceeding with Unknown Learner');
4226
+ if (window.console && window.console.info) {
4227
+ console.info('[PA-LRS] Actor resolved (timeout ' + scormMaxPolls + 's): name=' + (actor ? actor.name : 'none') +
4228
+ ', account=' + (actor && actor.account ? actor.account.name : 'none'));
4229
+ }
4230
+ actorReady = true;
4231
+ tryLaunchEvents();
4232
+ }
4233
+ }, 1000);
4234
+ } else {
4235
+ // Actor is valid - proceed immediately
4236
+ if (window.console && window.console.info) {
4237
+ console.info('[PA-LRS] Actor resolved: name=' + (actor ? actor.name : 'none') +
4238
+ ', mbox=' + (actor ? actor.mbox : 'none') +
4239
+ ', account=' + (actor && actor.account ? actor.account.name : 'none'));
4240
+ }
4241
+ actorReady = true;
4242
+ tryLaunchEvents();
4126
4243
  }
4127
- tryLaunchEvents();
4128
4244
  });
4129
4245
  } else {
4130
4246
  warn('Bridge setup failed - operating in offline mode');
@@ -4247,32 +4363,6 @@ function generateLrsBridgeCode(options) {
4247
4363
  }, 1000);
4248
4364
  }
4249
4365
 
4250
- // If actor is still Unknown Learner, poll for SCORM API availability
4251
- // The SCORM API may not be available during <head> init but appears later
4252
- // (e.g., scorm-calls.js finds and initializes it after DOM ready)
4253
- if (!LRS.actor || LRS.actor.name === 'Unknown Learner' || !LRS.actor.account || LRS.actor.account.name === 'unknown') {
4254
- var actorRetryCount = 0;
4255
- var actorMaxRetries = 15;
4256
- var actorRetryInterval = setInterval(function() {
4257
- actorRetryCount++;
4258
- // Check if SCORM API has become available (injected by scorm-calls.js or LMS)
4259
- var hasScormApi = LRS.scormApi ||
4260
- (typeof window.API !== 'undefined' && window.API) ||
4261
- (typeof window.API_1484_11 !== 'undefined' && window.API_1484_11);
4262
-
4263
- if (hasScormApi) {
4264
- log('SCORM API now available (attempt ' + actorRetryCount + '), refreshing actor...');
4265
- clearInterval(actorRetryInterval);
4266
- LRS.refreshActor(function(actor) {
4267
- log('Actor refreshed after SCORM API detected:', actor ? actor.name : 'none');
4268
- });
4269
- } else if (actorRetryCount >= actorMaxRetries) {
4270
- log('SCORM API not found after ' + actorMaxRetries + ' attempts, actor stays as-is');
4271
- clearInterval(actorRetryInterval);
4272
- }
4273
- }, 1000);
4274
- }
4275
-
4276
4366
  log('LRS bridge setup complete');
4277
4367
  }
4278
4368