Forum Discussion
Retrieve LMS User Name as Variable
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);
- OwenHolt2 years agoSuper Hero
But what if the last name is complex? de Jesus, Von Trapp, Van Gogh??????
- Jonathan_Hill2 years agoSuper Hero
That was my concern too - but on my LMS it appears this code parses the name from only the first break. So Brian De Palma is split into Brian and De Palma. As always with Javascript, results may vary but this worked for me on Docebo.