/*--
Copyright © 1998, 2012, Oracle and/or its affiliates.  All rights reserved.
--*/

// include lms_init.js
/*
document.write('<script type="text/javascript" src="../../../js/lms_init.js"></script>');
*/
// callback functions from toc

var lms_objectives = new Array();
var lms_questionstatus = new Array();
var lms_astatus;    // status for button inside assessment template
var lms_stateDiscarded = false; // saved state discarded (assessment changed)
var _interaction_index = -1;
var _objectivesWritten = false;

function lms_LoadAdapterComplete() {
    __Ialert("lms_LoadAdapterComplete() called");

    var lmscom = document.LmsCom;
    lmscom.Begin();
    var parentlmscom = lmscom.ParentLmsCom;
    if (parentlmscom && parentlmscom.itemCount) {
        lmscom.OpenAssessmentStatus(parentlmscom.itemCount, parentlmscom.hash);
        lms_questionstatus = lmscom.status_obj.questionstatus;
    } else
        lmscom.OpenBasicStatus();

    lms_astatus = lmscom.status_obj.getStatus();
    lms_stateDiscarded = lmscom.status_obj.stateDiscarded;
    if (lms_stateDiscarded)
        lmscom.doSetUPKValue("upk.reset_runtime", 1);
    lms_UpdateUI(lms_astatus);
    lms_initialized = true;
    setTimeout(lms_store.callbackfunction, 1);
}

function GetChildLmsCom(child_store) {
    __Ialert("lms_GetChildLmsCom() called");
    var lmscom = document.LmsCom;
    if (lmscom.status_obj.getStatus() != LMS_INCOMPLETE) {  // take or retake the assessment
        lmscom.status_obj.setStatus(LMS_INCOMPLETE);
        lmscom.SaveStatus();
        lmscom.NotifyParent();
        
        var parentlmscom = lmscom.ParentLmsCom;
        if (parentlmscom && parentlmscom.itemCount) {
            for (var i=0; i<parentlmscom.itemCount; i++)
                lms_questionstatus[i] = "N";
        }
    }
    lmscom.doSetUPKValue("upk.enable_next",0);
    lmscom.doSetUPKValue("upk.enable_prev",0);
    if (lms_IsPersonalCourseEnabled())
        lmscom.doSetUPKValue("upk.show_personal",0);
    lmscom.doUPKCommit();
    var childlmscom = child_store.window.document.LmsCom = Kpath_launch ? new LmsComKPath(lmscom) : new LmsComBase(lmscom);
    childlmscom.owner = child_store.window;
    childlmscom.SetOrdinalNumber(parseInt(child_store.childIndex, 10));
    childlmscom.lesson_data = "";
    childlmscom.assessment_guid = lms_store.cguid;  // tell question what assessment it is part of
    lmscom.child = childlmscom;
    _interaction_index = -1;
}

function ListenChildClose() {
    __alert("lms_ListenChildClose() called");
 
    var lmscom = document.LmsCom;
    var childlmscom = lmscom.child;
    if (childlmscom.lesson_status != LMS_NOT_ATTEMPTED && childlmscom.lesson_status != LMS_INCOMPLETE) {
        var index = childlmscom.GetOrdinalNumber();
        var state = Lms2Stat(childlmscom.lesson_status);
        lms_questionstatus[index] = state;
        lmscom.SaveStatus();    // to set the lesson_data
        lmscom.doSetUPKValue("upk.sco_data", lmscom.lesson_data);
        _interaction_index = lmscom.setInteraction(_interaction_index,childlmscom);
    }
}

// asset specific processing called from lms_init on ClosePage
function _closePage() {
    var lmscom = document.LmsCom;
    if (_objectivesWritten) // if objectives written in summary, may have been changed by user
        for (var i=0; i<lms_objectives.length; i++)
            lmscom.setObjective(i+1,lms_objectives[i]);
}

function lms_getQuestionStatus(index) {
    if (lms_questionStatus[index])
        return lms_questionStatus[index];
    return "N";
}

function lms_IsPersonalCourseEnabled() {
    __alert("lms_IsPersonalCourseEnabled() called");
    return document.LmsCom.doGetUPKValue("upk.allow_personal_course");
}

