Class: SurveyingController

Inherits:
ActionController::Base
  • Object
show all
Includes:
SurveyingHelper, UserManager
Defined in:
app/controllers/surveying_controller.rb

Overview

The Surveying controller handles the process of a user taking a survey. It is semi-restful since it does not have a concrete representation model. The “resource” could be considered a survey attempt or survey session. It is actually the union of a user filling out a survey and populating a response set.

Instance Method Summary collapse

Methods included from SurveyingHelper

#answer_id_helper, #dependency_explanation_helper, #fields_for_radio, #fields_for_repeater_response, #fields_for_response, #question_help_helper, #question_id_helper, #question_number_helper, #question_text_postfix_helper, #question_text_prefix_helper, #section_id_helper, #section_next_helper, #section_previous_helper, #section_submit_helper, #section_submit_name_helper

Methods included from UserManager

#current_user

Instance Method Details

#createObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/controllers/surveying_controller.rb', line 27

def create
  @survey = Survey.find_by_access_code(params[:survey_code])
  unless @survey
    flash[:notice] = "Unable to find that survey"
    redirect_to(available_surveys_path)
  else
    @response_set = ResponseSet.new(:survey => @survey, :user_id => 123)
    respond_to do |format|
      if @response_set.save!
        flash[:notice] = 'Survey was successfully created.'
        format.html { redirect_to(edit_my_survey_path(:survey_code => @survey.access_code, :response_set_code  => @response_set.access_code)) }
        # format.xml  { render :xml => @response_set, :status => :created, :location => @response_set }
      else
        format.html { redirect_to(available_surveys_path)}
        # format.xml  { render :xml => @response_set.errors, :status => :unprocessable_entity }
      end
    end
  end
end

#editObject



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/surveying_controller.rb', line 54

def edit
  # Checking from the questions on the page (which are in the response hash) if there are any dependent questions to show
  dependent_hash = dependents(@response_set)
  @dependents = dependent_hash[:show]
  respond_to do |format|
    format.html do
      render
    end
    format.json do
      render :json => {:show => dependent_hash[:show].map{|q| question_id_helper(q)}, :hide => dependent_hash[:hide].map{|q| question_id_helper(q)} }.to_json
    end
  end
end

#finishObject



88
89
90
91
92
93
94
95
# File 'app/controllers/surveying_controller.rb', line 88

def finish
  respond_to do |format|
    format.html do
      flash[:notice] = @response_set.complete! ? "Completed survey" : "Unable to complete survey"
      render :action => 'edit'
    end
  end
end

#indexObject



15
16
17
# File 'app/controllers/surveying_controller.rb', line 15

def index
  @surveys = Survey.find(:all) 
end

#newObject



19
20
21
22
23
24
25
# File 'app/controllers/surveying_controller.rb', line 19

def new
  @surveys = Survey.find(:all)
  respond_to do |format|
    format.html # new.html.erb
    # format.xml  { render :xml => @surveys }
  end
end

#showObject



47
48
49
50
51
52
# File 'app/controllers/surveying_controller.rb', line 47

def show
  respond_to do |format|
    format.html # show.html.erb
    # format.xml  { render :xml => @survey}
  end
end

#updateObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/controllers/surveying_controller.rb', line 68

def update
  if params[:responses] or params[:response_groups]
    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
  end
  respond_to do |format|
    format.html do
      flash[:notice] = saved ? "Updated survey" : "Unable to update survey" unless saved.nil? # Saved is nil if there are no questions on the page (ie if it only contains a label)
      redirect_to :action => "edit", :anchor => @anchor, :params => {:section => @section.id}
    end
    # No redirect needed if we're talking to the page via json
    format.json do
        dependent_hash = dependents(@response_set)
        @dependents = dependent_hash[:show]
        render :json => {:show => dependent_hash[:show].map{|q| question_id_helper(q)}, :hide => dependent_hash[:hide].map{|q| question_id_helper(q)} }.to_json
    end
  end

end