Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions lib/cadet/assessments/assessments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ defmodule Cadet.Assessments do
{q, a, nil, _} -> %{q | answer: %Answer{a | grader: nil}}
{q, a, g, u} -> %{q | answer: %Answer{a | grader: %CourseRegistration{g | user: u}}}
end)
|> load_contest_voting_entries(course_reg.id)
|> load_contest_voting_entries(course_reg.course_id, course_reg.id)

assessment = assessment |> Map.put(:questions, questions)
{:ok, assessment}
Expand Down Expand Up @@ -449,6 +449,12 @@ defmodule Cadet.Assessments do

cond do
not answers_exist ->
# Delete all realted submission_votes
SubmissionVotes
|> join(:inner, [sv, q], q in assoc(sv, :question))
|> where([sv, q], q.assessment_id == ^assessment_id)
|> Repo.delete_all()

# Delete all existing questions
Question
|> where(assessment_id: ^assessment_id)
Expand Down Expand Up @@ -876,14 +882,14 @@ defmodule Cadet.Assessments do
end
end

defp load_contest_voting_entries(questions, voter_id) do
defp load_contest_voting_entries(questions, course_id, voter_id) do
Enum.map(
questions,
fn q ->
if q.type == :voting do
submission_votes = all_submission_votes_by_question_id_and_voter_id(q.id, voter_id)
# fetch top 10 contest voting entries with the contest question id
question_id = fetch_associated_contest_question_id(q)
question_id = fetch_associated_contest_question_id(course_id, q)

leaderboard_results =
if is_nil(question_id),
Expand Down Expand Up @@ -917,14 +923,14 @@ defmodule Cadet.Assessments do
end

# Finds the contest_question_id associated with the given voting_question id
defp fetch_associated_contest_question_id(voting_question) do
defp fetch_associated_contest_question_id(course_id, voting_question) do
contest_number = voting_question.question["contest_number"]

if is_nil(contest_number) do
nil
else
Assessment
|> where(number: ^contest_number)
|> where(number: ^contest_number, course_id: ^course_id)
|> join(:inner, [a], q in assoc(a, :questions))
|> order_by([a, q], q.display_order)
|> select([a, q], q.id)
Expand Down
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 @@ -495,7 +495,7 @@ defmodule CadetWeb.AssessmentsControllerTest do

# insert contest submissions and answers
contest_submissions =
for student <- Enum.take(course_regs.students, 2) do
for student <- Enum.take(course_regs.students, 5) do
insert(:submission, %{assessment: contest_assessment, student: student})
end

Expand Down
6 changes: 5 additions & 1 deletion test/support/seeds.ex
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ defmodule Cadet.Test.Seeds do
assessment =
insert(:assessment, %{course: course, config: assessment_config, is_published: true})

contest_assessment =
insert(:assessment, %{course: course, config: assessment_config, is_published: true})

programming_questions =
Enum.map(1..3, fn id ->
insert(:programming_question, %{
Expand All @@ -163,7 +166,8 @@ defmodule Cadet.Test.Seeds do
display_order: id,
assessment: assessment,
max_xp: 100,
show_solution: assessment.config.type == "path"
show_solution: assessment.config.type == "path",
question: build(:voting_question_content, contest_number: contest_assessment.number)
})
end)

Expand Down