Pulling first name from LMS

Hi all - I'm wondering if anyone might be able to put together a step-by-step guide as to how to set up a variable / javascript function for this? (i.e. pulling a user's first name from the LMS).

I've read through this discussion (https://community.articulate.com/discussions/articulate-storyline/retrieve-lms-user-name-as-variable) but I'm very green and have had no experience with javascript functions (and only basic variables).

Any help for dummies would be amazing.

Thanks,

Sal

49 Replies
Matthew Bibby

Hi Sal,

Here you go:

1. Create a new text variable in Storyline called first_name (see here for details on how to create variables). 

2. Add an Execute JavaScript trigger to your slide and set it run when the timeline starts (see here for details on how to work with triggers). 

3. Add this code to your Execute JavaScript trigger: 

var lmsAPI = parent;
var name = lmsAPI.GetStudentName();
var nameArray = name.split(", ");
var firstName = nameArray[1];
var player = GetPlayer();
player.SetVar('first_name',firstName);

4. Add a reference to your to display the user's first name (see here for info on references)

4. Publish your course and upload it to your LMS.

5. Test and celebrate. 

Hope that helps. Let me know if you run into any issues. 

Matthew Bibby

No worries Alyssa. 

And for anyone wondering how to grab the last name as well, just use this code:

var lmsAPI = 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);

And you'll need another text variable in Storyline called last_name 

Sally Milford

LEGEND!!! Thanks so much Matthew – it worked! J

Sally Milford

Online Learning Co-ordinator | Alzheimer's Australia Vic

155 Oak Street Parkville VIC 3052

p: +61 3 9816 5715 | f: +61 3 9816 5733 | e: Sally.Milford@alzheimers.org.au

w: www.fightdementia.org.au/vic

For more information or support call the National Dementia Helpline 1800 100 500

[cid:image04c0f7.PNG@782dabdf.49ab508b]

[cid:image9012aa.PNG@0405ee47.41b28567] [cid:image80933a.PNG@da38934d.448e8541] [cid:image507196.PNG@b0e1ed36.4aa4578f] [cid:image4299e8.PNG@5426a41f.4296c905] [cid:image3dd57a.PNG@7456ccc9.419c82c7] [cid:image3f6647.PNG@cd7a83fa.40bb3878] [cid:image2841d6.PNG@c259a276.4aa0b90b]

DEMENTIA IS A NATIONAL HEALTH PRIORITY

Alzheimer's Australia Vic would like to acknowledge the traditional owners of the land on which we meet and pay our respects to Elders both past & present.

PRIVACY STATEMENT: This email, and any attachments, contains confidential information intended only for the person/s named above and may be subject to legal privilege. If you are not the intended recipient, any use, disclosure, copying or distribution of this transmission is prohibited. If you have received this message in error, please notify the sender immediately by return email and delete the original email and any attachments.

Matthew Bibby

Hi Sally,

You can grab the StudentID, which if you are lucky might be their email address (but it could also be a username or some other identifier, depending on your LMS).

You can do that using the following code:

var lmsAPI = parent;
var id = lmsAPI.GetStudentID();
var player = GetPlayer();
player.SetVar('email',id);

You'll need a text variable in Storyline called email for this bit to work.

As for just getting the first initial of the first name, try this:

var lmsAPI = parent;
var name = lmsAPI.GetStudentName();
var nameArray = name.split(", ");
var firstName = nameArray[1];
var firstInitial = firstName.charAt(0);
var player = GetPlayer();
player.SetVar('first_initial',firstInitial);

You'll need a text variable in Storyline called first_initial for this to work. Please note, I haven't tested this code, so if it doesn't work, let me know.

Now, if we put all that together so that you can grab the first name, last name, email and the first initial of the first name all at once... the code should look like this:

var lmsAPI = parent;
var name = lmsAPI.GetStudentName();
var id = lmsAPI.GetStudentID();
var nameArray = name.split(", ");
var firstName = nameArray[1];
var lastName = nameArray[0];
var firstInitial = firstName.charAt(0);
var player = GetPlayer();
player.SetVar('first_name',firstName);
player.SetVar('last_name',lastName);
player.SetVar('first_initial',firstInitial);
player.SetVar('email',id);

Hope that helps.

Sally Milford

Hi Matthew,

Probably a dumb question, but I have various screens which need various combos of first/last/email/first initial etc. Can I set the screens up with the necessary references, then add this full code to 1 trigger on each screen (regardless of whether I need to call all references?)

Will this break the page as it might be calling for references that aren't on this screen? Just trying to see if I can stream line..

Thanks for your continued help with this,

Sal

(FYI I'm building an induction module which needs to display usernames, Outlook logins, etc so this is really useful).

Sally Milford

Hi again Matthew - curly one for you. Any way to remove any punctuation from a last name?

i.e. O'Bryan > obyran

Taylor-Smith > taylorsmith

(I'm looking to arrive at a reference(s) which creates a username which is <firstinitial><surname>

i.e.

Carrie O'Bryan > cobryan

Thanks,

Sal

Matthew Bibby

Hi Sal,

You really only need to grab this info from the LMS once. Just chuck the code on the first slide and you should be good to go.

To remove punctuation, make everything lower case and combine the first initial with the last name you'll need something like this:

var lmsAPI = parent;
var name = lmsAPI.GetStudentName();
var id = lmsAPI.GetStudentID();
var nameArray = name.split(', ');
var firstName = nameArray[1];
var firstInitial = firstName.charAt(0);
var lastName = nameArray[0];
var removePunctionation = lastName.replace(/[~`!@#$%^&*(){}\[\];:"'<,.>?\/\\|_+=-]/g,"");
var removeSpaces = removePunctionation.replace(/\s/g,'');
var firstInitialLower = firstInitial.toLowerCase();
var lastNameLower = removeSpaces.toLowerCase();
var user = firstInitialLower.concat(lastNameLower);
var player = GetPlayer();
player.SetVar('first_name',firstName);
player.SetVar('last_name',lastName);
player.SetVar('first_initial',firstInitial);
player.SetVar('email',id);
player.SetVar('username',user);

You'll need a text variable called username in Storyline for this to work.

Hopefully, if I've coded it correctly (no guarantees this will work, I haven't tested it), the username should be formatted correctly.

(Lucio, yell out if this can be made more efficient, I'm still pretty new to JavaScript).

Leslie Foster

Further testing make it more bizarre (using ScormCloud):

Publish with HTML5 or HTML5+flash fallback - does not work

Publish with Flash or Flash+HTML5 fallback - works if you publish to scorm, but not if you publish to tincan

As I am using scormcloud, can I therefore assume that it is a Storyline bug?

Lucio P

This should provide the requested results:

var lmsAPI = parent;
var name = lmsAPI.GetStudentName();
var id = lmsAPI.GetStudentID();
var nameArray = str.split(" ");
var firstName = nameArray[1];
var firstInitial = firstName.charAt(0);
var lastName = nameArray[0];
var cleanLastName = lastName.replace(/[^a-zA-Z]/g, "");
var userName = firstInitial + cleanLastName;
var userName = userName.toLowerCase();
var player = GetPlayer();
player.SetVar('first_name', firstName);
player.SetVar('last_name', lastName);
player.SetVar('first_initial', firstInitial);
player.SetVar('email', id);
player.SetVar('username', userName);
Matthew Bibby

Thanks Lucio, your approach is much cleaner. Although the fourth line should be:

var nameArray = name.split(" ");

As there is no str variable.

Leslie, I'm not sure why you are seeing something different. I just published your course as HTML5 (using whatever other settings you were already using). 

What browser are you viewing this in?