Allow update answers on radio groups

This commit is contained in:
Bernie Chiu 2013-10-25 16:26:06 +08:00
parent 3a721c653b
commit a78f9e6ae6
1 changed files with 39 additions and 18 deletions

View File

@ -111,29 +111,29 @@ class Panel::Survey::BackEnd::SurveysController < OrbitBackendController
question.update_attributes(title: titles[index])
end
@survey.survey_answers.destroy
@survey_answers.destroy
# Modify multiline options if changed
@survey_questions.each_with_index do |question, index|
case question.type
when ::SurveyQuestion::Radio, ::SurveyQuestion::Select
(2..(question.survey_question_options.count + 1)).each do |row|
(1..@spreadsheet.row(row)[index*2 + 1].to_i).each do
@answer_model = @survey.survey_answers.new
@answer_model[question.id.to_s] = @spreadsheet.row(row)[index*2]
@answer_model.save!
end
end
# when ::SurveyQuestion::Radio, ::SurveyQuestion::Select
# (2..(question.survey_question_options.count + 1)).each do |row|
# (1..@spreadsheet.row(row)[index*2 + 1].to_i).each do
# @answer_model = @survey.survey_answers.new
# @answer_model[question.id.to_s] = @spreadsheet.row(row)[index*2]
# @answer_model.save!
# end
# end
when ::SurveyQuestion::Check
(2..(question.survey_question_options.count + 1)).each do |row|
(1..@spreadsheet.row(row)[index*2 + 1].to_i).each do
@answer_model = @survey.survey_answers.new
@answer_model[question.id.to_s] = [ @spreadsheet.row(row)[index*2] ]
@answer_model.save!
end
end
# when ::SurveyQuestion::Check
# (2..(question.survey_question_options.count + 1)).each do |row|
# (1..@spreadsheet.row(row)[index*2 + 1].to_i).each do
# @answer_model = @survey.survey_answers.new
# @answer_model[question.id.to_s] = [ @spreadsheet.row(row)[index*2] ]
# @answer_model.save!
# end
# end
when ::SurveyQuestion::Radiogroup
radio_groups = []
@ -145,6 +145,28 @@ class Panel::Survey::BackEnd::SurveysController < OrbitBackendController
radio_groups << @spreadsheet.row(line)[index * 2]
end
# Grab answers info
answers = []
(spreadsheet_radiogroups_lines..@spreadsheet.last_row).each do |line|
if not @spreadsheet.row(line)[index * 2].blank?
answers << eval(@spreadsheet.row(line)[index * 2])
end
end
# Save the answers
answers.each do |answer|
options = Hash[question.survey_question_options.collect { |o| [ o.id.to_s, o.name ] }]
@answer_model = @survey.survey_answers.new
@answer_model[question.id.to_s] = {}
answer.each do |option, value|
@answer_model[question.id.to_s][options.invert[option]] = value
end
@answer_model.save!
end
# Parse the needed info in the array
radio_titles = []
radio_options = []
@ -179,7 +201,6 @@ class Panel::Survey::BackEnd::SurveysController < OrbitBackendController
question.survey_question_radiogroups.each_with_index do |option, index|
groups_of_radios.each do |radios|
if radios[index] != option.name_translations["zh_tw"]
binding.pry
option.update_attributes(name: radios[index])
break
end