<commit_before># frozen_string_literal: true
class Course::Assessment::Submission::SubmissionsController < \
  Course::Assessment::Submission::Controller
  include Course::Assessment::Submission::SubmissionsControllerServiceConcern

  before_action :authorize_assessment!, only: :create
  skip_authorize_resource :submission, only: [:edit, :update, :auto_grade]
  before_action :authorize_submission!, only: [:edit, :update]
  before_action :check_password, only: [:edit, :update]
  before_action :load_or_create_answers, only: [:edit, :update]
  before_action :check_zombie_jobs, only: [:edit]
  # Questions may be added to assessments with existing submissions.
  # In these cases, new submission_questions must be created when the submission is next
  # edited or updated.
  before_action :load_or_create_submission_questions, only: [:edit, :update]

  delegate_to_service(:update)
  delegate_to_service(:submit_answer)
  delegate_to_service(:load_or_create_answers)
  delegate_to_service(:load_or_create_submission_questions)

  def index
    authorize!(:manage, @assessment)

    respond_to do |format|
      format.html {}
      format.json do
        @assessment = @assessment.calculated(:maximum_grade)
        @submissions = @submissions.calculated(:log_count).includes(:answers)
        @my_students = current_course_user&.my_students || []
        @course_students = current_course.course_users.students.order_alphabetically
      end
    end
  end

  def create
    @submission.session_id = authentication_service.generate_authentication_token!
    if @submission.save
      log_service.log_submission_access(request) if @assessment.password_protected?
      redirect_to edit_course_assessment_submission_path(current_course, @assessment, @submission,
                                                         new_submission: true)
    else
      redirect_to course_assessments_path(current_course),
                  danger: t('.failure', error: @submission.errors.full_messages.to_sentence)
    end
  end

  def edit
    return if @submission.attempting?

    respond_to do |format|
      format.html {}
      format.json do
        calculated_fields = [:graded_at]
        @submission = @submission.calculated(*calculated_fields)
      end
    end
  end

  def auto_grade<commit_msg>Fix n+1 query on submissions index page<commit_after># frozen_string_literal: true
class Course::Assessment::Submission::SubmissionsController < \
  Course::Assessment::Submission::Controller
  include Course::Assessment::Submission::SubmissionsControllerServiceConcern

  before_action :authorize_assessment!, only: :create
  skip_authorize_resource :submission, only: [:edit, :update, :auto_grade]
  before_action :authorize_submission!, only: [:edit, :update]
  before_action :check_password, only: [:edit, :update]
  before_action :load_or_create_answers, only: [:edit, :update]
  before_action :check_zombie_jobs, only: [:edit]
  # Questions may be added to assessments with existing submissions.
  # In these cases, new submission_questions must be created when the submission is next
  # edited or updated.
  before_action :load_or_create_submission_questions, only: [:edit, :update]

  delegate_to_service(:update)
  delegate_to_service(:submit_answer)
  delegate_to_service(:load_or_create_answers)
  delegate_to_service(:load_or_create_submission_questions)

  def index
    authorize!(:manage, @assessment)

    respond_to do |format|
      format.html {}
      format.json do
        @assessment = @assessment.calculated(:maximum_grade)
        @submissions = @submissions.calculated(:log_count, :graded_at).includes(:answers)
        @my_students = current_course_user&.my_students || []
        @course_students = current_course.course_users.students.order_alphabetically
      end
    end
  end

  def create
    @submission.session_id = authentication_service.generate_authentication_token!
    if @submission.save
      log_service.log_submission_access(request) if @assessment.password_protected?
      redirect_to edit_course_assessment_submission_path(current_course, @assessment, @submission,
                                                         new_submission: true)
    else
      redirect_to course_assessments_path(current_course),
                  danger: t('.failure', error: @submission.errors.full_messages.to_sentence)
    end
  end

  def edit
    return if @submission.attempting?

    respond_to do |format|
      format.html {}
      format.json do
        calculated_fields = [:graded_at]
        @submission = @submission.calculated(*calculated_fields)
      end
    end
  end

  def auto_grade
{'fraction_non_alphanumeric': 0.08527633851468049, 'fraction_numerical': 0.0002158894645941278, 'mean_word_length': 4.136363636363637, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6001194', 'n_tokens_mistral': 1407, 'n_tokens_neox': 1341, 'n_words': 278}