Step 45 – Building the Cache, and Variable Names



As I noted in my last post, I’m going to use a set of JavaScript varables to hold all of the SCORM data elements during run-time. So here’s what that would look like in the api.php file:

My first reaction is to use the SCORM-defined name of the data element as the JavaScript variable name. But here’s (yet another) odd thing about the SCORM 1.2 RTE. Although it’s fundamentally based on a JavaScript API, the names of the data elements include punctuation marks, and therefore can’t be used as JavaScript variable names (sigh).

To get around this, I’m going to make a strategic decision at this point. Rather than use the exact form of the SCORM data element name for internal storage, I’m going to switch to using a modified form where any character other than lower-case a-z will be replaced with an underscore which is allowed in JavaScript variable names. So:

cmi.core._children … would become … cmi_core__children

cmi.core.score.raw … would become … cmi_core_score_raw

adlcp:masteryscore … would become … adlcp_masteryscore


and so on. One downside to this change – this system isn’t compatible with the Rev 1.0 version of the RTE where I’ve used the full SCORM data element names in the SCORMvars database table. But I’ll cross that hurdle when (if) I have to. So this is what my cache code – empty at present – will look like in the api.php file.

This code has to go into the head section of the api.php file along with the rest of the JavaScript that makes up the API.

Finally, here’s a regular expression that will change variable names from their SCORM format to the format that I’m going to use internally – firstly in PHP:

$jvarname = preg_replace('/[^a-z]/','_',$varname);

and now in JavaScript:

var jvarname = varname.replace('/[^a-z]/g','_');

I’ll probably need both of these when I start (re-)coding the API calls, and the server-side code. I’m not going to provide a tutorial on regular expressions, but here are some references that might get you started if you don’t already know how regular expressions work:

Next time – modifying the LMSSetValue() and LMSGetValue() API calls to interact with the cache.

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

2 Responses to Step 45 – Building the Cache, and Variable Names

  1. Legend says:

    Try storing the cached vars in an object instead …

    var o = new Object();
    o[“cmi.core._children”] = “exit,message,blah”;
    o[“adlcp:masteryscore”] = 80;
    alert( o[‘cmi.core._children’] );
    alert( o[‘adlcp:masteryscore’] );

  2. Steve Addison says:

    This is a very good idea. I’ll look into it. Thank you!

Leave a Reply

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