Skip to content

Commit

Permalink
Bug: score logic (adaptlearning#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielghost committed Apr 3, 2023
1 parent c1b09c1 commit 898fa80
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions js/scorm/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,18 +212,34 @@ class ScormWrapper {
return this.getValue(this.isSCORM2004() ? 'cmi.score.raw' : 'cmi.core.score.raw');
}

setScore(score, minScore = 0, maxScore = 100) {
setScore(score, minScore = 0, maxScore = 100, isPercentageBased = true) {
if (this.isSCORM2004()) {
// `raw`, `min`, `max` sum absolute values assigned to questions
this.setValue('cmi.score.raw', score);
this.setValue('cmi.score.min', minScore);
this.setValue('cmi.score.max', maxScore);

const range = maxScore - minScore;
const scaledScore = ((score - minScore) / range).toFixed(7);
this.setValue('cmi.score.scaled', scaledScore);
// range split into negative/positive ranges (rather than minScore-maxScore) depending on score
const range = (score < 0) ? Math.abs(minScore) : maxScore;
// `scaled` converted to -1-1 range to indicate negative/positive weighting now that negative values can be assigned to questions
const scaledScore = score / range;
this.setValue('cmi.score.scaled', scaledScore.toFixed(7));
return;
}
// SCORM 1.2
if (isPercentageBased) {
// convert values to 0-100 range
// negative scores are capped to 0 due to SCORM 1.2 limitations
score = (score < 0) ? 0 : Math.round((score / maxScore) * 100);
minScore = 0;
maxScore = 100;
} else {
const validate = (attribute, value) => {
const isValid = value >= 0 && score <= 100;
if (!isValid) this.logger.warn(`${attribute} must be between 0-100.`);
}
validate('cmi.core.score.raw', score);
validate('cmi.core.score.min', minScore);
validate('cmi.core.score.max', maxScore);
}
this.setValue('cmi.core.score.raw', score);
if (this.isSupported('cmi.core.score.min')) this.setValue('cmi.core.score.min', minScore);
if (this.isSupported('cmi.core.score.max')) this.setValue('cmi.core.score.max', maxScore);
Expand Down

0 comments on commit 898fa80

Please sign in to comment.