Skip to content

Commit f2ee310

Browse files
tamma1ihalaij1
authored andcommitted
Retain submitted input in acceptPost exercises
Fixes #1475
1 parent cefd417 commit f2ee310

1 file changed

Lines changed: 31 additions & 17 deletions

File tree

exercise/static/exercise/chapter.js

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@
184184
response_selector: '.exercise-response',
185185
navigation_selector: 'ul.nav a[class!="dropdown-toggle"]',
186186
dropdown_selector: 'ul.nav .dropdown-toggle',
187-
last_submission_selector: 'ul.nav ul.dropdown-menu li:first-child a',
187+
last_submission_selector: 'ul.navbar-nav ul.dropdown-menu li:first-child a',
188188
// For active elements:
189189
active_element_attr: "data-aplus-active-element",
190190
interactive_code_attr: "data-aplus-interactive-code",
@@ -377,8 +377,12 @@
377377
.done(function(data) {
378378
exercise.hideLoader();
379379
exercise.update($(data));
380+
const hasTextarea = exercise.element.find('textarea').length > 0;
381+
const hasFieldset = exercise.element.find('fieldset').length > 0;
380382
if (exercise.active_element) {
381383
exercise.loadLastSubmission($(data));
384+
} else if (hasTextarea && !hasFieldset) { // Identify acceptPost exercises
385+
exercise.loadLastSubmission($(data), true);
382386
} else {
383387
exercise.renderMath();
384388
if (!onlyThis) exercise.chapter.nextExercise();
@@ -849,15 +853,15 @@
849853
}
850854
},
851855

852-
loadLastSubmission: function(input) {
856+
loadLastSubmission: function(input, fillInputs = false) {
853857
var link = input.find(this.settings.last_submission_selector);
854858
var exercise = this;
855859
if (link.length > 0) {
856860
var url = link.attr("href");
857861

858862
if (url && url !== "#") {
859863
var data_type = "html";
860-
if (exercise.active_element) {
864+
if (exercise.active_element || fillInputs) {
861865
// Active element input values are retrieved from the API, so
862866
// we must extract the submission number from the submission url
863867
var submission = url.match(/submissions\/\d+/)[0].split('/')[1];
@@ -875,20 +879,30 @@
875879

876880
if (!exercise.active_element) {
877881
const responseElement = exercise.element.find(exercise.settings.response_selector);
878-
// Remove only the exercise content, not the alerts above it
879-
responseElement.children().not('.alert').remove();
880-
responseElement.append(
881-
$(data).filter(exercise.settings.exercise_selector).contents()
882-
);
883-
exercise.dom_element.dispatchEvent(
884-
new CustomEvent("aplus:exercise-loaded",
885-
{bubbles: true, detail: {type: exercise.exercise_type}}));
886-
// TODO: remove magic constant (variable defined in group.js)
887-
responseElement.removeClass('group-augmented');
888-
exercise.bindFormEvents(exercise.element);
889-
exercise.dom_element.dispatchEvent(
890-
new CustomEvent("aplus:exercise-ready",
891-
{bubbles: true, detail: {type: exercise.exercise_type}}));
882+
if (!fillInputs) {
883+
// Remove only the exercise content, not the alerts above it
884+
responseElement.children().not('.alert').remove();
885+
responseElement.append(
886+
$(data).filter(exercise.settings.exercise_selector).contents()
887+
);
888+
exercise.dom_element.dispatchEvent(
889+
new CustomEvent("aplus:exercise-loaded",
890+
{bubbles: true, detail: {type: exercise.exercise_type}}));
891+
// TODO: remove magic constant (variable defined in group.js)
892+
responseElement.removeClass('group-augmented');
893+
exercise.bindFormEvents(exercise.element);
894+
exercise.dom_element.dispatchEvent(
895+
new CustomEvent("aplus:exercise-ready",
896+
{bubbles: true, detail: {type: exercise.exercise_type}}));
897+
} else {
898+
// Fill textareas with last submission inputs
899+
const lastInputs = data.submission_data.map((x) => x[1]);
900+
const textareas = responseElement.find('textarea');
901+
textareas.each(function(index) {
902+
$(this).val(lastInputs[index]);
903+
});
904+
}
905+
892906
} else {
893907
// Update the output box values
894908
exercise.updateOutput(data.feedback);

0 commit comments

Comments
 (0)