From 4a736cc92c0acd99b79a48a49743ad6ea4aacd89 Mon Sep 17 00:00:00 2001 From: thomaschen <99703040@nccu.edu.tw> Date: Fri, 25 Oct 2013 16:54:29 +0800 Subject: [PATCH] error fix --- .../survey/back_end/surveys_controller.rb | 167 ++++++++++-------- 1 file changed, 90 insertions(+), 77 deletions(-) diff --git a/vendor/built_in_modules/survey/app/controllers/panel/survey/back_end/surveys_controller.rb b/vendor/built_in_modules/survey/app/controllers/panel/survey/back_end/surveys_controller.rb index af5f96e1a..da7543eb5 100644 --- a/vendor/built_in_modules/survey/app/controllers/panel/survey/back_end/surveys_controller.rb +++ b/vendor/built_in_modules/survey/app/controllers/panel/survey/back_end/surveys_controller.rb @@ -111,105 +111,118 @@ class Panel::Survey::BackEnd::SurveysController < OrbitBackendController @survey_answers.destroy - # End of speadsheet + # Start and end of speadsheet @end_row = @spreadsheet.count - # Modify multiline options if changed - @survey_questions.each_with_index do |question, index| + @max_row_start = 0 + @survey_questions.each do |question| case question.type + when ::SurveyQuestion::Radio, ::SurveyQuestion::Select, ::SurveyQuestion::Check + if question.survey_question_options.count > @max_row_start + @max_row_start = question.survey_question_options.count + end + when ::SurveyQuestion::Radiogroup + if question.survey_question_options.count * question.survey_question_radiogroups.count > @max_row_start + @max_row_start = question.survey_question_options.count * question.survey_question_radiogroups.count + end + end + end + @start_row = @max_row_start + 2 - when ::SurveyQuestion::Radio, ::SurveyQuestion::Select - ((question.survey_question_options.count + 2)..@end_row).each do |row| - @answer_model = @survey.survey_answers.new + # Modify multiline options if changed + (@start_row..@end_row).each do |row| + @answer_model = @survey.survey_answers.new + @survey_questions.each_with_index do |question, index| + case question.type + + when ::SurveyQuestion::Radio, ::SurveyQuestion::Select if !@spreadsheet.row(row)[index*2].nil? && @spreadsheet.row(row)[index*2] != " " @answer_model[question.id.to_s] = @spreadsheet.row(row)[index*2] - @answer_model.save! end - end - when ::SurveyQuestion::Check - ((question.survey_question_options.count + 2)..@end_row).each do |row| - @answer_model = @survey.survey_answers.new + when ::SurveyQuestion::Check if !@spreadsheet.row(row)[index*2].nil? @answer_model[question.id.to_s] = @spreadsheet.row(row)[index*2].split("\"").select.each_with_index { |str, i| i.odd? } - @answer_model.save! end - end - when ::SurveyQuestion::Radiogroup - radio_groups = [] - spreadsheet_radiogroups_lines = - question.survey_question_options.count * question.survey_question_radiogroups.count + 2 - - # Grab each radiogroups line for update - (2...spreadsheet_radiogroups_lines).each do |line| - 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 + when ::SurveyQuestion::Radiogroup + radio_groups = [] + spreadsheet_radiogroups_lines = + question.survey_question_options.count * question.survey_question_radiogroups.count + 2 + + # Grab each radiogroups line for update + (2...spreadsheet_radiogroups_lines).each do |line| + radio_groups << @spreadsheet.row(line)[index * 2] end - @answer_model.save! - end + # Grab answers info + answers = [] + (spreadsheet_radiogroups_lines..@spreadsheet.last_row).each do |line| - # Parse the needed info in the array - radio_titles = [] - radio_options = [] - radio_groups.each do |group| - option_with_radio = group.split(' - ') - radio_titles << option_with_radio[0] - radio_options << option_with_radio[1] - end - - # Update the spreadsheet info to the DB - groups_of_options = [] - groups_of_radios = [] - radio_titles.each_slice(question.survey_question_radiogroups.count) do |options| - groups_of_options << options - end - - radio_options.each_slice(question.survey_question_radiogroups.count) do |options| - groups_of_radios << options - end - - # Update option names - question.survey_question_options.each_with_index do |option, index| - groups_of_options[index].each do |modified_option| - if option.name_translations["zh_tw"] != modified_option - option.update_attributes(name: modified_option) - break + if not @spreadsheet.row(line)[index * 2].blank? + answers << eval(@spreadsheet.row(line)[index * 2]) end end - end - # Update radio names - question.survey_question_radiogroups.each_with_index do |option, index| - groups_of_radios.each do |radios| - if radios[index] != option.name_translations["zh_tw"] - option.update_attributes(name: radios[index]) - break + # 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 = [] + radio_groups.each do |group| + option_with_radio = group.split(' - ') + radio_titles << option_with_radio[0] + radio_options << option_with_radio[1] + end + + # Update the spreadsheet info to the DB + groups_of_options = [] + groups_of_radios = [] + radio_titles.each_slice(question.survey_question_radiogroups.count) do |options| + groups_of_options << options + end + + radio_options.each_slice(question.survey_question_radiogroups.count) do |options| + groups_of_radios << options + end + + # Update option names + question.survey_question_options.each_with_index do |option, index| + groups_of_options[index].each do |modified_option| + if option.name_translations["zh_tw"] != modified_option + option.update_attributes(name: modified_option) + break + end end end - end + # Update radio names + question.survey_question_radiogroups.each_with_index do |option, index| + groups_of_radios.each do |radios| + if radios[index] != option.name_translations["zh_tw"] + option.update_attributes(name: radios[index]) + break + end + end + end + + end end - end + + @answer_model.save! + + end #end row # @start_row = 2 # @survey.survey_answers.destroy