survey/app/models/survey_answer.rb

57 lines
1.8 KiB
Ruby
Raw Normal View History

2014-10-06 05:55:25 +00:00
class SurveyAnswer
include Mongoid::Document
include Mongoid::Timestamps
2021-07-08 13:31:27 +00:00
include Mongoid::Attributes::Dynamic
field :user, type: BSON::ObjectId
2021-11-04 09:52:57 +00:00
field :consent_used, type: Boolean, :default => false
field :scored_points, type: Integer, :default => 0
2021-10-13 09:15:47 +00:00
field :avg_points, type: Integer
2016-10-29 11:33:54 +00:00
field :individual_total, type: Array, :default => []
2021-07-08 13:31:27 +00:00
field :select_question, type: Array, :default => []
field :color, type: String
2014-10-06 05:55:25 +00:00
belongs_to :questionnaire_survey
after_create do
if self.questionnaire_survey
2021-09-12 12:56:09 +00:00
info = {:questionnaire_survey_id=>self.questionnaire_survey.id,:user=>self.user}
if self.user.blank?
info[:survey_answer_ids] = [self.id]
end
answer_group = SurveyAnswerGroup.where(info).first
if answer_group.nil?
2021-09-12 12:56:09 +00:00
answer_group = SurveyAnswerGroup.new(info)
end
2021-11-09 14:48:29 +00:00
answer_group.last_modified = self.created_at
answer_group.survey_answer_ids << self.id
answer_group.save
end
end
after_save do
answer_group = SurveyAnswerGroup.where(:questionnaire_survey_id=>self.questionnaire_survey.id,:user=>user).first
if answer_group
2021-11-09 14:48:29 +00:00
answer_group.last_modified = self.created_at
answer_group.save
end
end
before_destroy do
2021-10-12 13:26:26 +00:00
answer_group = SurveyAnswerGroup.where(:user=>self.user,:questionnaire_survey_id=>self.questionnaire_survey_id).first
if answer_group
answer_group.survey_answer_ids.delete(self.id)
if answer_group.survey_answer_ids.count == 0
answer_group.destroy
else
answer_group.save
end
end
end
2021-10-13 09:15:47 +00:00
def get_avg_points
if self.avg_points.nil?
total_weight = self.questionnaire_survey.get_total_weight(self)
self.avg_points = (scored_points.to_f / total_weight).round
self.save
self.avg_points
else
self.avg_points
end
end
2014-10-06 05:55:25 +00:00
end