diff --git a/lib/cadet/assessments/assessments.ex b/lib/cadet/assessments/assessments.ex index 9f0d69d35..5c9a010e1 100644 --- a/lib/cadet/assessments/assessments.ex +++ b/lib/cadet/assessments/assessments.ex @@ -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} @@ -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) @@ -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), @@ -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) diff --git a/test/cadet_web/controllers/assessments_controller_test.exs b/test/cadet_web/controllers/assessments_controller_test.exs index 91f7510b8..580bb99ad 100644 --- a/test/cadet_web/controllers/assessments_controller_test.exs +++ b/test/cadet_web/controllers/assessments_controller_test.exs @@ -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 diff --git a/test/support/seeds.ex b/test/support/seeds.ex index 506256b94..ef05b1bd5 100644 --- a/test/support/seeds.ex +++ b/test/support/seeds.ex @@ -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, %{ @@ -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)