<commit_before>require 'csv'
require 'bcrypt'

class ProjectTemplate < ActiveRecord::Base
  attr_accessible :official_name, :description, :end_date, :name, :start_date
  validates_presence_of :name, :description, :start_date, :end_date

  # Accessor to allow setting of convenors via the new/edit form
  attr_accessor :convenors  

  # Model associations. 
  # When a ProjectTemplate is destroyed, any TaskTemplates, Teams, and ProjectConvenor instances will also be destroyed.
  has_many :task_templates, :dependent => :destroy	  			
  has_many :projects, :dependent => :destroy					 
  has_many :teams, :dependent => :destroy
  has_many :project_convenors, :dependent => :destroy
  
  # Adds a user to this project.
  def add_user(user_id, team_id, project_role)
    # Put the user in the appropriate team (ie. create a new team_membership)
    TeamMembership.populate(1) do |team_membership|
      puts "add_user: Adding User ##{user_id} to Team ##{team_id}"
      team_membership.team_id = team_id
      team_membership.user_id = user_id

      # Create a project instance
      Project.populate(1) do |project|
        project.project_status_id = 1   # @TODO: Remove hard-coded value
        project.project_template_id = id
        project.project_role = project_role

        # Set the foreign keys for the 1:1 relationship
        project.team_membership_id = team_membership.id
        team_membership.project_id = project.id

        # Create task instances for the project
        task_templates_for_project = TaskTemplate.where(:project_template_id => self.id)
        task_templates_for_project.each do |task_template|
          Task.populate(1) do |task|
            task.task_template_id = task_template.id
            task.project_id       = project.id
            task.task_status_id   = 1     # @TODO: Remove hard-coded value
            task.awaiting_signoff = false
            task.completion_date  = nil
          end
        end
      end
    end
  end

  # Removes a user (and their tasks etc.) from this project
  def remove_user(user_id)
    team_memberships = TeamMembership.joins(:project => :project_template).where(:user_id => user_id, :projects => {:project_template_id => self.id})

    team_memberships.each do |team_membership|
      team_membership.destroy
    end
  end

  # Imports users into a project from CSV file. 
  # Format: Student ID,Course ID,First Name,Initials,Surname,Mark,Assessment,Status
  # Only Student ID, First Name, and Surname are used.
  def import_users_from_csv(file)
    team_cache = {}

    CSV.foreach(file) do |row|
      # Make sure we're not looking at the header or an empty line
      next if row[0] =~ /Subject Code/
      next if row[5] !~ /^LA\d/

      subject_code, username  = row[0..1]
      first_name, last_name   = [row[2], row[3]].map{|name| name.titleize }
      email, class_id         = row[4..5]

      project_participant = User.find_or_create_by_username(:username => username) {|new_user|
        new_user.username           = username
        new_user.first_name         = first_name
        new_user.last_name          = last_name
        new_user.email              = email
        new_user.nickname           = first_name
        new_user.encrypted_password = <PASSWORD>("password")
        new_user.system_role        = "student"
      }

      project_participant.save!(:validate => false) unless project_participant.persisted?

      user_not_in_project = TeamMembership.joins(:project => :project_template).where(
        :user_id => project_participant.id,
        :projects => {:project_template_id => id}
      ).count == 0

      team = team_cache[class_id] || Team.where(:official_name => class_id).first
      team_cache[class_id] ||= team
      
      # Add the user to the project (if not already in there)
      if user_not_in_project
        puts "Adding User #{project_participant.id} to team ##{team.id} in project ##{id}"
        add_user(project_participant.id, team.id, "student")
      end
    end
  end

  def import_teams_from_csv(file)
    CSV.foreach(file) do |row|
      next if row[0] =~ /Subject Code/ # Skip header

      class_type, class_id, day, time, location, tutor_username = row[2..-1]
      next if class_type !~ /Lab/

      Team.find_or_create_by_project_template_id_and_official_name(id, class_id) do |team|
        team.meeting_day      = day
        team.meeting_time     = time
        team.meeting_location = location
        
        user_for_tutor = User.where(:username => tutor_username).first
        team.user_id          = user_for_tutor.id
      end
    end<commit_msg>Removed errant puts' in project_template CSV import<commit_after>require 'csv'
