Skip to content

Commit

Permalink
Remove rank from contest voting (#823)
Browse files Browse the repository at this point in the history
* Remove rank from contest voting

* Rename relative_score to final_score
  • Loading branch information
lizchow authored Sep 10, 2021
1 parent c7478b5 commit 277554a
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 45 deletions.
26 changes: 10 additions & 16 deletions lib/cadet/assessments/assessments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ defmodule Cadet.Assessments do
SubmissionVotes
|> where(question_id: ^question.id)
|> where(voter_id: ^submission.student_id)
|> where([sv], is_nil(sv.rank))
|> where([sv], is_nil(sv.score))
|> Repo.exists?()

unless has_nil_entries do
Expand Down Expand Up @@ -921,7 +921,7 @@ defmodule Cadet.Assessments do
|> where([v], v.voter_id == ^voter_id and v.question_id == ^question_id)
|> join(:inner, [v], s in assoc(v, :submission))
|> join(:inner, [v, s], a in assoc(s, :answers))
|> select([v, s, a], %{submission_id: v.submission_id, answer: a.answer, rank: v.rank})
|> select([v, s, a], %{submission_id: v.submission_id, answer: a.answer, score: v.score})
|> Repo.all()
end

Expand Down Expand Up @@ -1026,17 +1026,17 @@ defmodule Cadet.Assessments do
"""
def compute_relative_score(contest_voting_question_id) do
# query all records from submission votes tied to the question id ->
# map rank to user id ->
# map score to user id ->
# store as grade ->
# query grade for contest question id.
eligible_votes =
SubmissionVotes
|> where(question_id: ^contest_voting_question_id)
|> where([sv], not is_nil(sv.rank))
|> where([sv], not is_nil(sv.score))
|> join(:inner, [sv], ans in Answer, on: sv.submission_id == ans.submission_id)
|> select(
[sv, ans],
%{ans_id: ans.id, rank: sv.rank, ans: ans.answer["code"]}
%{ans_id: ans.id, score: sv.score, ans: ans.answer["code"]}
)
|> Repo.all()

Expand All @@ -1060,15 +1060,14 @@ defmodule Cadet.Assessments do
defp map_eligible_votes_to_entry_score(eligible_votes) do
# converts eligible votes to the {total cumulative score, number of votes, tokens}
entry_vote_data =
Enum.reduce(eligible_votes, %{}, fn %{ans_id: ans_id, rank: rank, ans: ans}, tracker ->
Enum.reduce(eligible_votes, %{}, fn %{ans_id: ans_id, score: score, ans: ans}, tracker ->
{prev_score, prev_count, _ans_tokens} = Map.get(tracker, ans_id, {0, 0, 0})

Map.put(
tracker,
ans_id,
# assume each voter is assigned 10 entries which will make it fair.
{prev_score + convert_vote_rank_to_score(rank, 10), prev_count + 1,
Lexer.count_tokens(ans)}
{prev_score + score, prev_count + 1, Lexer.count_tokens(ans)}
)
end)

Expand All @@ -1081,11 +1080,6 @@ defmodule Cadet.Assessments do
)
end

# implementation detail assuming to calculate scores out of 10 for rank [1, num_voted_entries]
defp convert_vote_rank_to_score(rank, num_voted_entries) do
11 - 10 * rank / num_voted_entries
end

# Calculate the score based on formula
# score(v,t) = v - 2^(t/50) where v is the normalized_voting_score
# normalized_voting_score = sum_of_scores / number_of_voters / 10 * 100
Expand Down Expand Up @@ -1549,13 +1543,13 @@ defmodule Cadet.Assessments do
end

def insert_or_update_voting_answer(submission_id, course_reg_id, question_id, answer_content) do
set_rank_to_nil =
set_score_to_nil =
SubmissionVotes
|> where(voter_id: ^course_reg_id, question_id: ^question_id)

voting_multi =
Multi.new()
|> Multi.update_all(:set_rank_to_nil, set_rank_to_nil, set: [rank: nil])
|> Multi.update_all(:set_score_to_nil, set_score_to_nil, set: [score: nil])

answer_content
|> Enum.with_index(1)
Expand All @@ -1567,7 +1561,7 @@ defmodule Cadet.Assessments do
voter_id: course_reg_id,
submission_id: entry.submission_id
)
|> SubmissionVotes.changeset(%{rank: entry.rank})
|> SubmissionVotes.changeset(%{score: entry.score})
|> Repo.insert_or_update()
end)
end)
Expand Down
4 changes: 2 additions & 2 deletions lib/cadet/assessments/submission_votes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Cadet.Assessments.SubmissionVotes do
alias Cadet.Assessments.{Question, Submission}

schema "submission_votes" do
field(:rank, :integer)
field(:score, :integer)

belongs_to(:voter, CourseRegistration)
belongs_to(:submission, Submission)
Expand All @@ -15,7 +15,7 @@ defmodule Cadet.Assessments.SubmissionVotes do
end

@required_fields ~w(voter_id submission_id question_id)a
@optional_fields ~w(rank)a
@optional_fields ~w(score)a

def changeset(submission_vote, params) do
submission_vote
Expand Down
2 changes: 1 addition & 1 deletion lib/cadet/jobs/autograder/grading_job.ex
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ defmodule Cadet.Autograder.GradingJob do
|> join(:inner, [s], sv in SubmissionVotes,
on: sv.voter_id == s.student_id and sv.question_id == ^question.id
)
|> where([_, sv], is_nil(sv.rank))
|> where([_, sv], is_nil(sv.score))
|> Repo.exists?()

xp = if is_nil_entries, do: 0, else: question.max_xp
Expand Down
4 changes: 2 additions & 2 deletions lib/cadet_web/views/assessments_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ defmodule CadetWeb.AssessmentsHelpers do
transform_map_for_view(entry, %{
submission_id: :submission_id,
answer: :answer,
rank: :rank
score: :score
})
end

Expand All @@ -140,7 +140,7 @@ defmodule CadetWeb.AssessmentsHelpers do
answer: :answer,
student_name: :student_name
}),
"score",
"final_score",
Float.round(leaderboard_ans.relative_score, 2)
)
end
Expand Down
7 changes: 7 additions & 0 deletions priv/repo/migrations/20210908024720_rename_rank_to_score.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
defmodule Cadet.Repo.Migrations.RenameRankToScore do
use Ecto.Migration

def change do
rename(table(:submission_votes), :rank, to: :score)
end
end
8 changes: 4 additions & 4 deletions test/cadet/assessments/assessments_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ defmodule Cadet.AssessmentsTest do
fn {submission, index} ->
insert(
:submission_vote,
rank: index + 1,
score: 10 - index,
voter: student,
submission: submission,
question: voting_question
Expand Down Expand Up @@ -466,7 +466,7 @@ defmodule Cadet.AssessmentsTest do
fn {submission, index} ->
insert(
:submission_vote,
rank: index + 1,
score: 10 - index,
voter: student,
submission: submission,
question: current_question
Expand All @@ -485,7 +485,7 @@ defmodule Cadet.AssessmentsTest do
fn {submission, index} ->
insert(
:submission_vote,
rank: index + 1,
score: 10 - index,
voter: student,
submission: submission,
question: yesterday_question
Expand All @@ -504,7 +504,7 @@ defmodule Cadet.AssessmentsTest do
fn {submission, index} ->
insert(
:submission_vote,
rank: index + 1,
score: 10 - index,
voter: student,
submission: submission,
question: past_question
Expand Down
2 changes: 1 addition & 1 deletion test/cadet/assessments/submission_votes_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ defmodule Cadet.Assessments.SubmissionVotesTest do
test "invalid changeset unique constraint", %{
valid_params: params
} do
params = Map.put(params, :rank, 2)
params = Map.put(params, :score, 2)
first_entry = SubmissionVotes.changeset(%SubmissionVotes{}, params)
{:ok, _} = Repo.insert(first_entry)
new_submission = insert(:submission)
Expand Down
4 changes: 2 additions & 2 deletions test/cadet/jobs/autograder/grading_job_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ defmodule Cadet.Autograder.GradingJobTest do
answers =
for question <- questions do
case Enum.random(0..1) do
0 -> insert(:submission_vote, %{voter: student, question: question, rank: 1})
0 -> insert(:submission_vote, %{voter: student, question: question, score: 1})
1 -> insert(:submission_vote, %{voter: student, question: question})
end

Expand Down Expand Up @@ -614,7 +614,7 @@ defmodule Cadet.Autograder.GradingJobTest do
SubmissionVotes
|> where(voter_id: ^student.id)
|> where(question_id: ^question.id)
|> where([sv], is_nil(sv.rank))
|> where([sv], is_nil(sv.score))
|> Repo.exists?()

answer_db = Repo.get(Answer, answer.id)
Expand Down
32 changes: 16 additions & 16 deletions test/cadet_web/controllers/answer_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ defmodule CadetWeb.AnswerControllerTest do
voting_conn =
post(conn, build_url(course_id, voting_question.id), %{
answer: [
%{"answer" => "hello world", "submission_id" => contest_submission.id, "rank" => 3}
%{"answer" => "hello world", "submission_id" => contest_submission.id, "score" => 3}
]
})

assert response(voting_conn, 200) =~ "OK"
rank = get_rank_for_submission_vote(voting_question, course_reg, contest_submission)
assert rank == 3
score = get_score_for_submission_vote(voting_question, course_reg, contest_submission)
assert score == 3
end

@tag authenticate: role
Expand Down Expand Up @@ -120,7 +120,7 @@ defmodule CadetWeb.AnswerControllerTest do
voting_conn =
post(conn, build_url(course_id, voting_question.id), %{
answer: [
%{"answer" => "hello world", "submission_id" => contest_submission.id, "rank" => 3}
%{"answer" => "hello world", "submission_id" => contest_submission.id, "score" => 3}
]
})

Expand All @@ -129,14 +129,14 @@ defmodule CadetWeb.AnswerControllerTest do
updated_voting_conn =
post(conn, build_url(course_id, voting_question.id), %{
answer: [
%{"answer" => "hello world", "submission_id" => contest_submission.id, "rank" => 5}
%{"answer" => "hello world", "submission_id" => contest_submission.id, "score" => 5}
]
})

assert response(updated_voting_conn, 200) =~ "OK"

rank = get_rank_for_submission_vote(voting_question, course_reg, contest_submission)
assert rank == 5
score = get_score_for_submission_vote(voting_question, course_reg, contest_submission)
assert score == 5
end

@tag authenticate: role
Expand All @@ -161,7 +161,7 @@ defmodule CadetWeb.AnswerControllerTest do

post(conn, build_url(course_id, voting_question.id), %{
answer: [
%{"answer" => "hello world", "submission_id" => contest_submission.id, "rank" => 3}
%{"answer" => "hello world", "submission_id" => contest_submission.id, "score" => 3}
]
})

Expand Down Expand Up @@ -253,7 +253,7 @@ defmodule CadetWeb.AnswerControllerTest do
%{
"answer" => "hello world",
"submission_id" => contest_submission.id,
"rank" => "a"
"score" => "a"
}
]
})
Expand All @@ -263,7 +263,7 @@ defmodule CadetWeb.AnswerControllerTest do
end

@tag authenticate: role
test "update duplicate rank in submission_votes is unsuccessful", %{
test "update duplicate score in submission_votes is unsuccessful", %{
conn: conn,
voting_question: voting_question
} do
Expand All @@ -277,14 +277,14 @@ defmodule CadetWeb.AnswerControllerTest do
voter_id: course_reg.id,
question_id: voting_question.id,
submission_id: first_contest_submission.id,
rank: 1
score: 1
})

Repo.insert(%SubmissionVotes{
voter_id: course_reg.id,
question_id: voting_question.id,
submission_id: second_contest_submission.id,
rank: 2
score: 2
})

voting_conn =
Expand All @@ -293,12 +293,12 @@ defmodule CadetWeb.AnswerControllerTest do
%{
"answer" => "hello world",
"submission_id" => first_contest_submission.id,
"rank" => 3
"score" => 3
},
%{
"answer" => "hello world",
"submission_id" => second_contest_submission.id,
"rank" => 3
"score" => 3
}
]
})
Expand Down Expand Up @@ -393,12 +393,12 @@ defmodule CadetWeb.AnswerControllerTest do
end
end

defp get_rank_for_submission_vote(question, course_reg, submission) do
defp get_score_for_submission_vote(question, course_reg, submission) do
SubmissionVotes
|> where(question_id: ^question.id)
|> where(voter_id: ^course_reg.id)
|> where(submission_id: ^submission.id)
|> select([sv], sv.rank)
|> select([sv], sv.score)
|> Repo.one()
end
end
2 changes: 1 addition & 1 deletion test/cadet_web/controllers/assessments_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ defmodule CadetWeb.AssessmentsControllerTest do
%{
"submission_id" => answer.submission.id,
"answer" => %{"code" => answer.answer.code},
"rank" => nil
"score" => nil
}
end)
end)
Expand Down

0 comments on commit 277554a

Please sign in to comment.