diff --git a/controllers/admin.py b/controllers/admin.py index 30518ee67..845237e18 100644 --- a/controllers/admin.py +++ b/controllers/admin.py @@ -2113,6 +2113,10 @@ def add__or_update_assignment_question(): question_type ], status="success", + question_id=question_name, + points=points, + autograde=autograde, + which_to_grade=which_to_grade, ) ) except Exception as ex: diff --git a/static/js/admin.js b/static/js/admin.js index 331b1124d..f886f2972 100644 --- a/static/js/admin.js +++ b/static/js/admin.js @@ -995,10 +995,11 @@ function configure_tree_picker( } } if (!data.instance.ignore_check) { - walk_jstree(data.instance, data.node, function (instance, node) { + walk_jstree(data.instance, data.node, async function (instance, node) { if (jstree_node_depth(instance, node) == leaf_depth) { // Add each checked item to the assignment list with default values. - checked_func(node); // checked_func is either updateReading or updateAssignmentRaw + let resp = await checked_func(node); // checked_func is either updateReading or updateAssignmentRaw + add_to_table(resp); } }); } @@ -1023,12 +1024,11 @@ function jstree_node_depth(instance, node) { } // Given a jstree node, invoke f on node and all its children. -function walk_jstree(instance, node, f) { - f(instance, node); - $(node.children).each(function (index, value) { - console.log(index, value) - walk_jstree(instance, instance.get_node(value), f); - }); +async function walk_jstree(instance, node, f) { + await f(instance, node); + for (let value of node.children) { + await walk_jstree(instance, instance.get_node(value), f); + } } // Given an editable element (a hyperlink) in a bootstrap table, return the containing row. @@ -1304,30 +1304,39 @@ function assignmentInfo() { // Update a reading. // This should be serialized is the walk_jstree function to make sure the order is correct -function updateReading(subchapter_id, activities_required, points, autograde, which_to_grade) { +async function updateReading(subchapter_id, activities_required, points, autograde, which_to_grade) { let assignid = getAssignmentId(); if (!assignid || assignid == 'undefined') { alert("No assignment selected"); return; } - $.getJSON('add__or_update_assignment_question', { + let res = await $.ajax({url: 'add__or_update_assignment_question', + data: { assignment: assignid, question: subchapter_id, activities_required: activities_required, points: points, autograde: autograde, which_to_grade: which_to_grade, - }).done(function (response_JSON) { - $('#totalPoints').html('Total points: ' + response_JSON['total']); - // See if this question already exists in the table. Only append if it doesn't exist. - if (readings_table.bootstrapTable('getRowByUniqueId', subchapter_id) === null) { - appendToReadingsTable(subchapter_id, response_JSON['activity_count'], response_JSON['activities_required'], points, autograde, - response_JSON['autograde_possible_values'], which_to_grade, - response_JSON['which_to_grade_possible_values']); - } - }).fail(function () { - alert(`Your added question ${subchapter_id} was not saved to the database for assignment ${assignid}, please file a bug report describing exactly what you were doing.`) - }); + }, + dataType: 'json'}); + + return res; +} + +function add_to_table (response_JSON) { + $('#totalPoints').html('Total points: ' + response_JSON['total']); + // See if this question already exists in the table. Only append if it doesn't exist. + if (readings_table.bootstrapTable('getRowByUniqueId', response_JSON['question_id']) === null) { + appendToReadingsTable(response_JSON['question_id'], + response_JSON['activity_count'], + response_JSON['activities_required'], + response_JSON['points'], + response_JSON['autograde'], + response_JSON['autograde_possible_values'], + response_JSON['which_to_grade'], + response_JSON['which_to_grade_possible_values']); + } } diff --git a/views/admin/assignments.html b/views/admin/assignments.html index 4c9e4f71d..ba70459c6 100644 --- a/views/admin/assignments.html +++ b/views/admin/assignments.html @@ -397,7 +397,7 @@ configure_tree_picker(readings_picker, eBookConfig.toc.reading_picker, $('#search-tree-readings-picker'), 2, // Called when a node is checked. - function (node) { updateReading(node.id, -1, 1, 'interact', 'best_answer'); }, + async function (node) { let res = await updateReading(node.id, -1, 1, 'interact', 'best_answer'); return res }, // Called when a node is unchecked. function (node) { remove_reading(node.id); } );