Username from an LMS on both Flash and HTML5

Jul 12, 2017

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!

41 Replies
matt thorne

Just wanted to pop in and say a big thanks for this code. Implemented it this morning in a test project and will now be including it in future courses going forward.

Is there a decent repository anywhere of valid Scorm fields that can be called if i wanted to use this script as the basis for pulling other user information fields? My LMS (LITMOS) doesn't display a "StudentName" it has First, Last and UserName. When I use "StudentName" in the script it pulls back just first name so I want to see if I can find a SCORM Dictionary of some sort.

Thanks again!

Nimet Cubukcu

Hello All, I tried the following code as suggested in this thread but it does not work with the latest version of IE 11 for both Flash and HTML5. This code works on Chrome but not on IE11. I tested my scorm packages on Moodle. I tried HTML5 with Flash and also FLash with HTML5 fallback and it didn't work on IE 11 but worked on Chrome. Could you please check the  code below and let me know if you could suggest any solution? I appreciate your help.

 

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

}

Knut Jackowski

Hi John,

when you first posted this, I started using your code to generate PDF certificates with user names from the LMS (with PDFMake), but I did not look very closely at it (it worked, so why bother).

Today for some reason I noticed that the condition at the start does not make sense:

if (html5 = true)  will will never fall to else, because it assigns true to html5 instead of testing testing if html5 is true ( that would be either if (html5==true) or if (html5) ).

So if you have never experienced problems with this code, the whole conditional is not necessary.

But it might be also the cause why it doesn't work for Nimet.

Knut Jackowski

Hi Nimet,

not sure about your problems with IE (stopped using and supporting it some time ago), but maybe you could try to change the line:

if (html5 = true) { //if html5 is running run this script

into

if (html5 == true) { //if html5 is running run this script

and try again?

If you are still trying to make it work.

Nick Schalk

Since this seems a related issue I thought I'd try this here too:

I've got IE11 running the flash version while other browsers are using HTML5. (I left just chrome in here as an example,  I'm open suggestions as the HTML5 path method above doesn't work for me. )  

Turns out not calling GetPlayer()  makes the flash version work,  while using the function below for HTML5 breaks the flash side:

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

if (navigator.userAgent.search("MSIE") >= 0) {
}

//Check if browser is Chrome
if (navigator.userAgent.search("Chrome") > 0) {
var lmsAPI = findLMSAPI(this);
var player = GetPlayer();
}

var myName = lmsAPI.GetStudentName();
var myID = lmsAPI.GetStudentID();

var array = myName.split(',');

var newName = array[1] + ' ' + array[0];

player.SetVar("FULLNAME", newName);
player.SetVar("UserID", myID);

 

///////

Any one have an Idea on how to get this to work for both sides?

 

Anji Heath

We are trying to track slide views per user per course publishing to HTML with Flash fall back. Trying to avoid publishing to SCORM. This is where we are at with our code but not having any luck. Do you have any ideas what we are missing? Our LMS is Moodle and we use SL360. I'm attaching the test file that we have been working with. Just one slide. Thank you in advance for replying!

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 full_name = array[1] + ' ' + array[0];
var first_name = array[1];

// Remove blank space from the front of variable
full_name = full_name.trim();
first_name = first_name.trim();

// Remove blank space from the end of variable
full_name = full_name.replace(/\s+$/, '');
first_name = first_name.replace(/\s+$/, '');

// If value exists change variable, if not change name to participant
if (first_name) {
player.SetVar("username", first_name);
}else{
player.SetVar("username", "Participant");
}

//Modified and tested by Kevin Brake

EDITED:

My colleague has been working on this. Also, we wanted me to mention the following:

We are putting the Storyline published content as resource (File) on Moodle. I read in some forums, it says you need put it as activity. With the above function it does not find LMSAPI. Then I change the lmsAPI variable to -

var lmsAPI = parent;

but it throws the error - lmsAPI.GetStudentName is not a function.

We are looking to get student name and student ID (if possible, course ID too) from Moodle.

Anji Heath

Sorry for not clearly mentioning it.

Yes, we are putting the course in an LMS (Moodle) as a file type resource after publishing it as HTML5/Flash but not SCORM. I tried this code with SCORM and it worked previously but I am trying to find a way to do it with just HTML5/Flash. We want to track slide views per user, per course. I was certain there is very little chance of it connecting with LMS without SCORM but then looking at some community posts I thought to try it out. 

Anji Heath

Exactly! We experienced issues in the past with SCORM - activities not working, browser issues, platform issues, passing/retrieving variables to LMS and such. So, it's not always our first choice. But we think we will give it a fresh start and see where we go with it. Thanks so very much for your reply and your help! Definitely appreciate you taking time out of your day to help.

Bernie Simons

Just started to pull a username from moodle into articulate SL.  This works fine when I select HTML/Flash in the publish formats. But when I use the modern player instead of the classic player then I can't publish with HTML5/Flash,but HTML5 only.

The problem is that when I publish in HTML5 only, the name is not pulled from moodle.

 

I use this code:

 

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

}

Bernie Simons
Matthew Bibby

Try deleting this from the start:

//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

When publishing just to html5, the file will be story.html not story_html5.html.

I changed the file but unfortunately no luck yet. I'm publishing for LMS scorm 1.2

See attachement for the SL-file.

Thanks

 

I changed java script to:

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 discussion is closed. You can start a new discussion or contact Articulate Support.