Comment on page
Helpers
A selection of useful helpers.
This gets the launch parameters supplied in the URL. Can be used before initializing.
import Cmi5, { LaunchParameters } from "@xapi/cmi5";
const cmi5 = new Cmi5();
const launchParameters: LaunchParameters = cmi5.getLaunchParameters();
This gets the launch data from the LMS. Can not be used before initializing the AU.
import Cmi5, { LaunchData } from "@xapi/cmi5";
const cmi5 = new Cmi5();
cmi5.initialize().then(() => {
const launchData: LaunchData = cmi5.getLaunchData();
});
This gets the session's authToken. Useful for storing in non-volatile storage local to the user (e.g. SessionStorage) to preserve the state between page refreshes. Pass this value and
initializedDate
to initialize
to restore the previous session.// Storing the auth token
const cmi5 = new Cmi5();
cmi5.initialize().then(() => {
const authToken = cmi5.getAuthToken();
sessionStorage.set("cmi5-auth-token", authToken);
});
// Restoring the auth token in a new Cmi5 instance
const cmi5 = new Cmi5();
const authToken = sessionStorage.get("cmi5-auth-token");
cmi5.initialize({
authToken,
initializedDate: ...
});
This returns a
string
or null
if the Cmi5 instance has not been initialized.This gets the session's initializedDate. Useful for storing in non-volatile storage local to the user (e.g. SessionStorage) to preserve the state between page refreshes. Pass this value and
authToken
to initialize
to restore the previous session.// Storing the auth token
const cmi5 = new Cmi5();
cmi5.initialize().then(() => {
const initializedDate = cmi5.getInitializedDate();
sessionStorage.set("cmi5-initialized-date", initializedDate);
});
// Restoring the initialized date in a new Cmi5 instance
const cmi5 = new Cmi5();
const initializedDate = new Date(sessionStorage.get("cmi5-initialized-date"));
cmi5.initialize({
initializedDate,
authToken: ...
});
This returns a
Date
or null
if the Cmi5 instance has not been initialized.This gets the learner preferences from the LMS. Can not be used before initializing the AU.
import Cmi5, { LearnerPreferences } from "@xapi/cmi5";
const cmi5 = new Cmi5();
cmi5.initialize().then(() => {
const learnerPreferences: LearnerPreferences = cmi5.getLearnerPreferences();
});
This returns
true
if the environment has all the necessary parameters for initialisation.import Cmi5 from "@xapi/cmi5";
const isCmiAvailable: boolean = Cmi5.isCmiAvailable();
This returns a
Boolean
.This returns
true
if the instance constructed successfully.import Cmi5 from "@xapi/cmi5";
const cmi5 = new Cmi5();
cmi5.initialize().then(() => {
const isAuthenticated: boolean = cmi5.isAuthenticated();
});
This returns a
Boolean
.Creates a new Cmi5 singleton instance, or if already called returns the same singleton Cmi5 instance.
import Cmi5 from "@xapi/cmi5";
const cmi5instance: Cmi5 = Cmi5.instance;
This returns a global
Cmi5
singleton instance.Clears the Cmi5 singleton instance.
import Cmi5 from "@xapi/cmi5";
const cmi5instance: Cmi5 = Cmi5.instance;
Cmi5.clearInstance();
Automatically satisfies the AU based upon the Launch Data from the LMS. It will send the "cmi5 defined" statements for:
- Pass or fail if a score is provided and the mastery score is configured
- complete statement
- terminate statement (unless disabled in the options parameter)
import Cmi5 from "@xapi/cmi5";
const cmi5 = new Cmi5();
// initialize etc
cmi5.moveOn();
Parameter | Type | Required | Description |
options | false | The additional options object. |
This returns a
Promise
containing an array with the resulting statementIds if successful.Returns the internal xAPI.js Wrapper library instance. Useful for accessing any xAPI Resources using the Cmi5 authentication. Can only be accessed after successfully calling the
initialize
method.import Cmi5 from "@xapi/cmi5";
import XAPI from "@xapi/xapi"; // For types as a developer dependency, not required but may be useful!
const cmi5: Cmi5 = new Cmi5();
await cmi5.initialize();
const xapi: XAPI = cmi5.xapi;
xapi.getStatement(...);
This returns the internal
XAPI
instance, authenticated with the LRS endpoint and authentication token.Last modified 2mo ago