Public Methods
This article details public methods the client can use to interact with the Events API.
Public Methods example
<html>
<head>
</head>
<body>
<button id="onButton">Add event handler</button>
<button id="publishButton">Publish events</button>
<button id="resetButton">Reset Events API</button>
<script src="//events.learnosity.com"></script>
<script>
init
description | Initialises the Events API. |
---|---|
arguments | initOptions (Object), eventOptions (Object) |
returns | Object, containing Events API Public Methods - all other public methods are called against this object. (See example). |
init example
var initOptions = {
/* See the Initialisation Options
section for guidance on the initialisation object.
*/
};
var eventOptions = {
readyListener: function () {
console.log("Learnosity Events API is ready");
},
errorListener: function (e) {
console.log('error', e);
}
};
var eventsApp = LearnosityEvents.init(initOptions, eventOptions);
addUsers
description |
Register additional users, after initialisation, to subscribe or publish to. |
---|---|
arguments | userHashes (Object). Object similar to the config.users initialisation option. |
returns | - |
addUsers example
document.getElementById('addUsers').onclick = function () {
var user_hashes = {
'5fb6d972-157b-4e25-b449-85e58089e714': '9bcffb8898d59fc3f3e425faa1041bf6b8f8950997cbb9cc37d576368be5d341'
};
eventsApp.addUsers(user_hashes);
};
on
description |
Set event handler callback for certain events callback is called with an array of events. |
---|---|
arguments |
events (Array). An array of objects describing the events to subscribe to. Required properties for each event object are user_id and kind. Optionally, activity_id can be passed to filter events by Activity ID. callback (Function) |
returns | - |
on example
document.getElementById('onButton').onclick = function () {
var eventHandler = function (events) {
console.log(events);
/* Example:
[
{
"id": "ac478bc1-2e0e-44c1-a19c-6cb065f06bad",
"timestamp": "2014-10-08T00:51:28.808Z",
"actor": {
"account": {
"homePage": "yis0TYCu7U9V4o7M",
"name": "jessepinkman"
},
"objectType": "Agent"
},
"verb": {
"id": "http://adlnet.gov/expapi/verbs/progressed",
"display": {
"en-US": "progressed"
}
},
"object": {
"id": "https://xapi.learnosity.com/activities/org/1/pool/null/activity/demoactivity",
"definition": {
"extensions": {
"data": 1
}
},
"objectType": "Activity"
}
}
]
*/
};
eventsApp.on(events, eventHandler);
};
publish
description |
Publish raw or valid XAPI compliant events through Events App. |
---|---|
arguments |
object (Object). Parameters are xapi - whether the objects are XAPI-compliant, and events, an array of event objects to publish. Every event must have a kind property. |
returns |
- |
publish example
document.getElementById('publishButton').onclick = function () {
eventsApp.publish({
"xapi": false, // whether it's a full(or raw) xAPI events array, true | false (default)
"events": [{
"kind": "assess_logging",
"actor": "jessepinkman", // string, user_id
"verb": "progressed", // string, verb
"object": { // string (unique "id") or object (full object)
"id": "https://xapi.learnosity.com/activities/org/1/pool/null/activity/demoactivity",
"definition": {
"extensions": {
"data": 1
}
}
}
}]
})
};
reset example
document.getElementById('resetButton').onclick = function () {
eventsApp.reset();
};
</script>
</body>
</html>