Skip to main content
Inspiring
April 1, 2016
Answered

User variables persisting between sessions

  • April 1, 2016
  • 1 reply
  • 1804 views

I read in a long and informative thread from 4 and a half years ago that even if "resume data" or "suspend data" were being sent to the LMS between sessions (to persist my Captivate user variables), that if my user variables are being set to an initial value in my Captivate file, they will be reinitialized each time on entry to the initial value, and the persisted value will be lost.  My question:  is that still true in Captivate 9?

I ask because we have discovered a problem with the behavior of our modules if a student exits a lesson and reenters it later, a problem which would perhaps be explained by the above phenomenon.

This topic has been closed for replies.
Correct answer TLCMediaDesign

OK, I will try what you suggested.

The only thing I'm unsure about now is WHEN to do it.  Since the student can leave the lesson at any time and come back later to the same place, it would seem that I'd have to use the localStorage copy of the variables as the master copy of the values, reading them in to the user variables whenever the values of the user variables need to be examined, and writing them back out whenever the user variables change inside the lesson.  And I'd have to find some way to have code executed on their very first entry (and never again) to set up the initial values of the localStorage variables, and code which is invariably executed on exit which always saves the user variables to localStorage.  I read that there used to be a place for an advanced action on exit, or something like that.  Does that still exist?  Perhaps I could always save the user variables THEN.  On entry I could have a dummy first slide that does nothing but quickly initialize all the variables and advance to slide 2, and ensure they can never back up from slide 2 to slide 1.  I'd appreciate it if you would comment on this approach as to whether it seems sound or viable.  Thanks!


It's best if you use an included js file to do this. You can create the file and put the include in the index.html

The 'moduleReadyEvent' is only triggered once, create a function initVariables to get the vars form localStorage or initialize the localStaorage if it doesn't yet exist.

In the initializeEventListeners function you add a 'CPAPI_SLIDEENTER' listener, set the storage everytime you enter a slide.

Add a window.onunload function to set it again when the page unloads.

window.addEventListener( 'moduleReadyEvent', function ( e )

{

interfaceObj = e.Data;

eventEmitterObj = interfaceObj.getEventEmitter();

initVariables();

initializeEventListeners();

});

function initVariables()

{

  //set up the localStorage

}

function initializeEventListeners()
{
if ( interfaceObj )
{
     if ( eventEmitterObj )
  {
         eventEmitterObj.addEventListener( 'CPAPI_SLIDEENTER', function ( e )
   { 
    //call a function to set the storage here.
            });
  }
}
}

window.onunload = function()
{
   //call a function to set the storage here.
}

1 reply

Lilybiri
Legend
April 1, 2016

User variables are reset when re-entering a course, still the same in CP9.

For HTML output only, the new CPExtra widget by InfoSemantics allows to transfer values of user variables between courses. It is also possible with JS.

Inspiring
April 1, 2016

Thank you for the swift reply!

I have a follow-up question if you would be so kind:  I tried an experiment:  I created a new user variable and did NOT give it an initial value, published the lesson, ran it using Chrome, and at the pause on slide 1, I used cpAPIInterface.getVariableValue("varName") in the Console to inspect the value Captivate thought it had, wondering if it would be undefined or what.  The call returned the empty string "".  We were considering a solution involving NOT setting an initial value, in the hopes that the variable would NOT be reset to anything on re-entry to the course, but I wonder if Captivate still considers that such a variable is to be initialized to the empty string, as opposed to not trying to change its value at all, which is what we'd need to have happen for such a solution to work.  Do you think a user variable with "no initial value" is actually considered to have an initial value of "", and will be reset to "" on re-entry?

Lilybiri
Legend
April 1, 2016

The problem with the point-and-click programming in Captivate is that there is no real difference in variables between strings and numbers.  For CP an empty string, the number 0 and no value are the same. Could you perhaps try with 0 as an initial value?