Forum Discussion

TristanDF's avatar
TristanDF
Community Member
11 days ago

Sending Points to the LMS using Javascript

I have a quiz that contains 14 free-form questions and one custom built question for a total of 15 points.

Variable: customScore stores the custom question result.

Variable: quiz.ScorePoints stores the built in quiz result.

Variable: finalScore combines both values.

On the result slide i can see all of these variables working as intended and being combined correctly. When i try and print the score to Moodle however the score repeatedly returns 0.

I have played around with switching Results.ScorePercent with quiz.ScorePercent and quiz.ScorePoints without luck. 

Can anyone see any error in the script?

Here is the code:

        var player = GetPlayer();
        var customScore = player.GetVar("customScore");
        var quizScore = player.GetVar("Results.ScorePercent");
        var finalScore = quizScore + customScore;

        // Set the final score (0-100 scale)
        lmsAPI.SetScore(finalScore, 100, 0);

        // Mark the course as completed
        lmsAPI.SetReachedEnd();

  • Better to share a sample...as is this is touch to debug.
    One thing i always use in my Javascripts calling the lmsAPI is this function.

    var lmsAPI = findLMSAPI(this);
    
    function findLMSAPI(win) {
    if (win.hasOwnProperty("GetStudentID")) {
                return win;
        }else if (win.parent == win) {
            return null;
        }else {
            return findLMSAPI(win.parent);
        }
    }



    As different LMS's handle the lmsAPI different. This to ensure you have it available.