# -*- encoding : utf-8 -*-
class SurveyType < ActiveRecord::Base
  attr_accessible :description, :intro_text, :name, :thank_text, :locked, :archived, :slug
  
  has_many :items, :class_name => "SurveyItem", :dependent => :destroy
  has_many :survey_sets
  
  def nbr_responses
    self.survey_sets.count
  end
  
  def to_survey_set
    @survey_set = SurveySet.new
    @survey_set.survey_type_id = self.id
    
    self.items.each do |item|
      
      question_set = @survey_set.question_sets.build(:survey_item_id => item.id)
      
      if item.questions_type == 3 || item.questions_type == 5
        
        item.answers.each do |answer|
        
          question_set.answer_sets.build(:survey_item_answer_id => answer.id)
          
        end
      end
      
    end
    @survey_set
    
  end
end
