Skip to content
15 changes: 6 additions & 9 deletions lib/cadet/assessments/assessments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,7 @@ defmodule Cadet.Assessments do

# Begin autograding job
GradingJob.force_grade_individual_submission(updated_submission)
update_xp_bonus(submission)
update_xp_bonus(updated_submission)

{:ok, nil}
else
Expand Down Expand Up @@ -1294,6 +1294,8 @@ defmodule Cadet.Assessments do
|> Submission.changeset(%{is_grading_published: true})
|> Repo.update()

update_xp_bonus(submission)

Notifications.write_notification_when_published(
submission.id,
:published_grading
Expand Down Expand Up @@ -1493,23 +1495,18 @@ defmodule Cadet.Assessments do
Answer
|> where(submission_id: ^submission_id)
|> order_by(:question_id)
|> group_by([a], a.id)
|> select([a], %{
# grouping by submission, so s.xp_bonus will be the same, but we need an
# aggregate function
total_xp: sum(a.xp) + sum(a.xp_adjustment)
total_xp: a.xp + a.xp_adjustment
})

total =
ans_xp
|> subquery
|> select([a], %{
total_xp: sum(a.total_xp)
total_xp: coalesce(sum(a.total_xp), 0)
})
|> Repo.one()

xp = decimal_to_integer(total.total_xp)

cur_time =
if submission.submitted_at == nil do
Timex.now()
Expand All @@ -1518,7 +1515,7 @@ defmodule Cadet.Assessments do
end

xp_bonus =
if xp <= 0 do
if total.total_xp <= 0 do
0
else
if Timex.before?(cur_time, Timex.shift(assessment.open_at, hours: early_hours)) do
Expand Down
Loading