<commit_before>class GradesController < ApplicationController
  respond_to :html, :json
  before_filter :set_assignment, only: [:show, :edit, :update, :destroy, :submit_rubric]
  before_filter :ensure_staff?, :except => [:self_log, :show, :predict_score]
  before_filter :ensure_student?, only: [:predict_score]

  def show
    @assignment = current_course.assignments.find(params[:assignment_id])   
    if current_user_is_student?
      redirect_to @assignment
    end
    if @assignment.has_groups?
      @group = @assignment.groups.find(params[:group_id])
      @title = "#{@group.name}'s Grade for #{ @assignment.name }"
      @grades_for_assignment = @assignment.grades.graded_or_released
    else
      @title = "#{current_student.name}'s Grade for #{ @assignment.name }"
      @grades_for_assignment = @assignment.grades_for_assignment(current_student)
    end
  end

  def edit
    session[:return_to] = request.referer
    redirect_to @assignment and return unless current_student.present?
    @grade = Grade.where(student_id: current_student[:id], assignment_id: @assignment[:id]).first
    @title = "Editing #{current_student.name}'s Grade for #{@assignment.name}"
    @rubric = @assignment.rubric
    @rubric_grades = serialized_rubric_grades
    @metrics = existing_metrics_as_json if @rubric
    @course_badges = serialized_course_badges
    @assignment_score_levels = @assignment.assignment_score_levels.order_by_value
  end

  private

  def serialized_rubric_grades
    ActiveModel::ArraySerializer.new(fetch_rubric_grades, each_serializer: ExistingRubricGradesSerializer).to_json
  end

  def fetch_rubric_grades
    RubricGrade.where(fetch_rubric_grades_params)
  end

  def fetch_rubric_grades_params
    { student_id: params[:student_id], assignment_id: params[:assignment_id], metric_id: existing_metric_ids }
  end

  def existing_metric_ids
    rubric_metrics_with_tiers.collect {|metric| metric[:id] }
  end

  public

  def update
    redirect_to @assignment and return unless current_student.present?
    @grade = current_student_data.grade_for_assignment(@assignment)
    self.check_uploads

    @grade.update_attributes params[:grade].merge(instructor_modified: true)

    GradeUpdater.perform_async([@grade.id]) if @grade.graded_or_released?

    if session[:return_to].present?
      redirect_to session[:return_to]
    else
      redirect_to @assignment
    end
  end

  def submit_rubric
    if @submission = Submission.where(current_assignment_and_student_ids).first
      @submission.update_attributes(graded: true)
    end

    @grade = Grade.where(assignment_id: @assignment[:id], student_id: params[:student_id]).first
    if @grade
      @grade.update_attributes grade_attributes_from_rubric
    else
      @grade = Grade.create(new_grade_from_rubric_grades_attributes)
    end

    delete_existing_rubric_grades if rubric_grades_exist? # destroy rubric grades where assignment_id and student_id match
    create_rubric_grades # create an individual record for each rubric grade

    delete_existing_earned_badges_for_metrics # if earned_badges_exist? # destroy earned_badges where assignment_id and student_id match
    create_earned_tier_badges # create_earned_tier_badges 

    # need to create an array of tier_ids
    # get tier_badges for those ids
    # delete previous earned badges associated with the grade
    # (need to create grade_id on EarnedBadge)
    # create new rubric_grades for that

    GradeUpdater.perform_async([@grade.id]) if @grade.graded_or_released?

    render status: 200, json: {}
  end
<commit_msg>AJ-2546 pull grade from student data rather than directly from db<commit_after>class GradesController < ApplicationController
  respond_to :html, :json
  before_filter :set_assignment, only: [:show, :edit, :update, :destroy, :submit_rubric]
  before_filter :ensure_staff?, :except => [:self_log, :show, :predict_score]
  before_filter :ensure_student?, only: [:predict_score]

  def show
    @assignment = current_course.assignments.find(params[:assignment_id])   
    if current_user_is_student?
      redirect_to @assignment
    end
    if @assignment.has_groups?
      @group = @assignment.groups.find(params[:group_id])
      @title = "#{@group.name}'s Grade for #{ @assignment.name }"
      @grades_for_assignment = @assignment.grades.graded_or_released
    else
      @title = "#{current_student.name}'s Grade for #{ @assignment.name }"
      @grades_for_assignment = @assignment.grades_for_assignment(current_student)
    end
  end

  def edit
    session[:return_to] = request.referer
    redirect_to @assignment and return unless current_student.present?
    # @grade = Grade.where(student_id: current_student[:id], assignment_id: @assignment[:id]).first
    @grade = current_student_data.grade_for_assignment(@assignment)
    @title = "Editing #{current_student.name}'s Grade for #{@assignment.name}"
    @rubric = @assignment.rubric
    @rubric_grades = serialized_rubric_grades
    @metrics = existing_metrics_as_json if @rubric
    @course_badges = serialized_course_badges
    @assignment_score_levels = @assignment.assignment_score_levels.order_by_value
  end

  private

  def serialized_rubric_grades
    ActiveModel::ArraySerializer.new(fetch_rubric_grades, each_serializer: ExistingRubricGradesSerializer).to_json
  end

  def fetch_rubric_grades
    RubricGrade.where(fetch_rubric_grades_params)
  end

  def fetch_rubric_grades_params
    { student_id: params[:student_id], assignment_id: params[:assignment_id], metric_id: existing_metric_ids }
  end

  def existing_metric_ids
    rubric_metrics_with_tiers.collect {|metric| metric[:id] }
  end

  public

  def update
    redirect_to @assignment and return unless current_student.present?
    @grade = current_student_data.grade_for_assignment(@assignment)
    self.check_uploads

    @grade.update_attributes params[:grade].merge(instructor_modified: true)

    GradeUpdater.perform_async([@grade.id]) if @grade.graded_or_released?

    if session[:return_to].present?
      redirect_to session[:return_to]
    else
      redirect_to @assignment
    end
  end

  def submit_rubric
    if @submission = Submission.where(current_assignment_and_student_ids).first
      @submission.update_attributes(graded: true)
    end

    # @grade = Grade.where(assignment_id: @assignment[:id], student_id: params[:student_id]).first
    # @grade = Grade.where(student_id: current_student[:id], assignment_id: @assignment[:id]).first
    @grade = current_student_data.grade_for_assignment(@assignment)

    if @grade
      @grade.update_attributes grade_attributes_from_rubric
    else
      @grade = Grade.create(new_grade_from_rubric_grades_attributes)
    end

    delete_existing_rubric_grades if rubric_grades_exist? # destroy rubric grades where assignment_id and student_id match
    create_rubric_grades # create an individual record for each rubric grade

    delete_existing_earned_badges_for_metrics # if earned_badges_exist? # destroy earned_badges where assignment_id and student_id match
    create_earned_tier_badges # create_earned_tier_badges 

    # need to create an array of tier_ids
    # get tier_badges for those ids
    # delete previous earned badges associated with the grade
    # (need to create grade_id on EarnedBadge)
    # create new rubric_grades for that

    GradeUpdater.perform_async([@grade.id]) if @grade.graded_or_released?

    render status: 200, json: {}
  end

{'fraction_non_alphanumeric': 0.08312037659717553, 'fraction_numerical': 0.0013449899125756557, 'mean_word_length': 4.755417956656347, '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': '4106275', 'n_tokens_mistral': 2343, 'n_tokens_neox': 2278, 'n_words': 485}