Forum Discussion
Retrieve LMS User Name as Variable
I know you can have a user fill in a text box with their name and then later use that variable for personalization. What we would like to do is programmatically retrieve the user name from the LMS. Is there a way to set a variable with data from the LMS when the course is loaded instead of having the user enter their name?
Thx
- TanmayGhoshCommunity Member
Thanks Tanmay for coming back to share the file here!
- AlexYong1Community Member
Thanks for sharing Tanmay!
- SteveFlowersCommunity Member
Ah. The very first one posted was Flash only. However, HTML5 output supports the second one posted no problem. Has for awhile:)
Only works in Flash:
player.SetVar("sLearnerName", SCORM_GetStudentName());
Works in all:
var player = GetPlayer();
//might need to use lmsAPI=parent; on some LMS
var myName = lmsAPI.GetStudentName();
var array = myName.split(',');
var newName = array[1] + ' ' + array[0];
player.SetVar("newName", newName);- AlexYong1Community Member
Gah! Ok, but the issue that Tanmay found was that this JS wouldn't work on the certificate or results slide. But now that i'm comparing the code I see he's using something different. The code you posted above will work in the results slide too? Or is that the limitation that Tanmay was working around?
Thanks in advanced for helping me understand, this is so very helpful!
- KyleMullaneyCommunity Member
This is not working in HTML5 on Sakai. It does work in flash on Sakai.
- BrianAllenCommunity Member
I'd be curious to know if Sakai exposes student name as part of it's API... Not sure if this is an industry standard, and not sure if Sakai adheres to those industry standards.
- SteveFlowersCommunity Member
It works everywhere I've tried it. Yes, it should work.
However, since triggers work in order but JS runs asynch, If you're doing something that relies on the setting of the variable, you'll want to be sure you load in the name from the LMS with a bit of time to spare. You could also add something to your JS to trigger an "It's loaded" pseudo-callback to make sure it has time to load if you're using it to make something else happen.
In most cases, it's just a matter of loading into a variable reference. Which shouldn't make any difference. Shouldn't need a callback in that case.
Tanmay's issue was likely the script bombing out somewhere. Unsure since I don't see an example that wasn't working:)
Steve
- MichaelLaudoneCommunity Member
I never saw a solution to the middle inital issue: Here's what I did:
<script type="text/javascript" language="JavaScript">
function getName(){
var sName = lmsAPI.LMSGetValue("cmi.core.student_name");
sName = sName.replace(',','');
var aSplit = sName.split(" ");
player.SetVar("v_LastName", aSplit[0]);
player.SetVar("v_FirstName", aSplit[1]);
player.SetVar("v_MiddleInitial", aSplit[2]);
}
</script> - EricDumasCommunity Member
Great info, thanks for all these insights.
I really need to learn more about JavaScript, it seems to enhance so many applications I use !
Javascript opens up a lot of possibilities - so I'm glad you found this thread helpful. :-)
- MirandaVerswijvCommunity Member
We are successfully using it in a privacy course, where we pull the learner's name through to feature in privacy breach scenarios. It has a great sensitising effect on them!
Bid problem though: This works brilliantly for us in Firefox and Chrome, but fails completely in IE.
Any thoughts?
- SteveFlowersCommunity Member
Hi Miranda -
What version of IE?
- MirandaVerswijvCommunity Member
Hi Steve,
We have encountered it on IE 10 and 11. We are using Totara LMS (the commercial version of Moodle)