Guest User

Untitled

a guest
Jul 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.62 KB | None | 0 0
  1. class Question < ActiveRecord::Base
  2. belongs_to :question_group
  3. has_many :all_options, :dependent => :destroy, :class_name => 'Option'
  4. has_many :options, :conditions => {:active => true}
  5. has_many :correct_options, :conditions => {:correct => true, :active => true}, :class_name => 'Option'
  6. has_many :askings
  7. has_many :notes, :as => :notable, :dependent => :destroy
  8.  
  9. scope :active, where(:active => true)
  10. scope :inactive, where(:active => false)
  11.  
  12. validates_presence_of :body, :question_group_id
  13. acts_as_textiled :body, :justification
  14. accepts_nested_attributes_for :all_options, :reject_if => proc { |attributes| attributes['title'].blank? }
  15.  
  16. has_attached_file :media,
  17. :storage => CONFIG.storage,
  18. :path => CONFIG.path,
  19. :url => CONFIG.url
  20.  
  21. def self.create_from_blob(blob=nil)
  22. raise ArgumentError if blob.blank?
  23. body = blob.scan(/^\?{1}.+$/).map{|s| s.gsub(/^\?/, '').strip}.join("\n\n")
  24. justification = blob.scan(/^={1}.+$/).map{|s| s.gsub(/^\=/, '').strip}.join("\n\n")
  25. question = create(:body => body, :justification => justification)
  26. option_blobs = blob.scan(/^[+-]{1}.+$/)
  27.  
  28. option_blobs.each do |blob|
  29. question.options.create_from_blob(blob)
  30. end
  31. question
  32. end
  33.  
  34. def update_option_positions(positions)
  35. options.active.each do |option|
  36. option.update_attribute(:position, positions.index(option.id.to_s) + 1)
  37. end
  38. end
  39.  
  40. def self.update_correctly_answered_proportions!
  41. Question.find_each do |question|
  42. question.correctly_answered_proportion(true)
  43. end
  44. end
  45.  
  46. def correctly_answered_proportion(force=false)
  47. db_value = read_attribute(:correctly_answered_proportion)
  48. if db_value.blank? || force
  49. new_value = correctly_answered_proportion!
  50. write_attribute(:correctly_answered_proportion, new_value)
  51. #self.correctly_answered_proportion = new_value
  52. save!
  53. new_value
  54. else
  55. db_value
  56. end
  57. end
  58.  
  59. def correctly_answered_proportion!
  60. askings_count = askings.count
  61. correct_askings_count = askings.select{|a| correct_option_ids.include? a.answer_id}.size
  62. correct_askings_count.to_f / askings_count.to_f unless askings_count.zero?
  63. end
  64.  
  65. #def calc_correctly_answered_proportion
  66. #askings_count = askings.count
  67. #correct_askings_count = askings.select{|a| correct_option_ids.include? a.answer_id}.size
  68. #prop = correct_askings_count.to_f / askings_count.to_f unless askings_count.zero?
  69. #puts prop
  70. #self.correctly_answered_proportion = prop
  71. #save(:validate => false)
  72. #prop
  73. #end
  74. end
Add Comment
Please, Sign In to add comment