Retrieve LMS User Name as Variable

Jul 09, 2012

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

215 Replies
Kyle Mullaney
Steve Flowers

Ah. The very first one posted was Flash only. However, HTML5 output supports the second one posted no problem. Has for awhile:)

Only works in Flash:

player.SetVar("sLearnerName", SCORM_GetStudentName());

 

Works in all:

var player = GetPlayer();
//might need to use lmsAPI=parent; on some LMS
var myName  = lmsAPI.GetStudentName();
var array  = myName.split(',');
var newName = array[1] + '  ' + array[0];
player.SetVar("newName", newName);

This is not working in HTML5 on Sakai. It does work in flash on Sakai.

Lucio P

If ever the "Works in all" portion doesn't work as is, try preceding it with the following script:

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 & look there
else return findLMSAPI(win.parent);
}

var lmsAPI = findLMSAPI(this);

… and the rest following "...GetPlayer() …"

phil Mek

Hello everybody,

I read every single message in this thread, and in some others.

But couldn't find what I am looking for :

My client LMS (Talentsoft) in SCORM 2004, can display cmi.learner_name as a variable.

I tried many many many syntaxes to grab this data, but I guess the only working one wasn't in my scope !

Can anymone help me ? what is the syntaxt to grab this value ?

thank you for the help

Philippe

 

phil Mek

Hello again,

Matthew Bibby gave me the answer. It works perfectly in Talensoft LMS.

Be aware that  I only needed the firstname.

Here is his code to close my question.

 

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 name = lmsAPI.SCORM2004_GetStudentName();
var nameArray = name.split(',');
var firstName = nameArray[1];
// var lastName = nameArray[0];
// var fullName = firstName + ' ' + lastName;
var player = GetPlayer();
player.SetVar("Name",firstName);
 

Philippe

Jonathan Hill

Hey all!  I've been using variations of this code for a while now to add my learner's name to in-course instructions and certificates and it's been very useful.  Thanks all!

However, I've found that platforms such as Litmos, Docebo, and Cornerstone, tend to add an unwanted space before the first name.

In the past I've worked around this by reducing the left margin on the text box that displays the variable, as I'm not really a coder.  But recently I've been 'chatting' with ChatGPT to analyze and refine code. 

During a recent chat, ChatGPT provided this updated code.  It pulls the user name from the LMS, removes any spaces, and splits it into First Name and Last Name.

And it works!

var player = GetPlayer();

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

function removeSpaces(name) {
  return name.replace(/\s/g, '');
}

var lmsAPI = findLMSAPI(this);
var myName = lmsAPI.GetStudentName();
if (myName.charAt(0) === ' ') {
    myName = myName.substring(1);
}
var array = myName.split(',');
var firstName = removeSpaces(array[1]);
var lastName = removeSpaces(array[0]);
player.SetVar("FirstName", firstName);
player.SetVar("LastName", lastName);

Jonathan Hill

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);

Phil Mayor

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

Darren Campbell

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. 

Joseph Francis

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.

Zee Bee

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!