Forum Discussion

XavierToyos's avatar
XavierToyos
Community Member
5 months ago

Problems with a javascript

Hello everyone. There are two things happening to me in the same project and I wanted to share it with you in case someone has encountered the same problem and, most importantly, if anyone knows how to solve it.
 
The project has, at the beginning, a javascript that gets the name of the student from the LMS (I have extracted it from different forums, but I liked the PUNTOMOV one better). The javascript works perfectly, it's as follows....
 

function findLMSAPI(win) {

if (win.hasOwnProperty("GetStudentID")) return win;

else if (win.parent == win) return null;

else return findLMSAPI(win.parent);

}

var lmsAPI = findLMSAPI(this);

var newName = "Invitado";

if (lmsAPI) {

var myName = lmsAPI.GetStudentName();

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

var firstName = array[1];

var lastName = array[0];

}

var player = GetPlayer();

player.SetVar("Nombre", firstName);

player.SetVar("Apellido", lastName);

Working with SCORM 2004 4ed, in Moodle Workplace 4.1.11, in Spanish, in Firefox.
 
The first thing that happens to me is that it can't work with the variables ("Nombre", "Apellido") separated. If I put only the "Nombre" variable somewhere in the project, it doesn't work. I have to put the "Apellido" variable next to it for it to work. I've tried to separate javascript into two, to get only one of the variables, but it doesn't work either. The two variables have to go together for them to work. Is there a way to get just one of the variables and have it work?
 
The second thing that happens to me is that when Javascript is executed, some triggers do not work, specifically those that pass to other slides in other scenarios. Without running javascript they do work. Does anyone know what is happening and how it can be solved?
 
 
Thanks a lot

  • Do you want to separate the variables in JavaScript, or in StoryLine?

    When you refer to the variable as "working", what do you want it to do, and what does it do instead?

    What are the other triggers that don't work? By "pass to", do you mean to jump to other slides, or pass variable contents to other slides?

    If you can define these questions, and attach your .story here, it will be much easier for someone to help you.

  • XavierToyos's avatar
    XavierToyos
    Community Member

    Thank you Walt for your interest

    Do you want to separate the variables in JavaScript, or in StoryLine?

    When setting up a story to share with you I realized that Javascript does not separate the first name from the last name, it only stores it (the whole name) in the "Last name" variable. How can I separate name from surname?

    When you refer to the variable as "working", what do you want it to do, and what does it do instead?

    That returns the value of the variable ("Name"; "Last name" of the person logged into the LMS.

    What are the other triggers that don't work? By "pass to", do you mean to jump to other slides, or pass variable contents to other slides?

    In the story I share it works without problems. In the original, when Javascript is active, the triggers to move to the questions do not work. When Javascript is disabled it works smoothly (you can access the questions).

    If you can define these questions, and attach your .story here, it will be much easier for someone to help you.

    I thought that sharing the story didn't make sense, since the problem is related to the LMS and I can't test it with the same LMS as me. I have also had to reduce it as much as possible, it contains many graphs, animations, GIFS and copyrighted elements that I cannot share. I leave you a Story reduced to the maximum

     

    Thank you for your interest and help

  • It works fine for me in scorm cloud, there is no linking of variables in Storyline I suspect the issue is elsewhere.

    Do you have other javascript in the course? If there is an error in the other code it can stop all code being run. Have you checked the console for errors? 

  • XavierToyos's avatar
    XavierToyos
    Community Member

    Thank you Phil

    I had never used this code to extract the variables in our LMS. There are a lot of bugs in the console. I assume that our LMS provider must have a firewall to prevent attacks on the database and stops the execution of all the code.

    I'll still use the method of asking for the name and saving it in a variable to address the user.

    It has been an attempt with something to learn.

     

    Thank you

    • PhilMayor's avatar
      PhilMayor
      Super Hero

      In your previous post you suggested it has worked, however you have to use the variables in tandem. What are the errors you are seeing? There would be no reason why they would have to be used together.

      Are you using any other unrelated code, that’s where the error maybe.

      Sent from my iPhone

  • Hi,

    If it's not separating the names, it might be because of this line

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

    You may have names separated by a space or other piece of punctuation, not a comma.

    Also within Moodle, you may be storing the names in the reverse order.

    Add

    var myName = lmsAPI.GetStudentName();
    alert(myName);

    to see what the actual value is you are getting from Moodle.

    Karl

  • XavierToyos's avatar
    XavierToyos
    Community Member

    Thank you Karl

    Name and Surname do not have a sign of separation. I've put space in...

    var array = myName.split(' ');

    … but it doesn't work either.

     

    Xavi

  • Hi,

    You have 3 names on the screenshot, the code is only using 2. Also Moodle is storing them the other way round.

    Try this:

    var array = myName.split(' ');

    firstName = array[0];

    lastName = array[1];

    With your login lastName = TOYOS


    If you want to use the last name from Moodle, allowing for 3 or 4 names, try this instead

    lastName = array[array.length - 1];

    With your login lastName would be RIERA

    Hope that helps!

    Karl

  • XavierToyos's avatar
    XavierToyos
    Community Member

    Solved. The final solution is.....

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

    var lmsAPI = findLMSAPI(this); 
    var newName = "Invitado"; 

    if (lmsAPI) { 
    var myName = lmsAPI.GetStudentName();
    var array = myName.split(' '); 
    var firstName = array[0];
    var lastName = array[1];

    var player = GetPlayer();
    player.SetVar("Nombre", firstName);
    player.SetVar("Apellido", lastName);

     

    .... works perfectly.

    Thank you all