Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
template-scorm/js/script.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
79 lines (72 sloc)
2.85 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MIN_SCORE = 0 | |
let MAX_SCORE = 100 | |
document.addEventListener('DOMContentLoaded', event => { | |
console.log('DOM CONTENT LOADED') | |
document.querySelector('button#update').addEventListener('click', event => { | |
console.log('UPDATE BUTTON CLICKED') | |
const score = parseInt(document.querySelector('input').value) | |
updateScore(score) | |
}) | |
document.querySelector('button#end').addEventListener('click', event => { | |
console.log('END BUTTON CLICKED') | |
document.querySelector('section').hidden = true | |
endGame(parseInt(document.querySelector('input').value)) | |
ScormProcessFinish() | |
}) | |
}) | |
function updateScore(score) { | |
score = parseInt(score) | |
console.log(`score: ${score}`) | |
const percent = (score / MAX_SCORE) * 100 | |
if(!isNaN(score) && score >= MIN_SCORE && score <= MAX_SCORE) { // is the score a number in a valid range | |
ScormProcessSetValue('cmi.core.score.raw', percent) | |
} else { | |
console.warn(`invalid score (${score}): needs to be a number between ${MIN_SCORE} and ${MAX_SCORE}`) | |
} | |
return percent | |
} | |
function endGame(score) { | |
const percent = updateScore(score) | |
document.querySelector('h2').innerText = `${percent}%` | |
// set the status | |
const mastery = parseInt(ScormProcessGetValue('cmi.student_data.mastery_score')) | |
console.log(`mastery: (${mastery})`) | |
if(!isNaN(mastery)) { // NaN if mastery score not provided by the API | |
const status = percent >= mastery ? 'passed' : 'failed' // if mastery score then pass/fail | |
console.log(`status: (${status})`) | |
ScormProcessSetValue('cmi.core.lesson_status', status) | |
document.querySelector('h3').innerText = `status: ${status.toUpperCase()}` | |
} else { | |
console.log('status is: (completed)') | |
ScormProcessSetValue('cmi.core.lesson_status', 'completed') // if no mastery then completed | |
} | |
} | |
window.addEventListener('load', event => { | |
console.log('LOAD') | |
ScormProcessInitialize() | |
const completionStatus = ScormProcessGetValue('cmi.core.lesson_status') | |
if (completionStatus == 'not attempted') { | |
console.log('not attempted') | |
ScormProcessSetValue('cmi.core.lesson_status', 'incomplete') | |
} | |
const name = ScormProcessGetValue('cmi.core.student_name') | |
console.log(`student name: ${name}`) | |
document.querySelector('p').innerText = name | |
const username = ScormProcessGetValue('cmi.core.student_id') | |
console.log(`student ID: ${username}`) | |
const maxScore = parseInt(ScormProcessGetValue('cmi.core.score.max')) // does the LMS provide a max score? | |
console.log(`moodle max score: (${maxScore})`) | |
ScormProcessSetValue('cmi.core.score.min', MIN_SCORE) | |
MAX_SCORE = !isNaN(maxScore) ? maxScore : MAX_SCORE | |
ScormProcessSetValue('cmi.core.score.max', MAX_SCORE) | |
}) | |
window.addEventListener('beforeunload', event => { | |
console.log('BEFOREUNLOAD') | |
ScormProcessSetValue('cmi.core.exit', '') | |
ScormProcessFinish() | |
}) | |
window.addEventListener('unload', event => { | |
console.log('UNLOAD') | |
ScormProcessSetValue("cmi.core.exit", "") | |
ScormProcessFinish() | |
}) |