<commit_before>module SubmissionsHelper

  def find_appropriate_grouping(assignment_id, params)
    if current_user.admin? || current_user.ta?
      Grouping.find(params[:grouping_id])
    else
      current_user.accepted_grouping_for(assignment_id)
    end
  end

  def set_release_on_results(groupings, release, errors)
    changed = 0
    groupings.each do |grouping|
      begin
        raise I18n.t('marking_state.no_submission', group_name: grouping.group.group_name) if !grouping.has_submission?
        submission = grouping.current_submission_used
        raise I18n.t('marking_state.no_result', group_name: grouping.group.group_name) if !submission.has_result?
        raise I18n.t('marking_state.not_complete', group_name: grouping.group.group_name) if
          submission.get_latest_result.marking_state != Result::MARKING_STATES[:complete] && release
        raise I18n.t('marking_state.not_complete_unrelease', group_name: grouping.group.group_name) if
          submission.get_latest_result.marking_state != Result::MARKING_STATES[:complete]
        @result = submission.get_latest_result
        @result.released_to_students = release
        unless @result.save
          raise I18n.t('marking_state.result_not_saved', group_name: grouping.group.group_name)
        end
        changed += 1
      rescue Exception => e
        errors.push(e.message)
      end
    end
    changed
  end

  def get_submissions_table_info(assignment)
    groupings = assignment.groupings
    #(.include:
                                #[:assignment, :group, :grace_period_deductions,
                                  #{current_submission_used: :results},
                                  #{accepted_student_memberships: :user}])
    submissions_table_info = groupings.map do |grouping|
      g = grouping.attributes

      g[:class_name] = get_any_tr_attributes(grouping)
      g[:group_name] = get_grouping_group_name(assignment, grouping)
      g[:repository] = get_grouping_repository(assignment, grouping)
      g[:commit_date] = get_grouping_commit_date(assignment, grouping)
      g[:marking_state] = get_grouping_marking_state(assignment, grouping)
      g[:grace_credits_used] = get_grouping_grace_credits_used(grouping)
      g[:final_grade] = get_grouping_final_grades(grouping)
      g[:can_begin_grading] = get_grouping_can_begin_grading(assignment, grouping)
      
      g
    end

    return submissions_table_info
  end

  # If the grouping is collected or has an error, 
  # style the table row green or red respectively.
  # Classname will be applied to the table row
  # and actually styled in CSS.
  def get_any_tr_attributes(grouping)
    if grouping.is_collected?
      return 'submission_collected'
    elsif grouping.error_collecting
      return 'submission_error'
    else
      return nil
    end
  end<commit_msg>Reinstate .includes when getting groupings<commit_after>module SubmissionsHelper

  def find_appropriate_grouping(assignment_id, params)
    if current_user.admin? || current_user.ta?
      Grouping.find(params[:grouping_id])
    else
      current_user.accepted_grouping_for(assignment_id)
    end
  end

  def set_release_on_results(groupings, release, errors)
    changed = 0
    groupings.each do |grouping|
      begin
        raise I18n.t('marking_state.no_submission', group_name: grouping.group.group_name) if !grouping.has_submission?
        submission = grouping.current_submission_used
        raise I18n.t('marking_state.no_result', group_name: grouping.group.group_name) if !submission.has_result?
        raise I18n.t('marking_state.not_complete', group_name: grouping.group.group_name) if
          submission.get_latest_result.marking_state != Result::MARKING_STATES[:complete] && release
        raise I18n.t('marking_state.not_complete_unrelease', group_name: grouping.group.group_name) if
          submission.get_latest_result.marking_state != Result::MARKING_STATES[:complete]
        @result = submission.get_latest_result
        @result.released_to_students = release
        unless @result.save
          raise I18n.t('marking_state.result_not_saved', group_name: grouping.group.group_name)
        end
        changed += 1
      rescue Exception => e
        errors.push(e.message)
      end
    end
    changed
  end

  def get_submissions_table_info(assignment)
    groupings = assignment.groupings.includes(:assignment,
                                              :group,
                                              :grace_period_deductions,
                                              {current_submission_used: :results},
                                              {accepted_student_memberships: :user})
    submissions_table_info = groupings.map do |grouping|
      g = grouping.attributes

      g[:class_name] = get_any_tr_attributes(grouping)
      g[:group_name] = get_grouping_group_name(assignment, grouping)
      g[:repository] = get_grouping_repository(assignment, grouping)
      g[:commit_date] = get_grouping_commit_date(assignment, grouping)
      g[:marking_state] = get_grouping_marking_state(assignment, grouping)
      g[:grace_credits_used] = get_grouping_grace_credits_used(grouping)
      g[:final_grade] = get_grouping_final_grades(grouping)
      g[:can_begin_grading] = get_grouping_can_begin_grading(assignment, grouping)
      
      g
    end

    return submissions_table_info
  end

  # If the grouping is collected or has an error, 
  # style the table row green or red respectively.
  # Classname will be applied to the table row
  # and actually styled in CSS.
  def get_any_tr_attributes(grouping)
    if grouping.is_collected?
      return 'submission_collected'
    elsif grouping.error_collecting
      return 'submission_error'
    else
      return nil
    end
  end
{'fraction_non_alphanumeric': 0.07741489545533091, 'fraction_numerical': 0.00414722654224987, 'mean_word_length': 3.1283880171184024, 'pattern_counts': {'":': 0, '<': 3, '<?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': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4532762', 'n_tokens_mistral': 1790, 'n_tokens_neox': 1725, 'n_words': 325}