@patch-adams/core 1.3.4 → 1.3.6

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
@@ -1570,6 +1570,132 @@ function generateLrsBridgeCode(options) {
1570
1570
  return null;
1571
1571
  }
1572
1572
 
1573
+ // Store for document API data
1574
+ var documentApiData = null;
1575
+ var documentApiFetched = false;
1576
+
1577
+ /**
1578
+ * Fetch document metadata from Bravais API to get GUIDs
1579
+ * Endpoint: /api/v3/documents/{documentId}
1580
+ */
1581
+ function fetchDocumentMetadata(documentId, callback) {
1582
+ if (!documentId) {
1583
+ callback(null);
1584
+ return;
1585
+ }
1586
+
1587
+ if (documentApiFetched) {
1588
+ callback(documentApiData);
1589
+ return;
1590
+ }
1591
+
1592
+ // Detect Bravais core URL
1593
+ var coreUrl = null;
1594
+ var urlsToCheck = [window.location.href, document.referrer];
1595
+
1596
+ try {
1597
+ var win = window;
1598
+ for (var i = 0; i < 10; i++) {
1599
+ if (win.parent && win.parent !== win) {
1600
+ win = win.parent;
1601
+ try {
1602
+ urlsToCheck.push(win.location.href);
1603
+ } catch (e) { break; }
1604
+ } else { break; }
1605
+ }
1606
+ } catch (e) {}
1607
+
1608
+ for (var j = 0; j < urlsToCheck.length; j++) {
1609
+ var match = urlsToCheck[j].match(/(https?:\\/\\/core-[a-zA-Z0-9_-]+\\.bravais\\.com)/);
1610
+ if (match) {
1611
+ coreUrl = match[1];
1612
+ break;
1613
+ }
1614
+ }
1615
+
1616
+ if (!coreUrl) {
1617
+ log('No Bravais core URL detected for document metadata fetch');
1618
+ documentApiFetched = true;
1619
+ callback(null);
1620
+ return;
1621
+ }
1622
+
1623
+ var apiUrl = coreUrl + '/api/v3/documents/' + documentId;
1624
+ log('Fetching document metadata from:', apiUrl);
1625
+
1626
+ var xhr = new XMLHttpRequest();
1627
+ xhr.open('GET', apiUrl, true);
1628
+ xhr.withCredentials = true;
1629
+ xhr.setRequestHeader('Accept', 'application/json');
1630
+
1631
+ xhr.onreadystatechange = function() {
1632
+ if (xhr.readyState === 4) {
1633
+ documentApiFetched = true;
1634
+ log('Document API response status:', xhr.status);
1635
+ if (xhr.status >= 200 && xhr.status < 300) {
1636
+ try {
1637
+ documentApiData = JSON.parse(xhr.responseText);
1638
+ log('Document metadata received:', documentApiData);
1639
+ callback(documentApiData);
1640
+ } catch (e) {
1641
+ warn('Error parsing document metadata:', e);
1642
+ callback(null);
1643
+ }
1644
+ } else {
1645
+ log('Document metadata fetch failed. Status:', xhr.status);
1646
+ callback(null);
1647
+ }
1648
+ }
1649
+ };
1650
+
1651
+ xhr.onerror = function() {
1652
+ documentApiFetched = true;
1653
+ log('Network error fetching document metadata');
1654
+ callback(null);
1655
+ };
1656
+
1657
+ try {
1658
+ xhr.send();
1659
+ } catch (e) {
1660
+ documentApiFetched = true;
1661
+ warn('Error sending document request:', e);
1662
+ callback(null);
1663
+ }
1664
+ }
1665
+
1666
+ /**
1667
+ * Update course info with data from document API
1668
+ */
1669
+ function updateCourseInfoFromApi(docData) {
1670
+ if (!docData || !LRS.courseInfo) return;
1671
+
1672
+ // Document GUID
1673
+ if (docData.guid && !LRS.courseInfo.guid) {
1674
+ LRS.courseInfo.guid = docData.guid;
1675
+ LRS.courseInfo.id = 'http://xyleme.com/bravais/document/' + docData.guid;
1676
+ log('Updated course guid from API:', docData.guid);
1677
+ }
1678
+
1679
+ // Version GUID from latestVersion
1680
+ if (docData.latestVersion && docData.latestVersion.guid && !LRS.courseInfo.versionGuid) {
1681
+ LRS.courseInfo.versionGuid = docData.latestVersion.guid;
1682
+ log('Updated version guid from API:', docData.latestVersion.guid);
1683
+ }
1684
+
1685
+ // Other metadata
1686
+ if (docData.name && LRS.courseInfo.title === 'Rise Course') {
1687
+ LRS.courseInfo.title = docData.name;
1688
+ }
1689
+ if (docData.format) {
1690
+ LRS.courseInfo.format = docData.format;
1691
+ }
1692
+ if (docData.resourceType) {
1693
+ LRS.courseInfo.resourceType = docData.resourceType;
1694
+ }
1695
+
1696
+ log('Course info updated from API:', LRS.courseInfo);
1697
+ }
1698
+
1573
1699
  /**
1574
1700
  * Extract Bravais document ID from URL
1575
1701
  * Pattern: /api/shared/.../files/{documentId}/scormcontent/
@@ -2804,7 +2930,7 @@ function generateLrsBridgeCode(options) {
2804
2930
  // ========================================================================
2805
2931
 
2806
2932
  function init() {
2807
- log('Initializing LRS bridge v2.0.0...');
2933
+ log('Initializing LRS bridge v2.2.0...');
2808
2934
 
2809
2935
  // Extract course info early
2810
2936
  extractCourseInfo();
@@ -2838,8 +2964,11 @@ function generateLrsBridgeCode(options) {
2838
2964
  setupQuizInterceptors();
2839
2965
  setupInteractionInterceptors();
2840
2966
 
2841
- // Send course launched event after a short delay to allow actor fetch
2842
- setTimeout(function() {
2967
+ // Fetch document metadata from API to get GUIDs (async)
2968
+ // Then send course launched event
2969
+ var documentId = LRS.courseInfo ? LRS.courseInfo.documentId : null;
2970
+
2971
+ function sendLaunchEvents() {
2843
2972
  if (TRACK_NAVIGATION) {
2844
2973
  LRS.courseLaunched();
2845
2974
  LRS.contentOpened({
@@ -2848,7 +2977,21 @@ function generateLrsBridgeCode(options) {
2848
2977
  action: 'launched'
2849
2978
  });
2850
2979
  }
2851
- }, 500);
2980
+ }
2981
+
2982
+ if (documentId && !LRS.courseInfo.guid) {
2983
+ // Try to fetch GUIDs from API before sending statements
2984
+ fetchDocumentMetadata(documentId, function(docData) {
2985
+ if (docData) {
2986
+ updateCourseInfoFromApi(docData);
2987
+ }
2988
+ // Send launch events after API fetch (success or failure)
2989
+ setTimeout(sendLaunchEvents, 100);
2990
+ });
2991
+ } else {
2992
+ // Already have GUID or no document ID - send after short delay
2993
+ setTimeout(sendLaunchEvents, 500);
2994
+ }
2852
2995
 
2853
2996
  // Setup beforeunload to send terminated
2854
2997
  window.addEventListener('beforeunload', function() {