Forum Discussion
Retrieve LMS User Name as Variable
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!