@patch-adams/core 1.5.3 → 1.5.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -3822,7 +3822,11 @@ function generateLrsBridgeCode(options) {
3822
3822
  if (!TRACK_QUIZZES) return;
3823
3823
 
3824
3824
  // Intercept submit button clicks on Knowledge Check blocks
3825
+ // NOTE: If the SCORM tracker is active, it handles quiz tracking via
3826
+ // cmi.interactions \u2014 skip DOM scraping to avoid duplicate statements
3825
3827
  document.addEventListener('click', function(e) {
3828
+ if (scormTrackerActive) return; // SCORM tracker handles this
3829
+
3826
3830
  var submitBtn = e.target.closest('.quiz-card__button');
3827
3831
  if (!submitBtn) return;
3828
3832
 
@@ -3978,6 +3982,219 @@ function generateLrsBridgeCode(options) {
3978
3982
  return answerText;
3979
3983
  }
3980
3984
 
3985
+ // ========================================================================
3986
+ // SCORM INTERACTION TRACKER
3987
+ // Intercepts cmi.interactions.N.* SetValue calls to capture quiz answers
3988
+ // directly from the SCORM data model \u2014 works regardless of Rise UI format
3989
+ // (Knowledge Check blocks, quiz lessons, etc.)
3990
+ //
3991
+ // IMPORTANT: This finds and wraps the ACTUAL SCORM API that Rise uses
3992
+ // (window.API, parent.API, or global functions), NOT the bridge's copy.
3993
+ // It runs unconditionally at init, not gated by actor resolution.
3994
+ // ========================================================================
3995
+ var scormInteractions = {}; // Pending interactions keyed by index N
3996
+ var scormInteractionsSent = {}; // Track which interactions were already sent
3997
+ var scormTrackerActive = false; // Set true when SCORM tracker wraps SetValue \u2014 KC handler defers
3998
+
3999
+ function interceptScormSetValue(key, value) {
4000
+ if (typeof key !== 'string') return;
4001
+ var interactionPattern = /^cmi\\.interactions\\.(\\d+)\\.(.+)$/;
4002
+ var match = key.match(interactionPattern);
4003
+ if (!match) return;
4004
+
4005
+ var idx = match[1];
4006
+ var field = match[2];
4007
+
4008
+ // Initialize interaction tracking for this index
4009
+ if (!scormInteractions[idx]) {
4010
+ scormInteractions[idx] = {};
4011
+ }
4012
+
4013
+ // Store the field value
4014
+ scormInteractions[idx][field] = String(value);
4015
+
4016
+ log('SCORM Interaction [' + idx + '].' + field + ' = ' + String(value).substring(0, 100));
4017
+
4018
+ // When 'result' is set, the interaction is complete \u2014 fire xAPI statement
4019
+ if (field === 'result' && !scormInteractionsSent[idx]) {
4020
+ scormInteractionsSent[idx] = true;
4021
+ var interaction = scormInteractions[idx];
4022
+
4023
+ // Map SCORM interaction type to readable type
4024
+ var typeMap = {
4025
+ 'choice': 'multiple-choice',
4026
+ 'true-false': 'true-false',
4027
+ 'fill-in': 'fill-in-blank',
4028
+ 'matching': 'matching',
4029
+ 'performance': 'performance',
4030
+ 'sequencing': 'sequencing',
4031
+ 'likert': 'likert',
4032
+ 'numeric': 'numeric'
4033
+ };
4034
+
4035
+ var scormType = interaction.type || 'unknown';
4036
+ var questionType = typeMap[scormType] || scormType;
4037
+
4038
+ // Determine correctness from SCORM result value
4039
+ // SCORM 1.2: 'correct', 'wrong', 'unanticipated', 'neutral'
4040
+ // SCORM 2004: 'correct', 'incorrect', 'unanticipated', 'neutral'
4041
+ var isCorrect = String(value) === 'correct';
4042
+
4043
+ // Get student response
4044
+ var studentResponse = interaction.student_response || '';
4045
+
4046
+ // Get correct response pattern
4047
+ var correctResponse = '';
4048
+ Object.keys(interaction).forEach(function(k) {
4049
+ if (k.indexOf('correct_responses') > -1 && k.indexOf('pattern') > -1) {
4050
+ correctResponse = interaction[k];
4051
+ }
4052
+ });
4053
+
4054
+ // Rise interaction IDs encode the question text (underscored)
4055
+ // e.g. "Development_Week_Qui_I_want_a_clear_picture_of_..."
4056
+ var interactionId = interaction.id || ('interaction-' + idx);
4057
+
4058
+ // Try to make a readable question text from the interaction ID
4059
+ var questionText = interactionId
4060
+ .replace(/_\\d+$/, '') // remove trailing _0
4061
+ .replace(/_/g, ' ') // underscores to spaces
4062
+ .substring(0, 200);
4063
+
4064
+ // Get lesson context
4065
+ var lessonInfo = getCachedLessonInfo();
4066
+
4067
+ // Increment question counter
4068
+ kcQuestionCounter++;
4069
+
4070
+ log('SCORM Interaction complete [' + idx + ']:', {
4071
+ questionNumber: kcQuestionCounter,
4072
+ questionText: questionText.substring(0, 60) + '...',
4073
+ type: questionType,
4074
+ studentResponse: studentResponse,
4075
+ correctResponse: correctResponse,
4076
+ result: String(value),
4077
+ correct: isCorrect
4078
+ });
4079
+
4080
+ // Send xAPI answered statement
4081
+ LRS.questionAnswered({
4082
+ questionId: interactionId,
4083
+ questionGuid: interactionId,
4084
+ questionNumber: kcQuestionCounter,
4085
+ questionText: questionText,
4086
+ questionType: questionType,
4087
+ answer: studentResponse,
4088
+ correctAnswer: correctResponse,
4089
+ correct: isCorrect,
4090
+ result: isCorrect ? 'correct' : 'incorrect',
4091
+ assessmentName: lessonInfo.name || 'Quiz',
4092
+ lessonName: lessonInfo.name,
4093
+ sectionName: lessonInfo.sectionName
4094
+ });
4095
+ }
4096
+ }
4097
+
4098
+ function setupScormInteractionTracker() {
4099
+ if (!TRACK_QUIZZES) return;
4100
+
4101
+ // Find the ACTUAL SCORM API that Rise uses \u2014 NOT our bridge's copy.
4102
+ // Rise discovers the API via standard SCORM lookup (window.API, parent chain).
4103
+ // The Bravais CDS player's ProxyApi.injectLmsApi() sets this up.
4104
+ var wrapped = false;
4105
+
4106
+ // Try 1: window.API (SCORM 1.2) or window.API_1484_11 (SCORM 2004)
4107
+ try {
4108
+ if (window.API && typeof window.API.LMSSetValue === 'function') {
4109
+ var origSetValue = window.API.LMSSetValue;
4110
+ window.API.LMSSetValue = function(key, value) {
4111
+ var result = origSetValue.apply(window.API, arguments);
4112
+ interceptScormSetValue(key, value);
4113
+ return result;
4114
+ };
4115
+ wrapped = true;
4116
+ log('SCORM Interaction Tracker: Wrapped window.API.LMSSetValue');
4117
+ }
4118
+ } catch (e) { log('SCORM Tracker: Cannot access window.API:', e.message); }
4119
+
4120
+ if (!wrapped) {
4121
+ try {
4122
+ if (window.API_1484_11 && typeof window.API_1484_11.SetValue === 'function') {
4123
+ var origSetValue2004 = window.API_1484_11.SetValue;
4124
+ window.API_1484_11.SetValue = function(key, value) {
4125
+ var result = origSetValue2004.apply(window.API_1484_11, arguments);
4126
+ interceptScormSetValue(key, value);
4127
+ return result;
4128
+ };
4129
+ wrapped = true;
4130
+ log('SCORM Interaction Tracker: Wrapped window.API_1484_11.SetValue');
4131
+ }
4132
+ } catch (e) { log('SCORM Tracker: Cannot access window.API_1484_11:', e.message); }
4133
+ }
4134
+
4135
+ // Try 2: Parent frame API
4136
+ if (!wrapped) {
4137
+ try {
4138
+ if (window.parent && window.parent !== window) {
4139
+ if (window.parent.API && typeof window.parent.API.LMSSetValue === 'function') {
4140
+ var origParentSetValue = window.parent.API.LMSSetValue;
4141
+ window.parent.API.LMSSetValue = function(key, value) {
4142
+ var result = origParentSetValue.apply(window.parent.API, arguments);
4143
+ interceptScormSetValue(key, value);
4144
+ return result;
4145
+ };
4146
+ wrapped = true;
4147
+ log('SCORM Interaction Tracker: Wrapped window.parent.API.LMSSetValue');
4148
+ }
4149
+ }
4150
+ } catch (e) { log('SCORM Tracker: Cannot access parent API (cross-origin)'); }
4151
+ }
4152
+
4153
+ // Try 3: Global LMS functions (Bravais/Xyleme mock API)
4154
+ if (!wrapped) {
4155
+ try {
4156
+ if (typeof window.LMSSetValue === 'function') {
4157
+ var origGlobalSetValue = window.LMSSetValue;
4158
+ window.LMSSetValue = function(key, value) {
4159
+ var result = origGlobalSetValue.apply(window, arguments);
4160
+ interceptScormSetValue(key, value);
4161
+ return result;
4162
+ };
4163
+ wrapped = true;
4164
+ log('SCORM Interaction Tracker: Wrapped window.LMSSetValue (global)');
4165
+ }
4166
+ } catch (e) { log('SCORM Tracker: Cannot wrap global LMSSetValue:', e.message); }
4167
+ }
4168
+
4169
+ // Try 4: Fall back to bridge's copy (least likely to work but worth trying)
4170
+ if (!wrapped && LRS.scormApi) {
4171
+ var setValueFn = LRS.scormApiType === '2004' ? 'SetValue' : 'LMSSetValue';
4172
+ if (typeof LRS.scormApi[setValueFn] === 'function') {
4173
+ var origBridgeSetValue = LRS.scormApi[setValueFn];
4174
+ LRS.scormApi[setValueFn] = function(key, value) {
4175
+ var result = origBridgeSetValue.apply(LRS.scormApi, arguments);
4176
+ interceptScormSetValue(key, value);
4177
+ return result;
4178
+ };
4179
+ wrapped = true;
4180
+ log('SCORM Interaction Tracker: Wrapped LRS.scormApi.' + setValueFn + ' (bridge copy)');
4181
+ }
4182
+ }
4183
+
4184
+ if (wrapped) {
4185
+ scormTrackerActive = true;
4186
+ log('SCORM Interaction Tracker active \u2014 KC DOM handler will defer');
4187
+ } else {
4188
+ // API not available yet \u2014 retry in 2 seconds (Bravais proxy may not be ready)
4189
+ log('SCORM Interaction Tracker: No SCORM API found yet, retrying in 2s...');
4190
+ setTimeout(function() {
4191
+ if (!scormTrackerActive) {
4192
+ setupScormInteractionTracker();
4193
+ }
4194
+ }, 2000);
4195
+ }
4196
+ }
4197
+
3981
4198
  function setupInteractionInterceptors() {
3982
4199
  if (!TRACK_INTERACTIONS) return;
3983
4200
 
@@ -4278,6 +4495,10 @@ function generateLrsBridgeCode(options) {
4278
4495
  setupQuizInterceptors();
4279
4496
  setupInteractionInterceptors();
4280
4497
 
4498
+ // Intercept SCORM cmi.interactions to capture quiz answers as xAPI statements
4499
+ // This wraps the ACTUAL SCORM API (window.API etc.) that Rise calls
4500
+ setupScormInteractionTracker();
4501
+
4281
4502
  // Fetch document metadata from API to get GUIDs (async)
4282
4503
  // Then send course launched event
4283
4504
  var sharedLinkToken = LRS.courseInfo ? LRS.courseInfo.sharedLinkToken : null;