From f52251e5835ffc3f5e6fbb9ea62a6dbd8f2ba9a2 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Mon, 3 Sep 2018 17:47:44 +0200 Subject: [PATCH] wip #2207 @6 --- js/libs/fluidbook/fluidbook.js | 1 + js/libs/scorm/scorm.js | 44 +++++++++++++++++++++++++++------- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/js/libs/fluidbook/fluidbook.js b/js/libs/fluidbook/fluidbook.js index 372fc3e8..26a087b6 100644 --- a/js/libs/fluidbook/fluidbook.js +++ b/js/libs/fluidbook/fluidbook.js @@ -117,6 +117,7 @@ Fluidbook.prototype = { } this.maxPage = Math.min(p, this.datas.pages); + $(this).trigger('fluidbook.maxpage.set', [this.maxPage]); this.updateMaxPage(); }, diff --git a/js/libs/scorm/scorm.js b/js/libs/scorm/scorm.js index 96b38e7a..36c333b2 100644 --- a/js/libs/scorm/scorm.js +++ b/js/libs/scorm/scorm.js @@ -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); -- 2.39.5