fix divide by zero on quiz statistics page
Change-Id: I1b3c51c3d39cabb2e86e40ff5535f1b694df6dc4 Reviewed-on: https://gerrit.instructure.com/4299 Reviewed-by: Bracken Mosbacker <bracken@instructure.com> Tested-by: Hudson <hudson@instructure.com>
This commit is contained in:
parent
a1fa31f8ac
commit
d590384c6b
|
@ -857,7 +857,7 @@ class Quiz < ActiveRecord::Base
|
|||
questions_hash[question[:id]] ||= question
|
||||
end
|
||||
end
|
||||
stats[:submission_score_average] = score_counter.mean.to_f rescue 0 #stats[:submission_count] > 0 ? stats[:submission_score_tally] / stats[:submission_count] : 0
|
||||
stats[:submission_score_average] = score_counter.mean.to_f rescue 0
|
||||
stats[:submission_score_high] = score_counter.max
|
||||
stats[:submission_score_low] = score_counter.min
|
||||
stats[:submission_duration_average] = stats[:submission_count] > 0 ? stats[:submission_duration_tally].to_f / stats[:submission_count].to_f : 0
|
||||
|
|
14
lib/stats.rb
14
lib/stats.rb
|
@ -130,14 +130,18 @@ module Stats
|
|||
end
|
||||
|
||||
def mean
|
||||
total.to_f / size
|
||||
size > 0 ? total.to_f / size : 0
|
||||
end
|
||||
|
||||
def variance
|
||||
all.map {|value|
|
||||
diff = value - mean
|
||||
diff*diff
|
||||
}.sum / size.to_f
|
||||
if size > 0
|
||||
all.map {|value|
|
||||
diff = value - mean
|
||||
diff*diff
|
||||
}.sum / size.to_f
|
||||
else
|
||||
0
|
||||
end
|
||||
end
|
||||
|
||||
def standard_deviation
|
||||
|
|
|
@ -30,11 +30,15 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper.rb')
|
|||
# end
|
||||
# end
|
||||
|
||||
# include Stats
|
||||
include Stats
|
||||
|
||||
describe Stats do
|
||||
# context Counter do
|
||||
#
|
||||
context Counter do
|
||||
it "should not return NaN with no values" do
|
||||
c = Counter.new
|
||||
c.mean.should == 0
|
||||
end
|
||||
|
||||
# before do
|
||||
# @a = [1,2,4,6,9]
|
||||
# @c = Counter.new(@a)
|
||||
|
@ -132,5 +136,5 @@ describe Stats do
|
|||
# @c.mean.should eql(2.5)
|
||||
# @c.standard_deviation.should be_close( 0.612372435695794, 0.000000001)
|
||||
# end
|
||||
# end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue