Skip to content

Commit 654c9f4

Browse files
committed
Re-format ai_comments to reference answer_id instead
1 parent 0940dd8 commit 654c9f4

File tree

8 files changed

+100
-112
lines changed

8 files changed

+100
-112
lines changed

lib/cadet/ai_comments.ex

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,14 @@ defmodule Cadet.AIComments do
2626
end
2727
end
2828

29-
@doc """
30-
Retrieves an AI comment for a specific submission and question.
31-
Returns `nil` if no comment exists.
32-
"""
33-
def get_ai_comments_for_submission(submission_id, question_id) do
34-
Repo.one(
35-
from(c in AIComment,
36-
where: c.submission_id == ^submission_id and c.question_id == ^question_id
37-
)
38-
)
39-
end
40-
4129
@doc """
4230
Retrieves the latest AI comment for a specific submission and question.
4331
Returns `nil` if no comment exists.
4432
"""
45-
def get_latest_ai_comment(submission_id, question_id) do
33+
def get_latest_ai_comment(answer_id) do
4634
Repo.one(
4735
from(c in AIComment,
48-
where: c.submission_id == ^submission_id and c.question_id == ^question_id,
36+
where: c.answer_id == ^answer_id,
4937
order_by: [desc: c.inserted_at],
5038
limit: 1
5139
)
@@ -56,8 +44,8 @@ defmodule Cadet.AIComments do
5644
Updates the final comment for a specific submission and question.
5745
Returns the most recent comment entry for that submission/question.
5846
"""
59-
def update_final_comment(submission_id, question_id, final_comment) do
60-
comment = get_latest_ai_comment(submission_id, question_id)
47+
def update_final_comment(answer_id, final_comment) do
48+
comment = get_latest_ai_comment(answer_id)
6149

6250
case comment do
6351
nil ->
@@ -86,22 +74,4 @@ defmodule Cadet.AIComments do
8674
|> Repo.update()
8775
end
8876
end
89-
90-
@doc """
91-
Updates the chosen comments for a specific submission and question.
92-
Accepts an array of comments and replaces the existing array in the database.
93-
"""
94-
def update_chosen_comments(submission_id, question_id, new_comments) do
95-
comment = get_latest_ai_comment(submission_id, question_id)
96-
97-
case comment do
98-
nil ->
99-
{:error, :not_found}
100-
101-
_ ->
102-
comment
103-
|> AIComment.changeset(%{comment_chosen: new_comments})
104-
|> Repo.update()
105-
end
106-
end
10777
end

lib/cadet/ai_comments/ai_comment.ex

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,23 @@ defmodule Cadet.AIComments.AIComment do
1414
field(:comment_chosen, {:array, :string})
1515
field(:final_comment, :string)
1616

17-
belongs_to(:submission, Cadet.Assessments.Submission)
18-
belongs_to(:question, Cadet.Assessments.Question)
17+
belongs_to(:answer, Cadet.Assessments.Answer)
1918

2019
timestamps()
2120
end
2221

2322
def changeset(ai_comment, attrs) do
2423
ai_comment
2524
|> cast(attrs, [
26-
:submission_id,
27-
:question_id,
25+
:answer_id,
2826
:raw_prompt,
2927
:answers_json,
3028
:response,
3129
:error,
3230
:comment_chosen,
3331
:final_comment
3432
])
35-
|> validate_required([:submission_id, :question_id, :raw_prompt, :answers_json])
36-
|> foreign_key_constraint(:submission_id)
37-
|> foreign_key_constraint(:question_id)
33+
|> validate_required([:answer_id, :raw_prompt, :answers_json])
34+
|> foreign_key_constraint(:answer_id)
3835
end
3936
end

lib/cadet/assessments/answer.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ defmodule Cadet.Assessments.Answer do
1010
alias Cadet.Assessments.Answer.AutogradingStatus
1111
alias Cadet.Assessments.AnswerTypes.{MCQAnswer, ProgrammingAnswer, VotingAnswer}
1212
alias Cadet.Assessments.{Question, QuestionType, Submission}
13+
alias Cadet.AIComments.AIComment
1314

1415
@type t :: %__MODULE__{}
1516

@@ -29,7 +30,7 @@ defmodule Cadet.Assessments.Answer do
2930
belongs_to(:grader, CourseRegistration)
3031
belongs_to(:submission, Submission)
3132
belongs_to(:question, Question)
32-
has_many(:ai_comments, through: [:submission, :ai_comments])
33+
has_many(:ai_comments, AIComment, on_delete: :delete_all)
3334

3435
timestamps()
3536
end

lib/cadet/assessments/assessments.ex

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3043,10 +3043,60 @@ defmodule Cadet.Assessments do
30433043
}
30443044
end
30453045

3046+
@spec get_answer(integer() | String.t()) ::
3047+
{:ok, Answer.t()} | {:error, {:bad_request, String.t()}}
3048+
def get_answer(id) when is_ecto_id(id) do
3049+
answer =
3050+
Answer
3051+
|> where(id: ^id)
3052+
# [a] are bindings (in SQL it is similar to FROM answers "AS a"),
3053+
# this line's alias is INNER JOIN ... "AS q"
3054+
|> join(:inner, [a], q in assoc(a, :question))
3055+
|> join(:inner, [_, q], ast in assoc(q, :assessment))
3056+
|> join(:inner, [..., ast], ac in assoc(ast, :config))
3057+
|> join(:left, [a, ...], g in assoc(a, :grader))
3058+
|> join(:left, [_, ..., g], gu in assoc(g, :user))
3059+
|> join(:inner, [a, ...], s in assoc(a, :submission))
3060+
|> join(:left, [_, ..., s], st in assoc(s, :student))
3061+
|> join(:left, [..., st], u in assoc(st, :user))
3062+
|> join(:left, [..., s, _, _], t in assoc(s, :team))
3063+
|> join(:left, [..., t], tm in assoc(t, :team_members))
3064+
|> join(:left, [..., tm], tms in assoc(tm, :student))
3065+
|> join(:left, [..., tms], tmu in assoc(tms, :user))
3066+
|> join(:left, [a, ...], ai in assoc(a, :ai_comments))
3067+
|> preload([_, q, ast, ac, g, gu, s, st, u, t, tm, tms, tmu, ai],
3068+
ai_comments: ai,
3069+
question: {q, assessment: {ast, config: ac}},
3070+
grader: {g, user: gu},
3071+
submission:
3072+
{s, student: {st, user: u}, team: {t, team_members: {tm, student: {tms, user: tmu}}}}
3073+
)
3074+
|> Repo.one()
3075+
3076+
if is_nil(answer) do
3077+
{:error, {:bad_request, "Answer not found."}}
3078+
else
3079+
3080+
3081+
if answer.question.type == :voting do
3082+
empty_contest_entries = Map.put(answer.question.question, :contest_entries, [])
3083+
empty_popular_leaderboard = Map.put(empty_contest_entries, :popular_leaderboard, [])
3084+
empty_contest_leaderboard = Map.put(empty_popular_leaderboard, :contest_leaderboard, [])
3085+
question = Map.put(answer.question, :question, empty_contest_leaderboard)
3086+
Map.put(answer, :question, question)
3087+
end
3088+
3089+
{:ok, answer}
3090+
end
3091+
3092+
3093+
3094+
end
3095+
30463096
@spec get_answers_in_submission(integer() | String.t()) ::
30473097
{:ok, {[Answer.t()], Assessment.t()}}
30483098
| {:error, {:bad_request, String.t()}}
3049-
def get_answers_in_submission(id, question_id \\ nil) when is_ecto_id(id) do
3099+
def get_answers_in_submission(id) when is_ecto_id(id) do
30503100
base_query =
30513101
Answer
30523102
|> where(submission_id: ^id)
@@ -3058,29 +3108,23 @@ defmodule Cadet.Assessments do
30583108
|> join(:left, [a, ...], g in assoc(a, :grader))
30593109
|> join(:left, [_, ..., g], gu in assoc(g, :user))
30603110
|> join(:inner, [a, ...], s in assoc(a, :submission))
3061-
|> join(:left, [..., s], ai in assoc(s, :ai_comments))
3062-
|> join(:left, [_, ..., s, _], st in assoc(s, :student))
3111+
|> join(:left, [_, ..., s], st in assoc(s, :student))
30633112
|> join(:left, [..., st], u in assoc(st, :user))
3064-
|> join(:left, [..., s, _, _, _], t in assoc(s, :team))
3113+
|> join(:left, [..., s, _, _], t in assoc(s, :team))
30653114
|> join(:left, [..., t], tm in assoc(t, :team_members))
30663115
|> join(:left, [..., tm], tms in assoc(tm, :student))
30673116
|> join(:left, [..., tms], tmu in assoc(tms, :user))
3068-
|> preload([_, q, ast, ac, g, gu, s, ai, st, u, t, tm, tms, tmu],
3069-
ai_comments: ai,
3117+
|> join(:left, [a, ...], ai in assoc(a, :ai_comments))
3118+
|> preload([_, q, ast, ac, g, gu, s, st, u, t, tm, tms, tmu, ai],
30703119
question: {q, assessment: {ast, config: ac}},
30713120
grader: {g, user: gu},
30723121
submission:
3073-
{s, student: {st, user: u}, team: {t, team_members: {tm, student: {tms, user: tmu}}}}
3122+
{s, student: {st, user: u}, team: {t, team_members: {tm, student: {tms, user: tmu}}}},
3123+
ai_comments: ai
30743124
)
30753125

3076-
answer_query =
3077-
case question_id do
3078-
nil -> base_query
3079-
_ -> base_query |> where(question_id: ^question_id)
3080-
end
3081-
30823126
answers =
3083-
answer_query
3127+
base_query
30843128
|> Repo.all()
30853129
|> Enum.sort_by(& &1.question.display_order)
30863130
|> Enum.map(fn ans ->
@@ -3091,6 +3135,7 @@ defmodule Cadet.Assessments do
30913135
question = Map.put(ans.question, :question, empty_contest_leaderboard)
30923136
Map.put(ans, :question, question)
30933137
else
3138+
30943139
ans
30953140
end
30963141
end)

lib/cadet_web/admin_views/admin_grading_view.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ defmodule CadetWeb.AdminGradingView do
151151

152152
def render("grading_info.json", %{answer: answer}) do
153153
transform_map_for_view(answer, %{
154+
id: &(&1.id),
154155
ai_comments: &extract_ai_comments_per_answer(&1.question_id, &1.ai_comments),
155156
student: &extract_student_data(&1.submission.student),
156157
team: &extract_team_data(&1.submission.team),

0 commit comments

Comments
 (0)