|
413 | 413 | exercise.element.find(exercise.settings.exercise_info_selector).remove(); |
414 | 414 | } |
415 | 415 |
|
| 416 | + // Restore form inputs if we have saved data from a recent submission |
| 417 | + if (exercise.savedFormData) { |
| 418 | + exercise.fillFormInputs(exercise.savedFormData, exercise.element); |
| 419 | + delete exercise.savedFormData; |
| 420 | + } |
| 421 | + |
416 | 422 | content.show(); |
417 | 423 |
|
418 | 424 | // Active element can have height settings in the A+ exercise div that need to be |
|
656 | 662 | } |
657 | 663 | var url = $(form_element).attr("action"); |
658 | 664 | var formData = new FormData(form_element); |
| 665 | + |
| 666 | + const hasTextarea = exercise.element.find('textarea').length > 0; |
| 667 | + const hasFieldset = exercise.element.find('fieldset').length > 0; |
| 668 | + if (hasTextarea && !hasFieldset) { // Identify acceptPost exercises |
| 669 | + // Save form data to restore after update |
| 670 | + exercise.savedFormData = Array.from(formData.entries()); |
| 671 | + } |
| 672 | + |
659 | 673 | const aplusJsonString = formData.get('__aplus__'); |
660 | 674 | if (aplusJsonString) { |
661 | 675 | const aplusDict = JSON.parse(aplusJsonString); |
|
853 | 867 | } |
854 | 868 | }, |
855 | 869 |
|
| 870 | + fillFormInputs: function(submissionData, container) { |
| 871 | + submissionData.forEach(function([fieldName, fieldValue]) { |
| 872 | + const field = container.find('[name="' + fieldName + '"]'); |
| 873 | + if (field.length === 0) return; |
| 874 | + |
| 875 | + const fieldType = field.attr('type'); |
| 876 | + const tagName = field.prop('tagName').toLowerCase(); |
| 877 | + |
| 878 | + if (tagName === 'textarea') { |
| 879 | + field.val(fieldValue); |
| 880 | + } else if (tagName === 'select') { |
| 881 | + field.val(fieldValue); |
| 882 | + } else if (fieldType === 'checkbox') { |
| 883 | + // For checkboxes, check if this value matches |
| 884 | + field.filter('[value="' + fieldValue + '"]').prop('checked', true); |
| 885 | + } else if (fieldType === 'radio') { |
| 886 | + // For radio buttons, select the one with matching value |
| 887 | + field.filter('[value="' + fieldValue + '"]').prop('checked', true); |
| 888 | + } else { |
| 889 | + // For text, hidden, and other input types |
| 890 | + field.val(fieldValue); |
| 891 | + } |
| 892 | + }); |
| 893 | + }, |
| 894 | + |
856 | 895 | loadLastSubmission: function(input, fillInputs = false) { |
857 | 896 | var link = input.find(this.settings.last_submission_selector); |
858 | 897 | var exercise = this; |
|
895 | 934 | new CustomEvent("aplus:exercise-ready", |
896 | 935 | {bubbles: true, detail: {type: exercise.exercise_type}})); |
897 | 936 | } 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 | | - }); |
| 937 | + // Fill form inputs with last submission data |
| 938 | + const submissionData = data.submission_data; |
| 939 | + exercise.fillFormInputs(submissionData, exercise.element); |
904 | 940 | } |
905 | 941 |
|
906 | 942 | } else { |
|
0 commit comments