From f31c09eff6a8d5ae7d8755c5c26969c79e538fb9 Mon Sep 17 00:00:00 2001 From: James Smith Date: Sat, 11 Apr 2020 16:59:39 -0600 Subject: [PATCH] add support for cloze_drag_and_drop --- lib/grader.rb | 18 ++++++++++++++++-- spec/grader_spec.rb | 16 ++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/grader.rb b/lib/grader.rb index bd1ab5b..fbfa6a1 100644 --- a/lib/grader.rb +++ b/lib/grader.rb @@ -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" @@ -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 } @@ -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 diff --git a/spec/grader_spec.rb b/spec/grader_spec.rb index a32d292..1a46d37 100644 --- a/spec/grader_spec.rb +++ b/spec/grader_spec.rb @@ -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