Forum Discussion

AndrewRost's avatar
AndrewRost
Community Member
13 years ago

Retrieve LMS User Name as Variable

I know you can have a user fill in a text box with their name and then later use that variable for personalization. What we would like to do is programmatically retrieve the user name from the LMS. Is there a way to set a variable with data from the LMS when the course is loaded instead of having the user enter their name?

Thx

  • For anyone not using Docebo (which handles complex surnames like De Palma or Van Gogh) , Chat GPT recommended the following amendments to the code:

    var player = GetPlayer();

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

    var lmsAPI = findLMSAPI(this);
    var myName = lmsAPI.GetStudentName();
    if (myName.charAt(0) === ' ') {
    myName = myName.substring(1);
    }
    var regex = /([^,]+),(.+)/;
    var match = myName.match(regex);
    var lastName = match[1];
    var firstName = match[2];
    player.SetVar("firstName", firstName);
    player.SetVar("lastName", lastName);

  • Is anyone able to share a .story file example where it pulls in a users profile photo from an LMS also? The name comes through perfectly but I don't currently don't have the expertise to re-purpose this and pull in a users photo also. Many thanks everyone, this is the best resource community by far. 

    • PhilMayor's avatar
      PhilMayor
      Super Hero

      I don’t see how you can pull in a users photo there is nowhere in the scorm standard for this. It maybe you can pull a unique I’d from thee LMS you can compare against a company database and pull the picture from there, but you would need to use a web object as Storyline variables are finite and a base64 image would be too large.

      Sent from my iPhone

      • DarrenCampbell-'s avatar
        DarrenCampbell-
        Community Member

        Ahh thanks Phil, I asked chat GPT which gave me hope here, this is what it recommended

        ---------------------

        // Get the LMS API object
        var lmsAPI = parent.GetAPI();

        // Check if the LMS API is available
        if (lmsAPI) {
        // Get the current user's ID
        var userID = lmsAPI.GetStudentID();

        // Use the user ID to retrieve the profile photo URL
        var profilePhotoURL = getProfilePhotoURL(userID);

        // Update the profile photo in the Storyline content
        updateProfilePhoto(profilePhotoURL);
        } else {
        console.error("LMS API is not available.");
        }

        // Function to retrieve the profile photo URL for the given user ID
        function getProfilePhotoURL(userID) {
        // Add your code here to retrieve the profile photo URL from the LMS for the given user ID
        // Example:
        // return "https://example.com/profile-photos/" + userID + ".jpg";

        return "";
        }

        // Function to update the profile photo in the Storyline content
        function updateProfilePhoto(profilePhotoURL) {
        // Add your code here to update the profile photo in the Storyline content
        // Example:
        // document.getElementById("profile-photo").src = profilePhotoURL;
        }

        ----------------

        If anyone else or even I do get a working version going I'll add to this thread. 

  • JoeFrancis's avatar
    JoeFrancis
    Community Member

    A while back, Sam Coulson replied to a request surrounding retrieving data from an LMS. He included the list of variables LMSAPI.js addresses. These are industry-standard variables and functions, originally used by AICC-compliant LMS' and courses.

    As Sam says in his message, "this is advanced coder territory, reverse-engineer with caution."
    Help with JavaScript and pulling data from an LMS

    The SCORM Run-Time Reference Guide describes the data elements supported by the specification. Learner-demographic data (which your LMS may or may NOT support) is confined to:

    • cmi.learner_id: Identifies the learner on behalf of whom the SCO was launched
    • cmi.learner_name: Name provided for the learner by the LMS
    • cmi.learner_preference.audio_level: Specifies an intended change in perceived audio level
    • cmi.learner_preference.language: The learner’s preferred language for SCOs with multilingual capability
    • cmi.learner_preference.delivery_speed: The learner’s preferred relative speed of content delivery
    • cmi.learner_preference.audio_captioning: Specifies whether captioning text corresponding to audio is displayed

    While AICC does have several more learner-demographic data elements than SCORM does (including address, company, and years of experience), AICC, which SCORM was based on, did not support a data element for the learner's photo.

  • ZeeBengtson's avatar
    ZeeBengtson
    Community Member

    I've tried different combos of the suggested code, for example: 

    var player = GetPlayer();

    function findLMSAPI(win) {
        // Look in this window
        if (win.hasOwnProperty("GetStudentID")) return win;

        // All done if no parent
        else if (win.parent == win) return null;

        // Climb up to parent window and look in there
        else return findLMSAPI(win.parent);
    }

    var lmsAPI = findLMSAPI(this);
    if (lmsAPI) {
        // Attempt to get student name using SCORM2004_GetStudentName()
        var myName = lmsAPI.SCORM2004_GetStudentName();
        console.log("Returned student name:", myName);

        // Check if the name is in the expected format
        if (myName && myName.indexOf(',') !== -1) {
            var array = myName.split(',');
            var firstName = array[1].trim(); // Assuming first name is after the comma
            var lastName = array[0].trim(); // Assuming last name is before the comma
            var newName = firstName + ' ' + lastName;
            player.SetVar("newName", newName);
        } else {
            // Handle if the name is not in the expected format
            console.error("Unexpected format for student name:", myName);
        }
    } else {
        console.error("Unable to find LMS API");
    }

    *I included console outputs to try and see what my LMS was returning. I am getting a result but it's consistently "User Schoology"

    Does this mean that my LMS does not place the student first and last name in the expected field (cmi.core.student_name)/does not support GetStudentName() function? My knowledge is basic so any added insight is appreciated!