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. Extending the SCORM wrapper and ActionScript classes
  2. SCORM API Wrapper updated to auto-handle exit and status
  3. Adding SCORM code to an HTML file using the pipwerks SCORM wrapper
  4. How to add basic SCORM code to a Flash movie
  5. New: SCORM API wrapper for ActionScript 2.0

What others are saying... (9 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!

More information about SCORM « Group projects

[...] class for Flash, which he indicates is a “Really simple SCORM AS3 wrapper example” located at http://pipwerks.com/2008/02/11/really-simple-scorm-as3-wrapper-example/ [...]

shripad

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.

philip

@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.

Chris Dennett

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

philip

@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.

Lectora X3 - Text issues When Publishing to HTML and HyperLinks

[...] compliancy using AS3 into my shell Here are some references for you (hopefully, some might help): pipwerks pipwerks-scorm – Project Hosting on Google Code (probably same version of wrapper) Integrating [...]

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.