<commit_before>class Group < ApplicationRecord
  include LogHelper

  belongs_to :group_set
  belongs_to :tutorial

  has_many :group_memberships, dependent: :destroy
  has_many :group_submissions
  has_many :projects, -> { where('group_memberships.active = :value and projects.enrolled = true', value: true) }, through: :group_memberships
  has_many :past_projects, -> { where('group_memberships.active = :value', value: false) }, through: :group_memberships, source: 'project'
  has_one :unit, through: :group_set
  has_one :tutor, through: :tutorial

  validates :name, presence: true, allow_nil: false
  validates :group_set, presence: true, allow_nil: false
  validates :tutorial, presence: true, allow_nil: false

  validates :name, uniqueness: { scope: :group_set,
                                 message: 'must be unique within the set of groups' }
  validate :must_be_in_same_tutorial, if: :limit_members_to_tutorial?

  before_destroy :ensure_no_submissions

  def active_group_members
    group_memberships.where(active: true)
  end

  def has_active_group_members?
    active_group_members.present?
  end

  #
  # Permissions around group data
  #
  def self.permissions
    # What can students do with groups?
    student_role_permissions = [
      :get_members
    ]
    # What can tutors do with groups?
    tutor_role_permissions = [
      :get_members,
      :manage_group,
      :lock_group,
      :move_tutorial
    ]
    # What can convenors do with groups?
    convenor_role_permissions = [
      :get_members,
      :manage_group,
      :lock_group,
      :can_exceed_capacity,
      :move_tutorial
    ]
    # What can admin do with groups?
    admin_role_permissions = [
      :get_members,
      :manage_group,
      :lock_group,
      :can_exceed_capacity,
      :move_tutorial
    ]    
    # What can nil users do with groups?
    nil_role_permissions = [
    ]

    # Return permissions hash
    {
      admin: admin_role_permissions,
      convenor: convenor_role_permissions,
      tutor: tutor_role_permissions,
      student: student_role_permissions,
      nil: nil_role_permissions
    }
  end

  def ensure_no_submissions
    return true if group_submissions.count.zero?
    errors[:base] << 'Cannot delete group while it has submissions.'
    false
  end

  def specific_permission_hash(role, perm_hash, _other)
    result = perm_hash[role] unless perm_hash.nil?
    if result && role == :student
      result << :manage_group if (!locked && !group_set.locked && group_set.allow_students_to_manage_groups)
    end
    result
  end

  def role_for(user)
    result = unit.role_for(user)
    if result == Role.student
      result = nil unless has_user user
    end

    result
  end

  def has_user(user)
    projects.where('user_id = :user_id', user_id: user.id).count == 1
  end

  def capacity
    result = group_set.capacity
    if result.present?
      result += capacity_adjustment
    end
    result
  end

  def at_capacity?
    capacity.present? && group_memberships.joins(:project).where(active: true, 'projects.enrolled' => true).count >= capacity
  end

  def beyond_capacity?
    capacity.present? && group_memberships.joins(:project).where(active: true, 'projects.enrolled' => true).count > capacity
  end

  def switch_to_tutorial tutorial
    return if tutorial_id == tutorial.id
<commit_msg>ENHANCE: Add student count to group<commit_after>class Group < ApplicationRecord
  include LogHelper

  belongs_to :group_set
  belongs_to :tutorial

  has_many :group_memberships, dependent: :destroy
  has_many :group_submissions
  has_many :projects, -> { where('group_memberships.active = :value and projects.enrolled = true', value: true) }, through: :group_memberships
  has_many :past_projects, -> { where('group_memberships.active = :value', value: false) }, through: :group_memberships, source: 'project'
  has_one :unit, through: :group_set
  has_one :tutor, through: :tutorial

  validates :name, presence: true, allow_nil: false
  validates :group_set, presence: true, allow_nil: false
  validates :tutorial, presence: true, allow_nil: false

  validates :name, uniqueness: { scope: :group_set,
                                 message: 'must be unique within the set of groups' }
  validate :must_be_in_same_tutorial, if: :limit_members_to_tutorial?

  before_destroy :ensure_no_submissions

  def active_group_members
    group_memberships.where(active: true)
  end

  def has_active_group_members?
    active_group_members.present?
  end

  #
  # Permissions around group data
  #
  def self.permissions
    # What can students do with groups?
    student_role_permissions = [
      :get_members
    ]
    # What can tutors do with groups?
    tutor_role_permissions = [
      :get_members,
      :manage_group,
      :lock_group,
      :move_tutorial
    ]
    # What can convenors do with groups?
    convenor_role_permissions = [
      :get_members,
      :manage_group,
      :lock_group,
      :can_exceed_capacity,
      :move_tutorial
    ]
    # What can admin do with groups?
    admin_role_permissions = [
      :get_members,
      :manage_group,
      :lock_group,
      :can_exceed_capacity,
      :move_tutorial
    ]
    # What can nil users do with groups?
    nil_role_permissions = [
    ]

    # Return permissions hash
    {
      admin: admin_role_permissions,
      convenor: convenor_role_permissions,
      tutor: tutor_role_permissions,
      student: student_role_permissions,
      nil: nil_role_permissions
    }
  end

  def ensure_no_submissions
    return true if group_submissions.count.zero?
    errors[:base] << 'Cannot delete group while it has submissions.'
    false
  end

  def specific_permission_hash(role, perm_hash, _other)
    result = perm_hash[role] unless perm_hash.nil?
    if result && role == :student
      result << :manage_group if (!locked && !group_set.locked && group_set.allow_students_to_manage_groups)
    end
    result
  end

  def role_for(user)
    result = unit.role_for(user)
    if result == Role.student
      result = nil unless has_user user
    end

    result
  end

  def has_user(user)
    projects.where('user_id = :user_id', user_id: user.id).count == 1
  end

  def capacity
    result = group_set.capacity
    if result.present?
      result += capacity_adjustment
    end
    result
  end

  def student_count
    group_memberships.joins(:project).where(active: true, 'projects.enrolled' => true).count
  end

  def at_capacity?
    capacity.present? && student_count >= capacity
  end

  def beyond_capacity?
    capacity.present? && student_count > capacity
  end

  def switch_to_tutorial tutorial
    return if tutorial_id == tutorial.id

{'fraction_non_alphanumeric': 0.08235998802036539, 'fraction_numerical': 0.0002994908655286014, 'mean_word_length': 3.2841565105837076, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3055140', 'n_tokens_mistral': 2165, 'n_tokens_neox': 2072, 'n_words': 524}