编辑代码

/**
 * API类;定义了SCORM标准里的8个api
 */
class SCORMAPI {
    constructor() {
        this.LMSInitialize = this.LMSInitializeMethod;
        this.LMSGetValue = this.LMSGetValueMethod;
        this.LMSSetValue = this.LMSSetValueMethod;
        this.LMSCommit = this.LMSCommitMethod;
        this.LMSFinish = this.LMSFinishMethod;
        this.LMSGetLastError = this.LMSGetLastErrorMethod;
        this.LMSGetErrorString = this.LMSGetErrorStringMethod;
        this.LMSGetDiagnostic = this.LMSGetDiagnosticMethod;
    }

    //初始化通信
    LMSInitializeMethod() {

        return 'true';
    }

    //get数据
    LMSGetValueMethod(parameter) {
        console.log(`Get ${parameter} value: ` + scormData.getValue(parameter));
        return scormData.getValue(parameter);
    }

    //set数据
    LMSSetValueMethod(parameter, value) {
        console.log(`Set ${parameter} value  ${value}: ` + scormData.setValue(parameter, value));
        return scormData.setValue(parameter, value);
    }

    //指示LMS提交所有数据:这里需要实现将datamodel的数据存到数据库
    LMSCommitMethod() {
        let allData = scormData.getAllData();
        console.log('allData', allData);
        return 'true';
    }

    //结束与LMS的通信会话,这里通常会先调用commit再进行finish
    LMSFinishMethod() {
        console.log('结束通信,上传数据');

        return 'true';
    }

    //返回上次API调用导致的错误代码
    LMSGetLastErrorMethod() {

    }

    //返回描述指定错误代码的短字符串
    LMSGetErrorStringMethod() {

    }

    //返回有关上次发生错误的详细信息
    LMSGetDiagnosticMethod() {

    }
}


/**
 * Data Model类,定义常用model以及get和set方法
 */
class SCORMDataModel {
    constructor() {
        this.data = {
            "cmi.core.student_id": "",
            "cmi.core.student_name": "",
            "cmi.core.lesson_location": "",
            "cmi.core.score.raw": "",
            "cmi.core.score.max": "",
            "cmi.core.score.min": "",
            "cmi.core.credit": "",
            "cmi.core.lesson_status": "",
            "cmi.core.session_time": "",
            "cmi.core.total_time": "",
            "cmi.core.exit": "",
            "cmi.launch_data": "",
            // 添加其他常用的数据模型元素
        };
    }

    getValue(parameter) {
        if (this.data.hasOwnProperty(parameter)) {
            return this.data[parameter];
        }
        return "";
    }

    setValue(parameter, value) {
        if (this.data.hasOwnProperty(parameter)) {
            this.data[parameter] = value;
            return 'true';
        }
        return 'false';
    }

    getAllData() {
        return this.data;
    }
}

let scormData = new SCORMDataModel();
var API = new SCORMAPI();

console.log('API', API);
console.log('API.LMSInitializeMethod:', API.LMSInitializeMethod());

API.LMSGetValue("cmi.core.lesson_location", 'test/index.html');
API.LMSGetValue("cmi.core.lesson_location");

API.LMSCommit();
API.LMSFinish();