Answer to (1) The javascript code that has to be entered in the End message:
Please note that the javascript code snippets below for MobileCoach apps built from the source code after 30th March 2020 is outdated (relevant commit is here). See the new answer.
The javascript code that needs to be entered is below. There are two cases,
a) If you don’t have to send any information from Limesurvey, please use the following,
<script>
function checkReady() {
if (window.postMessage != undefined && window.postMessage != null && typeof window.postMessage === 'function' && window.postMessage.length === 1) {
sendResults()
} else {
setTimeout(checkReady, 100)
}
}
function sendResults() {
window.postMessage('complete');
}
$(document).ready(function() {
setTimeout(checkReady, 100)
})
</script>
b) Alternatively, if you need to send any information to MobileCoach, i.e set a variable ($result) in MobileCoach with the results of the Limesurvey, please use the code below, (Please make sure that the access setting for the variable $result is set as “Manageable by Service” in the variables tab in MC Designer.
<script>
function checkReady() {
if (window.postMessage != undefined && window.postMessage != null && typeof window.postMessage === 'function' && window.postMessage.length === 1) {
sendResults()
} else {
setTimeout(checkReady, 100)
}
}
function sendResults() {
var send_data = {
'variable':'$result',
'value': '100'
};
window.postMessage(JSON.stringify(send_data));
window.postMessage('complete');
}
$(document).ready(function() {
setTimeout(checkReady, 100)
})
</script>
The MobileCoach variable $result will have the value 100 in it after this execution. Also if you want to send an answer of a question in Limesurvey to MobileCoach you can use the following code, (see sendResults method)
<script>
function checkReady() {
if (window.postMessage != undefined && window.postMessage != null && typeof window.postMessage === 'function' && window.postMessage.length === 1) {
sendResults()
} else {
setTimeout(checkReady, 100)
}
}
function sendResults() {
var send_data = {
'variable':'$result',
'value': '{Q01}'
};
window.postMessage(JSON.stringify(send_data));
window.postMessage('complete');
}
$(document).ready(function() {
setTimeout(checkReady, 100)
})
</script>
In the above code, {Q01} returns the answer to the question with question code Q01 in Limesurvey. This code is made for a text answer but for different question types the way of sending the answers to MobileCoach may differ.