Skip to content
Adrian Teh edited this page May 22, 2013 · 5 revisions

Survey (persisted)

  • activity_type_id:integer
  • title:string
    CiviCrm::CustomGroup.where(:extends => “Activity”, :extends_entity_column_value => 32).first.title
  • custom_group_id:integer
    CiviCrm::CustomGroup.where(:extends => “Activity”, :extends_entity_column_value => 32).first.id

CustomFields (persisted)

`CiviCrm::CustomField.where(:custom_group_id => survey.custom_group_id)`
  • custom_field_id:integer
  • survey_id:integer
  • label:string

Survey - Response Model

class Survey < ActiveRecord::Base
  has_many :custom_fields
  attr_accessible :activity_type_id
  def responses
    params = {
      activity_type_id: self.activity_type_id,
      'return.target_contact_id': 1
    }
    self.custom_fields.each do |f|
      params["return.custom_#{custom_field_id}"] = 1
    end
    CiviCrm::Activity.where(params).map do |response|
      Response.new(self, response)
    end
  end
end

class CustomField < ActiveRecord::Base
  belongs_to :survey
end

class Response
  attr_accessor :survey, :response
  def initialize(survey, response)
    @survey = survey
    @response = response
  end

  def answers
    @answers ||= begin
      survey.custom_fields.map do |field|
        OpenStruct.new(:label => field.label, 
                       :answer => response.send("custom_#{ field.custom_field_id }"))
      end
    end
  end

  def contact
    @contact ||= CiviCrm::Contact.where(id: response.target_contact_id)
  end
end

Clone this wiki locally