pipwerks: home

pipwerks

Standards-friendy eLearning and Web development (HTML 5 version)

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);
}

Like what you see? Why not share it!

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • FriendFeed
  • LinkedIn
  • StumbleUpon
  • Tumblr
  • Twitter

Related posts:

  1. SCORM API Wrapper updated to auto-handle exit and status
  2. How to add basic SCORM code to a Flash movie
  3. SCORM JavaScript wrapper updates
  4. SCORM JavaScript wrapper updated to 1.1.4
  5. New: SCORM API wrapper for ActionScript 2.0

What others are saying... (3 comments so far)

Shiny Button Code

[...] stuff on this blog, and now I start to make good on that promise. And no, this is not as clever as Philip Hutchinson’s SCORM Class(es), but give me a break! ActionScript 3 is kinda hard when you don’t touch Flash [...]

Chuck Stein

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”);

philip

Good catch… thanks!

Want a gravatar? They're easy and free! Just sign up at gravatar.com

Add your two cents!

You can use the following HTML tags in your comment: <a> <abbr> <b> <blockquote> <cite> <code> <em> <i> <q> <strike> <strong> <pre>

The anti-spam code is my LAST NAME, and can be found in the page footer.
Be careful not to misspell it.

I'm currently moderating comments. Please don't submit your comment twice; it will appear as soon as I can approve it.