<commit_before>class User < ActiveRecord::Base
  acts_as_authentic do |config|
    config.validates_uniqueness_of_email_field_options = {if: -> { false }} # Don't validate email uniqueness
    config.crypto_provider = Authlogic::CryptoProviders::Sha1
    Authlogic::CryptoProviders::Sha1.join_token = ''
    Authlogic::CryptoProviders::Sha1.stretches = 1
  end

  has_many :participants, class_name: 'Participant', foreign_key: 'user_id', dependent: :destroy
  has_many :assignment_participants, class_name: 'AssignmentParticipant', foreign_key: 'user_id', dependent: :destroy
  has_many :assignments, through: :participants
  has_many :bids, dependent: :destroy

  has_many :teams_users, dependent: :destroy
  has_many :teams, through: :teams_users
  has_many :sent_invitations, class_name: 'Invitation', foreign_key: 'from_id', dependent: :destroy
  has_many :received_invitations, class_name: 'Invitation', foreign_key: 'to_id', dependent: :destroy

  has_many :children, class_name: 'User', foreign_key: 'parent_id'
  belongs_to :parent, class_name: 'User'
  belongs_to :role

  validates_presence_of :name
  validates_uniqueness_of :name

  validates_presence_of :email, message: "can't be blank"
  validates_format_of :email, with: /\A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z/i, allow_blank: true

  before_validation :randomize_password, if: ->(user) { user.new_record? && user.password.blank? } # AuthLogic
  after_create :email_welcome

  scope :superadministrators, -> { where role_id: Role.superadministrator }
  scope :superadmins, -> { superadministrators }
  scope :administrators, -> { where role_id: Role.administrator }
  scope :admins, -> { administrators }
  scope :instructors, -> { where role_id: Role.instructor }
  scope :tas, -> { where role_id: Role.ta }
  scope :students, -> { where role_id: Role.student }

  has_paper_trail

  def salt_first?
    true
  end

  def list_mine(object_type, user_id)
    object_type.where(["instructor_id = ?", user_id])
  end

  def get_available_users(name)
    lesser_roles = role.get_parents
    all_users = User.all(conditions: ['name LIKE ?', "#{name}%"], limit: 20) # higher limit, since we're filtering
    visible_users = all_users.select {|user| lesser_roles.include? user.role }
    visible_users[0, 10] # the first 10
  end

  def can_impersonate?(user)
    return true if self.role.super_admin?
    return true if self.is_teaching_assistant_for?(user)
    return true if self.is_recursively_parent_of(user)
    false
  end

  def is_recursively_parent_of(user)
    p = user.parent
    return false if p.nil?
    return true if p == self
    return false if p.role.super_admin?
    self.is_recursively_parent_of(p)
  end

  def get_user_list(a, b)
    user_list = []
    # If the user is a super admin, fetch all users
    if self.role.super_admin?
      User.all.find_each do |user|
        user_list << user
      end
    end

    # If the user is an instructor, fetch all users in his course/assignment
    if self.role.instructor? || self.role.ta?
      User.all.find_each do |user|
        if self.can_impersonate?(user)
          user_list << user
        end
      end
    end

    if b.nil?
      user_list = user_list.paginate(page: a, per_page: 25)
    elsif b == "All"
        user_list
    else
      user_list = user_list.paginate(page: a, per_page: b)
    end
  end

  def first_name
    fullname.try(:[], /,.+/).try(:[], /\w+/) || ''
  end

  def super_admin?
    role.name == 'Super-Administrator'<commit_msg>Changed page number & per page variable to more meaningful name<commit_after>class User < ActiveRecord::Base
  acts_as_authentic do |config|
    config.validates_uniqueness_of_email_field_options = {if: -> { false }} # Don't validate email uniqueness
    config.crypto_provider = Authlogic::CryptoProviders::Sha1
    Authlogic::CryptoProviders::Sha1.join_token = ''
    Authlogic::CryptoProviders::Sha1.stretches = 1
  end

  has_many :participants, class_name: 'Participant', foreign_key: 'user_id', dependent: :destroy
  has_many :assignment_participants, class_name: 'AssignmentParticipant', foreign_key: 'user_id', dependent: :destroy
  has_many :assignments, through: :participants
  has_many :bids, dependent: :destroy

  has_many :teams_users, dependent: :destroy
  has_many :teams, through: :teams_users
  has_many :sent_invitations, class_name: 'Invitation', foreign_key: 'from_id', dependent: :destroy
  has_many :received_invitations, class_name: 'Invitation', foreign_key: 'to_id', dependent: :destroy

  has_many :children, class_name: 'User', foreign_key: 'parent_id'
  belongs_to :parent, class_name: 'User'
  belongs_to :role

  validates_presence_of :name
  validates_uniqueness_of :name

  validates_presence_of :email, message: "can't be blank"
  validates_format_of :email, with: /\A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z/i, allow_blank: true

  before_validation :randomize_password, if: ->(user) { user.new_record? && user.password.blank? } # AuthLogic
  after_create :email_welcome

  scope :superadministrators, -> { where role_id: Role.superadministrator }
  scope :superadmins, -> { superadministrators }
  scope :administrators, -> { where role_id: Role.administrator }
  scope :admins, -> { administrators }
  scope :instructors, -> { where role_id: Role.instructor }
  scope :tas, -> { where role_id: Role.ta }
  scope :students, -> { where role_id: Role.student }

  has_paper_trail

  def salt_first?
    true
  end

  def list_mine(object_type, user_id)
    object_type.where(["instructor_id = ?", user_id])
  end

  def get_available_users(name)
    lesser_roles = role.get_parents
    all_users = User.all(conditions: ['name LIKE ?', "#{name}%"], limit: 20) # higher limit, since we're filtering
    visible_users = all_users.select {|user| lesser_roles.include? user.role }
    visible_users[0, 10] # the first 10
  end

  def can_impersonate?(user)
    return true if self.role.super_admin?
    return true if self.is_teaching_assistant_for?(user)
    return true if self.is_recursively_parent_of(user)
    false
  end

  def is_recursively_parent_of(user)
    p = user.parent
    return false if p.nil?
    return true if p == self
    return false if p.role.super_admin?
    self.is_recursively_parent_of(p)
  end

  def get_user_list(page_num, per_page)
    user_list = []
    # If the user is a super admin, fetch all users
    if self.role.super_admin?
      User.all.find_each do |user|
        user_list << user
      end
    end

    # If the user is an instructor, fetch all users in his course/assignment
    if self.role.instructor? || self.role.ta?
      User.all.find_each do |user|
        if self.can_impersonate?(user)
          user_list << user
        end
      end
    end

    if per_page.nil?
      user_list = user_list.paginate(page: page_num, per_page: 25)
    elsif per_page == "All"
        user_list
    else
      user_list = user_list.paginate(page: page_num, per_page: per_page)
    end
  end

  def first_name
    fullname.try(:[], /,.+/).try(:[], /\w+/) || ''
  end

  def super_admin?
    role.name == 'Super-Administrator'
{'fraction_non_alphanumeric': 0.11352418558736427, 'fraction_numerical': 0.005358905655055705, 'mean_word_length': 3.8409556313993174, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 21, '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': '5856018', 'n_tokens_mistral': 2499, 'n_tokens_neox': 2410, 'n_words': 578}