]> _ Git - fluidbook-html5.git/commitdiff
wait #4945 @2
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 7 Dec 2021 16:38:16 +0000 (17:38 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 7 Dec 2021 16:38:16 +0000 (17:38 +0100)
js/libs/fluidbook/fluidbook.cache.js
js/libs/fluidbook/fluidbook.gamify.js
js/libs/fluidbook/fluidbook.scorm.js
js/libs/scorm/scorm.js

index c2c472c1085e68cd9f3976507840da2e25f20a41..300c99ae11a23c7e68c3513ada259ddd496e7a20 100644 (file)
@@ -19,7 +19,7 @@ FluidbookCache.prototype = {
         if (this._support) {
             for (var i = localStorage.length - 1; i >= 0; i--) {
                 var key = localStorage.key(i);
-                if (key.indexOf(this._prefix) == 0) {
+                if (key.indexOf(this._prefix) === 0) {
                     localStorage.removeItem(key);
                 }
             }
index cc345cb0dfda0815ebbee785ca602dfca420f5ed..9b1bdae7eef9836b9faa0f3f08a59bf342293bb3 100644 (file)
@@ -6,13 +6,49 @@ function FluidbookGamify(fluidbook) {
 
 FluidbookGamify.prototype = {
     init: function () {
-        this.coinsLinksSeen = this.fluidbook.cache.get('gamify_coins_links_seen', []);
-        this.addedCoins = this.fluidbook.cache.get('gamify_coins_added', {});
+        var $this = this;
+        if (!this.fluidbook.scorm.ok) {
+            this.initGamify();
+        } else {
+            $(this.fluidbook).on('fluidbook.scorm.ready', function () {
+                $this.initGamify();
+            });
+        }
+    },
+
+    getFromCache: function (key, defaultValue) {
+        if (!this.fluidbook.scorm.ok) {
+            return this.fluidbook.cache.get(key, defaultValue);
+        }
+        if (this.fluidbook.scorm.cache[key] === undefined || this.fluidbook.scorm.cache[key] === null) {
+            return defaultValue;
+        }
+        return this.fluidbook.scorm.cache[key];
+    },
+
+    setToCache(key, value) {
+        if (!this.fluidbook.scorm.ok) {
+            return this.fluidbook.cache.set(key, value);
+        } else {
+            this.fluidbook.scorm.cache[key] = value;
+            this.fluidbook.scorm.saveCache();
+        }
+    },
+
+    initGamify: function () {
+        this.coinsLinksSeen = this.getFromCache('gamify_coins_links_seen', []);
+        this.addedCoins = this.getFromCache('gamify_coins_added', {});
+
+        var $this = this;
+        $(this.fluidbook).on('fluidbook.page.change.end', function (e, page) {
+            $this.linkClicked('visit_page_' + page);
+        });
+
         this.updateTotalCoins();
     },
 
     linkClicked: function (id) {
-        if (this.coinsLinksSeen.indexOf(id) === -1) {
+        if (this.coinsLinksSeen.indexOf(id) === -1 && this.fluidbook.settings.gamifyCoins[id] !== undefined) {
             this.coinsLinksSeen.push(id);
             this.save();
             this.updateTotalCoins();
@@ -34,6 +70,7 @@ FluidbookGamify.prototype = {
 
     updateTotalCoins: function () {
         var $this = this;
+        var formerValue = this.totalCoins;
         this.totalCoins = 0;
         $.each(this.coinsLinksSeen, function (k, uid) {
             if ($this.fluidbook.settings.gamifyCoins[uid] !== undefined) {
@@ -43,10 +80,15 @@ FluidbookGamify.prototype = {
         $.each(this.addedCoins, function (id, coins) {
             $this.totalCoins += coins;
         });
+        if (this.totalCoins !== formerValue) {
+            setTimeout(function () {
+                $($this.fluidbook).trigger('fluidbook.gamify.coins.update', [$this.totalCoins]);
+            }, 1000);
+        }
     },
     save: function () {
-        this.fluidbook.cache.set('gamify_coins_links_seen', this.coinsLinksSeen);
-        this.fluidbook.cache.set('gamify_coins_added', this.addedCoins);
+        this.setToCache('gamify_coins_links_seen', this.coinsLinksSeen);
+        this.setToCache('gamify_coins_added', this.addedCoins);
     },
     getTotalCoins: function () {
         return this.totalCoins;
index 553841e2ad0d6ebfb6b78f736ff62f84e29e6f69..97f12258993b91e89566a67b701dda4bcf9f28f4 100644 (file)
@@ -2,6 +2,8 @@ function FluidbookScorm(fluidbook) {
     this.fluidbook = fluidbook;
     this.linksToComplete = [];
     this.manageScore = this.fluidbook.settings.scorm_score;
+    this.cache = {};
+    this.ok = false;
     this.init();
 }
 
@@ -9,10 +11,8 @@ FluidbookScorm.prototype = {
     init: function () {
         var $this = this;
 
-        var ok = false;
-
         if (this.fluidbook.settings.scorm_enable && window.initScorm !== undefined) {
-            ok = initScorm();
+            this.ok = initScorm();
         }
 
         if (this.fluidbook.settings.scorm_variables.linkstocomplete) {
@@ -37,7 +37,22 @@ FluidbookScorm.prototype = {
         if (this.fluidbook.settings.scorm_complete_on_last_page) {
             $(this.fluidbook).on('fluidbook.page.change.end', function () {
                 if ($this.fluidbook.currentPage === $this.fluidbook.settings.pages) {
-                    scormMarkAsComplete();
+                    if (window.scormMarkAsComplete !== undefined) {
+                        scormMarkAsComplete();
+                    }
+                }
+            });
+        }
+
+        // Mark complete when the user earn enough coins
+        if (this.fluidbook.settings.scorm_complete_coins > 0) {
+            $(this.fluidbook).on('fluidbook.gamify.coins.update', function (e, coins) {
+                console.log('update coins ', coins);
+                if (coins >= $this.fluidbook.settings.scorm_complete_coins) {
+                    console.log($this.fluidbook.settings.scorm_complete_coins + ' coins reached !!');
+                    if (window.scormMarkAsComplete !== undefined) {
+                        scormMarkAsComplete();
+                    }
                 }
             });
         }
@@ -62,6 +77,10 @@ FluidbookScorm.prototype = {
         return (this.linksToComplete.length === 0);
     },
 
+    saveCache: function () {
+        scormSaveCurrentPosition(undefined, undefined, this.cache);
+    },
+
     isActive: function () {
         if (!this.fluidbook.settings.scorm_enable) {
             return false;
index 3edeb7c2294f25e2cc27175e768209d20d88318d..79065d3c5ef02eaab281297e22103bad96363782 100644 (file)
@@ -61,6 +61,8 @@ function initScorm() {
             startScormTimer();
             initScormEvents();
             initScormInteractions();
+
+            $(fluidbook).trigger('fluidbook.scorm.ready', []);
         });
     } else {
         console.log('SCORM nok');
@@ -102,8 +104,7 @@ function initScormEvents() {
     }
     SCORM_LOCATION_INITED = true;
     var currentLocation = getScormValue('location');
-    console.log(currentLocation, 'null');
-    if (currentLocation !== 'null') {
+    if (currentLocation !== 'null' && currentLocation !== '') {
         try {
             var changePage = false;
             if (currentLocation.indexOf('page_') === 0) {
@@ -121,6 +122,9 @@ function initScormEvents() {
                     fluidbook.setCurrentPage(location.page);
                     changePage = true;
                 }
+                if (location.cache) {
+                    fluidbook.scorm.cache = location.cache;
+                }
             }
 
         } catch (err) {
@@ -151,7 +155,7 @@ function initScormEvents() {
     }
 }
 
-function scormSaveCurrentPosition(page, maxPage) {
+function scormSaveCurrentPosition(page, maxPage, cache) {
     if (!SCORM_OK) {
         return;
     }
@@ -161,7 +165,11 @@ function scormSaveCurrentPosition(page, maxPage) {
     if (maxPage === undefined) {
         maxPage = fluidbook.contentlock.getMaxPage();
     }
-    setScormValue('location', JSON.stringify({page: page, maxPage: maxPage}));
+    if (cache === undefined) {
+        cache = fluidbook.scorm.cache;
+    }
+
+    setScormValue('location', JSON.stringify({page: page, maxPage: maxPage, cache: cache}));
 }
 
 function finishScorm() {