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);
}
Code language: ActionScript (actionscript)
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);
}
Code language: ActionScript (actionscript)
Comments
Chuck Stein wrote on January 15, 2010 at 12:04 pm:
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 wrote on January 15, 2010 at 6:22 pm:
Good catch… thanks!
shripad wrote on April 29, 2010 at 5:26 am:
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 wrote on May 2, 2010 at 3:43 pm:
@shripad i assume you meant "I <b>cannot</b> 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 <a href="http://pwrk.us/etd" rel="nofollow ugc">elearning and development google group</a>.
Chris Dennett wrote on June 4, 2010 at 2:57 am:
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 <a href="http://www.scorm.com/scorm-solved/test-track/" rel="nofollow ugc">Test Track</a> 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 wrote on June 4, 2010 at 9:34 am:
@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 <code>cmi.interactions</code>; 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 <code>cmi.suspend_data</code>, 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 <code>cmi.suspend_data</code>. <code>cmi.suspend_data</code> is commonly used for these kinds of bits and pieces because it's easier than <code>cmi.interactions</code>. 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 <a href="http://pwrk.us/etd" rel="nofollow ugc">elearning and development google group</a>… there are lots of knowledgeable SCORM folks there who can give suggestions and maybe some code samples.
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”);
Good catch… thanks!
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.
@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.
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
@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 tocmi.suspend_data
.cmi.suspend_data
is commonly used for these kinds of bits and pieces because it’s easier thancmi.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.