<commit_before>class ResponseController < ApplicationController
  helper :submitted_content
  helper :file

  def action_allowed?
    case params[:action]
      # Deny access to anyone except reviewer & author's team
      when 'edit'  # If response has been submitted, no further editing allowed
        response = Response.find(params[:id])
        current_user_id?(response.map.reviewer.user_id)
        if (response.is_submitted)
          return false
        end
      when 'delete','update'
        response = Response.find(params[:id])
        current_user_id?(response.map.reviewer.user_id)
      when 'view'
        response = Response.find(params[:id])
        map = response.map

        # if it is a review response map, all the members of revieweee team should be able to view the reponse (can be done from heat map)
        if map.is_a? ReviewResponseMap
          reviewee_team = AssignmentTeam.find(map.reviewee_id)
          current_user_id?(response.map.reviewer.user_id) || reviewee_team.has_user(current_user) || (['Administrator','Instructor','Teaching Assistant'].include? current_user.role.name)
        else
          current_user_id?(response.map.reviewer.user_id)
        end
      else
        current_user
    end
  end

  def scores
    @review_scores = []
    @questions.each do |question|
      @review_scores << Answer.where(
          response_id: @response.id,
          question_id:  question.id
        ).first
    end
  end

  def delete
    @response = Response.find(params[:id])
    #user cannot delete other people's responses. Needs to be authenticated.
    map_id = @response.map.id
    @response.delete
    redirect_to :action => 'redirection', :id => map_id, :return => params[:return], :msg => "The response was deleted."
  end

  #Determining the current phase and check if a review is already existing for this stage.
  #If so, edit that version otherwise create a new version.


  #Prepare the parameters when student clicks "Edit"
  def edit
    @header = "Edit"
    @next_action = "update"
    @return = params[:return]
    @response = Response.find(params[:id])

    @map = @response.map
    @contributor = @map.contributor
    set_all_responses
    if @prev.present?
      @sorted=@review_scores.sort { |m1, m2| (m1.version_num and m2.version_num) ? m2.version_num <=> m1.version_num : (m1.version_num ? -1 : 1) }
      @largest_version_num=@sorted[0]
    end

    @modified_object = @response.response_id

    # set more handy variables for the view
    set_content

    @review_scores = Array.new

    @questions.each do |question|
      @review_scores << Answer.where(response_id: @response.response_id, question_id:  question.id).first
    end
    render :action => 'response'
  end

  #Update the response and answers when student "edit" existing response
  def update
    return unless action_allowed?

    # the response to be updated
    @response = Response.find(params[:id])

    msg = ""
    begin
      @map = @response.map
      @response.update_attribute('additional_comment', params[:review][:comments])
      if @map.type=="ReviewResponseMap" && @response.round
        @questionnaire = @map.questionnaire(@response.round)
      elsif @map.type=="ReviewResponseMap"
        @questionnaire = @map.questionnaire(nil)
      else
        @questionnaire = @map.questionnaire
      end
      questions = @questionnaire.questions.sort { |a,b| a.seq <=> b.seq }


       questions=sort_questions(@questionnaire.questions)
       create_answers(params,questions)
      questions = @questionnaire.questions.sort { |a,b| a.seq <=> b.seq }

      if !params[:responses].nil? # for some rubrics, there might be no questions but only file submission (Dr. Ayala's rubric)
        params[:responses].each_pair do |k, v|
          score = Answer.where(response_id: @response.id, question_id:  questions[k.to_i].id).first
          unless score
            score = Answer.create(:response_id => @response.id, :question_id => questions[k.to_i].id, :answer => v[:score], :comments => v[:comment])
          end
          score.update_attribute('answer', v[:score])
          score.update_attribute('comments', v[:comment])
        end
      end
      if (params['isSubmit'] && (params['isSubmit'].eql?'Yes'))
        # Update the submission flag.
        @response.update_attribute('is_submitted',true)
      else
        @response.update_attribute('is_submitted',false)
      end
    rescue
      msg = "Your response was not saved. Cause:189 #{$!}"
    end
    redirect_to :controller => 'response', :action => 'saving', :id => @map.map_id, :return => params[:return], :msg => msg, :save_options => params[:save_options]
  end

  def new
    @header = "New"
    @next_action = "create"
    @feedback = params[:feedback]
    @map = ResponseMap.find(params[:id])
    @return = params[:return]
    @modified_object = @map.id

    # set more handy variables for the view
    set_content(true)

    @stage = @assignment.get_current_stage(SignedUpTeam.topic_id(@participant.parent_id, @participant.user_id))
    render :action => 'response'
  end

  def new_feedback
    review = Response.find(params[:id])
    if review
      reviewer = AssignmentParticipant.where(user_id: session[:user].id, parent_id:  review.map.assignment.id).first
      map = FeedbackResponseMap.where(reviewed_object_id: review.id, reviewer_id:  reviewer.id).first
      if map.nil?
        #if no feedback exists by dat user den only create for dat particular response/review
        map = FeedbackResponseMap.create(:reviewed_object_id => review.id, :reviewer_id => reviewer.id, :reviewee_id => review.map.reviewer.id)
      end
      redirect_to :action => 'new', :id => map.id, :return => "feedback"
    else
      redirect_to :back
    end
  end

  #view response
  def view
    @response = Response.find(params[:id])
    @map = @response.map
    set_content

  end

  def create
    @map = ResponseMap.find(params[:id]) #assignment/review/metareview id is in params id

    set_all_responses

    #to save the response for ReviewResponseMap, a questionnaire_id is wrapped in the params
    if params[:review][:questionnaire_id]
      @questionnaire = Questionnaire.find(params[:review][:questionnaire_id])
      @round = params[:review][:round]
    else
      @round=nil
    end

    # create the response
    if params[:isSubmit].eql?('Yes')
      is_submitted = true
    else
      is_submitted = false
    end
    @response = Response.create(:map_id => @map.id, :additional_comment => params[:review][:comments],:round => @round, :is_submitted => is_submitted)#,:version_num=>@version)

    #Change the order for displaying questions for editing response views.
    questions=sort_questions(@questionnaire.questions)

    if params[:responses]
       create_answers(params, questions)
    end

    #@map.save
    msg = "Your response was successfully saved."
    @response.email();
    redirect_to :controller => 'response', :action => 'saving', :id => @map.map_id, :return => params[:return], :msg => msg, :error_msg => error_msg, :save_options => params[:save_options]
  end

  def saving
    @map = ResponseMap.find(params[:id])
    @return = params[:return]<commit_msg>Add error_msg variable<commit_after>class ResponseController < ApplicationController
  helper :submitted_content
  helper :file

  def action_allowed?
    case params[:action]
      # Deny access to anyone except reviewer & author's team
      when 'edit'  # If response has been submitted, no further editing allowed
        response = Response.find(params[:id])
        current_user_id?(response.map.reviewer.user_id)
        if (response.is_submitted)
          return false
        end
      when 'delete','update'
        response = Response.find(params[:id])
        current_user_id?(response.map.reviewer.user_id)
      when 'view'
        response = Response.find(params[:id])
        map = response.map

        # if it is a review response map, all the members of revieweee team should be able to view the reponse (can be done from heat map)
        if map.is_a? ReviewResponseMap
          reviewee_team = AssignmentTeam.find(map.reviewee_id)
          current_user_id?(response.map.reviewer.user_id) || reviewee_team.has_user(current_user) || (['Administrator','Instructor','Teaching Assistant'].include? current_user.role.name)
        else
          current_user_id?(response.map.reviewer.user_id)
        end
      else
        current_user
    end
  end

  def scores
    @review_scores = []
    @questions.each do |question|
      @review_scores << Answer.where(
          response_id: @response.id,
          question_id:  question.id
        ).first
    end
  end

  def delete
    @response = Response.find(params[:id])
    #user cannot delete other people's responses. Needs to be authenticated.
    map_id = @response.map.id
    @response.delete
    redirect_to :action => 'redirection', :id => map_id, :return => params[:return], :msg => "The response was deleted."
  end

  #Determining the current phase and check if a review is already existing for this stage.
  #If so, edit that version otherwise create a new version.


  #Prepare the parameters when student clicks "Edit"
  def edit
    @header = "Edit"
    @next_action = "update"
    @return = params[:return]
    @response = Response.find(params[:id])

    @map = @response.map
    @contributor = @map.contributor
    set_all_responses
    if @prev.present?
      @sorted=@review_scores.sort { |m1, m2| (m1.version_num and m2.version_num) ? m2.version_num <=> m1.version_num : (m1.version_num ? -1 : 1) }
      @largest_version_num=@sorted[0]
    end

    @modified_object = @response.response_id

    # set more handy variables for the view
    set_content

    @review_scores = Array.new

    @questions.each do |question|
      @review_scores << Answer.where(response_id: @response.response_id, question_id:  question.id).first
    end
    render :action => 'response'
  end

  #Update the response and answers when student "edit" existing response
  def update
    return unless action_allowed?

    # the response to be updated
    @response = Response.find(params[:id])

    msg = ""
    begin
      @map = @response.map
      @response.update_attribute('additional_comment', params[:review][:comments])
      if @map.type=="ReviewResponseMap" && @response.round
        @questionnaire = @map.questionnaire(@response.round)
      elsif @map.type=="ReviewResponseMap"
        @questionnaire = @map.questionnaire(nil)
      else
        @questionnaire = @map.questionnaire
      end
      questions = @questionnaire.questions.sort { |a,b| a.seq <=> b.seq }


       questions=sort_questions(@questionnaire.questions)
       create_answers(params,questions)
      questions = @questionnaire.questions.sort { |a,b| a.seq <=> b.seq }

      if !params[:responses].nil? # for some rubrics, there might be no questions but only file submission (Dr. Ayala's rubric)
        params[:responses].each_pair do |k, v|
          score = Answer.where(response_id: @response.id, question_id:  questions[k.to_i].id).first
          unless score
            score = Answer.create(:response_id => @response.id, :question_id => questions[k.to_i].id, :answer => v[:score], :comments => v[:comment])
          end
          score.update_attribute('answer', v[:score])
          score.update_attribute('comments', v[:comment])
        end
      end
      if (params['isSubmit'] && (params['isSubmit'].eql?'Yes'))
        # Update the submission flag.
        @response.update_attribute('is_submitted',true)
      else
        @response.update_attribute('is_submitted',false)
      end
    rescue
      msg = "Your response was not saved. Cause:189 #{$!}"
    end
    redirect_to :controller => 'response', :action => 'saving', :id => @map.map_id, :return => params[:return], :msg => msg, :save_options => params[:save_options]
  end

  def new
    @header = "New"
    @next_action = "create"
    @feedback = params[:feedback]
    @map = ResponseMap.find(params[:id])
    @return = params[:return]
    @modified_object = @map.id

    # set more handy variables for the view
    set_content(true)

    @stage = @assignment.get_current_stage(SignedUpTeam.topic_id(@participant.parent_id, @participant.user_id))
    render :action => 'response'
  end

  def new_feedback
    review = Response.find(params[:id])
    if review
      reviewer = AssignmentParticipant.where(user_id: session[:user].id, parent_id:  review.map.assignment.id).first
      map = FeedbackResponseMap.where(reviewed_object_id: review.id, reviewer_id:  reviewer.id).first
      if map.nil?
        #if no feedback exists by dat user den only create for dat particular response/review
        map = FeedbackResponseMap.create(:reviewed_object_id => review.id, :reviewer_id => reviewer.id, :reviewee_id => review.map.reviewer.id)
      end
      redirect_to :action => 'new', :id => map.id, :return => "feedback"
    else
      redirect_to :back
    end
  end

  #view response
  def view
    @response = Response.find(params[:id])
    @map = @response.map
    set_content

  end

  def create
    @map = ResponseMap.find(params[:id]) #assignment/review/metareview id is in params id

    set_all_responses

    #to save the response for ReviewResponseMap, a questionnaire_id is wrapped in the params
    if params[:review][:questionnaire_id]
      @questionnaire = Questionnaire.find(params[:review][:questionnaire_id])
      @round = params[:review][:round]
    else
      @round=nil
    end

    # create the response
    if params[:isSubmit].eql?('Yes')
      is_submitted = true
    else
      is_submitted = false
    end
    @response = Response.create(:map_id => @map.id, :additional_comment => params[:review][:comments],:round => @round, :is_submitted => is_submitted)#,:version_num=>@version)

    #Change the order for displaying questions for editing response views.
    questions=sort_questions(@questionnaire.questions)

    if params[:responses]
       create_answers(params, questions)
    end

    #@map.save
    msg = "Your response was successfully saved."
    error_msg="Error"
    @response.email();
    redirect_to :controller => 'response', :action => 'saving', :id => @map.map_id, :return => params[:return], :msg => msg, :error_msg => error_msg, :save_options => params[:save_options]
  end

  def saving
    @map = ResponseMap.find(params[:id])
    @return = params[:return]
{'fraction_non_alphanumeric': 0.11631451333564254, 'fraction_numerical': 0.0018011776931070316, 'mean_word_length': 3.2062937062937062, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 77, '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': '7376035', 'n_tokens_mistral': 4444, 'n_tokens_neox': 4283, 'n_words': 1092}