Skip to content

Commit

Permalink
[BUG FIX] [MER-3847] Sort attempts by date_evaluated (#5131)
Browse files Browse the repository at this point in the history
* sort attempts by date_evaluated

* add another list entry

* Auto format

* Update lib/oli/delivery/attempts/scoring.ex

Co-authored-by: Nicolás Cirio <nicolas.cirio@wyeworks.com>

* Auto format

* filter out nil dates

* Auto format

---------

Co-authored-by: darrensiegel <darrensiegel@users.noreply.github.com>
Co-authored-by: Nicolás Cirio <nicolas.cirio@wyeworks.com>
  • Loading branch information
3 people authored Sep 26, 2024
1 parent c807eba commit 5642b71
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
11 changes: 10 additions & 1 deletion lib/oli/delivery/attempts/scoring.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,16 @@ defmodule Oli.Delivery.Attempts.Scoring do

# The most recent is assumed to be the last item in the list
def calculate_score("most_recent", items) do
most_recent = Enum.reverse(items) |> hd
# Sort the resource_attemmpts by the date_evaluated field, so that
# the most recent evaluated attempt is the first item in the list.
#
# This makes this scoring strategy a little more robust in the face of
# attempts where somehow the most recent attempt does not match
# the natural database order - or somehow the query doesn't return
# in database order.
[most_recent | _] =
Enum.filter(items, &(&1.date_evaluated != nil))
|> Enum.sort_by(& &1.date_evaluated, &(DateTime.compare(&1, &2) != :lt))

%Result{
score: Map.get(most_recent, :score),
Expand Down
12 changes: 9 additions & 3 deletions test/oli/delivery/attempts/scoring_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@ defmodule Oli.Delivery.Attempts.ScoringTest do
end

test "most recent" do
items = [%{score: 5, out_of: 10}]
items = [%{score: 5, out_of: 10, date_evaluated: ~U[2021-01-01 00:00:00Z]}]
assert %Result{score: 5, out_of: 10} = Scoring.calculate_score("most_recent", items)

items = [%{score: 5, out_of: 10}, %{score: 1, out_of: 20}]
assert %Result{score: 1, out_of: 20} = Scoring.calculate_score("most_recent", items)
items = [
%{score: 2, out_of: 10, date_evaluated: ~U[2019-01-01 00:00:00Z]},
%{score: 5, out_of: 10, date_evaluated: ~U[2023-01-01 00:00:00Z]},
%{score: 1, out_of: 20, date_evaluated: ~U[2021-01-01 00:00:00Z]},
%{score: 9, out_of: 20, date_evaluated: nil}
]

assert %Result{score: 5, out_of: 10} = Scoring.calculate_score("most_recent", items)
end

test "best" do
Expand Down

0 comments on commit 5642b71

Please sign in to comment.