Skip to content

Commit

Permalink
Remove old query
Browse files Browse the repository at this point in the history
  • Loading branch information
RichDom2185 committed Nov 6, 2023
1 parent af5c9e1 commit 7646767
Showing 1 changed file with 7 additions and 99 deletions.
106 changes: 7 additions & 99 deletions lib/cadet/assessments/assessments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1242,118 +1242,26 @@ defmodule Cadet.Assessments do
def all_submissions_by_grader_for_index(
grader = %CourseRegistration{course_id: course_id},
group_only \\ false,
ungraded_only \\ false
_ungraded_only \\ false
) do
show_all = not group_only

group_where =
if show_all,
do: "",
else:
"where s.student_id in (select cr.id from course_registrations cr inner join groups g on cr.group_id = g.id where g.leader_id = $1) or s.student_id = $1"

group_filter =
if show_all,
do: "",
else:
"AND s.student_id IN (SELECT cr.id FROM course_registrations AS cr INNER JOIN groups AS g ON cr.group_id = g.id WHERE g.leader_id = #{grader.id}) OR s.student_id = #{grader.id}"

ungraded_where =
if ungraded_only,
do: "where s.\"gradedCount\" < assts.\"questionCount\"",
else: ""

params = if show_all, do: [], else: [grader.id]
# TODO: Restore ungraded filtering
# ... or more likely, decouple email logic from this function
# ungraded_where =
# if ungraded_only,
# do: "where s.\"gradedCount\" < assts.\"questionCount\"",
# else: ""

# We bypass Ecto here and use a raw query to generate JSON directly from
# PostgreSQL, because doing it in Elixir/Erlang is too inefficient.

# TODO: Remove old query
case Repo.query(
"""
select json_agg(q)::TEXT from
(
select
s.id,
s.status,
s."unsubmittedAt",
s.xp,
s."xpAdjustment",
s."xpBonus",
s."gradedCount",
assts.jsn as assessment,
students.jsn as student,
unsubmitters.jsn as "unsubmittedBy"
from
(select
s.id,
s.student_id,
s.assessment_id,
s.status,
s.unsubmitted_at as "unsubmittedAt",
s.unsubmitted_by_id,
sum(ans.xp) as xp,
sum(ans.xp_adjustment) as "xpAdjustment",
s.xp_bonus as "xpBonus",
count(ans.id) filter (where ans.grader_id is not null) as "gradedCount"
from submissions s
left join
answers ans on s.id = ans.submission_id
#{group_where}
group by s.id) s
inner join
(select
a.id, a."questionCount", to_json(a) as jsn
from
(select
a.id,
a.title,
a.number as "assessmentNumber",
bool_or(ac.is_manually_graded) as "isManuallyGraded",
max(ac.type) as "type",
sum(q.max_xp) as "maxXp",
count(q.id) as "questionCount"
from assessments a
left join
questions q on a.id = q.assessment_id
inner join
assessment_configs ac on ac.id = a.config_id
where a.course_id = #{course_id}
group by a.id) a) assts on assts.id = s.assessment_id
inner join
(select
cr.id, to_json(cr) as jsn
from
(select
cr.id,
u.name as "name",
u.username as "username",
g.name as "groupName",
g.leader_id as "groupLeaderId"
from course_registrations cr
left join
groups g on g.id = cr.group_id
inner join
users u on u.id = cr.user_id) cr) students on students.id = s.student_id
left join
(select
cr.id, to_json(cr) as jsn
from
(select
cr.id,
u.name
from course_registrations cr
inner join
users u on u.id = cr.user_id) cr) unsubmitters on s.unsubmitted_by_id = unsubmitters.id
#{ungraded_where}
) q
""",
params
) do
{:ok, %{rows: [[nil]]}} -> {:ok, "[]"}
{:ok, %{rows: [[json]]}} -> {:ok, json}
end

# TODO: Implement filtering
submissions =
case Repo.query("""
Expand Down

0 comments on commit 7646767

Please sign in to comment.