Forum Discussion

TristanDF's avatar
TristanDF
Community Member
2 months 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();

  • I'm just adding this here as I'll forget about it otherwise. There is an undocumented way to access the "built in" variables in storyline.

    For example, a built in variable such as Menu.Progress, can be accessed like this:

    const player = GetPlayer();
    // Get Menu.Progress variable from Storyline
    const progress = player.GetVar('_playerVars.#menuProgress');

    Use the above logic, you should then be able to access a system variable such as Results.ScorePercent in the following way:

    const player = GetPlayer();
    // Get Results.ScorePercent variable from Storyline
    const progress = player.GetVar('_playerVars.#resultsScorePercent');

     

  • TristanDF In case anybody else stumbles across this, I can see an issue with this piece of code:

    var quizScore = player.GetVar("Results.ScorePercent");

    Storyline wont allow you to access these pre-defined variables. In order to access the "Results.ScorePercent" variable, in Storyline, you would need to define a new variable to assign the value of "Results.ScorePercent" to. For example "ResultsScorePercent", and then assign the value of "Results.ScorePercent" to it. You can then use this:

    var quizScore = player.GetVar("ResultsScorePercent");

    This would have been a major issue in the above code, as "quizScore" would not have been set with the expected value. Possible an "undefined" or "null".

    A good method is to use your browser console to track the values of all the variables you are working with, so you can see where the script is going wrong.

    var player = GetPlayer();
    var customScore = player.GetVar("customScore");
    console.log("customScore",customScore);
    var quizScore = player.GetVar("Results.ScorePercent");
    console.log("quizScore",quizScore);
    var finalScore = quizScore + customScore;
    console.log("finalScore",finalScore);
    // Set the final score (0-100 scale)
    lmsAPI.SetScore(finalScore, 100, 0);
    // Mark the course as completed
    lmsAPI.SetReachedEnd();

     

    • MathNotermans-9's avatar
      MathNotermans-9
      Community Member

      Sam is right. Always use custom variables when setting / getting with javascript.

  • Maybe send the story so we can check...
    Let me share what I used...

    var player=GetPlayer();

    lmsAPI.SetScore(player.GetVar(‘Score’),100,80);

    lmsAPI.CommitData();

  • 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.