A javascript xAPI client.
The project contains a comprehensive xAPI javascript client sdk. It was developed to allow applications using javascript the capability to communicate with an LRS. It is currently compatible with xAPI Spec 1.0.2.
- Compatible with xAPI Spec 1.0.2
- Client side validation of xAPI statement
- Queuing of failed requests from network connectivity
- Standalone script (No third party dependencies)
- Support for Document APIs
- Support for statement attachments using multipart/mixed content type
- Include javascript
<script type="text/javascript" src="/lib/xapi.js"></script>
- Set LRS Endpoint
xapi.setLRS('http://www.rockinlrs.com');
All classes are stored in the xapi namespace.
Below are the many of the classes and methods defined in xAPI client script, including details about use and example code blocks.
xapi.Activity.create()
Build a new activity for an xAPI statement from the provided data.
// setup activity information
var activityData = {
"id": id,
"objectType": objectType,
"definition": definition,
};
// then create a new activity with the data
var myActivity = new xapi.Activity.create(activityData);
// alternativley, create an activity piece by piece.
var myActivity = new xapi.Activity.create(id, objectType, definition);
xapi.Activity.get()
Retrieves the activity's profile and sends the data to a callback.
// assume myActivity is a previously created activity object
myActivity.get(callbackFn);
xapi.ActivityProfile.create()
Build a new activityProfile for an xAPI statement from the provided data.
// setup profile information
var activityProfileData = {
"activityId": activityId,
"profileId": profileId,
"since": email,
"document": id
};
// then create a new activityProfile with the data
var myActivityProfile = new xapi.ActivityProfile.create(activityProfileData);
// alternativley, create an actor piece by piece.
var myActivityProfile = new xapi.ActivityProfile.create(activityId, profileId, since, document);
xapi.ActivityProfile.get()
Retrieve the specified profile document and feed it to the provided callback.
// assume myActivityProfile is a previously created activity profile object
myActivityProfile.get(callbackFn);
xapi.ActivityProfile.getAll()
Retrieve all profile id entries for the the activity profile and feed it to the provided callback.
// assume myActivityProfile is a previously created activity profile object
myActivityProfile.getAll(callbackFn);
xapi.ActivityProfile.update()
Modify a specified profile document and send resulting data to the provided callback.
// assume myActivityProfile is a previously created activity profile object
myActivityProfile.update(callbackFn);
xapi.ActivityProfile.post()
Creates an activity profile document for the specified profile document. Send resulting data to the provided callback.
// assume myActivityProfile is a previously created activity profile object
myActivityProfile.post(callbackFn);
xapi.ActivityProfile.remove()
Deletes the profile doument for the specified activity profile. Send resulting data to the provided callback.
// assume myActivityProfile is a previously created activity profile object
myActivityProfile.remove(callbackFn);
Actors can be Person
s or Agent
s.
xapi.Actor.create()
Build a new actor for an xAPI statement.
// setup actor information
var actorData = {
"objectType": objectType,
"name": name,
"mbox": email,
"openid": id,
"account": account,
"memebers": members
};
// then create a new actor with the data
var myActor = new xapi.Actor.create(actorData);
// alternativley, create an actor piece by piece.
var myActor = new xapi.Actor.create(objectType, name, mbox, openid, account, members);
xapi.Actor.get()
Retrieve the agent data from the given actor and feed it to the provided callback.
// assume myActor is a previously created actor object (which is an Agent)
// get the Actor's agent's data sent to the callbackFn
myActor.get(callbackFn);
xapi.AgentProfile.create()
Build a new Agent Profile.
// create an agent profile piece by piece
var myAgentProfile = new xapi.AgentProfile.create(agent, profileId, since, document);
// alternatively, build an agentProfileData object
var agentProfileData = {
"agent": agent,
"profileId": profileId,
"since": since,
"document": document
};
// then, create the profile by passing in the new object.
var myAgentProfile = new xapi.AgentProfile.create(agentProfileData);
xapi.AgentProfile.get()
Retrieve a profile that match the agentProfileData. Send the results to a callback function.
// assume myAgentProfile is a previously created profile
myAgentProfile.get(callbackFn);
xapi.AgentProfile.getAll()
Retrieve all agent profiles and that match the agentProfileData. Send the results to a callback function.
// assume myAgentProfile is a previously created profile
myAgentProfile.getAll(callbackFn);
xapi.AgentProfile.update()
Modifies a profile that match the agentProfileData. Send the results to a callback function.
// assume myAgentProfile is a previously created profile
myAgentProfile.update(callbackFn);
xapi.AgentProfile.post()
Adds the specified profile to the agent. Send the results to a callback function.
// assume myAgentProfile is a previously created profile
myAgentProfile.post(callbackFn);
xapi.AgentProfile.remove()
Removes an agent's stored profile.
// assume myAgentProfile is a previously created profile
myAgentProfile.remove(callbackFn);
Storage mechanism for holding and managing statements until able to send to the configured LRS
.
xapi.Backlog()
Builds a new statement backlog.
// create a new, empty backlog with the given string as a name
var myBacklog = xapi.Backlog('name');
xapi.Backlog.add()
Adds a statement or array of statements to the backlog and syncs with both the browser's LocalStorage and SessionStorage.
// assume myBacklog is a previously created backlog
// assume myStatement is a previously created xAPI statement
myBacklog.add(myStatement);
// alternativley, send a batch array of statements to the backlog at once.
// assume manyStatemens is a collection of previously created xAPI statements
myBacklog.add(manyStatemens);
xapi.Backlog.get()
Retrieves the array of xAPI statements in the backlog from localStorage and sessionStroage. Returns the array of backlog items.
// assume myBacklog is a previously created backlog
var myBackloggedStatements = myBacklog.get();
xapi.Backlog.syncStorage()
Sets the localStorage and sessionStorage caches contents to the provided collection of xAPI statements.
// assume myBacklog is a previously created backlog
// assume manyStatemens is a collection of previously created xAPI statements
myBacklog.syncStorage(manyStatemens);
xapi.Backlog.empty()
Clears out the backlog completely.
// assume myBacklog is a previously created backlog
myBacklog.empty();
xapi.Backlog.send()
Sends current statements in the backlog to the configured LRS
endpoint.
// assume myBacklog is a previously created backlog
myBacklog.send();
xapi.Document.create()
Statements are the objects that hold all the collected xAPI data for a single activity/verb/actor combination.
xapi.Statement.create()
Builds out a complete xAPI statement based on the provided data.
// build out a new statement with individual statement pieces
var myStatement = xapi.State.create(activityId, agent, registration, stateId, since, document);
// alternatively, build out an object containing all necessary data then call create with the aggregate object
var statementData = {
"activityId": activityId,
"agent": agent,
"registration": registration,
"stateId": stateId,
"since": since,
"document": document
};
var myStatement = xapi.State.create(statementData);
xapi.Statement.store()
Saves the xAPI statment in the local backlog
and syncronizes the local backlog
in the browser's localStorage and sessionStorage.
// assume myStatement is previously created xAPI statement
// store the statement
myStatement.store();
An object for selecting statements from the configured LRS
.
xapi.StatementQuery()
Builds out a statementQuery object for fetching xAPI statements.
//setup query parameters
var queryParams = {
"limit": "1000"
};
// build out a new query with individual statement pieces
var myQuery = xapi.StatementQuery(queryParams);
xapi.StatementQuery.get()
Call get to retrieve a the results of the query parameters set in the the Statement Query Object (myQuery).
// assume mQuery is a previously created query object
// get a statement(s) data sent to the callbackFn
myQuery.get(callbackFn);
xapi.Statement.live()
Sets up a live query.
// assume mQuery is a previously created query object
// setup live query options
var options = {
"update": updateFunction,
"connect": connectFunction,
"disconnect": disconnectFunction,
"error": errorFunction,
"data": dataFunction
};
myQuery.live(options);
An Object for creating and managing predefined xAPI verbs.
xapi.Verb.create()
Builds a verb for use in an xAPI statement.
//setup verb data
var verbData = {
"id": id,
"display": display
};
// then build create the verb from the verbData object
var myVerb = xapi.StatementQuery(verbData);
// alternatively, build a verb from individual peices
var myVerb = xapi.StatementQuery(id, display);