From 61593c2ba6c3c31b37aa39e2c8150fd4c10c6292 Mon Sep 17 00:00:00 2001 From: canvasplay Date: Wed, 11 Oct 2017 14:39:27 +0200 Subject: [PATCH 01/17] added "handleSessionTime" bool flag --- src/JavaScript/SCORM_API_wrapper.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/JavaScript/SCORM_API_wrapper.js b/src/JavaScript/SCORM_API_wrapper.js index 43636a6..9f5aef5 100755 --- a/src/JavaScript/SCORM_API_wrapper.js +++ b/src/JavaScript/SCORM_API_wrapper.js @@ -33,6 +33,7 @@ pipwerks.SCORM = { //Define the SCORM object version: null, //Store SCORM version. handleCompletionStatus: true, //Whether or not the wrapper should automatically handle the initial completion status handleExitMode: true, //Whether or not the wrapper should automatically handle the exit mode + handleSessionTime: true, //Whether or not the wrapper should automatically handle the session time API: { handle: null, isFound: false }, //Create API child object connection: { isActive: false }, //Create connection child object From b24152cad7d7361187be7d7b2dc383b22a63152a Mon Sep 17 00:00:00 2001 From: canvasplay Date: Wed, 11 Oct 2017 14:50:07 +0200 Subject: [PATCH 02/17] Added dtmInitialized property in data object --- src/JavaScript/SCORM_API_wrapper.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/JavaScript/SCORM_API_wrapper.js b/src/JavaScript/SCORM_API_wrapper.js index 9f5aef5..2312ceb 100755 --- a/src/JavaScript/SCORM_API_wrapper.js +++ b/src/JavaScript/SCORM_API_wrapper.js @@ -38,7 +38,8 @@ pipwerks.SCORM = { //Define the SCORM object isFound: false }, //Create API child object connection: { isActive: false }, //Create connection child object data: { completionStatus: null, - exitStatus: null }, //Create data child object + exitStatus: null, + dtmInitialized: null}, //Create data child object debug: {} //Create debug child object }; From c3eef3724729c50075009c85c5988a34cb814518 Mon Sep 17 00:00:00 2001 From: canvasplay Date: Wed, 11 Oct 2017 14:57:06 +0200 Subject: [PATCH 03/17] set value for scorm.data.dtmInitialized on pipwerks.SCORM.connection.initialize call --- src/JavaScript/SCORM_API_wrapper.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/JavaScript/SCORM_API_wrapper.js b/src/JavaScript/SCORM_API_wrapper.js index 2312ceb..5d13828 100755 --- a/src/JavaScript/SCORM_API_wrapper.js +++ b/src/JavaScript/SCORM_API_wrapper.js @@ -253,6 +253,9 @@ pipwerks.SCORM.connection.initialize = function(){ trace("connection.initialize called."); + //set init date-time + scorm.data.dtmInitialized = new Date(); + if(!scorm.connection.isActive){ var API = scorm.API.getHandle(), From 4057be4c33c1216a6e19c3d272e198216b3089b2 Mon Sep 17 00:00:00 2001 From: canvasplay Date: Wed, 11 Oct 2017 15:08:59 +0200 Subject: [PATCH 04/17] Added UTIL methods for time format handling --- src/JavaScript/SCORM_API_wrapper.js | 102 +++++++++++++++++++++++++++- 1 file changed, 101 insertions(+), 1 deletion(-) diff --git a/src/JavaScript/SCORM_API_wrapper.js b/src/JavaScript/SCORM_API_wrapper.js index 5d13828..9d1f8bf 100755 --- a/src/JavaScript/SCORM_API_wrapper.js +++ b/src/JavaScript/SCORM_API_wrapper.js @@ -361,6 +361,7 @@ pipwerks.SCORM.connection.terminate = function(){ scorm = pipwerks.SCORM, exitStatus = scorm.data.exitStatus, completionStatus = scorm.data.completionStatus, + dtmInitialized = scorm.data.dtmInitialized, trace = pipwerks.UTILS.trace, makeBoolean = pipwerks.UTILS.StringToBoolean, debug = scorm.debug, @@ -374,7 +375,30 @@ pipwerks.SCORM.connection.terminate = function(){ if(API){ - if(scorm.handleExitMode && !exitStatus){ + if(scorm.handleSessionTime){ + + var dtm = new Date(); + + //in the next line you substract the time recorded when initialising + //the connection from the present time. + var n = dtm.getTime() - dtmInitialized.getTime(); + switch(scorm.version){ + + //the time format is different on scorm 1.2 or 2004, so we use + //different conversions depending on the case + case "1.2" : + //success = this.set("cmi.core.session_time",MillisecondsToCMIDuration(n)); + this.set("cmi.core.session_time",pipwerks.UTILS.MillisecondsToCMIDuration(n)); + break; + case "2004": + //success = this.set("cmi.session_time",centisecsToISODuration(Math.floor(n/10))); + this.set("cmi.session_time",pipwerks.UTILS.centisecsToISODuration(Math.floor(n/10))); + break; + } + + } + + if(scorm.handleExitMode && !exitStatus){ if(completionStatus !== "completed" && completionStatus !== "passed"){ @@ -846,3 +870,79 @@ pipwerks.UTILS.trace = function(msg){ } }; + + +/* ------------------------------------------------------------------------- + pipwerks.UTILS.MillisecondsToCMIDuration() + Converts time to scorm 1.2 time format + + Parameters: n (number) + Return: String +---------------------------------------------------------------------------- */ + +pipwerks.UTILS.MillisecondsToCMIDuration = function(n){ + + //Convert duration from milliseconds to 0000:00:00.00 format + var hms = ""; + var dtm = new Date(); dtm.setTime(n); + var h = "0" + Math.floor(n / 3600000); + var m = "0" + dtm.getMinutes(); + var s = "0" + dtm.getSeconds(); + hms = h.substr(h.length-2)+":"+m.substr(m.length-2)+":"; + hms += s.substr(s.length-2); + return hms + +}; + + +/* ------------------------------------------------------------------------- + pipwerks.UTILS.centisecsToISODuration() + Converts time to scorm 2004 time format + + Parameters: n (number) + Return: String +---------------------------------------------------------------------------- */ + +pipwerks.UTILS.centisecsToISODuration = function(n){ + + // Note: SCORM and IEEE 1484.11.1 require centisec precision + // Months calculated by approximation based on average number + // of days over 4 years (365*4+1), not counting the extra day + // every 1000 years. If a reference date was available, + // the calculation could be more precise, but becomes complex, + // since the exact result depends on where the reference date + // falls within the period (e.g. beginning, end or ???) + // 1 year ~ (365*4+1)/4*60*60*24*100 = 3155760000 centiseconds + // 1 month ~ (365*4+1)/48*60*60*24*100 = 262980000 centiseconds + // 1 day = 8640000 centiseconds + // 1 hour = 360000 centiseconds + // 1 minute = 6000 centiseconds + n = Math.max(n,0); // there is no such thing as a negative duration + var str = "P"; + var nCs = n; + // Next set of operations uses whole seconds + var nY = Math.floor(nCs / 3155760000); + nCs -= nY * 3155760000; + var nM = Math.floor(nCs / 262980000); + nCs -= nM * 262980000; + var nD = Math.floor(nCs / 8640000); + nCs -= nD * 8640000; + var nH = Math.floor(nCs / 360000); + nCs -= nH * 360000; + var nMin = Math.floor(nCs /6000); + nCs -= nMin * 6000 + // Now we can construct string + if (nY > 0) str += nY + "Y"; + if (nM > 0) str += nM + "M"; + if (nD > 0) str += nD + "D"; + if ((nH > 0) || (nMin > 0) || (nCs > 0)) { + str += "T"; + if (nH > 0) str += nH + "H"; + if (nMin > 0) str += nMin + "M"; + if (nCs > 0) str += (nCs / 100) + "S"; + } + if (str == "P") str = "PT0H0M0S"; + // technically PT0S should do but SCORM test suite assumes longer form. + return str; + +}; From 21df630b920d534ba1a775946c0c24d9d0e1bd68 Mon Sep 17 00:00:00 2001 From: canvasplay Date: Fri, 22 Dec 2017 09:29:12 +0100 Subject: [PATCH 05/17] remove commented out lines --- src/JavaScript/SCORM_API_wrapper.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/JavaScript/SCORM_API_wrapper.js b/src/JavaScript/SCORM_API_wrapper.js index 9d1f8bf..4b97c6f 100755 --- a/src/JavaScript/SCORM_API_wrapper.js +++ b/src/JavaScript/SCORM_API_wrapper.js @@ -387,11 +387,9 @@ pipwerks.SCORM.connection.terminate = function(){ //the time format is different on scorm 1.2 or 2004, so we use //different conversions depending on the case case "1.2" : - //success = this.set("cmi.core.session_time",MillisecondsToCMIDuration(n)); this.set("cmi.core.session_time",pipwerks.UTILS.MillisecondsToCMIDuration(n)); break; case "2004": - //success = this.set("cmi.session_time",centisecsToISODuration(Math.floor(n/10))); this.set("cmi.session_time",pipwerks.UTILS.centisecsToISODuration(Math.floor(n/10))); break; } From e83689bb9260426ec45cb0ce5a738ea1a9f9bba8 Mon Sep 17 00:00:00 2001 From: canvasplay Date: Fri, 22 Dec 2017 11:37:57 +0100 Subject: [PATCH 06/17] first approach to unit testing for the javascript part --- package.json | 2 +- test/JavaScript/test.js | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 test/JavaScript/test.js diff --git a/package.json b/package.json index 738ac8d..bddd212 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "The pipwerks SCORM API Wrapper ", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "node ./test/JavaScript/test.js" }, "repository": { "type": "git", diff --git a/test/JavaScript/test.js b/test/JavaScript/test.js new file mode 100644 index 0000000..639bd88 --- /dev/null +++ b/test/JavaScript/test.js @@ -0,0 +1,18 @@ +var assert = require('assert'); +var pipwerks = require('../../src/JavaScript/SCORM_API_wrapper.js'); + +try{ + + let t1 = 1513937992632; + let t2 = 1513938095752; + let d = t2 - t1; + + assert.equal(pipwerks.UTILS.centisecsToISODuration(d),'PT17M11.2S'); + assert.equal(pipwerks.UTILS.MillisecondsToCMIDuration(d),'00:01:43'); + + console.log('All tests passed!'); + +} +catch(e){ + console.log(e); +} \ No newline at end of file From 938d7186777618cdb6ec9178a7a8214f555319c7 Mon Sep 17 00:00:00 2001 From: canvasplay Date: Tue, 2 Jan 2018 15:59:57 +0100 Subject: [PATCH 07/17] set qunit.js as unit testing engine --- package.json | 2 +- test/JavaScript/index.html | 16 ++++++++++++++++ test/JavaScript/test.js | 27 ++++++++++++++------------- 3 files changed, 31 insertions(+), 14 deletions(-) create mode 100644 test/JavaScript/index.html diff --git a/package.json b/package.json index bddd212..738ac8d 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "The pipwerks SCORM API Wrapper ", "main": "index.js", "scripts": { - "test": "node ./test/JavaScript/test.js" + "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { "type": "git", diff --git a/test/JavaScript/index.html b/test/JavaScript/index.html new file mode 100644 index 0000000..17c7fea --- /dev/null +++ b/test/JavaScript/index.html @@ -0,0 +1,16 @@ + + + + + + SCORM_API_wrapper.js - tests + + + +
+
+ + + + + \ No newline at end of file diff --git a/test/JavaScript/test.js b/test/JavaScript/test.js index 639bd88..74093bb 100644 --- a/test/JavaScript/test.js +++ b/test/JavaScript/test.js @@ -1,18 +1,19 @@ -var assert = require('assert'); -var pipwerks = require('../../src/JavaScript/SCORM_API_wrapper.js'); +QUnit.test("centisecsToISODuration", function (assert){ -try{ + var t1 = 1513937992632; + var t2 = 1513938095752; + var d = t2 - t1; - let t1 = 1513937992632; - let t2 = 1513938095752; - let d = t2 - t1; + assert.equal(pipwerks.UTILS.centisecsToISODuration(d), 'PT17M11.2S'); - assert.equal(pipwerks.UTILS.centisecsToISODuration(d),'PT17M11.2S'); - assert.equal(pipwerks.UTILS.MillisecondsToCMIDuration(d),'00:01:43'); +}); - console.log('All tests passed!'); +QUnit.test("MillisecondsToCMIDuration", function (assert) { -} -catch(e){ - console.log(e); -} \ No newline at end of file + var t1 = 1513937992632; + var t2 = 1513938095752; + var d = t2 - t1; + + assert.equal(pipwerks.UTILS.MillisecondsToCMIDuration(d), '00:01:43'); + +}); \ No newline at end of file From b334dc920c80000dc7321fc17f74a522c0a6b9db Mon Sep 17 00:00:00 2001 From: canvasplay Date: Tue, 2 Jan 2018 16:09:42 +0100 Subject: [PATCH 08/17] add some null like tests for time util functions --- test/JavaScript/test.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/JavaScript/test.js b/test/JavaScript/test.js index 74093bb..d22bd4c 100644 --- a/test/JavaScript/test.js +++ b/test/JavaScript/test.js @@ -5,6 +5,11 @@ QUnit.test("centisecsToISODuration", function (assert){ var d = t2 - t1; assert.equal(pipwerks.UTILS.centisecsToISODuration(d), 'PT17M11.2S'); + assert.equal(pipwerks.UTILS.centisecsToISODuration(), 'PT0H0M0S'); + assert.equal(pipwerks.UTILS.centisecsToISODuration(undefined), 'PT0H0M0S'); + assert.equal(pipwerks.UTILS.centisecsToISODuration(null), 'PT0H0M0S'); + assert.equal(pipwerks.UTILS.centisecsToISODuration(''), 'PT0H0M0S'); + assert.equal(pipwerks.UTILS.centisecsToISODuration(0), 'PT0H0M0S'); }); @@ -15,5 +20,10 @@ QUnit.test("MillisecondsToCMIDuration", function (assert) { var d = t2 - t1; assert.equal(pipwerks.UTILS.MillisecondsToCMIDuration(d), '00:01:43'); + assert.equal(pipwerks.UTILS.MillisecondsToCMIDuration(), '00:00:00'); + assert.equal(pipwerks.UTILS.MillisecondsToCMIDuration(undefined), '00:00:00'); + assert.equal(pipwerks.UTILS.MillisecondsToCMIDuration(null), '00:00:00'); + assert.equal(pipwerks.UTILS.MillisecondsToCMIDuration(''), '00:00:00'); + assert.equal(pipwerks.UTILS.MillisecondsToCMIDuration(0), '00:00:00'); }); \ No newline at end of file From 31f70a31e3d5dc3923fe7355ca84f5407a542a75 Mon Sep 17 00:00:00 2001 From: canvasplay Date: Tue, 2 Jan 2018 16:19:11 +0100 Subject: [PATCH 09/17] add tests for negative duration and fix the functions to pass them --- src/JavaScript/SCORM_API_wrapper.js | 1 + test/JavaScript/test.js | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/JavaScript/SCORM_API_wrapper.js b/src/JavaScript/SCORM_API_wrapper.js index 4b97c6f..fd4e28b 100755 --- a/src/JavaScript/SCORM_API_wrapper.js +++ b/src/JavaScript/SCORM_API_wrapper.js @@ -881,6 +881,7 @@ pipwerks.UTILS.trace = function(msg){ pipwerks.UTILS.MillisecondsToCMIDuration = function(n){ //Convert duration from milliseconds to 0000:00:00.00 format + n = (!n || n<0)? 0 : n; //default value and force positive duration var hms = ""; var dtm = new Date(); dtm.setTime(n); var h = "0" + Math.floor(n / 3600000); diff --git a/test/JavaScript/test.js b/test/JavaScript/test.js index d22bd4c..53394b4 100644 --- a/test/JavaScript/test.js +++ b/test/JavaScript/test.js @@ -10,6 +10,7 @@ QUnit.test("centisecsToISODuration", function (assert){ assert.equal(pipwerks.UTILS.centisecsToISODuration(null), 'PT0H0M0S'); assert.equal(pipwerks.UTILS.centisecsToISODuration(''), 'PT0H0M0S'); assert.equal(pipwerks.UTILS.centisecsToISODuration(0), 'PT0H0M0S'); + assert.equal(pipwerks.UTILS.centisecsToISODuration(-1), 'PT0H0M0S'); }); @@ -25,5 +26,6 @@ QUnit.test("MillisecondsToCMIDuration", function (assert) { assert.equal(pipwerks.UTILS.MillisecondsToCMIDuration(null), '00:00:00'); assert.equal(pipwerks.UTILS.MillisecondsToCMIDuration(''), '00:00:00'); assert.equal(pipwerks.UTILS.MillisecondsToCMIDuration(0), '00:00:00'); + assert.equal(pipwerks.UTILS.MillisecondsToCMIDuration(-1), '00:00:00'); }); \ No newline at end of file From 9401af2302c8bd715ff0c49cc3cc51811e16c553 Mon Sep 17 00:00:00 2001 From: canvasplay Date: Fri, 12 Jan 2018 08:27:46 +0100 Subject: [PATCH 10/17] fix typo 'subtract' --- src/JavaScript/SCORM_API_wrapper.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JavaScript/SCORM_API_wrapper.js b/src/JavaScript/SCORM_API_wrapper.js index fd4e28b..20fb842 100755 --- a/src/JavaScript/SCORM_API_wrapper.js +++ b/src/JavaScript/SCORM_API_wrapper.js @@ -379,7 +379,7 @@ pipwerks.SCORM.connection.terminate = function(){ var dtm = new Date(); - //in the next line you substract the time recorded when initialising + //in the next line you subtract the time recorded when initialising //the connection from the present time. var n = dtm.getTime() - dtmInitialized.getTime(); switch(scorm.version){ From 4b5763d62a6cd9a33a9d516458ff16239ef6871c Mon Sep 17 00:00:00 2001 From: canvasplay Date: Fri, 12 Jan 2018 08:33:15 +0100 Subject: [PATCH 11/17] move single comment into comment block --- src/JavaScript/SCORM_API_wrapper.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JavaScript/SCORM_API_wrapper.js b/src/JavaScript/SCORM_API_wrapper.js index 20fb842..974b18f 100755 --- a/src/JavaScript/SCORM_API_wrapper.js +++ b/src/JavaScript/SCORM_API_wrapper.js @@ -873,6 +873,7 @@ pipwerks.UTILS.trace = function(msg){ /* ------------------------------------------------------------------------- pipwerks.UTILS.MillisecondsToCMIDuration() Converts time to scorm 1.2 time format + Convert duration from milliseconds to 0000:00:00.00 format Parameters: n (number) Return: String @@ -880,7 +881,6 @@ pipwerks.UTILS.trace = function(msg){ pipwerks.UTILS.MillisecondsToCMIDuration = function(n){ - //Convert duration from milliseconds to 0000:00:00.00 format n = (!n || n<0)? 0 : n; //default value and force positive duration var hms = ""; var dtm = new Date(); dtm.setTime(n); From cccb97ab4278b7a2956206b30b2bdfe74a23e6fb Mon Sep 17 00:00:00 2001 From: canvasplay Date: Fri, 12 Jan 2018 08:46:45 +0100 Subject: [PATCH 12/17] fix formatting for readability --- src/JavaScript/SCORM_API_wrapper.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/JavaScript/SCORM_API_wrapper.js b/src/JavaScript/SCORM_API_wrapper.js index 974b18f..8ae1a87 100755 --- a/src/JavaScript/SCORM_API_wrapper.js +++ b/src/JavaScript/SCORM_API_wrapper.js @@ -881,14 +881,14 @@ pipwerks.UTILS.trace = function(msg){ pipwerks.UTILS.MillisecondsToCMIDuration = function(n){ - n = (!n || n<0)? 0 : n; //default value and force positive duration + n = (!n || n < 0)? 0 : n; //default value and force positive duration var hms = ""; var dtm = new Date(); dtm.setTime(n); var h = "0" + Math.floor(n / 3600000); var m = "0" + dtm.getMinutes(); var s = "0" + dtm.getSeconds(); - hms = h.substr(h.length-2)+":"+m.substr(m.length-2)+":"; - hms += s.substr(s.length-2); + hms = h.substr(h.length - 2) + ":"+ m.substr(m.length - 2) + ":"; + hms += s.substr(s.length - 2); return hms }; @@ -938,7 +938,7 @@ pipwerks.UTILS.centisecsToISODuration = function(n){ str += "T"; if (nH > 0) str += nH + "H"; if (nMin > 0) str += nMin + "M"; - if (nCs > 0) str += (nCs / 100) + "S"; + if (nCs > 0) str += (nCs / 100) + "S"; } if (str == "P") str = "PT0H0M0S"; // technically PT0S should do but SCORM test suite assumes longer form. From ab4e47b1df3f0dd01773c0848cb102c7b1845894 Mon Sep 17 00:00:00 2001 From: canvasplay Date: Fri, 12 Jan 2018 08:58:24 +0100 Subject: [PATCH 13/17] rename time util functions to msToCMIDuration and csToISODuration --- src/JavaScript/SCORM_API_wrapper.js | 12 +++++------ test/JavaScript/test.js | 32 ++++++++++++++--------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/JavaScript/SCORM_API_wrapper.js b/src/JavaScript/SCORM_API_wrapper.js index 8ae1a87..e4fe851 100755 --- a/src/JavaScript/SCORM_API_wrapper.js +++ b/src/JavaScript/SCORM_API_wrapper.js @@ -387,10 +387,10 @@ pipwerks.SCORM.connection.terminate = function(){ //the time format is different on scorm 1.2 or 2004, so we use //different conversions depending on the case case "1.2" : - this.set("cmi.core.session_time",pipwerks.UTILS.MillisecondsToCMIDuration(n)); + this.set("cmi.core.session_time",pipwerks.UTILS.msToCMIDuration(n)); break; case "2004": - this.set("cmi.session_time",pipwerks.UTILS.centisecsToISODuration(Math.floor(n/10))); + this.set("cmi.session_time",pipwerks.UTILS.csToISODuration(Math.floor(n/10))); break; } @@ -871,7 +871,7 @@ pipwerks.UTILS.trace = function(msg){ /* ------------------------------------------------------------------------- - pipwerks.UTILS.MillisecondsToCMIDuration() + pipwerks.UTILS.msToCMIDuration() Converts time to scorm 1.2 time format Convert duration from milliseconds to 0000:00:00.00 format @@ -879,7 +879,7 @@ pipwerks.UTILS.trace = function(msg){ Return: String ---------------------------------------------------------------------------- */ -pipwerks.UTILS.MillisecondsToCMIDuration = function(n){ +pipwerks.UTILS.msToCMIDuration = function(n){ n = (!n || n < 0)? 0 : n; //default value and force positive duration var hms = ""; @@ -895,14 +895,14 @@ pipwerks.UTILS.MillisecondsToCMIDuration = function(n){ /* ------------------------------------------------------------------------- - pipwerks.UTILS.centisecsToISODuration() + pipwerks.UTILS.csToISODuration() Converts time to scorm 2004 time format Parameters: n (number) Return: String ---------------------------------------------------------------------------- */ -pipwerks.UTILS.centisecsToISODuration = function(n){ +pipwerks.UTILS.csToISODuration = function(n){ // Note: SCORM and IEEE 1484.11.1 require centisec precision // Months calculated by approximation based on average number diff --git a/test/JavaScript/test.js b/test/JavaScript/test.js index 53394b4..b560097 100644 --- a/test/JavaScript/test.js +++ b/test/JavaScript/test.js @@ -1,31 +1,31 @@ -QUnit.test("centisecsToISODuration", function (assert){ +QUnit.test("csToISODuration", function (assert){ var t1 = 1513937992632; var t2 = 1513938095752; var d = t2 - t1; - assert.equal(pipwerks.UTILS.centisecsToISODuration(d), 'PT17M11.2S'); - assert.equal(pipwerks.UTILS.centisecsToISODuration(), 'PT0H0M0S'); - assert.equal(pipwerks.UTILS.centisecsToISODuration(undefined), 'PT0H0M0S'); - assert.equal(pipwerks.UTILS.centisecsToISODuration(null), 'PT0H0M0S'); - assert.equal(pipwerks.UTILS.centisecsToISODuration(''), 'PT0H0M0S'); - assert.equal(pipwerks.UTILS.centisecsToISODuration(0), 'PT0H0M0S'); - assert.equal(pipwerks.UTILS.centisecsToISODuration(-1), 'PT0H0M0S'); + assert.equal(pipwerks.UTILS.csToISODuration(d), 'PT17M11.2S'); + assert.equal(pipwerks.UTILS.csToISODuration(), 'PT0H0M0S'); + assert.equal(pipwerks.UTILS.csToISODuration(undefined), 'PT0H0M0S'); + assert.equal(pipwerks.UTILS.csToISODuration(null), 'PT0H0M0S'); + assert.equal(pipwerks.UTILS.csToISODuration(''), 'PT0H0M0S'); + assert.equal(pipwerks.UTILS.csToISODuration(0), 'PT0H0M0S'); + assert.equal(pipwerks.UTILS.csToISODuration(-1), 'PT0H0M0S'); }); -QUnit.test("MillisecondsToCMIDuration", function (assert) { +QUnit.test("msToCMIDuration", function (assert) { var t1 = 1513937992632; var t2 = 1513938095752; var d = t2 - t1; - assert.equal(pipwerks.UTILS.MillisecondsToCMIDuration(d), '00:01:43'); - assert.equal(pipwerks.UTILS.MillisecondsToCMIDuration(), '00:00:00'); - assert.equal(pipwerks.UTILS.MillisecondsToCMIDuration(undefined), '00:00:00'); - assert.equal(pipwerks.UTILS.MillisecondsToCMIDuration(null), '00:00:00'); - assert.equal(pipwerks.UTILS.MillisecondsToCMIDuration(''), '00:00:00'); - assert.equal(pipwerks.UTILS.MillisecondsToCMIDuration(0), '00:00:00'); - assert.equal(pipwerks.UTILS.MillisecondsToCMIDuration(-1), '00:00:00'); + assert.equal(pipwerks.UTILS.msToCMIDuration(d), '00:01:43'); + assert.equal(pipwerks.UTILS.msToCMIDuration(), '00:00:00'); + assert.equal(pipwerks.UTILS.msToCMIDuration(undefined), '00:00:00'); + assert.equal(pipwerks.UTILS.msToCMIDuration(null), '00:00:00'); + assert.equal(pipwerks.UTILS.msToCMIDuration(''), '00:00:00'); + assert.equal(pipwerks.UTILS.msToCMIDuration(0), '00:00:00'); + assert.equal(pipwerks.UTILS.msToCMIDuration(-1), '00:00:00'); }); \ No newline at end of file From a7d3bc19a6568d38d3a1fc03c1a3eb66d30b7317 Mon Sep 17 00:00:00 2001 From: canvasplay Date: Fri, 12 Jan 2018 09:47:55 +0100 Subject: [PATCH 14/17] Add reference to ISO code --- src/JavaScript/SCORM_API_wrapper.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JavaScript/SCORM_API_wrapper.js b/src/JavaScript/SCORM_API_wrapper.js index e4fe851..c146d2e 100755 --- a/src/JavaScript/SCORM_API_wrapper.js +++ b/src/JavaScript/SCORM_API_wrapper.js @@ -896,7 +896,7 @@ pipwerks.UTILS.msToCMIDuration = function(n){ /* ------------------------------------------------------------------------- pipwerks.UTILS.csToISODuration() - Converts time to scorm 2004 time format + Converts time to scorm 2004 time format using ISO 8601 Parameters: n (number) Return: String From 556ec5bdd1fc509a68137e999c506c5fa70e5b77 Mon Sep 17 00:00:00 2001 From: canvasplay Date: Fri, 12 Jan 2018 11:50:27 +0100 Subject: [PATCH 15/17] add more relevant tests and descriptions --- test/JavaScript/test.js | 44 ++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/test/JavaScript/test.js b/test/JavaScript/test.js index b560097..b073fc6 100644 --- a/test/JavaScript/test.js +++ b/test/JavaScript/test.js @@ -1,31 +1,31 @@ QUnit.test("csToISODuration", function (assert){ - var t1 = 1513937992632; - var t2 = 1513938095752; - var d = t2 - t1; - - assert.equal(pipwerks.UTILS.csToISODuration(d), 'PT17M11.2S'); - assert.equal(pipwerks.UTILS.csToISODuration(), 'PT0H0M0S'); - assert.equal(pipwerks.UTILS.csToISODuration(undefined), 'PT0H0M0S'); - assert.equal(pipwerks.UTILS.csToISODuration(null), 'PT0H0M0S'); - assert.equal(pipwerks.UTILS.csToISODuration(''), 'PT0H0M0S'); - assert.equal(pipwerks.UTILS.csToISODuration(0), 'PT0H0M0S'); - assert.equal(pipwerks.UTILS.csToISODuration(-1), 'PT0H0M0S'); + assert.equal(pipwerks.UTILS.csToISODuration(100), 'PT1S', 'check result for 1 second'); + assert.equal(pipwerks.UTILS.csToISODuration(100 * 61), 'PT1M1S', 'check result for 1 minute and 1 second'); + assert.equal(pipwerks.UTILS.csToISODuration(100 * 60 * 15), 'PT15M', 'check result for 15 minutes'); + assert.equal(pipwerks.UTILS.csToISODuration(100 * 60 * 90), 'PT1H30M', 'check result for 1 hour and 30 minutes'); + assert.equal(pipwerks.UTILS.csToISODuration(100 * 60 * 60 * 24 + 100 * 60 * 37 + 100 * 25), 'P1DT37M25S', 'check result for 1 day 37 minutes and 25 seconds'); + assert.equal(pipwerks.UTILS.csToISODuration(), 'PT0H0M0S', 'check no arguments is correctly handled'); + assert.equal(pipwerks.UTILS.csToISODuration(undefined), 'PT0H0M0S', 'check undefined is correctly handled'); + assert.equal(pipwerks.UTILS.csToISODuration(null), 'PT0H0M0S', 'check null is correctly handled'); + assert.equal(pipwerks.UTILS.csToISODuration(''), 'PT0H0M0S', 'check string is correctly handled'); + assert.equal(pipwerks.UTILS.csToISODuration(0), 'PT0H0M0S', 'check 0 is correctly handled'); + assert.equal(pipwerks.UTILS.csToISODuration(-1), 'PT0H0M0S', 'check negative numbers are correctly handled'); }); QUnit.test("msToCMIDuration", function (assert) { - var t1 = 1513937992632; - var t2 = 1513938095752; - var d = t2 - t1; - - assert.equal(pipwerks.UTILS.msToCMIDuration(d), '00:01:43'); - assert.equal(pipwerks.UTILS.msToCMIDuration(), '00:00:00'); - assert.equal(pipwerks.UTILS.msToCMIDuration(undefined), '00:00:00'); - assert.equal(pipwerks.UTILS.msToCMIDuration(null), '00:00:00'); - assert.equal(pipwerks.UTILS.msToCMIDuration(''), '00:00:00'); - assert.equal(pipwerks.UTILS.msToCMIDuration(0), '00:00:00'); - assert.equal(pipwerks.UTILS.msToCMIDuration(-1), '00:00:00'); + assert.equal(pipwerks.UTILS.msToCMIDuration(1000), '00:00:01', 'check result for 1 second'); + assert.equal(pipwerks.UTILS.msToCMIDuration(1000 * 61), '00:01:01', 'check result for 1 minute and 1 second'); + assert.equal(pipwerks.UTILS.msToCMIDuration(1000 * 60 * 15), '00:15:00', 'check result for 15 minutes'); + assert.equal(pipwerks.UTILS.msToCMIDuration(1000 * 60 * 90), '01:30:00', 'check result for 1 hour and 30 minutes'); + assert.equal(pipwerks.UTILS.msToCMIDuration(1000 * 60 * 60 * 24 + 1000 * 60 * 37 + 1000 * 25), '24:37:25', 'check result for 1 day 37 minutes and 25 seconds'); + assert.equal(pipwerks.UTILS.msToCMIDuration(), '00:00:00', 'check no arguments is correctly handled'); + assert.equal(pipwerks.UTILS.msToCMIDuration(undefined), '00:00:00', 'check undefined is correctly handled'); + assert.equal(pipwerks.UTILS.msToCMIDuration(null), '00:00:00', 'check null is correctly handled'); + assert.equal(pipwerks.UTILS.msToCMIDuration(''), '00:00:00', 'check string is correctly handled'); + assert.equal(pipwerks.UTILS.msToCMIDuration(0), '00:00:00', 'check 0 is correctly handled'); + assert.equal(pipwerks.UTILS.msToCMIDuration(-1), '00:00:00', 'check negative numbers are correctly handled'); }); \ No newline at end of file From 4cec5a65f93b693eaf4593e3ec79b4a49072e212 Mon Sep 17 00:00:00 2001 From: canvasplay Date: Mon, 15 Jan 2018 09:37:13 +0100 Subject: [PATCH 16/17] Add a couple of missing semicolons and fix all trailing spaces --- src/JavaScript/SCORM_API_wrapper.js | 96 ++++++++++++++--------------- 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/src/JavaScript/SCORM_API_wrapper.js b/src/JavaScript/SCORM_API_wrapper.js index c146d2e..e93f6d4 100755 --- a/src/JavaScript/SCORM_API_wrapper.js +++ b/src/JavaScript/SCORM_API_wrapper.js @@ -387,7 +387,7 @@ pipwerks.SCORM.connection.terminate = function(){ //the time format is different on scorm 1.2 or 2004, so we use //different conversions depending on the case case "1.2" : - this.set("cmi.core.session_time",pipwerks.UTILS.msToCMIDuration(n)); + this.set("cmi.core.session_time",pipwerks.UTILS.msToCMIDuration(n)); break; case "2004": this.set("cmi.session_time",pipwerks.UTILS.csToISODuration(Math.floor(n/10))); @@ -883,13 +883,13 @@ pipwerks.UTILS.msToCMIDuration = function(n){ n = (!n || n < 0)? 0 : n; //default value and force positive duration var hms = ""; - var dtm = new Date(); dtm.setTime(n); - var h = "0" + Math.floor(n / 3600000); - var m = "0" + dtm.getMinutes(); - var s = "0" + dtm.getSeconds(); - hms = h.substr(h.length - 2) + ":"+ m.substr(m.length - 2) + ":"; - hms += s.substr(s.length - 2); - return hms + var dtm = new Date(); dtm.setTime(n); + var h = "0" + Math.floor(n / 3600000); + var m = "0" + dtm.getMinutes(); + var s = "0" + dtm.getSeconds(); + hms = h.substr(h.length - 2) + ":"+ m.substr(m.length - 2) + ":"; + hms += s.substr(s.length - 2); + return hms; }; @@ -902,46 +902,46 @@ pipwerks.UTILS.msToCMIDuration = function(n){ Return: String ---------------------------------------------------------------------------- */ -pipwerks.UTILS.csToISODuration = function(n){ - - // Note: SCORM and IEEE 1484.11.1 require centisec precision - // Months calculated by approximation based on average number - // of days over 4 years (365*4+1), not counting the extra day - // every 1000 years. If a reference date was available, - // the calculation could be more precise, but becomes complex, - // since the exact result depends on where the reference date - // falls within the period (e.g. beginning, end or ???) - // 1 year ~ (365*4+1)/4*60*60*24*100 = 3155760000 centiseconds - // 1 month ~ (365*4+1)/48*60*60*24*100 = 262980000 centiseconds - // 1 day = 8640000 centiseconds - // 1 hour = 360000 centiseconds - // 1 minute = 6000 centiseconds - n = Math.max(n,0); // there is no such thing as a negative duration - var str = "P"; - var nCs = n; - // Next set of operations uses whole seconds - var nY = Math.floor(nCs / 3155760000); - nCs -= nY * 3155760000; - var nM = Math.floor(nCs / 262980000); - nCs -= nM * 262980000; - var nD = Math.floor(nCs / 8640000); - nCs -= nD * 8640000; - var nH = Math.floor(nCs / 360000); - nCs -= nH * 360000; - var nMin = Math.floor(nCs /6000); - nCs -= nMin * 6000 - // Now we can construct string - if (nY > 0) str += nY + "Y"; - if (nM > 0) str += nM + "M"; - if (nD > 0) str += nD + "D"; - if ((nH > 0) || (nMin > 0) || (nCs > 0)) { - str += "T"; - if (nH > 0) str += nH + "H"; - if (nMin > 0) str += nMin + "M"; - if (nCs > 0) str += (nCs / 100) + "S"; - } - if (str == "P") str = "PT0H0M0S"; - // technically PT0S should do but SCORM test suite assumes longer form. +pipwerks.UTILS.csToISODuration = function(n){ + + // Note: SCORM and IEEE 1484.11.1 require centisec precision + // Months calculated by approximation based on average number + // of days over 4 years (365*4+1), not counting the extra day + // every 1000 years. If a reference date was available, + // the calculation could be more precise, but becomes complex, + // since the exact result depends on where the reference date + // falls within the period (e.g. beginning, end or ???) + // 1 year ~ (365*4+1)/4*60*60*24*100 = 3155760000 centiseconds + // 1 month ~ (365*4+1)/48*60*60*24*100 = 262980000 centiseconds + // 1 day = 8640000 centiseconds + // 1 hour = 360000 centiseconds + // 1 minute = 6000 centiseconds + n = Math.max(n,0); // there is no such thing as a negative duration + var str = "P"; + var nCs = n; + // Next set of operations uses whole seconds + var nY = Math.floor(nCs / 3155760000); + nCs -= nY * 3155760000; + var nM = Math.floor(nCs / 262980000); + nCs -= nM * 262980000; + var nD = Math.floor(nCs / 8640000); + nCs -= nD * 8640000; + var nH = Math.floor(nCs / 360000); + nCs -= nH * 360000; + var nMin = Math.floor(nCs /6000); + nCs -= nMin * 6000; + // Now we can construct string + if (nY > 0) str += nY + "Y"; + if (nM > 0) str += nM + "M"; + if (nD > 0) str += nD + "D"; + if ((nH > 0) || (nMin > 0) || (nCs > 0)) { + str += "T"; + if (nH > 0) str += nH + "H"; + if (nMin > 0) str += nMin + "M"; + if (nCs > 0) str += (nCs / 100) + "S"; + } + if (str == "P") str = "PT0H0M0S"; + // technically PT0S should do but SCORM test suite assumes longer form. return str; }; From 8b3e524ab5d723cda08ddd53516bbe283ac19e3d Mon Sep 17 00:00:00 2001 From: canvasplay Date: Mon, 15 Jan 2018 09:39:05 +0100 Subject: [PATCH 17/17] Change default value for handleSessionTime to false --- src/JavaScript/SCORM_API_wrapper.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/JavaScript/SCORM_API_wrapper.js b/src/JavaScript/SCORM_API_wrapper.js index e93f6d4..01d5050 100755 --- a/src/JavaScript/SCORM_API_wrapper.js +++ b/src/JavaScript/SCORM_API_wrapper.js @@ -33,7 +33,7 @@ pipwerks.SCORM = { //Define the SCORM object version: null, //Store SCORM version. handleCompletionStatus: true, //Whether or not the wrapper should automatically handle the initial completion status handleExitMode: true, //Whether or not the wrapper should automatically handle the exit mode - handleSessionTime: true, //Whether or not the wrapper should automatically handle the session time + handleSessionTime: false, //Whether or not the wrapper should automatically handle the session time API: { handle: null, isFound: false }, //Create API child object connection: { isActive: false }, //Create connection child object