Skip to content

Instantly share code, notes, and snippets.

@robinvanemden
Last active January 4, 2016 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robinvanemden/6a9b84b6480092d19d4b to your computer and use it in GitHub Desktop.
Save robinvanemden/6a9b84b6480092d19d4b to your computer and use it in GitHub Desktop.
/**
*
* Qualtrics_barebones_question_for_lif.js
* Javascript code to integrate Streamingbandit & Qualtrics.
* Sends an receives JSON to and from StreamingBandit server
*
**/
/* --- insert as <script> into look and feel ---*/
function createRequest() {
var result = null;
if (window.XMLHttpRequest) {
result = new XMLHttpRequest();
if (typeof result.overrideMimeType != 'undefined') {
result.overrideMimeType('application/json');
}
} else if (window.ActiveXObject) {
result = new ActiveXObject("Microsoft.XMLHTTP");
} else {
// No known mechanism -- consider aborting the application
}
return result;
}
/* --- end </script> ---*/
/* --- insert into Question JS ---*/
Qualtrics.SurveyEngine.addOnload(function() {
/* ------------------------------------------- question settings ------------------------------------*/
// Send result previous question, change QID accordingly
var selectedChoice = '${q://QID15/ChoiceTextEntryValue}';
var selectedChoiceStr = String(selectedChoice);
var reqPrev = createRequest();
reqPrev.open("GET",
selectedChoiceStr,
true);
reqPrev.send();
/* ------------------------------------------- survey settings --------------------------------------*/
strmID = 1;
strmKey = "36c067b1d3";
ServerURL = "https://strm.mnds.org:8080/"
qPreNr = 5555
x0 = 259
// Select position in table of adaptive field for the current question
roundToFixed = 0;
/* ------------------------------------------- custom code ------------------------------------------*/
QualtricsQID = document.querySelector('.QuestionOuter').id;
questionNumber = qPreNr + QualtricsQID.substring(3)
answerID = "QR~" + QualtricsQID
var req = createRequest();
req.onreadystatechange = function() {
if (req.readyState != 4) return; // Not there yet
if (req.status != 200) {
// Handle request failure here...
return;
}
var resp = req.responseText;
var parsedJSON = eval('(' + resp + ')');
var xvalue = parsedJSON.action.x;
var tvalue = parsedJSON.action.t;
var QuestionBody = document.getElementsByClassName('QuestionBody')[0];
var imgTags = QuestionBody.getElementsByTagName('img');
var firstImg = imgTags[0];
firstImg.setAttribute("style", "height:" + xvalue.toFixed(roundToFixed) + "px; width:100px;");
document.getElementById("Questions").style.display = "block";
document.getElementById("Buttons").style.display = "block";
var radios = document.getElementsByName('size');
for (var i = 0, len = radios.length; i < len; i++) {
radios[i].onclick = function() {
rvalue = this.value /* ADAPTIVE! */
answerURL = ServerURL + strmID + '/setReward.json?key=' + strmKey + '&action={%22x%22:' + xvalue + ',%22t%22:' + tvalue + '}&reward=' + rvalue + '&context={%22question%22:' + questionNumber + ',%22x0%22:' + x0 + '}'
// Set text field to URL to be send in next question
document.getElementById(answerID).value = answerURL;
};
}
}
req.open("GET",
ServerURL + strmID + "/getAction.json?key=" + strmKey + "&context={%22question%22:" + questionNumber + ",%22x0%22:" + x0 + "}",
true);
req.send();
/* ------------------------------------------- custom code ------------------------------------------*/
});
/* --- end Question JS ---*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment