app/controllers/questionnaires_controller.rb
def add_new_questions
    questionnaire_id = params[:id] unless params[:id].nil?
    num_of_existed_questions = Questionnaire.find(questionnaire_id).questions.size
    ((num_of_existed_questions + 1)..(num_of_existed_questions + params[:question][:total_num].to_i)).each do |i|
      question = Object.const_get(params[:question][:type]).create(txt: '', questionnaire_id: questionnaire_id, seq: i, type: params[:question][:type], break_before: true)
      if question.is_a? ScoredQuestion
        question.weight = 1
        question.max_label = 'Strongly agree'
        question.min_label = 'Strongly disagree'
      end
      question.size = '50, 3' if question.is_a? Criterion
      question.alternatives = '0|1|2|3|4|5' if question.is_a? Dropdown
      question.size = '60, 5' if question.is_a? TextArea
      question.size = '30' if question.is_a? TextField
      begin
        question.save
      rescue StandardError
        flash[:error] = $ERROR_INFO
      end
    end
    redirect_to edit_questionnaire_path(questionnaire_id.to_sym)
  end
# Zhewei: This method is used to add new questions when editing questionnaire.
This method is used to add new questions when editing questionnaire.
This method is used to add new questions when editing questionnaire .
def add_new_questions
    questionnaire_id = params[:id] unless params[:id].nil?
    num_of_existed_questions = Questionnaire.find(questionnaire_id).questions.size
    ((num_of_existed_questions + 1)..(num_of_existed_questions + params[:question][:total_num].to_i)).each do |i|
      question = Object.const_get(params[:question][:type]).create(txt: '', questionnaire_id: questionnaire_id, seq: i, type: params[:question][:type], break_before: true)
      if question.is_a? ScoredQuestion
        question.weight = 1
        question.max_label = 'Strongly agree'
        question.min_label = 'Strongly disagree'
      end
      question.size = '50, 3' if question.is_a? Criterion
      question.alternatives = '0|1|2|3|4|5' if question.is_a? Dropdown
      question.size = '60, 5' if question.is_a? TextArea
      question.size = '30' if question.is_a? TextField
      begin
        question.save
      rescue StandardError
        flash[:error] = $ERROR_INFO
      end
    end
    redirect_to edit_questionnaire_path(questionnaire_id.to_sym)
  end
def add_new_questions questionnaire_id = params [ :id ] unless params [ :id ] . nil? num_of_existed_questions = Questionnaire . find ( questionnaire_id ) . questions . size ( ( num_of_existed_questions + 1 ) .. ( num_of_existed_questions + params [ :question ] [ :total_num ] . to_i ) ) . each do | i | question = Object . const_get ( params [ :question ] [ :type ] ) . create ( txt : '' , questionnaire_id : questionnaire_id , seq : i , type : params [ :question ] [ :type ] , break_before : true ) if question . is_a? ScoredQuestion question . weight = 1 question . max_label = 'Strongly agree' question . min_label = 'Strongly disagree' end question . size = '50, 3' if question . is_a? Criterion question . alternatives = '0|1|2|3|4|5' if question . is_a? Dropdown question . size = '60, 5' if question . is_a? TextArea question . size = '30' if question . is_a? TextField begin question . save rescue StandardError flash [ :error ] = $ERROR_INFO end end redirect_to edit_questionnaire_path ( questionnaire_id . to_sym ) end
Zhewei: This method is used to add new questions when editing questionnaire.
Zhewei : This method is used to add new questions when editing questionnaire .