survey/app/models/questionnaire_survey.rb

173 lines
5.9 KiB
Ruby
Raw Normal View History

2014-10-06 05:55:25 +00:00
# encoding: utf-8
class QuestionnaireSurvey
include Mongoid::Document
include Mongoid::Timestamps
include Slug
ResultChart = 0
ResultExtern = 1
ResultFile = 2
ResultCriteria = 3
2021-07-08 13:31:27 +00:00
include OrbitCategory::Categorizable
2014-10-06 05:55:25 +00:00
scope :can_display, ->{where(is_hidden: false)}
field :title, as: :slug_title, type: String, localize: true
field :description, :localize => true
field :create_user_id
field :update_user_id
field :postdate, :type => DateTime
field :deadline, :type => DateTime
field :is_hidden, :type => Boolean, :default => false
field :needs_login, :type => Boolean, :default => false
field :total_points, type: Integer, :default => 0
2014-10-06 05:55:25 +00:00
field :result_type, :type => Integer, :default => 0
field :extern_link
mount_uploader :upload_file, AssetUploader
field :result_criteria, type: Array, :default => []
2014-10-06 05:55:25 +00:00
field :jump_mode, :type => Boolean, :default => false
field :redirect_mode, :type => Boolean, :default => false
field :redirect_url, :type => String
# validates :title, :at_least_one => true
# validates :title, :presence => { :message => I18n.t("survey.title") }
has_many :survey_questions, :autosave => true, :dependent => :destroy
has_many :survey_answers, :dependent => :destroy
2018-01-03 08:36:00 +00:00
has_many :survey_sections, :dependent => :destroy
2014-10-06 05:55:25 +00:00
accepts_nested_attributes_for :survey_questions, :allow_destroy => true
before_save :check_deadline#, :update_avliable_language
2021-07-08 13:31:27 +00:00
has_many :survey_paginations, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :survey_paginations, :allow_destroy => true
2014-10-06 05:55:25 +00:00
def update_user
User.find(update_user_id) rescue nil
end
def update_user=(user)
self.update_user_id = user.id
end
def self.time_range(data)
r = "#{data.postdate.to_date}"
if data.deadline
r += " - #{data.deadline.to_date}"
else
r += " - #{I18n.t(:no_deadline)}"
end
r
end
def self.result(data)
if ( data.result_type == QuestionnaireSurvey::ResultChart && data.deadline && Time.now > data.deadline ) ||
( data.result_type == QuestionnaireSurvey::ResultExtern && !data.extern_link.blank? ) ||
( data.result_type == QuestionnaireSurvey::ResultFile && data.upload_file? )
('<a target="_blank" href="'+ OrbitHelper.url_to_show(data.to_param) + '?method=result&force_chart=true' + '"><span class="result"><i class="icon-align-left"></i></span></a>').html_safe
else
''
end
end
def self.write(data)
unless data.deadline && Time.now > data.deadline
('<a target="_blank" href="'+ OrbitHelper.url_to_show(data.to_param) + '"><span class="write"><i class="icon-edit"></i></span></a>').html_safe
else
''
end
end
def generate_chart_data
survey_questions = self.survey_questions.all
2021-07-08 13:31:27 +00:00
survey_answers = self.survey_answers.all.order_by(created_at: :desc)
2014-10-06 05:55:25 +00:00
chart_data = {}
2021-07-08 13:31:27 +00:00
answers =(0...survey_answers.count).map{{}}
2014-10-06 05:55:25 +00:00
2021-07-08 13:31:27 +00:00
SurveysHelper.set_locale(I18n.locale)
2014-10-06 05:55:25 +00:00
survey_questions.each do |question|
qid = question.id.to_s
2021-07-08 13:31:27 +00:00
use_custom_option = question.custom_option_new_option
chart_data[qid] = {}
2014-10-06 05:55:25 +00:00
case question.type
when SurveyQuestion::Radio, SurveyQuestion::Select
2021-07-08 13:31:27 +00:00
options = question.survey_question_options.map{|v| [v.name,v.id.to_s]}
survey_answers.each_with_index do |answer,i|
answers[i][qid] = SurveysHelper.parse_exel_value(answer[qid],
options: options,
chart_data: chart_data[qid],
use_custom_option: use_custom_option)
2014-10-06 05:55:25 +00:00
end
when SurveyQuestion::Check
2021-07-08 13:31:27 +00:00
options = question.survey_question_options.map{|v| [v.name,v.id.to_s]}
survey_answers.each_with_index do |answer,i|
answers[i][qid] = SurveysHelper.parse_exel_checkbox_value(answer[qid],
options: options,
chart_data: chart_data[qid],
use_custom_option: use_custom_option)
2014-10-06 05:55:25 +00:00
end
when SurveyQuestion::Radiogroup
2021-07-08 13:31:27 +00:00
options = question.survey_question_options.map{|v| [v.name,v.id.to_s]}
radiogroups = question.survey_question_radiogroups.map{|v| [v.name,v.id.to_s]}
survey_answers.each_with_index do |answer,i|
answers[i][qid] = SurveysHelper.parse_exel_rg_value(answer[qid],
options: options,
radiogroups: radiogroups,
chart_data: chart_data[qid],
use_custom_option: use_custom_option)
end
when SurveyQuestion::DoubleLevelOption
custom_option_title = question.custom_option_title.to_s
survey_answers.each_with_index do |answer,i|
answers[i][qid] = SurveysHelper.parse_exel_double_level_value(answer[qid],
other_title: custom_option_title,
use_custom_option: use_custom_option,
chart_data: chart_data[qid])
end
else
survey_answers.each_with_index do |answer,i|
answers[i][qid] = answer[qid]
2014-10-06 05:55:25 +00:00
end
end
end
2021-07-08 13:31:27 +00:00
[chart_data, survey_questions, answers]
2014-10-06 05:55:25 +00:00
end
def expired?
(self.deadline < Time.now) rescue false
end
protected
def check_deadline
if(!self.deadline.nil? and (self.deadline < self.postdate ))
self.deadline = nil
end
end
# def update_avliable_language
# VALID_LOCALES.each do |locale|
# if (title_translations[locale].blank? rescue true)
# self["available_for_#{locale}".to_sym] = false
# else
# self["available_for_#{locale}".to_sym] = true
# end
# end
# end
2017-01-11 06:41:48 +00:00
# paginates_per 10
2014-10-06 05:55:25 +00:00
end