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", "completed");
 
        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);
}
Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • FriendFeed
  • LinkedIn
  • Netvibes
  • RSS
  • Technorati
  • Twitter
  • Live
  • StumbleUpon

Possibly related journal entries:

One Response to “Really simple SCORM AS3 wrapper example”

  1. 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 [...]