Commit 026446e4 by Tim Krones

Clean up.

- Improve naming of functions that handle submission results
- Add comments
- Remove empty leftover comments
- DRY out code that handles showing step-level feedback
- Don't name things using snake_case in JS code
- Use "callIfExists" in more places
parent 9c847f23
...@@ -31,8 +31,7 @@ function MentoringWithStepsBlock(runtime, element) { ...@@ -31,8 +31,7 @@ function MentoringWithStepsBlock(runtime, element) {
return (data.num_attempts < data.max_attempts); return (data.num_attempts < data.max_attempts);
} }
function updateActiveStep(response) { function showFeedback(response) {
// Update UI
if (response.completed === 'correct') { if (response.completed === 'correct') {
checkmark.addClass('checkmark-correct icon-ok fa-check'); checkmark.addClass('checkmark-correct icon-ok fa-check');
} else if (response.completed === 'partial') { } else if (response.completed === 'partial') {
...@@ -40,7 +39,14 @@ function MentoringWithStepsBlock(runtime, element) { ...@@ -40,7 +39,14 @@ function MentoringWithStepsBlock(runtime, element) {
} else { } else {
checkmark.addClass('checkmark-incorrect icon-exclamation fa-exclamation'); checkmark.addClass('checkmark-incorrect icon-exclamation fa-exclamation');
} }
}
function handleResults(response) {
showFeedback(response);
// Update active step:
// If we end up at the review step, proceed with updating the number of attempts used.
// Otherwise, get UI ready for showing next step.
var handlerUrl = runtime.handlerUrl(element, 'update_active_step'); var handlerUrl = runtime.handlerUrl(element, 'update_active_step');
$.post(handlerUrl, JSON.stringify(activeStep+1)) $.post(handlerUrl, JSON.stringify(activeStep+1))
.success(function(response) { .success(function(response) {
...@@ -48,7 +54,7 @@ function MentoringWithStepsBlock(runtime, element) { ...@@ -48,7 +54,7 @@ function MentoringWithStepsBlock(runtime, element) {
if (activeStep === -1) { if (activeStep === -1) {
updateNumAttempts(); updateNumAttempts();
} else { } else {
handleResults(); updateControls();
} }
}); });
} }
...@@ -58,6 +64,7 @@ function MentoringWithStepsBlock(runtime, element) { ...@@ -58,6 +64,7 @@ function MentoringWithStepsBlock(runtime, element) {
$.post(handlerUrl, JSON.stringify({})) $.post(handlerUrl, JSON.stringify({}))
.success(function(response) { .success(function(response) {
attemptsDOM.data('num_attempts', response.num_attempts); attemptsDOM.data('num_attempts', response.num_attempts);
// Now that relevant info is up-to-date, get the latest grade
updateGrade(); updateGrade();
}); });
} }
...@@ -79,11 +86,11 @@ function MentoringWithStepsBlock(runtime, element) { ...@@ -79,11 +86,11 @@ function MentoringWithStepsBlock(runtime, element) {
$.post(handlerUrl, JSON.stringify({})) $.post(handlerUrl, JSON.stringify({}))
.success(function(response) { .success(function(response) {
gradeDOM.data('assessment_review_tips', response.review_tips); gradeDOM.data('assessment_review_tips', response.review_tips);
handleResults(); updateControls();
}); });
} }
function handleResults() { function updateControls() {
submitDOM.attr('disabled', 'disabled'); submitDOM.attr('disabled', 'disabled');
nextDOM.removeAttr("disabled"); nextDOM.removeAttr("disabled");
...@@ -106,7 +113,7 @@ function MentoringWithStepsBlock(runtime, element) { ...@@ -106,7 +113,7 @@ function MentoringWithStepsBlock(runtime, element) {
function submit() { function submit() {
// We do not handle submissions at this level, so just forward to "submit" method of active step // We do not handle submissions at this level, so just forward to "submit" method of active step
var step = steps[activeStep]; var step = steps[activeStep];
step.submit(updateActiveStep); step.submit(handleResults);
} }
function getResults() { function getResults() {
...@@ -116,13 +123,7 @@ function MentoringWithStepsBlock(runtime, element) { ...@@ -116,13 +123,7 @@ function MentoringWithStepsBlock(runtime, element) {
function handleReviewResults(response) { function handleReviewResults(response) {
// Show step-level feedback // Show step-level feedback
if (response.completed === 'correct') { showFeedback(response);
checkmark.addClass('checkmark-correct icon-ok fa-check');
} else if (response.completed === 'partial') {
checkmark.addClass('checkmark-partially-correct icon-ok fa-check');
} else {
checkmark.addClass('checkmark-incorrect icon-exclamation fa-exclamation');
}
// Forward to active step to show answer level feedback // Forward to active step to show answer level feedback
var step = steps[activeStep]; var step = steps[activeStep];
var results = response.results; var results = response.results;
...@@ -233,12 +234,10 @@ function MentoringWithStepsBlock(runtime, element) { ...@@ -233,12 +234,10 @@ function MentoringWithStepsBlock(runtime, element) {
nextDOM.removeAttr('disabled'); nextDOM.removeAttr('disabled');
} }
// ...
tryAgainDOM.hide(); tryAgainDOM.hide();
submitDOM.show(); submitDOM.show();
submitDOM.attr('disabled', 'disabled'); submitDOM.attr('disabled', 'disabled');
reviewLinkDOM.show(); reviewLinkDOM.show();
// ...
getResults(); getResults();
} }
...@@ -305,10 +304,12 @@ function MentoringWithStepsBlock(runtime, element) { ...@@ -305,10 +304,12 @@ function MentoringWithStepsBlock(runtime, element) {
showAssessmentMessage(); showAssessmentMessage();
showReviewStep(); showReviewStep();
showAttempts(); showAttempts();
// Disable "Try again" button if no attempts left // Disable "Try again" button if no attempts left
if (!someAttemptsLeft()) { if (!someAttemptsLeft()) {
tryAgainDOM.attr("disabled", "disabled"); tryAgainDOM.attr("disabled", "disabled");
} }
nextDOM.off(); nextDOM.off();
nextDOM.on('click', reviewNextStep); nextDOM.on('click', reviewNextStep);
reviewLinkDOM.hide(); reviewLinkDOM.hide();
......
...@@ -34,13 +34,13 @@ function MentoringStepBlock(runtime, element) { ...@@ -34,13 +34,13 @@ function MentoringStepBlock(runtime, element) {
return is_valid; return is_valid;
}, },
submit: function(result_handler) { submit: function(resultHandler) {
var handler_name = 'submit'; var handler_name = 'submit';
var data = {}; var data = {};
for (var i = 0; i < children.length; i++) { for (var i = 0; i < children.length; i++) {
var child = children[i]; var child = children[i];
if (child && child.name !== undefined && typeof(child[handler_name]) !== "undefined") { if (child && child.name !== undefined) {
data[child.name.toString()] = child[handler_name](); data[child.name.toString()] = callIfExists(child, handler_name);
} }
} }
var handlerUrl = runtime.handlerUrl(element, handler_name); var handlerUrl = runtime.handlerUrl(element, handler_name);
...@@ -49,11 +49,11 @@ function MentoringStepBlock(runtime, element) { ...@@ -49,11 +49,11 @@ function MentoringStepBlock(runtime, element) {
} }
submitXHR = $.post(handlerUrl, JSON.stringify(data)) submitXHR = $.post(handlerUrl, JSON.stringify(data))
.success(function(response) { .success(function(response) {
result_handler(response); resultHandler(response);
}); });
}, },
getResults: function(result_handler) { getResults: function(resultHandler) {
var handler_name = 'get_results'; var handler_name = 'get_results';
var data = []; var data = [];
for (var i = 0; i < children.length; i++) { for (var i = 0; i < children.length; i++) {
...@@ -68,7 +68,7 @@ function MentoringStepBlock(runtime, element) { ...@@ -68,7 +68,7 @@ function MentoringStepBlock(runtime, element) {
} }
resultsXHR = $.post(handlerUrl, JSON.stringify(data)) resultsXHR = $.post(handlerUrl, JSON.stringify(data))
.success(function(response) { .success(function(response) {
result_handler(response); resultHandler(response);
}); });
}, },
......
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