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!

46 Replies
John Brindle

Hi Kayode,

There are lots of options for printing certificates in Storyline although none of them are supported properly by the software itself (these are all workarounds or hacks).

This article links back to this community and shows 6 ways to try and print a certificate.

You may want to try this method too.

This is a really complex and fairly hit-and-miss way of generating a PDF certificate that is auto-populated. I have tested this several times and difficulty making it work with our projects. You'll have to get used to the PDFmake syntax though to generate a decent PDF. You can pass variables from Storyline to it though to pick up results etc.

Failing that we have yet to find a reliable way to print a certificate from Storyline that renders correctly every time. I believe that Blackboard may have a certificate module, perhaps it would be beneficial to enable conditional unlocking and host the auto-populated certificate separately?

Other than that there's not much more help that I can personally give you I'm afraid. Just search the discussions on here and see what you can find to suit your needs :-)

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.

Matthew Bibby
Anji Heath

So just to confirm, there is NO WAY to track this unless publish to SCORM?

When you say "track this", what exactly are you referring to?

All that the above JavaScript does is grab the learners name from the LMS, format it in a friendly way and then pass it to a Storyline variable.

When you aren't using an LMS, there is nowhere for the code to grab the name from.

Does that make sense?

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.