From: Vincent Vanwaelscappel Date: Tue, 7 Dec 2021 16:38:16 +0000 (+0100) Subject: wait #4945 @2 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=03f2c551dac44e9abca5782ab688c94ef176ef1e;p=fluidbook-html5.git wait #4945 @2 --- diff --git a/js/libs/fluidbook/fluidbook.cache.js b/js/libs/fluidbook/fluidbook.cache.js index c2c472c1..300c99ae 100644 --- a/js/libs/fluidbook/fluidbook.cache.js +++ b/js/libs/fluidbook/fluidbook.cache.js @@ -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); } } diff --git a/js/libs/fluidbook/fluidbook.gamify.js b/js/libs/fluidbook/fluidbook.gamify.js index cc345cb0..9b1bdae7 100644 --- a/js/libs/fluidbook/fluidbook.gamify.js +++ b/js/libs/fluidbook/fluidbook.gamify.js @@ -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; diff --git a/js/libs/fluidbook/fluidbook.scorm.js b/js/libs/fluidbook/fluidbook.scorm.js index 553841e2..97f12258 100644 --- a/js/libs/fluidbook/fluidbook.scorm.js +++ b/js/libs/fluidbook/fluidbook.scorm.js @@ -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; diff --git a/js/libs/scorm/scorm.js b/js/libs/scorm/scorm.js index 3edeb7c2..79065d3c 100644 --- a/js/libs/scorm/scorm.js +++ b/js/libs/scorm/scorm.js @@ -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() {