Commit 8715a610 by Matjaz Gregoric

Update display when answer is changed.

When the user first submits the answers, the checkmark/cross icons,
tip popups, and a message are shown to the user.

If the user changes some of the answers, the checkmarks, popups, and message
should automatically update. This is done by automatically resubmitting the answer.
Auto submission is only enabled if number of attempts is not limited (max_attempts = 0),
parent e039d7fc
...@@ -138,6 +138,13 @@ class MentoringBlock(XBlockWithLightChildren): ...@@ -138,6 +138,13 @@ class MentoringBlock(XBlockWithLightChildren):
child.save() child.save()
completed = completed and child_result['completed'] completed = completed and child_result['completed']
if self.max_attempts_reached:
message = self.get_message_html('max_attempts_reached')
elif completed:
message = self.get_message_html('completed')
else:
message = self.get_message_html('incomplete')
# Once it has been completed once, keep completion even if user changes values # Once it has been completed once, keep completion even if user changes values
if self.completed: if self.completed:
completed = True completed = True
...@@ -146,13 +153,6 @@ class MentoringBlock(XBlockWithLightChildren): ...@@ -146,13 +153,6 @@ class MentoringBlock(XBlockWithLightChildren):
if self.max_attempts_reached: if self.max_attempts_reached:
completed = False completed = False
if completed:
message = self.get_message_html('completed')
elif self.max_attempts_reached:
message = self.get_message_html('max_attempts_reached')
else:
message = self.get_message_html('incomplete')
if self.has_missing_dependency: if self.has_missing_dependency:
completed = False completed = False
message = 'You need to complete all previous steps before being able to complete '+\ message = 'You need to complete all previous steps before being able to complete '+\
......
...@@ -3,7 +3,7 @@ function AnswerBlock(runtime, element) { ...@@ -3,7 +3,7 @@ function AnswerBlock(runtime, element) {
init: function(options) { init: function(options) {
// register the child validator // register the child validator
$(':input', element).on('keyup', options.blockValidator); $(':input', element).on('keyup', _.debounce(options.onChange, 1000));
var checkmark = $('.answer-checkmark', element); var checkmark = $('.answer-checkmark', element);
var completed = $('.xblock-answer', element).data('completed'); var completed = $('.xblock-answer', element).data('completed');
......
function MentoringBlock(runtime, element) { function MentoringBlock(runtime, element) {
var attemptsTemplate = _.template($('#xblock-attempts-template').html()); var attemptsTemplate = _.template($('#xblock-attempts-template').html());
var children; // Keep track of children. A Child need a single object scope for its data. var children; // Keep track of children. A Child need a single object scope for its data.
var submitXHR;
function renderAttempts() { function renderAttempts() {
var data = $('.attempts', element).data(); var data = $('.attempts', element).data();
...@@ -83,27 +84,46 @@ function MentoringBlock(runtime, element) { ...@@ -83,27 +84,46 @@ function MentoringBlock(runtime, element) {
} }
} }
function initXBlock() { function submit() {
var submit_dom = $(element).find('.submit .input-main'); var success = true;
var data = {};
submit_dom.bind('click', function() { var children = getChildren(element);
var success = true; for (var i = 0; i < children.length; i++) {
var data = {}; var child = children[i];
var children = getChildren(element); if (child.name !== undefined) {
for (var i = 0; i < children.length; i++) { data[child.name] = callIfExists(child, 'submit');
var child = children[i];
if (child.name !== undefined) {
data[child.name] = callIfExists(child, 'submit');
}
} }
var handlerUrl = runtime.handlerUrl(element, 'submit'); }
$.post(handlerUrl, JSON.stringify(data)).success(handleSubmitResults); var handlerUrl = runtime.handlerUrl(element, 'submit');
}); if (submitXHR) {
submitXHR.abort();
}
submitXHR = $.post(handlerUrl, JSON.stringify(data)).success(handleSubmitResults);
}
function resubmit() {
// Don't autosubmit if number of attempts is limited.
var max_attempts = $('.attempts', element).data('max_attempts');
// Only autosubmit if messages are already shown.
var messages_dom = $('.messages', element);
if (messages_dom.html().trim() && !max_attempts) {
submit();
}
}
function onChange() {
validateXBlock();
resubmit();
}
function initXBlock() {
$(element).find('.submit .input-main').bind('click', submit);
// init children (especially mrq blocks) // init children (especially mrq blocks)
var children = getChildren(element); var children = getChildren(element);
var options = { var options = {
blockValidator: validateXBlock onChange: onChange
}; };
_.each(children, function(child) { _.each(children, function(child) {
callIfExists(child, 'init', options); callIfExists(child, 'init', options);
......
...@@ -46,7 +46,7 @@ function MessageView(element) { ...@@ -46,7 +46,7 @@ function MessageView(element) {
function MCQBlock(runtime, element) { function MCQBlock(runtime, element) {
return { return {
init: function(options) { init: function(options) {
$('input[type=radio]', element).on('change', options.blockValidator); $('input[type=radio]', element).on('change', options.onChange);
}, },
submit: function() { submit: function() {
...@@ -123,7 +123,7 @@ function MCQBlock(runtime, element) { ...@@ -123,7 +123,7 @@ function MCQBlock(runtime, element) {
function MRQBlock(runtime, element) { function MRQBlock(runtime, element) {
return { return {
init: function(options) { init: function(options) {
$('input[type=checkbox]', element).on('change', options.blockValidator); $('input[type=checkbox]', element).on('change', options.onChange);
}, },
submit: function() { submit: function() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment