Skip to content

Commit

Permalink
Merge pull request #6 from e-ucm/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Synpheros authored Jan 22, 2018
2 parents 4a18af9 + ed91aec commit 54ac4f6
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 9 deletions.
69 changes: 65 additions & 4 deletions dist/js-tracker.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -27310,8 +27310,47 @@ function TrackerAsset() {
});
};

this.setScore = function(value) {
this.addExtension('score', value);
this.setScore = function(raw, min, max, scaled) {
if (exists(raw)) {
this.setScoreRaw(raw);
}

if (exists(min)) {
this.setScoreMin(min);
}

if (exists(max)) {
this.setScoreMax(max);
}

if (exists(scaled)) {
this.setScoreScaled(scaled);
}
};

this.setScoreRaw = function(raw) {
this.setScoreValue('raw', raw);

};

this.setScoreMin = function(min) {
this.setScoreValue('min', min);
};

this.setScoreMax = function(max) {
this.setScoreValue('max', max);
};

this.setScoreScaled = function(scaled) {
this.setScoreValue('scaled', scaled);
};

this.setScoreValue = function(key, value) {
if (!exists(this.extensions.score)) {
this.extensions.score = {};
}

this.extensions.score[key] = Number(value);
};

this.setCompletion = function(value) {
Expand Down Expand Up @@ -27615,7 +27654,25 @@ TrackerEvent.TraceResult = function() {
var success = (this.Success !== null) ? ',success,' + this.Success.toString() : '';
var completion = (this.Completion !== null) ? ',completion,' + this.Completion.toString() : '';
var response = (this.Response) ? ',response,' + this.Response.replaceAll(',', '\\,') : '';
var score = (this.Score !== null) ? ',score,' + this.Score.toString() : '';
var score = '';

if (exists(this.Score)) {
if (exists(this.Score.raw)) {
score += ',score,' + this.Score.raw;
}

if (exists(this.Score.min)) {
score += ',score_min,' + this.Score.min;
}

if (exists(this.Score.max)) {
score += ',score_max,' + this.Score.max;
}

if (exists(this.Score.scaled)) {
score += ',score_scaled,' + this.Score.scaled;
}
}

var result = success + completion + response + score;

Expand Down Expand Up @@ -27667,7 +27724,7 @@ TrackerEvent.TraceResult = function() {
}

if (this.Score !== null) {
ret.score = {raw: Number(this.Score)};
ret.score = this.Score;
}

if (this.Extensions !== null && obsize(this.Extensions) > 0) {
Expand Down Expand Up @@ -27845,6 +27902,10 @@ Array.prototype.dequeue = function(n) {
return tmp;
};

var exists = function(value) {
return !(typeof value === 'undefined' || value === null);
};

module.exports = TrackerAsset;

/***/ })
Expand Down
2 changes: 1 addition & 1 deletion dist/js-tracker.bundle.min.js

Large diffs are not rendered by default.

69 changes: 65 additions & 4 deletions src/js-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,47 @@ function TrackerAsset() {
});
};

this.setScore = function(value) {
this.addExtension('score', value);
this.setScore = function(raw, min, max, scaled) {
if (exists(raw)) {
this.setScoreRaw(raw);
}

if (exists(min)) {
this.setScoreMin(min);
}

if (exists(max)) {
this.setScoreMax(max);
}

if (exists(scaled)) {
this.setScoreScaled(scaled);
}
};

this.setScoreRaw = function(raw) {
this.setScoreValue('raw', raw);

};

this.setScoreMin = function(min) {
this.setScoreValue('min', min);
};

this.setScoreMax = function(max) {
this.setScoreValue('max', max);
};

this.setScoreScaled = function(scaled) {
this.setScoreValue('scaled', scaled);
};

this.setScoreValue = function(key, value) {
if (!exists(this.extensions.score)) {
this.extensions.score = {};
}

this.extensions.score[key] = Number(value);
};

this.setCompletion = function(value) {
Expand Down Expand Up @@ -781,7 +820,25 @@ TrackerEvent.TraceResult = function() {
var success = (this.Success !== null) ? ',success,' + this.Success.toString() : '';
var completion = (this.Completion !== null) ? ',completion,' + this.Completion.toString() : '';
var response = (this.Response) ? ',response,' + this.Response.replaceAll(',', '\\,') : '';
var score = (this.Score !== null) ? ',score,' + this.Score.toString() : '';
var score = '';

if (exists(this.Score)) {
if (exists(this.Score.raw)) {
score += ',score,' + this.Score.raw;
}

if (exists(this.Score.min)) {
score += ',score_min,' + this.Score.min;
}

if (exists(this.Score.max)) {
score += ',score_max,' + this.Score.max;
}

if (exists(this.Score.scaled)) {
score += ',score_scaled,' + this.Score.scaled;
}
}

var result = success + completion + response + score;

Expand Down Expand Up @@ -833,7 +890,7 @@ TrackerEvent.TraceResult = function() {
}

if (this.Score !== null) {
ret.score = {raw: Number(this.Score)};
ret.score = this.Score;
}

if (this.Extensions !== null && obsize(this.Extensions) > 0) {
Expand Down Expand Up @@ -1011,4 +1068,8 @@ Array.prototype.dequeue = function(n) {
return tmp;
};

var exists = function(value) {
return !(typeof value === 'undefined' || value === null);
};

module.exports = TrackerAsset;
41 changes: 41 additions & 0 deletions test/xapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,45 @@ describe('TrackerAsset xAPI Tests', function() {
expect(tracejson.result.extensions.extension3).to.equal(3);
expect(tracejson.result.extensions.extension4).to.equal(4.56);
});

it('Test Scores', function() {
tracker.setScore(1.1,2.2,3.3,4.4);
var t = tracker.Alternative.Selected('a1','o1');
var tracejson = JSON.parse(t.ToXapi());

expect(obsize(tracejson)).to.equal(5);
expect(tracejson.object.id).to.contain('a1');
expect(tracejson.verb.id).to.equal('https://w3id.org/xapi/adb/verbs/selected');

expect(obsize(tracejson.result)).to.equal(2);
expect(tracejson.result.response).to.equal('o1');
expect(tracejson.result.score.raw).to.equal(1.1);
expect(tracejson.result.score.min).to.equal(2.2);
expect(tracejson.result.score.max).to.equal(3.3);
expect(tracejson.result.score.scaled).to.equal(4.4);
});

it('Test Score Raw', function() {
tracker.setScore(1.1);
var t = tracker.Alternative.Selected('a1','o1');
var tracejson = JSON.parse(t.ToXapi());
expect(tracejson.result.score.raw).to.equal(1.1);
});

it('Test Score Raw and Max', function() {
tracker.setScoreRaw(1.1);
tracker.setScoreMax(3.3);
var t = tracker.Alternative.Selected('a1','o1');
var tracejson = JSON.parse(t.ToXapi());
expect(tracejson.result.score.max).to.equal(3.3);
});

it('Test Score Min and Scaled', function() {
tracker.setScoreMin(2.2);
tracker.setScoreScaled(4.4);
var t = tracker.Alternative.Selected('a1','o1');
var tracejson = JSON.parse(t.ToXapi());
expect(tracejson.result.score.min).to.equal(2.2);
expect(tracejson.result.score.scaled).to.equal(4.4);
});
});

0 comments on commit 54ac4f6

Please sign in to comment.