]> _ Git - fluidbook-html5.git/commitdiff
wip #6472 @1.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 6 Dec 2023 16:15:00 +0000 (17:15 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Wed, 6 Dec 2023 16:15:00 +0000 (17:15 +0100)
js/libs/scorm/scorm.js

index 551b4f3ee2acb4b2c69af5406c4f9cb3660872cc..37c0e1504f0d6186478c96a87eeab9b66b3854f1 100644 (file)
@@ -1,6 +1,6 @@
 SCORM = true;
 SCORM_INITED = false;
-SCORM_START_TIME = null;
+SCORM_START_TIME = 0;
 SCORM_INTERACTION_TIMESTAMPS = [];
 SCORM_CORRECT_ANSWERS = [];
 SCORM_ID_TO_N = {};
@@ -12,6 +12,8 @@ SCORM_EVENTS_INITED = false;
 SCORM_INTERACTIONS_INITED = false;
 SCORM_LOCATION_INITED = false;
 SCORM_OK = false;
+SCORM_TIME_ELAPSED = 0;
+SCORM_LAST_ACTIVITY = (new Date()).getTime();
 
 var _CMI12 = {
     'location': 'cmi.core.lesson_location',
@@ -99,6 +101,30 @@ function initScormEvents() {
         finishScorm();
     });
 
+    $(document).on('mousemove touchmove', function () {
+        resetInactivityTimer();
+        startScormTimer();
+    });
+
+    setInterval(function () {
+        let inactivity = Math.round(((new Date()).getTime() - SCORM_LAST_ACTIVITY) / 1000);
+        if (inactivity > 15) {
+            pauseScormTimer();
+        }
+        if (inactivity > 300) {
+            finishScorm();
+            scormClose();
+        }
+    }, 10000);
+
+    $(window).on('blur', function () {
+        pauseScormTimer();
+    });
+
+    $(window).on('focus', function () {
+        startScormTimer();
+    });
+
     if (SCORM_LOCATION_INITED) {
         return;
     }
@@ -223,7 +249,25 @@ function scormExit() {
 }
 
 function startScormTimer() {
-    SCORM_START_TIME = new Date();
+    if (SCORM_START_TIME === 0) {
+        console.log('start timer');
+        SCORM_START_TIME = new Date().getTime();
+    }
+}
+
+function pauseScormTimer() {
+    if (SCORM_START_TIME === 0) {
+
+        return;
+    }
+    console.log('pause timer');
+    var currentTime = new Date().getTime();
+    SCORM_TIME_ELAPSED += (currentTime - SCORM_START_TIME);
+    SCORM_START_TIME = 0;
+}
+
+function resetInactivityTimer() {
+    SCORM_LAST_ACTIVITY = new Date().getTime();
 }
 
 function scormComplete() {
@@ -432,11 +476,11 @@ function setSessionTime() {
     if (!SCORM_OK) {
         return;
     }
-    var currentTime = new Date();
+
     var sessionTime;
 
-    var endTime = currentTime.getTime()
-    var calculatedTime = endTime - SCORM_START_TIME;
+    pauseScormTimer();
+    let calculatedTime = SCORM_TIME_ELAPSED;
 
     if (pipwerks.SCORM.version == '1.2') {
         var totalHours = Math.floor(calculatedTime / 1000 / 60 / 60);