@patch-adams/core 1.3.4 → 1.3.7

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/cli.js CHANGED
@@ -1897,6 +1897,132 @@ function generateLrsBridgeCode(options) {
1897
1897
  return null;
1898
1898
  }
1899
1899
 
1900
+ // Store for document API data
1901
+ var documentApiData = null;
1902
+ var documentApiFetched = false;
1903
+
1904
+ /**
1905
+ * Fetch document metadata from Bravais API to get GUIDs
1906
+ * Endpoint: /api/v3/documents/{documentId}
1907
+ */
1908
+ function fetchDocumentMetadata(documentId, callback) {
1909
+ if (!documentId) {
1910
+ callback(null);
1911
+ return;
1912
+ }
1913
+
1914
+ if (documentApiFetched) {
1915
+ callback(documentApiData);
1916
+ return;
1917
+ }
1918
+
1919
+ // Detect Bravais core URL
1920
+ var coreUrl = null;
1921
+ var urlsToCheck = [window.location.href, document.referrer];
1922
+
1923
+ try {
1924
+ var win = window;
1925
+ for (var i = 0; i < 10; i++) {
1926
+ if (win.parent && win.parent !== win) {
1927
+ win = win.parent;
1928
+ try {
1929
+ urlsToCheck.push(win.location.href);
1930
+ } catch (e) { break; }
1931
+ } else { break; }
1932
+ }
1933
+ } catch (e) {}
1934
+
1935
+ for (var j = 0; j < urlsToCheck.length; j++) {
1936
+ var match = urlsToCheck[j].match(/(https?:\\/\\/core-[a-zA-Z0-9_-]+\\.bravais\\.com)/);
1937
+ if (match) {
1938
+ coreUrl = match[1];
1939
+ break;
1940
+ }
1941
+ }
1942
+
1943
+ if (!coreUrl) {
1944
+ log('No Bravais core URL detected for document metadata fetch');
1945
+ documentApiFetched = true;
1946
+ callback(null);
1947
+ return;
1948
+ }
1949
+
1950
+ var apiUrl = coreUrl + '/api/v3/documents/' + documentId;
1951
+ log('Fetching document metadata from:', apiUrl);
1952
+
1953
+ var xhr = new XMLHttpRequest();
1954
+ xhr.open('GET', apiUrl, true);
1955
+ xhr.withCredentials = true;
1956
+ xhr.setRequestHeader('Accept', 'application/json');
1957
+
1958
+ xhr.onreadystatechange = function() {
1959
+ if (xhr.readyState === 4) {
1960
+ documentApiFetched = true;
1961
+ log('Document API response status:', xhr.status);
1962
+ if (xhr.status >= 200 && xhr.status < 300) {
1963
+ try {
1964
+ documentApiData = JSON.parse(xhr.responseText);
1965
+ log('Document metadata received:', documentApiData);
1966
+ callback(documentApiData);
1967
+ } catch (e) {
1968
+ warn('Error parsing document metadata:', e);
1969
+ callback(null);
1970
+ }
1971
+ } else {
1972
+ log('Document metadata fetch failed. Status:', xhr.status);
1973
+ callback(null);
1974
+ }
1975
+ }
1976
+ };
1977
+
1978
+ xhr.onerror = function() {
1979
+ documentApiFetched = true;
1980
+ log('Network error fetching document metadata');
1981
+ callback(null);
1982
+ };
1983
+
1984
+ try {
1985
+ xhr.send();
1986
+ } catch (e) {
1987
+ documentApiFetched = true;
1988
+ warn('Error sending document request:', e);
1989
+ callback(null);
1990
+ }
1991
+ }
1992
+
1993
+ /**
1994
+ * Update course info with data from document API
1995
+ */
1996
+ function updateCourseInfoFromApi(docData) {
1997
+ if (!docData || !LRS.courseInfo) return;
1998
+
1999
+ // Document GUID
2000
+ if (docData.guid && !LRS.courseInfo.guid) {
2001
+ LRS.courseInfo.guid = docData.guid;
2002
+ LRS.courseInfo.id = 'http://xyleme.com/bravais/document/' + docData.guid;
2003
+ log('Updated course guid from API:', docData.guid);
2004
+ }
2005
+
2006
+ // Version GUID from latestVersion
2007
+ if (docData.latestVersion && docData.latestVersion.guid && !LRS.courseInfo.versionGuid) {
2008
+ LRS.courseInfo.versionGuid = docData.latestVersion.guid;
2009
+ log('Updated version guid from API:', docData.latestVersion.guid);
2010
+ }
2011
+
2012
+ // Other metadata
2013
+ if (docData.name && LRS.courseInfo.title === 'Rise Course') {
2014
+ LRS.courseInfo.title = docData.name;
2015
+ }
2016
+ if (docData.format) {
2017
+ LRS.courseInfo.format = docData.format;
2018
+ }
2019
+ if (docData.resourceType) {
2020
+ LRS.courseInfo.resourceType = docData.resourceType;
2021
+ }
2022
+
2023
+ log('Course info updated from API:', LRS.courseInfo);
2024
+ }
2025
+
1900
2026
  /**
1901
2027
  * Extract Bravais document ID from URL
1902
2028
  * Pattern: /api/shared/.../files/{documentId}/scormcontent/
@@ -3131,7 +3257,7 @@ function generateLrsBridgeCode(options) {
3131
3257
  // ========================================================================
3132
3258
 
3133
3259
  function init() {
3134
- log('Initializing LRS bridge v2.0.0...');
3260
+ log('Initializing LRS bridge v2.2.0...');
3135
3261
 
3136
3262
  // Extract course info early
3137
3263
  extractCourseInfo();
@@ -3165,8 +3291,11 @@ function generateLrsBridgeCode(options) {
3165
3291
  setupQuizInterceptors();
3166
3292
  setupInteractionInterceptors();
3167
3293
 
3168
- // Send course launched event after a short delay to allow actor fetch
3169
- setTimeout(function() {
3294
+ // Fetch document metadata from API to get GUIDs (async)
3295
+ // Then send course launched event
3296
+ var documentId = LRS.courseInfo ? LRS.courseInfo.documentId : null;
3297
+
3298
+ function sendLaunchEvents() {
3170
3299
  if (TRACK_NAVIGATION) {
3171
3300
  LRS.courseLaunched();
3172
3301
  LRS.contentOpened({
@@ -3175,7 +3304,21 @@ function generateLrsBridgeCode(options) {
3175
3304
  action: 'launched'
3176
3305
  });
3177
3306
  }
3178
- }, 500);
3307
+ }
3308
+
3309
+ if (documentId && !LRS.courseInfo.guid) {
3310
+ // Try to fetch GUIDs from API before sending statements
3311
+ fetchDocumentMetadata(documentId, function(docData) {
3312
+ if (docData) {
3313
+ updateCourseInfoFromApi(docData);
3314
+ }
3315
+ // Send launch events after API fetch (success or failure)
3316
+ setTimeout(sendLaunchEvents, 100);
3317
+ });
3318
+ } else {
3319
+ // Already have GUID or no document ID - send after short delay
3320
+ setTimeout(sendLaunchEvents, 500);
3321
+ }
3179
3322
 
3180
3323
  // Setup beforeunload to send terminated
3181
3324
  window.addEventListener('beforeunload', function() {