<commit_before>require 'csv'
require 'bcrypt'
require 'json'
require 'moss_ruby'
require 'csv_helper'
require 'grade_helper'

class Unit < ActiveRecord::Base
  include ApplicationHelper
  include FileHelper
  include LogHelper
  include MimeCheckHelpers
  include CsvHelper

  #
  # Permissions around unit data
  #
  def self.permissions
    # What can students do with units?
    student_role_permissions = [
      :get_unit
    ]
    # What can tutors do with units?
    tutor_role_permissions = [
      :get_unit,
      :get_students,
      :enrol_student,
      :provide_feedback,
      :download_stats,
      :download_unit_csv,
      :download_grades
    ]

    # What can convenors do with units?
    convenor_role_permissions = [
      :get_unit,
      :get_students,
      :enrol_student,
      :upload_csv,
      :download_unit_csv,
      :update,
      :employ_staff,
      :add_tutorial,
      :add_task_def,
      :provide_feedback,
      :change_project_enrolment,
      :download_stats,
      :download_grades,
      :rollover_unit
    ]

    # What can admin do with units?
    admin_role_permissions = [
      :get_unit,
      :get_students,
      :enrol_student,
      :upload_csv,
      :rollover_unit,
      :change_project_enrolment,
      :update,
      :employ_staff,
      :add_tutorial,
      :add_task_def,
      :download_stats,
      :download_unit_csv,
      :download_grades,
    ]

    # What can other users do with units?
    nil_role_permissions = [

    ]

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

  def role_for(user)
    if convenors.where('unit_roles.user_id=:id', id: user.id).count == 1
      Role.convenor
    elsif tutors.where('unit_roles.user_id=:id', id: user.id).count == 1
      Role.tutor
    elsif active_projects.where('projects.user_id=:id', id: user.id).count == 1
      Role.student
    elsif user.has_admin_capability?
      Role.admin
    else
      nil
    end
  end

  # Model associations.
  # When a Unit is destroyed, any TaskDefinitions, Tutorials, and ProjectConvenor instances will also be destroyed.
  has_many :tutorial_streams, dependent: :destroy
  has_many :task_definitions, -> { order 'start_date ASC, abbreviation ASC' }, dependent: :destroy<commit_msg>FIX: Ensure that main convenor is cleared on destroy of unit<commit_after>require 'csv'
require 'bcrypt'
require 'json'
require 'moss_ruby'
require 'csv_helper'
require 'grade_helper'

class Unit < ActiveRecord::Base
  include ApplicationHelper
  include FileHelper
  include LogHelper
  include MimeCheckHelpers
  include CsvHelper

  #
  # Permissions around unit data
  #
  def self.permissions
    # What can students do with units?
    student_role_permissions = [
      :get_unit
    ]
    # What can tutors do with units?
    tutor_role_permissions = [
      :get_unit,
      :get_students,
      :enrol_student,
      :provide_feedback,
      :download_stats,
      :download_unit_csv,
      :download_grades
    ]

    # What can convenors do with units?
    convenor_role_permissions = [
      :get_unit,
      :get_students,
      :enrol_student,
      :upload_csv,
      :download_unit_csv,
      :update,
      :employ_staff,
      :add_tutorial,
      :add_task_def,
      :provide_feedback,
      :change_project_enrolment,
      :download_stats,
      :download_grades,
      :rollover_unit
    ]

    # What can admin do with units?
    admin_role_permissions = [
      :get_unit,
      :get_students,
      :enrol_student,
      :upload_csv,
      :rollover_unit,
      :change_project_enrolment,
      :update,
      :employ_staff,
      :add_tutorial,
      :add_task_def,
      :download_stats,
      :download_unit_csv,
      :download_grades,
    ]

    # What can other users do with units?
    nil_role_permissions = [

    ]

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

  def role_for(user)
    if convenors.where('unit_roles.user_id=:id', id: user.id).count == 1
      Role.convenor
    elsif tutors.where('unit_roles.user_id=:id', id: user.id).count == 1
      Role.tutor
    elsif active_projects.where('projects.user_id=:id', id: user.id).count == 1
      Role.student
    elsif user.has_admin_capability?
      Role.admin
    else
      nil
    end
  end

  # Ensure before destroy is above relations - as this needs to clear main convenor before unit roles are deleted
  before_destroy do
    update(main_convenor_id: nil)
  end

  # Model associations.
  # When a Unit is destroyed, any TaskDefinitions, Tutorials, and ProjectConvenor instances will also be destroyed.
  has_many :tutorial_streams, dependent: :destroy
  has_many :task_definitions, -> { order 'start_date ASC, abbreviation ASC' }, dependent: :destroy
{'fraction_non_alphanumeric': 0.07971586424625099, 'fraction_numerical': 0.0011838989739542227, 'mean_word_length': 2.84597875569044, 'pattern_counts': {'":': 0, '<': 5, '<?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': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1411216', 'n_tokens_mistral': 1684, 'n_tokens_neox': 1634, 'n_words': 378}