Really simple SCORM AS3 wrapper example

Here’s a simple example of how the SCORM AS3 class can be utilized. (This example uses SCORM 2004 calls.)


import pipwerks.SCORM;
import flash.external.ExternalInterface;

//Declare variables
var success:Boolean = false,
    completion_status:String;

//Initialize a new SCORM object!
var scorm:SCORM = new SCORM();

//Initialize SCORM connection
success = scorm.connect();

//If connection attempt was successful
if(success){
    
    //Get completion status
    completion_status = scorm.get("cmi.completion_status");
        
    //If course has already been completed
    if(completion_status == "passed" || completion_status == "completed"){
        
        //Disconnect from the LMS.
        scorm.disconnect();
        
    } else {
        
        //Set course status to incomplete
        success = scorm.set("cmi.completion_status", "incomplete");

        if(success){ 
            success = scorm.disconnect();
        } else {
            serverUnresponsive();
        }
      
    }        

} else {
    serverUnresponsive();
}

function serverUnresponsive():void {
    var msg:String = "Sorry, can't connect to server. Please try again later.";
    trace(msg);
    ExternalInterface.call("alert", msg);
}

Here’s a slightly more fleshed out example. (This example uses SCORM 2004 calls.)


import pipwerks.SCORM;
import flash.external.ExternalInterface;

//Declare variables
var success:Boolean = false,
    completion_status:String,
    learner_name:String,
    bookmark:String;

//Initialize a new SCORM object!
var scorm:SCORM = new SCORM();

//Initialize SCORM connection
success = scorm.connect();

//If connection attempt was successful
if(success){
    
    //Get completion status
    completion_status = scorm.get("cmi.completion_status");
        
    //If course has already been completed
    if(completion_status == "passed" || completion_status == "completed"){
        
        //Disconnect from the LMS.
        scorm.disconnect();
        
    } else {
        
        //Set course status to incomplete
        success = scorm.set("cmi.completion_status", "incomplete");

        // --- Set other SCORM settings as needed -----
        if(success){ success = scorm.set("cmi.score.min", "0"); }
        if(success){ success = scorm.set("cmi.score.max", "100"); }
        if(success){
            
            scorm.save();
        
        } else {
            
            serverUnresponsive();
            
        }
        
        //You can get data from LMS using the following syntax
        learner_name = scorm.get("cmi.learner_name");
        bookmark = scorm.get("cmi.location");
        
        if(bookmark != null && bookmark != ""){
            
            //Use bookmark
        
        } else {
            
            //Start from beginning
            success = scorm.set("cmi.location", "page1");
            
            if(success){
                scorm.save();
            } else {
                serverUnresponsive();
            }
            
        }
        
        //Do other course stuff as needed...
        
        //End course
        var score:int = 80;
        finishCourse(score);
        
    }        

} else {
    
    serverUnresponsive();
    
}

function finishCourse(score:int):void {
    
    //Score must be converted to a string before it can be passed to LMS
    var success:Boolean = scorm.set("cmi.score.raw", String(score) );
    
    if(success){ success = scorm.set("cmi.completion_status", "completed"); }
    
    if(success){ 
        success = scorm.disconnect();
    } else {
        serverUnresponsive();
    }
    
}

function serverUnresponsive():void {
    var msg:String = "Sorry, can't connect to server. Please try again later.";
    trace(msg);
    ExternalInterface.call("alert", msg);
}

New SCORM ebook coming soon!

I'm writing an ebook explaining how to build an HTML-based SCORM course. Subscribe to be notified when it's ready, as well as receive early bird pricing and some free goodies!

No spam, no sharing your email address, unsubscribe at any time. Powered by ConvertKit
Advertisements

9 Replies to “Really simple SCORM AS3 wrapper example”

  1. Pingback: Shiny Button Code
  2. In the first example:
    //Set course status to incomplete
    success = scorm.set(“cmi.completion_status”, “completed”);

    Should be instead like the second example:
    //Set course status to incomplete
    success = scorm.set(“cmi.completion_status”, “incomplete”);

  3. Pingback: More information about SCORM « Group projects
  4. Hi.

    Thanks for sharing this code. But I have one question : I am trying to get last location using (cmi.location) tags when user open swf, but I can save location.

    Code :

    var bookmark=scorm.get(“cmi.location”);
    if (bookmark!=null&&bookmark!=””) {
    trace(“Old location —-“+bookmark);
    } else {
    success=scorm.set(“cmi.location”,”page1″);
    if (success) {
    scorm.save();
    }
    }
    bookmark=scorm.get(“cmi.location”);
    trace(New location —-“+bookmark);

    each time its trcing only new location.

  5. @shripad i assume you meant “I cannot save location”

    You have to commit your changes; if you do a scorm.set, you have to follow it at some point with a scorm.save() to ensure the data is saved in the database.

    Also, be sure that your exit mode is set to “resume” or else every launch will be treated as a new attempt and will not give you access to data from previous attempts.

    If you have further technical questions please post them on the elearning and development google group.

  6. Hello,

    Thank you, thank you, thank you. Yours is the only site covering SCORM that has made sense to me. I’ve used and amended the files above and tested in Test Track and it works just as it should.

    Do you know if it is possible to save multiple scores from a single swf?

    Thanks again

    Chris

  7. @chris glad it works for you.

    RE: saving multiple scores, you primarily have two options:

    1. The ‘official’ place to score multiple scores would probably be cmi.interactions; each interaction can save its own score. However, most LMSs don’t report on SCORM interactions, so it may not help you. Also, SCORM 1.2 interactions are write-only while 2004 are read/write.

    2. You can also store multiple scores in cmi.suspend_data, which is basically a text field you can use to store a custom string. If you save your scores into a JSON string (or maybe array.join) you can write them to cmi.suspend_data. cmi.suspend_data is commonly used for these kinds of bits and pieces because it’s easier than cmi.interactions. However it also suffers from poor reporting in most LMSs.

    If you have more questions or need clarification about SCORM, please post them on the elearning and development google group… there are lots of knowledgeable SCORM folks there who can give suggestions and maybe some code samples.

  8. Pingback: Lectora X3 - Text issues When Publishing to HTML and HyperLinks

Comments are closed.