Skip to content

Commit

Permalink
Refactor the answer matching code for the survey
Browse files Browse the repository at this point in the history
Since we already match the user input for the trip during
populateInputsAndInferences, we don't need to re-pull all the entries and
re-match during the survey launch.

This is likely to be particularly bad if we collect data over a long period of
time. Reading all the user inputs for a year every time we want to launch the
survey is quickly going to get old.

This also means that we need to store the entire survey object instead of only the label
when we match entries.
e-mission/e-mission-docs#727 (comment)
  • Loading branch information
shankari committed May 11, 2022
1 parent 7a8b99b commit 72ba468
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 28 deletions.
17 changes: 7 additions & 10 deletions www/js/survey/enketo/enketo-trip-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ angular.module('emission.survey.enketo.trip.button',
if (!result) {
return;
}
$scope.$apply(() => trip.userInput[EnketoTripButtonService.SINGLE_KEY] = {text: result.label});
$scope.$apply(() => trip.userInput[EnketoTripButtonService.SINGLE_KEY] = {data: result});
// store is commented out since the enketo survey launch currently
// stores the value as well
// $scope.store(inputType, result, false);
Expand Down Expand Up @@ -186,23 +186,20 @@ angular.module('emission.survey.enketo.trip.button',
// Check unprocessed labels first since they are more recent
const unprocessedLabelEntry = InputMatcher.getUserInputForTrip(trip, nextTrip,
inputList);
var userInputLabel = unprocessedLabelEntry? unprocessedLabelEntry.data.label : undefined;
if (!angular.isDefined(userInputLabel)) {
userInputLabel = trip.user_input[etbs.inputType2retKey(inputType)];
var userInputEntry = unprocessedLabelEntry;
if (!angular.isDefined(userInputEntry)) {
userInputEntry = trip.user_input[etbs.inputType2retKey(inputType)];
}
etbs.populateInput(trip.userInput, inputType, userInputLabel);
etbs.populateInput(trip.userInput, inputType, userInputEntry);
// Logger.log("Set "+ inputType + " " + JSON.stringify(userInputEntry) + " for trip starting at " + JSON.stringify(trip.start_fmt_time));
etbs.editingTrip = angular.undefined;
}

/**
* Insert the given userInputLabel into the given inputType's slot in inputField
*/
etbs.populateInput = function(tripField, inputType, userInputLabel) {
if (angular.isDefined(userInputLabel)) {
var userInputEntry;
userInputEntry = {text: userInputLabel};
console.log("Mapped label "+userInputLabel+" to entry "+JSON.stringify(userInputEntry));
etbs.populateInput = function(tripField, inputType, userInputEntry) {
if (angular.isDefined(userInputEntry)) {
tripField[inputType] = userInputEntry;
}
}
Expand Down
26 changes: 9 additions & 17 deletions www/js/survey/enketo/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,20 +105,14 @@ angular.module('emission.survey.enketo.service', [
* @param {EnketoAnswer[]} answers survey answers
* @returns {Promise<string>} answer string promise
*/
function _restoreAnswer(answers) {
return EnketoSurveyAnswer.filterByNameAndVersion(_state.name, answers).then(answers => {
let answer = null;
if (!answers.length) {
return null;
}
if (_state.opts.trip) {
answer = InputMatcher.getUserInputForTrip(_state.opts.trip, undefined, answers);
if (answer) {
return answer.data.xmlResponse;
}
}
function _restoreAnswer() {
if (_state.opts.trip) {
answer = _state.opts.trip.userInput["SURVEY"];
return answer ? answer.data.xmlResponse : null;
});
} else {
// TODO: Figure out how to retrieve and match the profile survey
return answer ? answer.data.xmlResponse : null;
}
}

/**
Expand Down Expand Up @@ -183,10 +177,8 @@ angular.module('emission.survey.enketo.service', [
* @returns {string[]} errors
*/
function showModal() {
const tq = $window.cordova.plugins.BEMUserCache.getAllTimeQuery();
return UnifiedDataLoader.getUnifiedMessagesForInterval(DATA_KEY, tq)
.then(answers => _restoreAnswer(answers))
.then(instanceStr => _loadForm({ instanceStr }));
const instanceStr = _restoreAnswer();
return Promise.resolve(_loadForm({ instanceStr }));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion www/templates/survey/enketo/summary-trip-button.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<div class="row input-confirm-row">
<div ng-if="trip.userInput['SURVEY']" class="input-confirm-container col">
<button ng-click ="openPopover($event, trip, 'SURVEY')" class="button btn-input-confirm btn-input-confirm-green">
{{trip.userInput['SURVEY'].text}}
{{trip.userInput['SURVEY'].data.label}}
</button>
</div>
<!-- Buttons (if inferred but not confirmed)
Expand Down

0 comments on commit 72ba468

Please sign in to comment.