From 81262ce2b16951bb5caa128a48e43e2bbfa7d24d Mon Sep 17 00:00:00 2001 From: YaleChen299 Date: Fri, 3 Sep 2021 10:45:46 +0800 Subject: [PATCH 1/5] fix insert submission_votes --- lib/cadet/assessments/assessments.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cadet/assessments/assessments.ex b/lib/cadet/assessments/assessments.ex index 5c9a010e1..029877a4a 100644 --- a/lib/cadet/assessments/assessments.ex +++ b/lib/cadet/assessments/assessments.ex @@ -524,7 +524,7 @@ defmodule Cadet.Assessments do user_ids = CourseRegistration - |> where(role: "student") + |> where(role: "student", course_id: ^course_id) |> select([cr], cr.id) |> Repo.all() From 93d182c0f101e341e1e1867738a1701651b36c9d Mon Sep 17 00:00:00 2001 From: YaleChen299 Date: Fri, 3 Sep 2021 17:04:58 +0800 Subject: [PATCH 2/5] filter valid contest entries by student --- lib/cadet/assessments/assessments.ex | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/cadet/assessments/assessments.ex b/lib/cadet/assessments/assessments.ex index 029877a4a..5493774a1 100644 --- a/lib/cadet/assessments/assessments.ex +++ b/lib/cadet/assessments/assessments.ex @@ -507,9 +507,11 @@ defmodule Cadet.Assessments do contest_submission_ids = Submission |> join(:inner, [s], ans in assoc(s, :answers)) + |> join(:inner, [s, ans], cr in assoc(s, :student)) + |> where([s, ans, cr], cr.role == "student") |> where([s, _], s.assessment_id == ^contest_assessment.id and s.status == "submitted") |> where( - [_, ans], + [_, ans, cr], fragment( "?->>'code' like ?", ans.answer, @@ -522,7 +524,7 @@ defmodule Cadet.Assessments do contest_submission_ids_length = Enum.count(contest_submission_ids) - user_ids = + voter_ids = CourseRegistration |> where(role: "student", course_id: ^course_id) |> select([cr], cr.id) @@ -534,7 +536,7 @@ defmodule Cadet.Assessments do if Enum.empty?(contest_submission_ids) do 0 else - trunc(Float.ceil(votes_per_user * length(user_ids) / contest_submission_ids_length)) + trunc(Float.ceil(votes_per_user * length(voter_ids) / contest_submission_ids_length)) end submission_id_list = @@ -545,11 +547,11 @@ defmodule Cadet.Assessments do |> List.flatten() {_submission_map, submission_votes_changesets} = - user_ids - |> Enum.reduce({submission_id_list, []}, fn user_id, acc -> + voter_ids + |> Enum.reduce({submission_id_list, []}, fn voter_id, acc -> {submission_list, submission_votes} = acc - user_contest_submission_id = Map.get(contest_submission_ids, user_id) + user_contest_submission_id = Map.get(contest_submission_ids, voter_id) {votes, rest} = submission_list @@ -583,7 +585,7 @@ defmodule Cadet.Assessments do new_submission_votes = votes |> Enum.map(fn s_id -> - %SubmissionVotes{voter_id: user_id, submission_id: s_id, question_id: question_id} + %SubmissionVotes{voter_id: voter_id, submission_id: s_id, question_id: question_id} end) |> Enum.concat(submission_votes) From b71e68cae68d963eba25990323a58168b46996b5 Mon Sep 17 00:00:00 2001 From: YaleChen299 Date: Fri, 3 Sep 2021 19:05:43 +0800 Subject: [PATCH 3/5] filter by student in leaderboard --- lib/cadet/assessments/assessments.ex | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/cadet/assessments/assessments.ex b/lib/cadet/assessments/assessments.ex index 5493774a1..4ec04a49c 100644 --- a/lib/cadet/assessments/assessments.ex +++ b/lib/cadet/assessments/assessments.ex @@ -960,6 +960,7 @@ defmodule Cadet.Assessments do |> join(:left, [a], s in assoc(a, :submission)) |> join(:left, [a, s], student in assoc(s, :student)) |> join(:inner, [a, s, student], student_user in assoc(student, :user)) + |> where([a, s, student], student.role == "student") |> select([a, s, student, student_user], %{ submission_id: a.submission_id, answer: a.answer, From f19dfab8090580e6d34e9d26fd9de0235817a5da Mon Sep 17 00:00:00 2001 From: YaleChen299 Date: Sun, 5 Sep 2021 01:09:28 +0800 Subject: [PATCH 4/5] temporary fix to hide leaderboarder --- lib/cadet/assessments/assessments.ex | 9 ++++---- .../assessments_controller_test.exs | 21 ++++++++++--------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/lib/cadet/assessments/assessments.ex b/lib/cadet/assessments/assessments.ex index 4ec04a49c..73af48a51 100644 --- a/lib/cadet/assessments/assessments.ex +++ b/lib/cadet/assessments/assessments.ex @@ -893,10 +893,11 @@ defmodule Cadet.Assessments do # fetch top 10 contest voting entries with the contest question id question_id = fetch_associated_contest_question_id(course_id, q) - leaderboard_results = - if is_nil(question_id), - do: [], - else: fetch_top_relative_score_answers(question_id, 10) + leaderboard_results = [] + # temporary fix to hide the leaderboard + # if is_nil(question_id), + # do: [], + # else: fetch_top_relative_score_answers(question_id, 10) # populate entries to vote for and leaderboard data into the question voting_question = diff --git a/test/cadet_web/controllers/assessments_controller_test.exs b/test/cadet_web/controllers/assessments_controller_test.exs index 580bb99ad..6326bc855 100644 --- a/test/cadet_web/controllers/assessments_controller_test.exs +++ b/test/cadet_web/controllers/assessments_controller_test.exs @@ -510,16 +510,17 @@ defmodule CadetWeb.AssessmentsControllerTest do }) end - expected_leaderboard = - for answer <- contest_answers do - %{ - "answer" => %{"code" => answer.answer.code}, - "score" => answer.relative_score, - "student_name" => answer.submission.student.user.name, - "submission_id" => answer.submission.id - } - end - |> Enum.sort_by(& &1["score"], &>=/2) + expected_leaderboard = [] + # temporary fix to hide the leaderboard + # for answer <- contest_answers do + # %{ + # "answer" => %{"code" => answer.answer.code}, + # "score" => answer.relative_score, + # "student_name" => answer.submission.student.user.name, + # "submission_id" => answer.submission.id + # } + # end + # |> Enum.sort_by(& &1["score"], &>=/2) for role <- Role.__enum_map__() do course_reg = Map.get(role_crs, role) From de0e9e0b5e31a69b2f9c925db5be338d89036bd0 Mon Sep 17 00:00:00 2001 From: YaleChen299 Date: Sun, 5 Sep 2021 01:14:31 +0800 Subject: [PATCH 5/5] fix format --- lib/cadet/assessments/assessments.ex | 8 ++++---- .../assessments_controller_test.exs | 20 +++++++++---------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/cadet/assessments/assessments.ex b/lib/cadet/assessments/assessments.ex index 73af48a51..8f8bdd9cc 100644 --- a/lib/cadet/assessments/assessments.ex +++ b/lib/cadet/assessments/assessments.ex @@ -894,10 +894,10 @@ defmodule Cadet.Assessments do question_id = fetch_associated_contest_question_id(course_id, q) leaderboard_results = [] - # temporary fix to hide the leaderboard - # if is_nil(question_id), - # do: [], - # else: fetch_top_relative_score_answers(question_id, 10) + # temporary fix to hide the leaderboard + # if is_nil(question_id), + # do: [], + # else: fetch_top_relative_score_answers(question_id, 10) # populate entries to vote for and leaderboard data into the question voting_question = diff --git a/test/cadet_web/controllers/assessments_controller_test.exs b/test/cadet_web/controllers/assessments_controller_test.exs index 6326bc855..c19f154f3 100644 --- a/test/cadet_web/controllers/assessments_controller_test.exs +++ b/test/cadet_web/controllers/assessments_controller_test.exs @@ -511,16 +511,16 @@ defmodule CadetWeb.AssessmentsControllerTest do end expected_leaderboard = [] - # temporary fix to hide the leaderboard - # for answer <- contest_answers do - # %{ - # "answer" => %{"code" => answer.answer.code}, - # "score" => answer.relative_score, - # "student_name" => answer.submission.student.user.name, - # "submission_id" => answer.submission.id - # } - # end - # |> Enum.sort_by(& &1["score"], &>=/2) + # temporary fix to hide the leaderboard + # for answer <- contest_answers do + # %{ + # "answer" => %{"code" => answer.answer.code}, + # "score" => answer.relative_score, + # "student_name" => answer.submission.student.user.name, + # "submission_id" => answer.submission.id + # } + # end + # |> Enum.sort_by(& &1["score"], &>=/2) for role <- Role.__enum_map__() do course_reg = Map.get(role_crs, role)