From af8e40145979538955a3809969de3b9b5f87cef1 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Wed, 6 Dec 2023 17:15:00 +0100 Subject: [PATCH] wip #6472 @1.5 --- js/libs/scorm/scorm.js | 54 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/js/libs/scorm/scorm.js b/js/libs/scorm/scorm.js index 551b4f3e..37c0e150 100644 --- a/js/libs/scorm/scorm.js +++ b/js/libs/scorm/scorm.js @@ -1,6 +1,6 @@ SCORM = true; SCORM_INITED = false; -SCORM_START_TIME = null; +SCORM_START_TIME = 0; SCORM_INTERACTION_TIMESTAMPS = []; SCORM_CORRECT_ANSWERS = []; SCORM_ID_TO_N = {}; @@ -12,6 +12,8 @@ SCORM_EVENTS_INITED = false; SCORM_INTERACTIONS_INITED = false; SCORM_LOCATION_INITED = false; SCORM_OK = false; +SCORM_TIME_ELAPSED = 0; +SCORM_LAST_ACTIVITY = (new Date()).getTime(); var _CMI12 = { 'location': 'cmi.core.lesson_location', @@ -99,6 +101,30 @@ function initScormEvents() { finishScorm(); }); + $(document).on('mousemove touchmove', function () { + resetInactivityTimer(); + startScormTimer(); + }); + + setInterval(function () { + let inactivity = Math.round(((new Date()).getTime() - SCORM_LAST_ACTIVITY) / 1000); + if (inactivity > 15) { + pauseScormTimer(); + } + if (inactivity > 300) { + finishScorm(); + scormClose(); + } + }, 10000); + + $(window).on('blur', function () { + pauseScormTimer(); + }); + + $(window).on('focus', function () { + startScormTimer(); + }); + if (SCORM_LOCATION_INITED) { return; } @@ -223,7 +249,25 @@ function scormExit() { } function startScormTimer() { - SCORM_START_TIME = new Date(); + if (SCORM_START_TIME === 0) { + console.log('start timer'); + SCORM_START_TIME = new Date().getTime(); + } +} + +function pauseScormTimer() { + if (SCORM_START_TIME === 0) { + + return; + } + console.log('pause timer'); + var currentTime = new Date().getTime(); + SCORM_TIME_ELAPSED += (currentTime - SCORM_START_TIME); + SCORM_START_TIME = 0; +} + +function resetInactivityTimer() { + SCORM_LAST_ACTIVITY = new Date().getTime(); } function scormComplete() { @@ -432,11 +476,11 @@ function setSessionTime() { if (!SCORM_OK) { return; } - var currentTime = new Date(); + var sessionTime; - var endTime = currentTime.getTime() - var calculatedTime = endTime - SCORM_START_TIME; + pauseScormTimer(); + let calculatedTime = SCORM_TIME_ELAPSED; if (pipwerks.SCORM.version == '1.2') { var totalHours = Math.floor(calculatedTime / 1000 / 60 / 60); -- 2.39.5