-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathassessments_helpers.ex
More file actions
217 lines (195 loc) · 6.1 KB
/
Copy pathassessments_helpers.ex
File metadata and controls
217 lines (195 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
defmodule CadetWeb.AssessmentsHelpers do
@moduledoc """
Helper functions for Assessments and Grading
"""
import CadetWeb.ViewHelper
defp build_library(%{library: library}) do
transform_map_for_view(library, %{
chapter: :chapter,
variant: :variant,
execTimeMs: :exec_time_ms,
globals: :globals,
external: &build_external_library(%{external_library: &1.external})
})
end
defp build_external_library(%{external_library: external_library}) do
transform_map_for_view(external_library, [:name, :symbols])
end
def build_question_by_question_config(
%{question: question},
all_testcases? \\ false
) do
Map.merge(
build_generic_question_fields(%{question: question}),
build_question_content_by_config(
%{question: question},
all_testcases?
)
)
end
def build_question_with_answer_and_solution_if_ungraded(%{question: question}) do
components = [
build_question_by_question_config(%{
question: question
}),
build_answer_fields_by_question_type(%{question: question}),
build_solution_if_ungraded_by_config(%{question: question})
]
components
|> Enum.filter(& &1)
|> Enum.reduce(%{}, &Map.merge/2)
end
defp build_generic_question_fields(%{question: question}) do
transform_map_for_view(question, %{
id: :id,
type: :type,
library: &build_library(%{library: &1.library}),
maxXp: :max_xp,
blocking: :blocking
})
end
defp build_solution_if_ungraded_by_config(%{
question: %{question: question, type: question_type, show_solution: show_solution}
}) do
if show_solution do
solution_getter =
case question_type do
:programming -> &Map.get(&1, "solution")
:mcq -> &find_correct_choice(&1["choices"])
:voting -> nil
end
transform_map_for_view(question, %{solution: solution_getter})
end
end
defp answer_builder_for(:programming), do: & &1.answer["code"]
defp answer_builder_for(:mcq), do: & &1.answer["choice_id"]
defp answer_builder_for(:voting), do: nil
defp build_answer_fields_by_question_type(%{
question: %{answer: answer, type: question_type}
}) do
# No need to check if answer exists since empty answer would be a
# `%Answer{..., answer: nil}` and nil["anything"] = nil
%{grader: grader} = answer
transform_map_for_view(answer, %{
answer: answer_builder_for(question_type),
lastModifiedAt: :last_modified_at,
grader: grader_builder(grader),
gradedAt: graded_at_builder(grader),
xp: &((&1.xp || 0) + (&1.xp_adjustment || 0)),
autogradingStatus: :autograding_status,
autogradingResults: :autograding_results,
comments: :comments
})
end
defp build_contest_entry(entry) do
transform_map_for_view(entry, %{
submission_id: :submission_id,
answer: :answer,
score: :score
})
end
def build_contest_leaderboard_entry(leaderboard_ans) do
Map.put(
transform_map_for_view(leaderboard_ans, %{
submission_id: :submission_id,
answer: :answer,
student_name: :student_name,
student_username: :student_username,
rank: :rank
}),
"final_score",
Float.round(leaderboard_ans.relative_score, 2)
)
end
def build_popular_leaderboard_entry(leaderboard_ans) do
Map.put(
transform_map_for_view(leaderboard_ans, %{
submission_id: :submission_id,
answer: :answer,
student_name: :student_name,
student_username: :student_username,
rank: :rank
}),
"final_score",
Float.round(leaderboard_ans.popular_score, 2)
)
end
defp build_choice(choice) do
transform_map_for_view(choice, %{
id: "choice_id",
content: "content",
hint: "hint"
})
end
defp build_testcase(testcase, type) do
transform_map_for_view(testcase, %{
answer: "answer",
score: "score",
program: "program",
# Create a 1-arity function to return the type of the testcase as a string
type: fn _ -> type end
})
end
defp build_testcases(all_testcases?) do
if all_testcases? do
&Enum.concat(
Enum.concat(
Enum.map(&1["public"], fn testcase -> build_testcase(testcase, "public") end),
Enum.map(&1["opaque"], fn testcase -> build_testcase(testcase, "opaque") end)
),
Enum.map(&1["secret"], fn testcase -> build_testcase(testcase, "secret") end)
)
else
&Enum.concat(
Enum.map(&1["public"], fn testcase -> build_testcase(testcase, "public") end),
Enum.map(&1["opaque"], fn testcase -> build_testcase(testcase, "opaque") end)
)
end
end
defp build_question_content_by_config(
%{
question: %{
question: question,
type: question_type
}
},
all_testcases?
) do
case question_type do
:programming ->
transform_map_for_view(question, %{
content: "content",
prepend: "prepend",
solutionTemplate: "template",
postpend: "postpend",
testcases: build_testcases(all_testcases?)
})
:mcq ->
transform_map_for_view(question, %{
content: "content",
choices: &Enum.map(&1["choices"], fn choice -> build_choice(choice) end)
})
:voting ->
transform_map_for_view(question, %{
content: "content",
prepend: "prepend",
solutionTemplate: "template",
contestEntries:
&Enum.map(&1[:contest_entries], fn entry -> build_contest_entry(entry) end),
scoreLeaderboard:
&Enum.map(&1[:contest_leaderboard], fn entry ->
build_contest_leaderboard_entry(entry)
end),
popularVoteLeaderboard:
&Enum.map(&1[:popular_leaderboard], fn entry ->
build_popular_leaderboard_entry(entry)
end)
})
end
end
defp find_correct_choice(choices) do
choices
|> Enum.find(&Map.get(&1, "is_correct"))
|> Map.get("choice_id")
end
end