Skip to content

Commit

Permalink
fix rendering of time spent in SCORM cmi.session_time
Browse files Browse the repository at this point in the history
It was creating a Date object and using the getHours/Minutes/Seconds
methods, but `(new Date(0)).getHours() == 1`, so it was always an hour
off.

This replaces that with some simple arithmetic.
  • Loading branch information
christianp committed Nov 28, 2022
1 parent 9dddb2f commit accdb43
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 6 additions & 2 deletions runtime/scripts/scorm-storage.js
Expand Up @@ -684,8 +684,12 @@ SCORMStorage.prototype = /** @lends Numbas.storage.SCORMStorage.prototype */ {
*/
setSessionTime: function()
{
var timeSpent = new Date(this.exam.timeSpent*1000);
var sessionTime = 'PT'+timeSpent.getHours()+'H'+timeSpent.getMinutes()+'M'+timeSpent.getSeconds()+'S';
var timeSpent = this.exam.timeSpent;
var seconds = Math.floor(timeSpent % 60);
var minutes = Math.floor(timeSpent/60) % 60;
var hours = Math.floor(timeSpent/60/60);

var sessionTime = 'PT' + hours + 'H' + minutes + 'M' + seconds + 'S';
this.set('session_time',sessionTime);
},

Expand Down
8 changes: 6 additions & 2 deletions tests/numbas-runtime.js
Expand Up @@ -26253,8 +26253,12 @@ SCORMStorage.prototype = /** @lends Numbas.storage.SCORMStorage.prototype */ {
*/
setSessionTime: function()
{
var timeSpent = new Date(this.exam.timeSpent*1000);
var sessionTime = 'PT'+timeSpent.getHours()+'H'+timeSpent.getMinutes()+'M'+timeSpent.getSeconds()+'S';
var timeSpent = this.exam.timeSpent;
var seconds = Math.floor(timeSpent % 60);
var minutes = Math.floor(timeSpent/60) % 60;
var hours = Math.floor(timeSpent/60/60);

var sessionTime = 'PT' + hours + 'H' + minutes + 'M' + seconds + 'S';
this.set('session_time',sessionTime);
},

Expand Down

0 comments on commit accdb43

Please sign in to comment.