How to get the scorm events in JavaScript? Need to save the all events in Database

650 Views Asked by At

I'm trying to create an LMS Application using SCORM and I found complicated documentation on the SCORM website. Finally, I got some example codes and integrated them with my local system but nothing is working. https://drive.google.com/drive/folders/1FsnCRSQ4uersEU7EX5tLyp65GI4mN2J6?usp=sharing here the sample document is available. just download this folder and take the index file from the res folder. Angular or React working NPM's are accepted.

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Trivial SCORM Player Example</title>
  
    <script src="tsp.js"></script>
    <script>
    function supports_html5_storage() {
        try {
            return 'localStorage' in window && window['localStorage'] !== null;
        } catch (e) {
            return false;
        }
    }
    
    //users of old browsers will not be able to save their progress localy (but they will be able to store it server side)
    if (!supports_html5_storage()){
        window.localStorage = {};
    }
    
    tspInit(
            window,
            window.localStorage,
            //this has to be unique per each scorm you serve
            'SCORM_ID.',
            function(progress){
                //this will be called whenever student makes a progress in test.
                console.log(progress);
            });
     
    </script>
    
</head>
<body>
    <iframe 
        src="cb980550-6523-48ea-9825-23dca8bb0219/res/index.html"
        name="course" 
        frameborder="0" 
        style="overflow:hidden;overflow-x:hidden;overflow-y:hidden;height:600px;width:800px" 
        height="600"
        width="800">
    </iframe>
</body>

function tspInit(window, storage, prefix, callback){ 
    prefix = typeof prefix !== 'undefined' ? prefix : '';
    callback = typeof callback !== 'undefined' ? callback : console.log;
    debugger
    window.API = {};
    
    window.scormStatus = {
            lesson_status: '',
            score_raw: 0,
            score_max: 100,
            score_min: 0,
            session_time: 0,
            detailed_answers: {}
    }; 
    
    
    window.API.LMSInitialize = function(arg){
        var success = this.api.LMSInitialize(arg); 
        console.log(success, 'LMSInitialize');
    }
    window.API.LMSTerminate = function() {
        console.log('LMSTerminate');
    }
    
    window.API.LMSGetValue = function(varname) {
        varname = prefix + varname;
        var ret = storage.getItem(varname);
        if (ret == null && (varname.indexOf('_count', this.length - '_count'.length) !== -1)) {
            ret = 0;
            storage.setItem(varname, ret);
        }
        console.log('LMSGetValue', varname, '=', ret);
        return ret;
    }
    
    window.API.LMSSetValue = function(varname, varvalue) {
        varname = prefix + varname;
    
        var m = varname.match(/([0-9]+)\.cmi\.interactions\.([0-9]+)\.id/);
        if (m != null) {
            storage.setItem('{{scorm.id}}.cmi.interactions._count', parseInt(m[2]) + 1);
        }
    
        m = varname.match(/([0-9]+)\.cmi\.interactions\.([0-9]+)\.result/);
        if (m != null) {
            var key = storage.getItem(prefix + 'cmi.interactions.' + parseInt(m[2]) + '.id');
            window.scormStatus.detailed_answers[key] = varvalue;
        }
    
        if (varname == prefix + 'cmi.core.lesson_status')
            window.scormStatus.lesson_status = varvalue;
        if (varname == prefix + 'cmi.core.score.raw')
            window.scormStatus.score_raw = varvalue;
        if (varname == prefix + 'cmi.core.score.max')
            window.scormStatus.score_max = varvalue;
        if (varname == prefix + 'cmi.core.score.min')
            window.scormStatus.score_min = varvalue;
        if (varname == prefix + 'cmi.core.session_time')
            window.scormStatus.session_time = varvalue;
    
        storage.setItem(varname, varvalue); 
        console.log('LMSSetValue', varname, '=', varvalue);
    }
    
    window.API.LMSCommit = function() {
        console.log('LMSCommit');
        //saving to API
        callback(window.scormStatus);
        return true; 
    
    }
    
    window.API.LMSFinish = function() {
        console.log('LMSFinish');
    }
    
    window.API.LMSGetLastError = function() {
        console.log('LMSGetLastError');
    }
    
    window.API.LMSGetErrorString = function() {
        console.log('LMSGetErrorString');
    }
    
    window.API.LMSGetDiagnostic = function() {
        console.log('LMSGetDiagnostic');
    }
    
}

0

There are 0 best solutions below