Class: SurveyorController

Inherits:
ApplicationController
  • Object
show all
Includes:
SurveyorControllerExtensions
Defined in:
app/controllers/surveyor_controller.rb

Overview

The Surveyor controller a user taking a survey. It is semi-RESTful since it does not have a concrete representation model. The “resource” is a survey attempt/session populating a response set.

Instance Method Summary collapse

Instance Method Details

#createObject


28
29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/surveyor_controller.rb', line 28

def create
  @survey = Survey.find_by_access_code(params[:survey_code])
  @response_set = ResponseSet.create(:survey => @survey, :user_id => (@current_user.nil? ? @current_user : @current_user.id))
  if (@survey && @response_set)
    flash[:notice] = "Survey was successfully started."
    redirect_to(edit_my_survey_path(:survey_code => @survey.access_code, :response_set_code  => @response_set.access_code))
  else
    flash[:notice] = "Unable to find that survey"
    redirect_to(available_surveys_path)
  end
end

#editObject


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/surveyor_controller.rb', line 50

def edit
  @response_set = ResponseSet.find_by_access_code(params[:response_set_code], :include => {:responses => [:question, :answer]})
  if @response_set
    @survey = Survey.with_sections.find_by_id(@response_set.survey_id)
    @sections = @survey.sections
    if params[:section]  
      @section = @sections.with_includes.find(section_id_from(params[:section])) || @sections.with_includes.first 
    else
      @section = @sections.with_includes.first
    end
    @questions = @section.questions
    @dependents = (@response_set.unanswered_dependencies - @section.questions) || []
  else
    flash[:notice] = "Unable to find your responses to the survey"
    redirect_to(available_surveys_path)
  end
end

#newObject

Actions


23
24
25
26
# File 'app/controllers/surveyor_controller.rb', line 23

def new
  @surveys = Survey.find(:all)
  redirect_to surveyor_default(:index) unless available_surveys_path == surveyor_default(:index)
end

#showObject


40
41
42
43
44
45
46
47
48
# File 'app/controllers/surveyor_controller.rb', line 40

def show
  @response_set = ResponseSet.find_by_access_code(params[:response_set_code], :include => {:responses => [:question, :answer]})
  respond_to do |format|
    format.html #{render :action => :show}
    format.csv {
      send_data(@response_set.to_csv, :type => 'text/csv; charset=utf-8; header=present',:filename => "#{@response_set.updated_at.strftime('%Y-%m-%d')}_#{@response_set.access_code}.csv")
    }
  end
end

#updateObject


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/controllers/surveyor_controller.rb', line 68

def update
  saved = nil
  ActiveRecord::Base.transaction do 
    if @response_set = ResponseSet.find_by_access_code(params[:response_set_code], :include => {:responses => :answer},:lock => true)
      @response_set.current_section_id = params[:current_section_id]
    else
      flash[:notice] = "Unable to find your responses to the survey"
      redirect_to(available_surveys_path) and return
    end

    if params[:responses] or params[:response_groups]
      @response_set.clear_responses
      saved = @response_set.update_attributes(:response_attributes => (params[:responses] || {}).dup ,
                                              :response_group_attributes => (params[:response_groups] || {}).dup) #copy (dup) to preserve params because we manipulate params in the response_set methods
      if (saved && params[:finish])
        @response_set.complete!
        saved = @response_set.save!
      end
    end
  end
  respond_to do |format|
    format.html do
      if saved && params[:finish]
        flash[:notice] = "Completed survey"
        redirect_to surveyor_default(:finish)
      else
        flash[:notice] = "Unable to update survey" if !saved #and !saved.nil? # saved.nil? is true if there are no questions on the page (i.e. if it only contains a label)
        redirect_to :action => "edit", :anchor => anchor_from(params[:section]), :params => {:section => section_id_from(params[:section])}
      end
    end
    # No redirect needed if we're talking to the page via json
    format.js do
#        render :json => @response_set.all_dependencies.to_json
      render :json => @response_set.all_things_hash.to_json
    end
  end
end