require 'bcrypt'

class ProjectTemplate < ActiveRecord::Base
  attr_accessible :official_name, :description, :end_date, :name, :start_date
  validates_presence_of :name, :description, :start_date, :end_date

  # Accessor to allow setting of convenors via the new/edit form
  attr_accessor :convenors  

  # Model associations. 
  # When a ProjectTemplate is destroyed, any TaskTemplates, Teams, and ProjectConvenor instances will also be destroyed.
  has_many :task_templates, :dependent => :destroy	  			
  has_many :projects, :dependent => :destroy					 
  has_many :teams, :dependent => :destroy
  has_many :project_convenors, :dependent => :destroy
  
  # Adds a user to this project.
  def add_user(user_id, team_id, project_role)
    # Put the user in the appropriate team (ie. create a new team_membership)
    TeamMembership.populate(1) do |team_membership|
      team_membership.team_id = team_id
      team_membership.user_id = user_id

      # Create a project instance
      Project.populate(1) do |project|
        project.project_status_id = 1   # @TODO: Remove hard-coded value
        project.project_template_id = id
        project.project_role = project_role

        # Set the foreign keys for the 1:1 relationship
        project.team_membership_id = team_membership.id
        team_membership.project_id = project.id

        # Create task instances for the project
        task_templates_for_project = TaskTemplate.where(:project_template_id => self.id)
        task_templates_for_project.each do |task_template|
          Task.populate(1) do |task|
            task.task_template_id = task_template.id
            task.project_id       = project.id
            task.task_status_id   = 1     # @TODO: Remove hard-coded value
            task.awaiting_signoff = false
            task.completion_date  = nil
          end
        end
      end
    end
  end

  # Removes a user (and their tasks etc.) from this project
  def remove_user(user_id)
    team_memberships = TeamMembership.joins(:project => :project_template).where(:user_id => user_id, :projects => {:project_template_id => self.id})

    team_memberships.each do |team_membership|
      team_membership.destroy
    end
  end

  # Imports users into a project from CSV file. 
  # Format: Student ID,Course ID,First Name,Initials,Surname,Mark,Assessment,Status
  # Only Student ID, First Name, and Surname are used.
  def import_users_from_csv(file)
    team_cache = {}

    CSV.foreach(file) do |row|
      # Make sure we're not looking at the header or an empty line
      next if row[0] =~ /Subject Code/
      next if row[5] !~ /^LA\d/

      subject_code, username  = row[0..1]
      first_name, last_name   = [row[2], row[3]].map{|name| name.titleize }
      email, class_id         = row[4..5]

      project_participant = User.find_or_create_by_username(:username => username) {|new_user|
        new_user.username           = username
        new_user.first_name         = first_name
        new_user.last_name          = last_name
        new_user.email              = email
        new_user.nickname           = first_name
        new_user.encrypted_password = <PASSWORD>("password")
        new_user.system_role        = "student"
      }

      project_participant.save!(:validate => false) unless project_participant.persisted?

      user_not_in_project = TeamMembership.joins(:project => :project_template).where(
        :user_id => project_participant.id,
        :projects => {:project_template_id => id}
      ).count == 0

      team = team_cache[class_id] || Team.where(:official_name => class_id).first
      team_cache[class_id] ||= team
      
      # Add the user to the project (if not already in there)
      if user_not_in_project
        add_user(project_participant.id, team.id, "student")
      end
    end
  end

  def import_teams_from_csv(file)
    CSV.foreach(file) do |row|
      next if row[0] =~ /Subject Code/ # Skip header

      class_type, class_id, day, time, location, tutor_username = row[2..-1]
      next if class_type !~ /Lab/

      Team.find_or_create_by_project_template_id_and_official_name(id, class_id) do |team|
        team.meeting_day      = day
        team.meeting_time     = time
        team.meeting_location = location
        
        user_for_tutor = User.where(:username => tutor_username).first
        team.user_id          = user_for_tutor.id
      end
    end
{'fraction_non_alphanumeric': 0.08800263678312459, 'fraction_numerical': 0.004174906613931004, 'mean_word_length': 2.785031185031185, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 39, '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': '3687771', 'n_tokens_mistral': 2886, 'n_tokens_neox': 2771, 'n_words': 697}