<commit_before>class Student < ActiveRecord::Base

  include PgSearch
  pg_search_scope :search_by_full_name, :against => [:surname, :name],
    :using => {
    :tsearch => {:prefix => true}
  }

  has_one :user, :as => :verifable, :dependent => :destroy, :class_name => "User"
  accepts_nested_attributes_for :user, :allow_destroy => true
  has_many :student_studies, :class_name => "StudentStudies", :dependent => :destroy
  accepts_nested_attributes_for :student_studies, :allow_destroy => true
  has_many :studies, :class_name => "Studies", :through => :student_studies
  has_many :courses, :through => :studies

  validates :index_number, uniqueness: true

  def ==(other)
    index_number == other.index_number
  end

  def self.include_courses
    includes(:student_studies => [:studies => [:course => :translations,
          :study_type => :translations, :study_degree => :translations]])
  end

  def self.search(query)
    if query.present?
      search_by_full_name(query)
    else
      self
    end
  end

  if defined?(Diamond)
    has_many :thesis_enrollments, :class_name => "Diamond::ThesisEnrollment", :dependent => :destroy
    has_many :theses, :class_name => "Diamond::Thesis", :dependent => :nullify, :through => :thesis_enrollments

    def self.not_enrolled
      student_ids = Student.select("DISTINCT #{Student.table_name}.id")
      .joins("LEFT JOIN #{Diamond::ThesisEnrollment.table_name} ON #{Diamond::ThesisEnrollment.table_name}.student_id = #{Student.table_name}.id ")
      .where("NOT EXISTS(SELECT id from #{Diamond::ThesisEnrollment.table_name}
WHERE #{Diamond::ThesisEnrollment.table_name}.student_id = #{Student.table_name}.id AND #{Diamond::ThesisEnrollment.table_name}.state = 'accepted')")
      student_ids |= Student
      .select("DISTINCT #{Student.table_name}.id")
      .joins("LEFT JOIN #{Diamond::ThesisEnrollment.table_name} ON #{Diamond::ThesisEnrollment.table_name}.student_id = #{Student.table_name}.id")
      .joins("LEFT JOIN #{StudentStudies.table_name} ON #{StudentStudies.table_name}.student_id = #{Student.table_name}.id")
      .where("(#{Diamond::ThesisEnrollment.table_name}.student_id = #{Student.table_name}.id AND #{Diamond::ThesisEnrollment.table_name}.state = 'accepted')
OR #{Diamond::ThesisEnrollment.table_name}.id IS NULL")
      .group("#{Student.table_name}.id")
      .having("count(DISTINCT #{Diamond::ThesisEnrollment.table_name}.id) < count(DISTINCT #{StudentStudies.table_name}.id)")
      Student.where(id: student_ids)
    end

    def is_authorized_for_thesis_enrollments?
      student_studies.any? do |student_studies|
        (student_studies.studies.second_degree? && student_studies.semester_number >= 1) ||
          (student_studies.studies.first_degree? && student_studies.semester_number >= 6)
      end
    end

    def enrolled_for_thesis?(thesis)
      thesis.enrollments.any? {|enrollment| enrollment.student_id == id }
    end

    def enrolled?
      thesis_enrollments.accepted.count == student_studies.count
    end

    def has_thesis_enrollment?(enrollment)
      thesis_enrollments.include?(enrollment)
    end
  end

  if defined?(Graphite)
    has_many :elective_enrollments, :class_name => "Graphite::ElectiveBlock::Enrollment",
      dependent: :destroy

    def elective_modules
      Graphite::ElectiveBlock.joins(:studies, :modules)
      .select("DISTINCT #{Graphite::ElectiveBlock.table_name}.*")
      .where("#{Studies.table_name}.id" => student_studies.pluck(:studies_id))
    end

    def any_pending_module_enrollments?
      elective_enrollments
      .select("1 AS ONE")
      .pending
      .present?<commit_msg>Extend student model with additional scopes<commit_after>class Student < ActiveRecord::Base

  include PgSearch
  pg_search_scope :search_by_full_name, :against => [:surname, :name],
    :using => {
    :tsearch => {:prefix => true}
  }

  has_one :user, :as => :verifable, :dependent => :destroy, :class_name => "User"
  accepts_nested_attributes_for :user, :allow_destroy => true
  has_many :student_studies, :class_name => "StudentStudies", :dependent => :destroy
  accepts_nested_attributes_for :student_studies, :allow_destroy => true
  has_many :studies, :class_name => "Studies", :through => :student_studies
  has_many :courses, :through => :studies

  validates :index_number, uniqueness: true

  def ==(other)
    index_number == other.index_number
  end

  def self.include_courses
    includes(:student_studies => [:studies => [:course => :translations,
          :study_type => :translations, :study_degree => :translations]])
  end

  def self.search(query)
    if query.present?
      search_by_full_name(query)
    else
      self
    end
  end

  if defined?(Diamond)
    has_many :thesis_enrollments, :class_name => "Diamond::ThesisEnrollment", :dependent => :destroy
    has_many :theses, :class_name => "Diamond::Thesis", :dependent => :nullify, :through => :thesis_enrollments

    def self.not_enrolled
      student_ids = Student.select("DISTINCT #{Student.table_name}.id")
      .joins("LEFT JOIN #{Diamond::ThesisEnrollment.table_name} ON #{Diamond::ThesisEnrollment.table_name}.student_id = #{Student.table_name}.id ")
      .where("NOT EXISTS(SELECT id from #{Diamond::ThesisEnrollment.table_name}
WHERE #{Diamond::ThesisEnrollment.table_name}.student_id = #{Student.table_name}.id AND #{Diamond::ThesisEnrollment.table_name}.state = 'accepted')")
      student_ids |= Student
      .select("DISTINCT #{Student.table_name}.id")
      .joins("LEFT JOIN #{Diamond::ThesisEnrollment.table_name} ON #{Diamond::ThesisEnrollment.table_name}.student_id = #{Student.table_name}.id")
      .joins("LEFT JOIN #{StudentStudies.table_name} ON #{StudentStudies.table_name}.student_id = #{Student.table_name}.id")
      .where("(#{Diamond::ThesisEnrollment.table_name}.student_id = #{Student.table_name}.id AND #{Diamond::ThesisEnrollment.table_name}.state = 'accepted')
OR #{Diamond::ThesisEnrollment.table_name}.id IS NULL")
      .group("#{Student.table_name}.id")
      .having("count(DISTINCT #{Diamond::ThesisEnrollment.table_name}.id) < count(DISTINCT #{StudentStudies.table_name}.id)")
      Student.where(id: student_ids)
    end

    def is_authorized_for_thesis_enrollments?
      student_studies.any? do |student_studies|
        (student_studies.studies.second_degree? && student_studies.semester_number >= 1) ||
          (student_studies.studies.first_degree? && student_studies.semester_number >= 6)
      end
    end

    def enrolled_for_thesis?(thesis)
      thesis.enrollments.any? {|enrollment| enrollment.student_id == id }
    end

    def enrolled?
      thesis_enrollments.accepted.count == student_studies.count
    end

    def has_thesis_enrollment?(enrollment)
      thesis_enrollments.include?(enrollment)
    end
  end

  if defined?(Graphite)
    has_many :elective_enrollments, :class_name => "Graphite::ElectiveBlock::Enrollment",
      dependent: :destroy

    scope :elective_blocks, ->(blocks) do
      joins(:elective_enrollments)
      .where("#{Graphite::ElectiveBlock::Enrollment.table_name}.block_id" => blocks)
    end
    scope :queued_blocks, -> do
      where("#{Graphite::ElectiveBlock::Enrollment.table_name}.state" => 'queued')
    end

    def elective_modules
      Graphite::ElectiveBlock.joins(:studies, :modules)
      .select("DISTINCT #{Graphite::ElectiveBlock.table_name}.*")
      .where("#{Studies.table_name}.id" => student_studies.pluck(:studies_id))
    end

    def any_pending_module_enrollments?
      elective_enrollments
      .select("1 AS ONE")
      .pending
      .present?
{'fraction_non_alphanumeric': 0.13359683794466404, 'fraction_numerical': 0.0007905138339920949, 'mean_word_length': 4.496741491672701, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 63, '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': '5681578', 'n_tokens_mistral': 2559, 'n_tokens_neox': 2487, 'n_words': 420}