Skip to content

Commit

Permalink
Numerical questions accept flexible ranges
Browse files Browse the repository at this point in the history
The back-end will now accept answer ranges in a more flexible manner, so
providing a range of [50,10] or [-3,-8] will work.

Closes CNVS-11282

TEST PLAN
---- ----

  - create a quiz with a numerical question:
    - choose "answer in range" for an answer type
    - play with something similar to the combinations listed below and
      verify that you get the proper results
  - answer the quiz question by the student every-time you try a new
    combination and verify the grading is correct

Combinations:

  - from: -5, to: 10, answer: 7 (correct)
  - from: 10, to: -5, answer: 7, or 0, or -3 (correct)
  - from: -2.5, to: -3.5, answer: -3 (correct)
  - from: -3.5, to: -2.5, answer: 3 (incorrect)

Or just use your imagination :) The point is, the ranges should be
accepted and work properly regardless of if you specify the smaller
number first (this was an issue for negative ranges)

Change-Id: I3c3f8638f8244d6417effeec4b4f0e29198ea845
Reviewed-on: https://gerrit.instructure.com/32935
Tested-by: Jenkins <[email protected]>
Reviewed-by: Jason Madsen <[email protected]>
QA-Review: Caleb Guanzon <[email protected]>
Product-Review: Derek DeVries <[email protected]>
  • Loading branch information
amireh authored and Derek DeVries committed Apr 10, 2014
1 parent 2c118f7 commit 580e8d8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
13 changes: 12 additions & 1 deletion app/models/quizzes/quiz_question/numerical_question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@
require 'bigdecimal'

class Quizzes::QuizQuestion::NumericalQuestion < Quizzes::QuizQuestion::Base
class FlexRange
def initialize(a, b)
numbers = [ BigDecimal.new(a.to_s), BigDecimal.new(b.to_s) ].sort
@range = numbers[0]..numbers[1]
end

def cover?(number)
@range.cover?(number)
end
end

def answers
@question_data[:answers].sort_by { |a| a[:weight] || CanvasSort::First }
end
Expand Down Expand Up @@ -46,7 +57,7 @@ def correct_answer_parts(user_answer)
max = val + margin
answer_number >= min && answer_number <= max
else
answer_number >= BigDecimal.new(answer[:start].to_s) && answer_number <= BigDecimal.new(answer[:end].to_s)
FlexRange.new(answer[:start], answer[:end]).cover?(answer_number)
end
end

Expand Down
32 changes: 32 additions & 0 deletions spec/models/quizzes/quiz_question/numerical_question_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,37 @@

question.correct_answer_parts(user_answer).should be_false
end

describe 'flexible ranges' do
def self.test_range(range, answer, is_correct)
desc = "should calculate if %s falls %s (%d,%d)" % [
answer, is_correct ? 'within' : 'out of', range[0], range[1]
]

it desc do
answer_data = {:"question_#{question_id}" => "#{answer}"}
question = Quizzes::QuizQuestion::NumericalQuestion.new({
answers: [{
id: 1,
weight: 100,
start: range[0],
end: range[1]
}]
})

user_answer = Quizzes::QuizQuestion::UserAnswer.new(question_id, points_possible, answer_data)
question.correct_answer_parts(user_answer).should == is_correct
end
end

test_range [-3, 3], -2.5, true
test_range [3, -3], -2.5, true
test_range [-3, 3], -3.5, false
test_range [3, -3], -3.5, false
test_range [2.5, 3.5], 2.5, true
test_range [2.5, 3.5], 2.49, false
test_range [100, 50], 75, true
test_range [50, 100], 75, true
end
end
end

0 comments on commit 580e8d8

Please sign in to comment.