Pull variable from LMS and insert into text box

rwatkins9162
Community Member Posts: 17
Hi,
For some time I've been trying to get a code snippet to access our LMS and parse the student's name so I can use it in our eLearnings as the reserved variable for this in our LMS is populated with a number, not a name. With the help of Math Notermans and others on the forum, our IT department came up with this code that access the info, parses it and shows me the first name in a system message window. So I know the code is doing its job. It should also rename the First Name result as icwStudentName.
<SCRIPT type = "text/javascript">
var icwStudentName = getStudentName();
alert(icwStudentName );
function getStudentName()
{
var studentName = "";
if (!LMSIsInitialized()) {
var result = LMSInitialize();
}
studentName = LMSGetValue("cmi.core.student_name");
return getFirstName(studentName);
}
function getFirstName(theFirstName) {
var str = theFirstName.split(",");
var firstName = str[1].trim();
return firstName;
}
</SCRIPT>
My question is, How do I get this value onto the screen in Lectora? The inline variable replacement doesn't seem to be the right approach as this is not a reserved variable. My thought is to create a user-defined variable, but how do I tell it that the value of the user-defined variable is what the code snippet is pulling? It seems like I'm missing a step in my thought processes. Any advice is appreciated.
For some time I've been trying to get a code snippet to access our LMS and parse the student's name so I can use it in our eLearnings as the reserved variable for this in our LMS is populated with a number, not a name. With the help of Math Notermans and others on the forum, our IT department came up with this code that access the info, parses it and shows me the first name in a system message window. So I know the code is doing its job. It should also rename the First Name result as icwStudentName.
<SCRIPT type = "text/javascript">
var icwStudentName = getStudentName();
alert(icwStudentName );
function getStudentName()
{
var studentName = "";
if (!LMSIsInitialized()) {
var result = LMSInitialize();
}
studentName = LMSGetValue("cmi.core.student_name");
return getFirstName(studentName);
}
function getFirstName(theFirstName) {
var str = theFirstName.split(",");
var firstName = str[1].trim();
return firstName;
}
</SCRIPT>
My question is, How do I get this value onto the screen in Lectora? The inline variable replacement doesn't seem to be the right approach as this is not a reserved variable. My thought is to create a user-defined variable, but how do I tell it that the value of the user-defined variable is what the code snippet is pulling? It seems like I'm missing a step in my thought processes. Any advice is appreciated.
Comments
-
The best way should be to assign the value to a user-defined variable so that you can use it as usual from that point on. Create a variable, e.g. "username".
You need to use it in a Lectora action to initialize it, otherwise it wouldn't exist. Maybe "Set as empty" when the page is loaded, just make sure it's done before the javascript is executed.
The code should be:
Varusername.set(icwStudentName);
Then you can use "username" in any way, e.g. in a Change contents action. Inline variable replacement probably won't work on the page the code is run but should work on all pages that come later. -
Tim's solution for sure is the best, because it doesnot needs any other Javascript libraries. However as i use Greensocks GSAP library intensively in all my projects, there are approaches with GSAP's TweenMax that are perfect.
It uses TweenMax and the GSAP Textplugin.
Another option i use quite a lot recently is using Lectora's shape elements for text. Using shapes for some text like that for me has several bonuses. I can easily animate them and alignment of text is better...
I have a genericJavascript HTMLExtension (MetaTags) in the top of my Lectora file...so functions inthere are available for every page in my title and because most of the functions i use often, its easy to copy to new titles.
Inthere one function is this:
function changeMyShape(_target,_color,_duration,_percentage,_string){
var displayText = $("."+_target, getDisplayDocument());// using JQuery as selector and adding the scope
TweenMax.to(displayText, _duration, {text:_string,color:_color,fontSize:"14pt",fontFamily:'pragmataflash12regular', textAlign:'left',yPercent:_percentage});
}
This function changes the text of a Lectora shape in a specified time, color and font. One benefit is you can animate text easily.
You need to define a CSS-class for your text-shape. Then you can target it easily. On a click you can then add a Javascript to call the function like this:
changeMyShape("myShapeCSSClassname","#fff999",0,0,"Some text that will show in the shape");
Do remember this approach needs GSAP and jQuery. If you dont want that, Tim's approach is better.
But still you can get this done without GSAP and jQuery in Vanilla Javascript.
The function and approach below dont use any extra libraries and still change the text of any given textfield:
Make sure you add a CSSclass to your textfield...
function changeText(_Msg){
var elem = document.querySelector(".someTextfield");// targets a textfield with class someTextfield
var text = elem.textContent; // target the text content
elem.textContent = 'We can dynamically change the content.'; // changes the text
elem.textContent += _Msg; // adds text at the end of the existing content
elem.textContent = 'We can add this to the beginning. ' + elem.textContent; // Adds text to the beginning
};
changeText("Hello World");
Biggest disadvantage when compared to Tim's approach ( and the GSAP approach ) is you do loose your chosen formatting on the textfield here. Offcourse you can add that after you changed your text dynamically, just like i do with GSAP.
To do that just add the styles wanted in the function. Offcourse you can make those dynamic too..
elem.style.color = "green";
elem.style.fontSize = "12px"; -
Categories
- 4.8K All Categories
- 6 New Feature Releases
- The Training Arcade®
- Arcades™
- 125 ✫ Announcements
- 9 ✪ Contests
- 34.1K eLearning Brothers® Products
- 33.6K Lectora®
- 31.6K Lectora Discussions
- 1.9K Lectora Feature Requests
- 71 Lectora User Groups
- 17 Asset Library
- 10 AssetLibrary Discussions
- 7 AssetLibrary Feature Requests
- 304 CenarioVR®
- 178 CenarioVR Discussions
- 126 CenarioVR Feature Requests
- 40 Rockstar Learning Platform®
- 28 Rockstar Learning Platform Discussions
- 12 Rockstar Learning Platform Feature Requests
- 4 Off-the-Shelf Course Library
- 3 Off-the-Shelf Course Library Discussions
- 1 Off-the-Shelf Course Library Feature Requests
- 114 CourseMill®
- 107 CourseMill Discussions
- 44 ReviewLink®
- 32 ReviewLink Discussions
- 12 ReviewLink Suggestions
- 2 The Training Arcade®
- 1 The Training Arcade Discussions
- 1 The Training Arcade Feature Requests
- 10 Additional Learning Products
- 2 Adobe®
- 4 Articulate®
- 2 Camtasia®
- Docebo®
- 1 iSpring®
- 1 Microsoft® PowerPoint®
- 932 All Things eLearning
- 5 Course Development Showcase
- 1 eLearning Brothers® Downloads
- 1 Compliance Training
- 11 eLearning Development
- eLearning Game Design
- 7 Instructional Design
- 545 Learning Management System (LMS) Integration
- 3 Mobile Learning
- 22 Visual Design
- 331 Web Accessibility
- 1.2K ♪ The Green Room
- 8 ♡ Community Feedback
- 7 Community Tips