function lms_IsRemediationEnabled() {
    __alert("lms_IsRemediationEnabled() called");
    return document.LmsCom.doGetUPKValue("upk.feedback_enabled");
}

function lms_GetRemediationMode() {
    __alert("lms_GetRemediationInfo called");
    if (GetTopLevelLmsMode() === "KPT")
        return -1;
    return document.LmsCom.doGetUPKValue("upk.feedback_mode");
}

function lms_GetShowRelated() {
    __alert("lms_GetShowRelated called");
    return document.LmsCom.doGetUPKValue("upk.show_related");
}

// lms_GetQuestionOrdering
function lms_GetQuestionOrdering() {
    __alert("lms_GetQuestionOrdering called");
    return document.LmsCom.doGetUPKValue("upk.question_mode");
}

// lms_GetAnswerOrdering
function lms_GetAnswerOrdering() {
    __alert("lms_GetAnswerOrdering called");
    return document.LmsCom.doGetUPKValue("upk.answer_mode");
}

// lms_IsSummaryEnabled
function lms_IsSummaryEnabled() {
    __alert("lms_IsSummaryEnabled called");
    return document.LmsCom.doGetUPKValue("upk.summary");
}

// lms_GetRequiredScore
function lms_GetRequiredScore() {
    __alert("lms_GetRequiredScore called");
    var rs = document.LmsCom.doGetUPKValue("upk.required_score");
    return rs < 0 ? -1 : rs * 100;
}

// lms_getAnsweredQuestions
// lms returns the question list are already answered
function lms_getAnsweredQuestions() {
    __alert("lms_getAnsweredQuestions called");
    return _answeredQuestions;
}

// lms_GetAssessmentType
// 0 - inline; 1 - preassessment; 2 - postassessment
function lms_GetAssessmentType() {
    return document.LmsCom.doGetUPKValue("upk.assess_type");
}

// lms_GetContinueAvailable
function lms_GetContinueAvailable() {
    return lms_GetAssessmentType() > 0;
}

// lms_OnContinue
function lms_OnQuestionContinue() {
    var lmscom = document.LmsCom;
    lmscom.requestContinue();
    // the following is NOT the same as lmscom.NotifyParent
    var parentlmscom = lmscom.ParentLmsCom;
    if (parentlmscom && !parentlmscom.owner.closed && parentlmscom.owner.lms_ClosePage)
        parentlmscom.owner.lms_ClosePage();
}

// lms_ObjectiveObj
function lms_ObjectiveObj(objectiveGuid, score, completed) {
    __alert("lms_ObjectiveObj called");
    this.objectiveGuid = objectiveGuid;
    this.score = score;
    this.completed = completed;
}

// lms_summaryReport1
// assessment calls this function before summary page appears
// should be sent score and pass/fail, though I could compute that myself if I knew
// what the passing score was
function lms_summaryReport1(finalscore, passed) {
    __alert("lms_summaryReport1 called");
  
    // close off last question
    var lmscom = document.LmsCom;
    lmscom.doSetUPKValue("upk.enable_next",1);
    lmscom.doSetUPKValue("upk.enable_prev",1);
    if (lms_IsPersonalCourseEnabled())
        lmscom.doSetUPKValue("upk.show_personal",1);
    lmscom.doUPKCommit();
    var childlmscom = lmscom.child;
    if (childlmscom && childlmscom.session_active && childlmscom.owner && !childlmscom.owner.closed)
        childlmscom.owner.lms_ClosePage();
    if (lmscom.status_obj.getStatus() == LMS_INCOMPLETE)    // take or retake the assessment
        lmscom.status_obj.setStatus(passed ? LMS_PASSED : LMS_FAILED);
    lmscom.status_obj.setScore(finalscore);
    lmscom.setObjective(0,new lms_ObjectiveObj("PRIMARYOBJ",finalscore,passed));
    for (var i=0; i<lms_objectives.length; i++)
        lmscom.setObjective(i+1,lms_objectives[i]); 
    _objectivesWritten = true;  
    // report back to the lms now
    lmscom.SaveStatus();
    lmscom.NotifyParent();
}

//----------------------------------------------------------------------------------------------/

var _lms_module_name = "lms_assessment.js";
var _lms_show_alert = false;
var _lms_show_Ialert = false;
