Skip to content

Commit a8f07e8

Browse files
dwang3851KesterTan
andauthored
Changing the manage submissions button to be ajax requests (#2271)
* fixing request bodies * fixed disable buttons * Fixing bugs --------- Co-authored-by: Kester <kestertan040@gmail.com>
1 parent 8b46f67 commit a8f07e8

File tree

5 files changed

+112
-39
lines changed

5 files changed

+112
-39
lines changed

app/assets/javascripts/manage_submissions.js

Lines changed: 70 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
const manage_submissions_endpoints = {
22
'regrade-selected': 'regradeBatch',
3+
'delete-selected': 'submissions/destroy_batch',
4+
'download-selected': 'submissions/download_batch',
5+
'excuse-selected': 'submissions/excuse_batch',
36
'score_details': 'submissions/score_details',
47
};
58

@@ -327,20 +330,73 @@ $(document).ready(function() {
327330
}
328331

329332
function changeButtonStates(state) {
330-
state ? buttonIDs.forEach((id) => $(id).addClass('disabled')) : buttonIDs.forEach((id) => $(id).removeClass('disabled'));
331-
332-
// prop each selected button with selected submissions
333-
if (!state) {
334-
var urlParam = $.param({'submission_ids': selectedSubmissions});
335-
buttonIDs.forEach(function(id) {
336-
var newHref = baseURLs[id] + '?' + urlParam;
337-
$(id).prop('href', newHref);
338-
});
339-
} else {
340-
buttonIDs.forEach(function(id) {
341-
$(id).prop('href', baseURLs[id]);
342-
});
343-
}
333+
buttonIDs.forEach((id) => {
334+
const button = $(id);
335+
if (state) {
336+
if (id === "#download-selected") {
337+
$(id).prop('href', baseURLs[id]);
338+
}
339+
button.addClass("disabled");
340+
button.off("click").prop("disabled", true);
341+
} else {
342+
button.removeClass("disabled").prop("disabled", false);
343+
if (id == "#download-selected") {
344+
var urlParam = $.param({'submission_ids': selectedSubmissions});
345+
buttonIDs.forEach(function(id) {
346+
var newHref = baseURLs[id] + '?' + urlParam;
347+
$(id).prop('href', newHref);
348+
});
349+
return;
350+
}
351+
$(document).off("click", id).on("click", id, function (event) {
352+
console.log(`${id} button clicked`);
353+
event.preventDefault();
354+
if (selectedSubmissions.length === 0) {
355+
alert("No submissions selected.");
356+
return;
357+
}
358+
const endpoint = manage_submissions_endpoints[id.replace("#", "")];
359+
const requestData = { submission_ids: selectedSubmissions };
360+
if (id === "#delete-selected") {
361+
if (!confirm("Deleting will delete all checked submissions and cannot be undone. Are you sure you want to delete these submissions?")) {
362+
return;
363+
}
364+
}
365+
let refreshInterval = setInterval(() => {
366+
location.reload();
367+
}, 5000);
368+
$.ajax({
369+
url: endpoint,
370+
type: "POST",
371+
contentType: "application/json",
372+
data: JSON.stringify(requestData),
373+
dataType: "json",
374+
headers: {
375+
"X-CSRF-Token": $('meta[name="csrf-token"]').attr("content"),
376+
},
377+
success: function (response) {
378+
clearInterval(refreshInterval);
379+
if (response.redirect) {
380+
window.location.href = response.redirect;
381+
return;
382+
}
383+
if (response.error) {
384+
alert(response.error);
385+
}
386+
if (response.success) {
387+
alert(response.success);
388+
}
389+
selectedSubmissions = [];
390+
changeButtonStates(true);
391+
},
392+
error: function (error) {
393+
clearInterval(refreshInterval);
394+
alert("An error occurred while processing the request.");
395+
},
396+
});
397+
});
398+
}
399+
});
344400
}
345401

346402
changeButtonStates(true); // disable all buttons by default

app/controllers/assessment/autograde.rb

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,19 @@ def regrade
8787
#
8888
# action_auth_level :regradeBatch, :instructor
8989
def regradeBatch
90-
submission_ids = params[:submission_ids]
90+
request_body = request.body.read
91+
submission_ids = begin if request_body.present?
92+
parsed_data = JSON.parse(request_body)
93+
Array(parsed_data['submission_ids'])
94+
else
95+
params[:submission_ids]
96+
end
97+
rescue JSON::ParserError => e
98+
params[:submission_ids] || []
99+
end
100+
101+
# Ensure submission_ids is an array
102+
submission_ids = Array(submission_ids)
91103

92104
# Now regrade only the most recent submissions. Keep track of
93105
# any handins that fail.
@@ -129,7 +141,10 @@ def regradeBatch
129141
# For both :success and :error
130142
flash[:html_safe] = true
131143

132-
redirect_to([@course, @assessment, :submissions]) && return
144+
respond_to do |format|
145+
format.html { redirect_to [@course, @assessment, :submissions] }
146+
format.json { render json: { redirect: url_for([@course, @assessment, :submissions]) } }
147+
end
133148
end
134149

135150
#

app/controllers/submissions_controller.rb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,9 @@ def destroy
200200
# remove a given submission for a student
201201
action_auth_level :destroy_batch, :instructor
202202
def destroy_batch
203-
submission_ids = params[:submission_ids]
203+
request_body = request.body.read
204+
submission_ids = JSON.parse(request_body)['submission_ids']
205+
submission_ids = Array(submission_ids)
204206
submissions = Submission.where(id: submission_ids)
205207
scount = 0
206208
fcount = 0
@@ -241,8 +243,10 @@ def destroy_batch
241243
fcount, 'submission'
242244
)} failed."
243245
end
244-
redirect_to(course_assessment_submissions_path(submissions[0].course_user_datum.course,
245-
submissions[0].assessment)) && return
246+
respond_to do |format|
247+
format.html { redirect_to [@course, @assessment, :submissions] }
248+
format.json { render json: { redirect: url_for([@course, @assessment, :submissions]) } }
249+
end
246250
end
247251

