]> _ Git - fluidbook-html5.git/commitdiff
wip #2207 @6
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 3 Sep 2018 15:47:44 +0000 (17:47 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Mon, 3 Sep 2018 15:47:44 +0000 (17:47 +0200)
js/libs/fluidbook/fluidbook.js
js/libs/scorm/scorm.js

index 372fc3e89e5cf31ce99ded28b4cd51425c412d41..26a087b6fd1e2124dfdabb8092ea61a943391514 100644 (file)
@@ -117,6 +117,7 @@ Fluidbook.prototype = {
         }
 
         this.maxPage = Math.min(p, this.datas.pages);
+        $(this).trigger('fluidbook.maxpage.set', [this.maxPage]);
         this.updateMaxPage();
     },
 
index 96b38e7a61037df0906933675841edc46d24ed8d..36c333b259daf39e1b813008722593584f54d656 100644 (file)
@@ -70,30 +70,53 @@ function initScormEvents() {
         finishScorm();
     });
 
-    var currentPage = getScormValue('location');
+    var currentLocation = getScormValue('location');
     try {
-        if (currentPage != '') {
+        if (currentLocation.indexOf('page_') === 0) {
             var e = currentPage.split('_');
             if (e.length == 2 && e[0] == 'page') {
                 fluidbook.setCurrentPage(e[1]);
             }
+        } else if (currentLocation.indexOf('{') === 0) {
+            var location = JSON.parse(currentLocation);
+            if (location.page) {
+                fluidbook.setCurrentPage(location.page);
+            }
+            if (location.maxPage) {
+                fluidbook.setMaxPage(location.maxPage, true);
+            }
         }
     } catch (err) {
         console.log(err);
     }
 
     $(fluidbook).on('fluidbook.page.navigation', function (e, page) {
-        setScormValue('location', 'page_' + page);
+        scormSaveCurrentPosition(page);
+    });
+
+    $(fluidbook).on('fluidbook.maxpage.set', function (e, page) {
+        scormSaveCurrentPosition(undefined, page);
     });
 
     // Commit data once a minute
-    setTimeout(function (args) {
+    setTimeout(function () {
         pipwerks.SCORM.save();
-    }, 60000);
+    }, 2000);
+}
+
+function scormSaveCurrentPosition(page, maxPage) {
+    if (page === undefined) {
+        page = fluidbook.currentPage;
+    }
+    if (maxPage === undefined) {
+        maxPage = fluidbook.getMaxPage();
+    }
+    setScormValue('location', JSON.stringify({page: page, maxPage: maxPage}));
 }
 
 function finishScorm() {
     setSessionTime();
+    pipwerks.SCORM.save();
     pipwerks.SCORM.quit();
 }
 
@@ -103,8 +126,8 @@ function startScormTimer() {
 
 function scormComplete() {
     setScormValue('status', "completed");
-    window.close();
     finishScorm();
+    window.close();
 }
 
 function getScormValue(elementName) {
@@ -288,10 +311,11 @@ function setSessionTime() {
             totalSeconds = "0" + totalSeconds;
         }
         sessionTime = totalHours + ":" + totalMinutes + ":" + totalSeconds;
+        setScormValue('session_time', sessionTime);
     } else {
-        sessionTime = Math.round(calculatedTime / 1000);
+        setScormValue('session_time', scormSecondsToTimeInterval(calculatedTime / 1000));
     }
-    setScormValue('session_time', sessionTime);
+
 }
 
 function dateToScormTime(date) {
@@ -303,6 +327,10 @@ function dateToScormTime(date) {
 
 function getScormTimeInterval(start, end) {
     var diff = Math.round((end.getTime() - start.getTime()) / 1000);
+    return scormSecondsToTimeInterval(diff);
+}
+
+function scormSecondsToTimeInterval(diff) {
     var h = Math.floor(diff / 3600);
     diff = diff % 3600;
     var m = Math.floor(diff / 60);