Step 51 – Testing for API Session State

In Step 48, I introduced a flag to indicate whether a session had been finished or not. In that post, it was used to ensure that LMSFinish wasn’t called multiple times. But I really ought to extend it to make sure that none of the API functions are processed after the session has been closed.

In addition, I should probably make sure that no API calls are processed before the API session has been initialized. This isn’t as much of a problem as long as the SCO has been well-written since, according to the SCORM standard, LMSInitialize should always be the first API function called by the SCO/course.

So, building on the work I did in Step 48, I’m going to introduce a second JavaScript variable – flagInitialized – into api.php:

var flagFinished = false;
var flagInitialized = false;

There are now five API calls to be changed so that they test for appropriate values of flagInitialized and flagFinished.

1. LMSInitialize

This API call can only be executed if the session HASN’T already been initialized, and the session HASN’T been closed. So I modify my code like this:

function LMSInitialize(dummyString) {

  // already initialized or already finished
  if ((flagInitialized) || (flagFinished)) { 
    return "false";
  }

  // set initialization flag
  flagInitialized = true;

  // return success value
  return "true";

}

If either flagInitialized or flagFinished is set to ‘true’, execution of the function will be aborted. If it proceeds, line 7 sets the ‘flagInitialized’ variable so that other API calls know that the session is initialized.

2. LMSFinish

This API call can only be executed if the session HAS been initialized but HAS NOT yet been closed. So the code looks like this

function LMSFinish(dummyString) {

  // not initialized or already finished
  if ((! flagInitialized) || (flagFinished)) { 
    return "false";
  }

  ...
  ...

  // set finish flag
  flagFinished = true;

  // return to calling program
  return "true";

}

Very similar to LMSInitialize except that I only allow the code to run if flagInitialized is set to ‘true’, and flagFinished is set to ‘false’. If the function executes correctly, line 10 sets flagFinished to ‘true’ so that there can be no further communication between the SCO/course and the LMS.

3. LMSGetValue, LMSSetValue and LMSCommit

Finally, I need to add the following code to each of these three API calls to ensure that the code is only executed if the session HAS been initialized, but HAS NOT been closed.

  // not initialized or already finished
  if ((! flagInitialized) || (flagFinished)) { 
    return "false";
  }

And that’s it.

This entry was posted in Run Time Environment. Bookmark the permalink.

2 Responses to Step 51 – Testing for API Session State

  1. Pingback: Download VS SCORM 1.2 RTE Rev 2.1 < VS SCORM

  2. Daniel says:

    Could you not have a single flag ‘initialised’, and just set it back to false upon finish?

Leave a Reply

Your email address will not be published. Required fields are marked *