Last active
May 16, 2017 12:24
-
-
Save mpkliewer/8381fc7c3c033dbac7a8076c37e249b7 to your computer and use it in GitHub Desktop.
xAPI Party 2017 - Google Form > xAPI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Google Form > xAPI | |
## xAPI Party 2017 (Maker Space Project) | |
### General purpose form to capture various activity types into a Learning Record Store (LRS) | |
See live example here: http://bit.ly/xapi-party | |
The file `xapi-send.gs` is a Google Apps Script (https://developers.google.com/apps-script/overview). It's essentially JavaScript with some Google-specific API methods. | |
It, and the form, are connected to a Google Sheet. | |
### To create a new form for your own use: | |
* Create a new Google Sheet | |
* Go to **Tools > Create a form** | |
* Follow the structure and field names from http://bit.ly/xapi-party | |
* Or, create your own - however, you'll need to make sure to update the code in the script to match | |
* Go to **Tools > Script editor...** | |
* Create a new script file (any name is fine) | |
* Copy/paste the code from `xapi-send.gs` | |
* Edit the fields as necessary, particularly LRS credentials at the top | |
After everything is created and saved, upon the next open (or reload) of the Sheet, the script functions will be attached. | |
When the form is submitted, it will be added to the spreadsheet (default action) as well as send the xAPI statement. | |
### If you don't see any statements come through: | |
You can check logs in the Script Editor: **View > Execution transcript** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// make sure the endpoint ends with "/statements" - we're sending directly using the Statement API | |
var endpoint = '[YOUR_ENDPOINT_HERE]'; | |
var username = '[YOUR_USERNAME_HERE]'; | |
var password = '[YOUR_PASSWORD_HERE]'; | |
// if this list is edited, make sure to update the form accordingly | |
var verbList = { | |
experienced: 'http://adlnet.gov/expapi/verbs/experienced', | |
attended: 'http://adlnet.gov/expapi/verbs/attended', | |
completed: 'http://adlnet.gov/expapi/verbs/completed', | |
answered: 'http://adlnet.gov/expapi/verbs/answered', | |
attempted: 'http://adlnet.gov/expapi/verbs/attempted', | |
shared: 'http://adlnet.gov/expapi/verbs/shared', | |
watched: 'http://activitystrea.ms/schema/1.0/watch', | |
visited: 'http://adlnet.gov/expapi/verbs/visited', | |
arrived: 'http://adlnet.gov/expapi/verbs/arrived', | |
played: 'http://activitystrea.ms/schema/1.0/play', | |
mentored: 'http://id.tincanapi.com/verb/mentored', | |
joined: 'http://activitystrea.ms/schema/1.0/join', | |
'checked in': 'http://activitystrea.ms/schema/1.0/checkin', | |
listened: 'http://activitystrea.ms/schema/1.0/listen', | |
presented: 'http://activitystrea.ms/schema/1.0/present', | |
read: 'http://activitystrea.ms/schema/1.0/read', | |
started: 'http://activitystrea.ms/schema/1.0/start', | |
interviewed: 'http://id.tincanapi.com/verb/interviewed', | |
earned: 'http://id.tincanapi.com/verb/earned', | |
}; | |
function getVerbID(verb) { | |
return verbList[verb] || 'http://adlnet.gov/expapi/verbs/experienced'; | |
} | |
function buildStatement(data) { | |
var verbField = data['Verb'][0]; | |
var tagArray = data['Tags'][0].split(','); | |
var trimmedTags = []; | |
tagArray.forEach(function(tag) { | |
trimmedTags.push(tag.trim()); | |
}); | |
var dateNow = new Date().toISOString(); | |
var stmt = { | |
actor: { | |
name: data.Name[0], | |
mbox: 'mailto:' + data.Email[0] | |
}, | |
verb: { | |
id: getVerbID(verbField), | |
display: { | |
"en-US": verbField | |
} | |
}, | |
object: { | |
id: data['Activity URL'][0], | |
definition: { | |
name: { | |
"en-US": data['Activity Name'][0] | |
}, | |
description: { | |
"en-US": data['Activity Description'][0] | |
} | |
} | |
}, | |
context: { | |
extensions: { | |
"http://www.torrancelearning.com/xapi-cohort/extensions/": { | |
date: data['Activity Date'][0] || dateNow, | |
tags: trimmedTags, | |
notes: data['Notes'][0] | |
} | |
} | |
}, | |
timestamp: dateNow | |
}; | |
sendStatement(stmt); | |
} | |
function sendStatement(stmt) { | |
var options = { | |
'method' : 'post', | |
'contentType': 'application/json', | |
'payload' : JSON.stringify(stmt), | |
'headers': { | |
'Authorization': 'Basic ' + Utilities.base64Encode(username + ':' + password), | |
'X-Experience-API-Version': '1.0.0', | |
} | |
}; | |
var response = UrlFetchApp.fetch(endpoint, options); | |
Logger.log('LRS response: ' + response); | |
} | |
function onFormSubmit(e) { | |
var data = e.namedValues; | |
Logger.log('form data' + data); | |
buildStatement(data); | |
} | |
function onOpen() { | |
var ss = SpreadsheetApp.getActive(); | |
ScriptApp.newTrigger('onFormSubmit').forSpreadsheet(ss).onFormSubmit() | |
.create(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment