<commit_before>class QuestionsController < ApplicationController
  respond_to :html
  after_action :verify_authorized

  #create a question
  def admin_create
    begin
      @question = Question.new(question_params)
      authorize @question
      @question.modifiable = true
      if @question.question_format.textfield?
        @question.default_value = params["question-default-value-textfield"]
      elsif @question.question_format.textarea?
        @question.default_value = params["question-default-value-textarea"]
      end
      if @question.save
        @question.section.phase.template.dirty = true
        @question.section.phase.template.save!
        if params[:example_answer].present?
          example_answer = Annotation.new({question_id: @question.id, org_id: current_user.org_id, text: params[:example_answer], type: Annotation.types[:example_answer]})
          example_answer.save
        end
        if params[:guidance].present?
          guidance = Annotation.new({question_id: @question.id, org_id: current_user.org_id, text: params[:guidance], type: Annotation.types[:guidance]})
          guidance.save
        end
        redirect_to admin_show_phase_path(id: @question.section.phase_id, section_id: @question.section_id, question_id: @question.id, edit: 'true'), notice: _('Information was successfully created.')
      else
        @edit = (@question.section.phase.template.org == current_user.org)
        @open = true
        @phase = @question.section.phase
        @section = @question.section
        @sections = @phase.sections
        @section_id = @question.section.id
        @question_id = @question.id

        flash[:notice] = failed_create_error(@question, _('question'))
        if @phase.template.customization_of.present?
          @original_org = Template.where(dmptemplate_id: @phase.template.customization_of).first.org
        else
          @original_org = @phase.template.org
        end
        render template: 'phases/admin_show'
      end
    rescue ActionController::ParameterMissing => e
      flash[:notice] = e.message
    end
  end

  #update a question of a template
  def admin_update
    @question = Question.find(params[:id])
    authorize @question
    guidance = @question.get_guidance_annotation(current_user.org_id)
    if params["question-guidance-#{params[:id]}"].present?
      if guidance.blank?
        guidance = @question.annotations.build
        guidance.type = :guidance
      end
      guidance.text = params["question-guidance-#{params[:id]}"]
      guidance.save
    end
    if @question.question_format.textfield?
      @question.default_value = params["question-default-value-textfield"]
    elsif @question.question_format.textarea?
      @question.default_value = params["question-default-value-textarea"]
    end
    @section = @question.section
    @phase = @section.phase
    template = @phase.template<commit_msg>fixed question update logic, addresses #460<commit_after>class QuestionsController < ApplicationController
  respond_to :html
  after_action :verify_authorized

  #create a question
  def admin_create
    begin
      @question = Question.new(question_params)
      authorize @question
      @question.modifiable = true
      if @question.question_format.textfield?
        @question.default_value = params["question-default-value-textfield"]
      elsif @question.question_format.textarea?
        @question.default_value = params["question-default-value-textarea"]
      end
      if @question.save
        @question.section.phase.template.dirty = true
        @question.section.phase.template.save!
        if params[:example_answer].present?
          example_answer = Annotation.new({question_id: @question.id, org_id: current_user.org_id, text: params[:example_answer], type: Annotation.types[:example_answer]})
          example_answer.save
        end
        if params[:guidance].present?
          guidance = Annotation.new({question_id: @question.id, org_id: current_user.org_id, text: params[:guidance], type: Annotation.types[:guidance]})
          guidance.save
        end
        redirect_to admin_show_phase_path(id: @question.section.phase_id, section_id: @question.section_id, question_id: @question.id, edit: 'true'), notice: _('Information was successfully created.')
      else
        @edit = (@question.section.phase.template.org == current_user.org)
        @open = true
        @phase = @question.section.phase
        @section = @question.section
        @sections = @phase.sections
        @section_id = @question.section.id
        @question_id = @question.id

        flash[:notice] = failed_create_error(@question, _('question'))
        if @phase.template.customization_of.present?
          @original_org = Template.where(dmptemplate_id: @phase.template.customization_of).first.org
        else
          @original_org = @phase.template.org
        end
        render template: 'phases/admin_show'
      end
    rescue ActionController::ParameterMissing => e
      flash[:notice] = e.message
    end
  end

  #update a question of a template
  def admin_update
    @question = Question.find(params[:id])
    authorize @question
    guidance = @question.get_guidance_annotation(current_user.org_id)
    if params["question-guidance-#{params[:id]}"].present?
      if guidance.blank?
        guidance = @question.annotations.build
        guidance.type = :guidance
        guidance.org_id = current_user.org_id
      end
      guidance.text = params["question-guidance-#{params[:id]}"]
      guidance.save
    end
    example_answer = @question.get_example_answer(current_user.org_id)
    if params["question"]["annotations_attributes"]["0"]["id"].present?
      if example_answer.blank?
        example_answer = @question.annotations.build
        example_answer.type = :example_answer
        example_answer.org_id = current_user.org_id
      end
      example_answer.text = params["question"]["annotations_attributes"]["0"]["text"]
      example_answer.save
    end
    if @question.question_format.textfield?
      @question.default_value = params["question-default-value-textfield"]
    elsif @question.question_format.textarea?
      @question.default_value = params["question-default-value-textarea"]
    end
    @section = @question.section
    @phase = @section.phase
    template = @phase.template
{'fraction_non_alphanumeric': 0.10909664196752325, 'fraction_numerical': 0.0007882705344474224, 'mean_word_length': 3.25201072386059, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3505168', 'n_tokens_mistral': 1776, 'n_tokens_neox': 1794, 'n_words': 331}