Get learner name from LMS with JavaScript, for Captivate and Storyline

Getting the learner name from the LMS is a nice wee trick to personalise a course (used sparingly!)

Using whatever API (in this instance I was using Pipwerks SCORM 1.2 demo shell),

var learnerName = get('cmi.core.student_name');

The SCORM standard stores the name as LastName, FirstName MiddleInitial, so the name comes out as ‘Blair, Lawrence’ for example. You need to javascript it out:

function parseName(learnerName) {
  var arr = learnerName.split(",");
  var firstName = arr[1];
  var lastName = arr[0];
}

If your system stores a middle name you’ll need to lob the last initial off too.

A neat wee trick, aye %firstName%!

Here’s another example using javascript in Storyline, using the Storyline API, getting the learner name:

var player = GetPlayer();
var myName = lmsAPI.GetStudentName();
var array = myName.split(',');
var newName = array[1] + ' ' + array[0];
player.SetVar("studentFirstName", array[1]);
player.SetVar("studentLastName", array[0]);
player.SetVar("studentName", newName);

Then you can just use those name variables in your module. Sweet!

Updated one for SL2:

var player = GetPlayer();
lmsAPI=parent;
var myName = lmsAPI.GetStudentName();
var array = myName.split(',');
var newName = array[1];
player.SetVar("newName", newName);

Thanks https://community.articulate.com/discussions/articulate-storyline/retrieve-lms-user-name-as-variable?page=6

 

Articulate 360/HTML5 update (need to target lmsAPI)
Thanks https://community.articulate.com/discussions/articulate-storyline/lmsapi-functionality-in-html5-output

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);
var myName = lmsAPI.GetStudentName();
var array = myName.split(',');
var newName = array[1] + ' ' + array[0];
player.SetVar("newName", newName);

 

 

 

And here’s CP7 (thanks Jim Leichliter) – I just added additional first and last name variables to his code.

/* Calling a self-invoked function to not pollute the global scope and immediately destroy the scope chain */
(function(){
 
 /* Initialize JS Variable to hold the student name */
 var sName='';
 var sFirstName = '';
 var sLastName = '';
 
 /* Obtain the student name from the LMS and put it into a JavaScript variable called sName */
 if (typeof window.GetStudentName==='undefined'){
 sName='Name Not Found';
 } else {
 sName=GetStudentName();
 
 if(sName==''){
 sName='Name Not Found';
 } else {
 /* Uncomment the below line to show the name in 'First Last' format */
 /* sName=sName.split(', ')[1] + ' ' + sName.split(', ')[0]; */
 sFirstName = sName.split(", ")[1];
 sLastName = sName.split(", ")[0];
 }
 }
 
 /* Check for HTML5 vs. SWF output */
 if (typeof window.cp==='undefined') {
 /* We have SWF output, so Get the Captivate Object */
 var objCp=document.getElementById('Captivate');
 
 /* Set the studentName Captivate User variable with the JavaScript variabe, sName */
 if(objCp && objCp.cpEISetValue){
 objCp.cpEISetValue('m_VarHandle.studentName', sName);
 objCp.cpEISetValue('m_VarHandle.studentFirstName', sFirstName);
 objCp.cpEISetValue('m_VarHandle.studentLastName', sLastName);
 }
 } else {
 /* We have HTML5 output */
 if(cp.vm && cp.vm.setVariableValue){
 cp.vm.setVariableValue('studentName', sName);
 cp.vm.setVariableValue('studentFirstName', sFirstName);
 cp.vm.setVariableValue('studentLastName', sLastName);
 }
 }
})();

Leave a Reply

Your email address will not be published. Required fields are marked *

Proudly powered by WordPress | Theme: Baskerville 2 by Anders Noren.

Up ↑