Username from an LMS on both Flash and HTML5

So, I have noticed that javascript has been a little bit unpredictable when deploying dual export (HTML5 and Flash). Particularly when pulling a user's name from an LMS (we use Moodle). 

After trawling through the solutions on here I have put together a frankenstien of a few different solutions that actually works in both HTML5 and Flash (well it does on our system).

Thanks to Knut Jackowski for pointing out an error in the code!

updated 16/04/2018

So ... here you go ...


//find out if html5 version is running

var path = window.location.pathname;
var html5 = path.includes("html5");

if (html5 == true) { //if html5 is running run this script this is where the error was
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];
player.SetVar("newName", newName);

} else { //if html5 isn't running (in other words flash) run this script

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

}

This code tests if html5 is being used (thanks to Matthew Bibby for the great example). If it is then the first block runs ... if not then the second block runs.

I hope people find this useful, this may have been posted elsewhere, but I couldn't find it! 

It's worth noting that this only pulls the first name and you have to create a newName text variable in Storyline!

50 Replies
Matthew Bibby

Bernie, try this 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 name = lmsAPI.GetStudentName();
var nameArray = name.split(", ");
var firstName = nameArray[1];
var lastName = nameArray[0];
var player = GetPlayer();
player.SetVar('first_name',firstName);
player.SetVar('last_name',lastName);

You'll need two text variables in Storyline, one called first_name and one called last_name for this to work.