248252
# this is good
@@ -384,7 +388,9 @@ def tweak_total
384388

385389
action_auth_level :excuse_batch, :course_assistant
386390
def excuse_batch
387-
submission_ids = params[:submission_ids]
391+
request_body = request.body.read
392+
submission_ids = JSON.parse(request_body)['submission_ids']
393+
submission_ids = Array(submission_ids)
388394
flash[:error] = "Cannot index submissions for nil assessment" if @assessment.nil?
389395

390396
unless @assessment.valid?
@@ -434,7 +440,10 @@ def excuse_batch
434440

435441
flash[:success] =
436442
"#{ActionController::Base.helpers.pluralize(auds_to_excuse.size, 'student')} excused."
437-
redirect_to course_assessment_submissions_path(@course, @assessment)
443+
respond_to do |format|
444+
format.html { redirect_to [@course, @assessment, :submissions] }
445+
format.json { render json: { redirect: url_for([@course, @assessment, :submissions]) } }
446+
end
438447
end
439448

440449
action_auth_level :unexcuse, :course_assistant

app/views/submissions/index.html.erb

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -97,36 +97,29 @@
9797
<%# Selected buttons, hidden so HTML can be accessed in DataTables %>
9898
<div class="selected-buttons-placeholder">
9999
<div id="regrade-batch-html">
100-
<%= link_to "<i class='material-icons'>cached</i>Regrade Selected".html_safe,
101-
regradeBatch_course_assessment_path(@course, @assessment),
102-
{ method: :post,
103-
title: "Regrade selected submissions",
104-
class: "btn submissions-selected" } %>
100+
<%= link_to "Regrade Selected", class: "btn submissions-selected", title: "Regrade selected submissions" do %>
101+
<i class="material-icons">cached</i> Regrade Selected
102+
<% end %>
105103
</div>
106104

107105
<div id="delete-batch-html">
108-
<%= link_to "<i class='material-icons'>delete_outline</i>Delete Selected".html_safe,
109-
destroy_batch_course_assessment_submissions_path(@course, @assessment, @submissions),
110-
{ method: :post,
111-
title: "Delete selected submissions",
112-
class: "btn submissions-selected",
113-
data: { confirm: "Deleting will delete all checked submissions and cannot be undone. Are you sure you want to delete these submissions?" } } %>
106+
<a class="btn submissions-selected" title="Delete selected submissions">
107+
<i class="material-icons">delete_outline</i>Delete Selected
108+
</a>
114109
</div>
115110

116111
<div id="download-batch-html">
117112
<%= link_to "<i class='material-icons'>download</i>Download Selected".html_safe,
118113
download_batch_course_assessment_submissions_path(@course, @assessment, @submissions),
119-
{ method: :get,
114+
{ method: :post,
120115
title: "Download selected submissions",
121116
class: "btn submissions-selected" } %>
122117
</div>
123118

124119
<div id="excuse-batch-html">
125-
<%= link_to "<i class='material-icons'>done</i>Excuse Selected".html_safe,
126-
excuse_batch_course_assessment_submissions_path(@course, @assessment, @submissions),
127-
{ method: :post,
128-
title: "Excuse selected submissions",
129-
class: "btn submissions-selected" } %>
120+
<a class="btn submissions-selected" title="Excuse selected submissions">
121+
<i class="material-icons">done</i>Excuse Selected
122+
</a>
130123
</div>
131124
</div>
132125

config/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@
178178
collection do
179179
get "download_all"
180180
post "destroy_batch"
181-
get "download_batch"
181+
post "download_batch"
182182
get "popover"
183183
post "excuse_batch"
184184
post "unexcuse"

0 commit comments

Comments
 (0)