Created
January 28, 2011 00:05
-
-
Save r38y/799568 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Question < ActiveRecord::Base | |
belongs_to :question_group | |
has_many :all_options, :dependent => :destroy, :class_name => 'Option' | |
has_many :options, :conditions => {:active => true} | |
has_many :correct_options, :conditions => {:correct => true, :active => true}, :class_name => 'Option' | |
has_many :askings | |
has_many :notes, :as => :notable, :dependent => :destroy | |
scope :active, where(:active => true) | |
scope :inactive, where(:active => false) | |
validates_presence_of :body, :question_group_id | |
acts_as_textiled :body, :justification | |
accepts_nested_attributes_for :all_options, :reject_if => proc { |attributes| attributes['title'].blank? } | |
has_attached_file :media, | |
:storage => CONFIG.storage, | |
:path => CONFIG.path, | |
:url => CONFIG.url | |
def self.create_from_blob(blob=nil) | |
raise ArgumentError if blob.blank? | |
body = blob.scan(/^\?{1}.+$/).map{|s| s.gsub(/^\?/, '').strip}.join("\n\n") | |
justification = blob.scan(/^={1}.+$/).map{|s| s.gsub(/^\=/, '').strip}.join("\n\n") | |
question = create(:body => body, :justification => justification) | |
option_blobs = blob.scan(/^[+-]{1}.+$/) | |
option_blobs.each do |blob| | |
question.options.create_from_blob(blob) | |
end | |
question | |
end | |
def update_option_positions(positions) | |
options.active.each do |option| | |
option.update_attribute(:position, positions.index(option.id.to_s) + 1) | |
end | |
end | |
def self.update_correctly_answered_proportions! | |
Question.find_each do |question| | |
question.correctly_answered_proportion(true) | |
end | |
end | |
def correctly_answered_proportion(force=false) | |
db_value = read_attribute(:correctly_answered_proportion) | |
if db_value.blank? || force | |
new_value = correctly_answered_proportion! | |
write_attribute(:correctly_answered_proportion, new_value) | |
#self.correctly_answered_proportion = new_value | |
save! | |
new_value | |
else | |
db_value | |
end | |
end | |
def correctly_answered_proportion! | |
askings_count = askings.count | |
correct_askings_count = askings.select{|a| correct_option_ids.include? a.answer_id}.size | |
correct_askings_count.to_f / askings_count.to_f unless askings_count.zero? | |
end | |
#def calc_correctly_answered_proportion | |
#askings_count = askings.count | |
#correct_askings_count = askings.select{|a| correct_option_ids.include? a.answer_id}.size | |
#prop = correct_askings_count.to_f / askings_count.to_f unless askings_count.zero? | |
#puts prop | |
#self.correctly_answered_proportion = prop | |
#save(:validate => false) | |
#prop | |
#end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment