Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions lib/grader.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Question = Struct.new(:question_type, :prompt)

Choice = Struct.new(:key, :text, :correct)
Choice = Struct.new(:key, :text, :correct, :correct_order)

def grader(question, choices, responses)
if question.question_type == "MULTIPLE_CHOICE"
Expand All @@ -10,7 +10,7 @@ def grader(question, choices, responses)
correct_responses = choices[0].select { |choice| choice.correct }
chosen_responses = choices[0].select { |choice| choice.correct && responses.include?(choice.key) }
chosen_responses.size.to_f / correct_responses.size.to_f
elsif question.question_type = "MULTIPLE_DROPDOWNS"
elsif question.question_type == "MULTIPLE_DROPDOWNS"
correct_responses = choices.to_enum(:each_with_index).collect do |dropdown_choice, index|
current_response = responses[index]
current_chosen_choice = dropdown_choice.detect { |choice| current_response == choice.key }
Expand All @@ -19,6 +19,20 @@ def grader(question, choices, responses)
sum + curr_response
end
correct_responses / choices.size.to_f
elsif question.question_type == "CLOZE_DRAG_AND_DROP"
ccio = choices[0].sort_by { |choice| choice.correct_order }
ccio_keys = ccio.collect { |choice| choice.key }
if ccio_keys == responses
1.0
else
all_keys_present = true
for r in responses
if !ccio_keys.include?(r)
all_keys_present = false
end
end
(all_keys_present) ? 0.5 : 0.0
end
else
0.0
end
Expand Down
16 changes: 16 additions & 0 deletions spec/grader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,20 @@
expect(grader(question, choices, ["A", "C"])).to eq(0.5)
end
end

context "CLOZE_DRAG_AND_DROP" do
question = Question.new("CLOZE_DRAG_AND_DROP", "For {target1}, then {target2}.")
choices = [[
Choice.new("A", "A", true, 1),
Choice.new("B", "B", true, 2)
]]

it("should return 1.0 if responses match correct choices in order") do
expect(grader(question, choices, ["A", "B"])).to eq(1.0)
end

it("should return 0.5 if responses are out of order") do
expect(grader(question, choices, ["B", "A"])).to eq(0.5)
end
end
end