Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix addValueSet in project_questions #1108

Merged
merged 4 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
225 changes: 123 additions & 102 deletions rdmo/projects/static/projects/js/project_questions/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -884,14 +884,14 @@ angular.module('project_questions')
if (response.status == 500) {
service.error = response;
} else if (response.status == 400) {
service.error = true;
service.error = response;
if (angular.isDefined(response.data.text)) {
value.errors = response.data.text
} else {
value.errors = Object.keys(response.data);
}
} else if (response.status == 404) {
service.error = true;
service.error = response;
value.errors = ['not_found']
}
})
Expand Down Expand Up @@ -944,8 +944,8 @@ angular.module('project_questions')
service.error = null; // reset error before saving
if (service.settings.project_questions_autosave) {
service.save(false, true).then(function() {
if (service.error !== null) {
// pass, dont jump
if (service.error !== null && service.error.status !== 400) {
// pass, dont jump (but not on a validation error)
} else if (angular.isDefined(page)) {
service.initView(page.id);
} else if (angular.isDefined(section)) {
Expand All @@ -970,118 +970,127 @@ angular.module('project_questions')
return service.storeValues().then(function() {
if (service.error !== null) {
// pass
} else if (service.page.id == false) {
// pass, the interview is done
} else if (angular.isDefined(jump) && jump) {
// after the jump initView is called, so we do not need to do anything here
} else if (angular.isDefined(proceed) && proceed) {
if (service.settings.project_questions_cycle_sets && service.page.is_collection) {
if (service.set_index === null) {
service.next();
} else {
var valuesets = service.valuesets[service.page.id][service.set_prefix];
var index = service.findIndex(valuesets, 'set_index', service.set_index);

if (index === valuesets.length - 1) {
// this is the last valueset, go to the next page
} else {
service.updateProgress()

if (service.page.id == false) {
// pass, the interview is done
} else if (angular.isDefined(jump) && jump) {
// after the jump initView is called, so we do not need to do anything here
} else if (angular.isDefined(proceed) && proceed) {
if (service.settings.project_questions_cycle_sets && service.page.is_collection) {
if (service.set_index === null) {
service.next();
} else {
// activate the next valueset
service.set_index = valuesets[index + 1].set_index;
$window.scrollTo(0, 0);
var valuesets = service.valuesets[service.page.id][service.set_prefix];
var index = service.findIndex(valuesets, 'set_index', service.set_index);

if (index === valuesets.length - 1) {
// this is the last valueset, go to the next page
service.next();
} else {
// activate the next valueset
service.set_index = valuesets[index + 1].set_index;
$window.scrollTo(0, 0);
}
}
} else {
service.next();
}
} else {
service.next();
service.updateView();
}
} else {
// check if we need to refresh the site
angular.forEach([service.page].concat(service.questionsets), function(questionset) {
angular.forEach(questionset.elements, function(element) {
if (element.model == 'questions.question') {
var question = element;
angular.forEach(question.optionsets, function(optionset) {
if (optionset.has_refresh) {
return service.initView(service.page.id, false);
}
});
}
});
});
}
});
};

// update progress
if (service.project.read_only !== true) {
resources.projects.postAction({
id: service.project.id,
detail_action: 'progress'
}, function(response) {
service.progress = response
});
}
service.updateProgress = function() {
if (service.project.read_only !== true) {
resources.projects.postAction({
id: service.project.id,
detail_action: 'progress'
}, function(response) {
service.progress = response
});
}
}

// update navigation
if (service.project.read_only !== true) {
resources.projects.query({
id: service.project.id,
detail_id: service.page.section.id,
detail_action: 'navigation'
}, function(response) {
service.navigation = service.initNavigation(response)
service.updateView = function() {
// check if we need to refresh the site
angular.forEach([service.page].concat(service.questionsets), function(questionset) {
angular.forEach(questionset.elements, function(element) {
if (element.model == 'questions.question') {
var question = element;
angular.forEach(question.optionsets, function(optionset) {
if (optionset.has_refresh) {
return service.initView(service.page.id, false);
}
});
}
});
});

// re-evaluate conditions
angular.forEach([service.page].concat(service.questionsets), function(questionset) {
angular.forEach(service.valuesets[questionset.id], function(valuesets) {
angular.forEach(valuesets, function(valueset) {
angular.forEach(questionset.elements, function(element) {
if (element.model == 'questions.questionset') {
var qs = element;
if (qs.has_conditions) {
promises.push(resources.projects.get({
id: service.project.id,
detail_action: 'resolve',
questionset: qs.id,
set_prefix: valueset.set_prefix,
set_index: valueset.set_index
}, function(response) {
valueset.hidden.questionsets[qs.id] = !response.result;
}).$promise);
}
} else {
var q = element;
if (q.has_conditions) {
promises.push(resources.projects.get({
id: service.project.id,
detail_action: 'resolve',
question: q.id,
set_prefix: valueset.set_prefix,
set_index: valueset.set_index
}, function(response) {
valueset.hidden.questions[q.id] = !response.result;
}).$promise);
}
angular.forEach(q.optionsets, function(optionset) {
if (optionset.has_conditions) {
promises.push(resources.projects.get({
id: service.project.id,
detail_action: 'resolve',
optionset: optionset.id,
set_prefix: valueset.set_prefix,
set_index: valueset.set_index
}, function(response) {
valueset.hidden.optionsets[optionset.id] = !response.result;
}).$promise);
}
});
// update navigation
if (service.project.read_only !== true) {
resources.projects.query({
id: service.project.id,
detail_id: service.page.section.id,
detail_action: 'navigation'
}, function(response) {
service.navigation = service.initNavigation(response)
});
}

// re-evaluate conditions
angular.forEach([service.page].concat(service.questionsets), function(questionset) {
angular.forEach(service.valuesets[questionset.id], function(valuesets) {
angular.forEach(valuesets, function(valueset) {
angular.forEach(questionset.elements, function(element) {
if (element.model == 'questions.questionset') {
var qs = element;
if (qs.has_conditions) {
promises.push(resources.projects.get({
id: service.project.id,
detail_action: 'resolve',
questionset: qs.id,
set_prefix: valueset.set_prefix,
set_index: valueset.set_index
}, function(response) {
valueset.hidden.questionsets[qs.id] = !response.result;
}).$promise);
}
} else {
var q = element;
if (q.has_conditions) {
promises.push(resources.projects.get({
id: service.project.id,
detail_action: 'resolve',
question: q.id,
set_prefix: valueset.set_prefix,
set_index: valueset.set_index
}, function(response) {
valueset.hidden.questions[q.id] = !response.result;
}).$promise);
}
angular.forEach(q.optionsets, function(optionset) {
if (optionset.has_conditions) {
promises.push(resources.projects.get({
id: service.project.id,
detail_action: 'resolve',
optionset: optionset.id,
set_prefix: valueset.set_prefix,
set_index: valueset.set_index
}, function(response) {
valueset.hidden.optionsets[optionset.id] = !response.result;
}).$promise);
}
});
});
}
});
});
}
});
});
};
}

service.changed = function(value, autosave=false) {
value.changed = true;
Expand Down Expand Up @@ -1234,7 +1243,9 @@ angular.module('project_questions')
var last_set_index = valuesets[0].set_index;
set_index = last_set_index + 1;
}
service.valuesets[questionset.id][set_prefix].push(factories.valuesets(set_prefix, set_index));

var valueset = factories.valuesets(set_prefix, set_index);
service.valuesets[questionset.id][set_prefix].push(valueset);

// loop over questions to initialize them with at least one value, and init checkboxes and widgets
angular.forEach(questionset.elements, function(element) {
Expand All @@ -1259,6 +1270,10 @@ angular.module('project_questions')
service.values[question.attribute][set_prefix][set_index] = service.initCheckbox(question, service.values[question.attribute][set_prefix][set_index]);
}

if (question.has_conditions) {
valueset.hidden.questions[question.id] = true
}

angular.forEach(service.values[question.attribute][set_prefix][set_index], function(value) {
service.initWidget(question, value);
});
Expand All @@ -1285,7 +1300,10 @@ angular.module('project_questions')
if (angular.isUndefined(service.values[questionset.attribute][set_prefix][set_index][0])) {
service.values[questionset.attribute][set_prefix][set_index].push(value);
}
service.storeValue(value, null, set_prefix, set_index, 0)
service.storeValue(value, null, set_prefix, set_index, 0).then(function(value) {
service.updateProgress();
service.updateView();
})
}

// recursively loop over child questionsets and sets
Expand Down Expand Up @@ -1375,6 +1393,9 @@ angular.module('project_questions')
service.removeValueSet(qs, sp, si);
});
}

service.updateProgress();
service.updateView();
});

// if this is the top level questionset,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% load i18n %}
{% load core_tags %}

<div class="text-danger" ng-show="service.error.statusText && service.error.status">
<div class="text-danger" ng-show="service.error.statusText && service.error.status != 400">
<p>
{% trans 'An error occurred while saving the answer. Please contact support if this problem persists.' %}
</p>
Expand Down