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
|
|
|
|
|
2015-11-27 11:55:47 +00:00
|
|
|
field :user, type: BSON::ObjectId
|
2016-08-18 13:18:02 +00:00
|
|
|
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 => []
|
2014-10-06 05:55:25 +00:00
|
|
|
belongs_to :questionnaire_survey
|
2021-09-12 12:20:03 +00:00
|
|
|
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
|
2021-09-12 12:20:03 +00:00
|
|
|
if answer_group.nil?
|
2021-09-12 12:56:09 +00:00
|
|
|
answer_group = SurveyAnswerGroup.new(info)
|
2021-09-12 12:20:03 +00:00
|
|
|
end
|
|
|
|
answer_group.last_modified = self.updated_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
|
|
|
|
answer_group.last_modified = self.updated_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
|
2021-09-12 12:20:03 +00:00
|
|
|
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
|