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);
}
}
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();
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) {
$.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;
this.fluidbook = fluidbook;
this.linksToComplete = [];
this.manageScore = this.fluidbook.settings.scorm_score;
+ this.cache = {};
+ this.ok = false;
this.init();
}
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) {
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();
+ }
}
});
}
return (this.linksToComplete.length === 0);
},
+ saveCache: function () {
+ scormSaveCurrentPosition(undefined, undefined, this.cache);
+ },
+
isActive: function () {
if (!this.fluidbook.settings.scorm_enable) {
return false;
startScormTimer();
initScormEvents();
initScormInteractions();
+
+ $(fluidbook).trigger('fluidbook.scorm.ready', []);
});
} else {
console.log('SCORM nok');
}
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) {
fluidbook.setCurrentPage(location.page);
changePage = true;
}
+ if (location.cache) {
+ fluidbook.scorm.cache = location.cache;
+ }
}
} catch (err) {
}
}
-function scormSaveCurrentPosition(page, maxPage) {
+function scormSaveCurrentPosition(page, maxPage, cache) {
if (!SCORM_OK) {
return;
}
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() {