<commit_before>class AssignmentTeam < Team

  belongs_to  :assignment, :class_name => 'Assignment', :foreign_key => 'parent_id'
  has_many    :review_mappings, :class_name => 'TeamReviewResponseMap', :foreign_key => 'reviewee_id'
  has_many    :responses, :finder_sql => 'SELECT r.* FROM responses r, response_maps m, teams t WHERE r.map_id = m.id AND m.type = \'TeamReviewResponseMap\' AND m.reviewee_id = t.id AND t.id = #{id}'

# START of contributor methods, shared with AssignmentParticipant

# Whether this team includes a given participant or not
  def includes?(participant)
    return participants.include?(participant)
  end

  def assign_reviewer(reviewer)
    TeamReviewResponseMap.create(:reviewee_id => self.id, :reviewer_id => reviewer.id,
                                 :reviewed_object_id => assignment.id)
  end

  # Evaluates whether any contribution by this team was reviewed by reviewer
  # @param[in] reviewer AssignmentParticipant object 
  def reviewed_by?(reviewer)
    return TeamReviewResponseMap.count(:conditions => ['reviewee_id = ? AND reviewer_id = ? AND reviewed_object_id = ?',
                                                       self.id, reviewer.id, assignment.id]) > 0
  end

  # Topic picked by the team
  def topic
    team_topic = nil

    participants.each do |participant|
      team_topic = participant.topic
      break if team_topic
    end

    team_topic
  end

  # Whether the team has submitted work or not
  def has_submissions?
    participants.each do |participant|
      return true if participant.has_submissions?
    end
    return false
  end

  def reviewed_contributor?(contributor)
    return TeamReviewResponseMap.find(:all,
                                      :conditions => ['reviewee_id = ? AND reviewer_id = ? AND reviewed_object_id = ?',
                                                      contributor.id, self.id, assignment.id]).empty? == false
  end

# END of contributor methods

  def participants
    @participants ||= AssignmentParticipant.find(:all, :conditions => ['parent_id = ? and user_id IN (?)', parent_id, users])
  end

  def delete
    if read_attribute(:type) == 'AssignmentTeam'
      signup = SignedUpUser.find_team_participants(parent_id.to_s).select{|p| p.creator_id == self.id}
      signup.each &:destroy
    end

    super
  end

  def self.get_first_member(team_id)
    participant = nil
    begin
      team = Team.find(team_id)
      user_id = team.teams_users.first.user_id
      participant = Participant.find_by_user_id_and_parent_id(user_id,team.parent_id)
    rescue NoMethodError => e
      puts "Ignoring error: #{e}"
    rescue ActiveRecord::RecordNotFound => e
      puts "Ignoring error: #{e}"
    end
    return participant
  end

  def get_hyperlinks
    links = Array.new
    for team_member in self.get_participants
      links.concat(team_member.get_hyperlinks_array)
    end
    return links
  end

  def get_path
    self.get_participants.first.get_path
  end

  def get_submitted_files
    self.get_participants.first.get_submitted_files
  end

  def get_review_map_type
    return 'TeamReviewResponseMap'
  end

  def self.handle_duplicate(team, name, assgt_id, handle_dups)
    puts ">>>at beginning of handle_duplicate, name = "+ name
    puts ">>>at beginning of handle_duplicate, handle_dups = "+ handle_dups
    if team.nil? #no duplicate
      return name
    end
    if handle_dups == "ignore" #ignore: do not create the new team
      p '>>>setting name to nil ...'
      return nil
    end
    if handle_dups == "rename" #rename: rename new team
      return self.generate_team_name(Assignment.find(assgt_id).name)
    end
    if handle_dups == "replace" #replace: delete old team
      team.delete
      return name
    else # handle_dups = "insert"
      return nil
    end
  end

  def self.import(row,session,assgt_id,options)
    if (row.length < 2 and options[:has_column_names] == "true") or (row.length < 1 and options[:has_column_names] != "true")
      raise ArgumentError, "Not enough fields on this line"
    end

    if Assignment.find(assgt_id) == nil
      raise ImportError, "The assignment with id \""+assgt_id.to_s+"\" was not found. <a href='/assignment/new'>Create</a> this assignment?"
    end

    if options[:has_column_names] == "true"
      name = row[0].to_s.strip
      team = find(:first, :conditions => ["name =? and parent_id =?", name, assgt_id])
      team_exists = !team.nil?
      name = handle_duplicate(team, name, assgt_id, options[:handle_dups])
      index = 1
    else
      name = self.generate_team_name(Assignment.find(assgt_id).name)
      index = 0
    end

    # create new team for the team to be inserted
    # do not create new team if we choose 'ignore' or 'insert' duplicate teams
    if name
      team=AssignmentTeam.create_team_and_node(assgt_id)
      team.name = name
      team.save
    end

    # insert team members into team unless team was pre-existing & we ignore duplicate teams
    if !(team_exists && options[:handle_dups] == "ignore")
      team.import_team_members(index, row)
    end
  end

  def email
    self.get_team_users.first.email
  end

  def get_participant_type
    "AssignmentParticipant"
  end

  def get_parent_model
    "Assignment"
  end

  def fullname
    self.name
  end

  def get_participants
    users = self.users
    participants = Array.new
    users.each{
        | user |
      participant = AssignmentParticipant.find_by_user_id_and_parent_id(user.id,self.parent_id)
      if participant != nil
        participants << participant
      end
    }
    return participants
  end

  def copy(course_id)
    new_team = CourseTeam.create_team_and_node(course_id)
    new_team.name = name
    new_team.save
    #new_team = CourseTeam.create({:name => self.name, :parent_id => course_id})
    copy_members(new_team)
  end

  def add_participant(assignment_id, user)
    if AssignmentParticipant.find_by_parent_id_and_user_id(assignment_id, user.id) == nil
      AssignmentParticipant.create(:parent_id => assignment_id, :user_id => user.id, :permission_granted => user.master_permission_granted)
    end
  end

  def assignment
    Assignment.find(self.parent_id)
  end

  # return a hash of scores that the team has received for the questions
  def get_scores(questions)
    scores = Hash.new
    scores[:team] = self # This doesn't appear to be used anywhere
    assignment.questionnaires.each do |questionnaire|
      scores[questionnaire.symbol] = Hash.new
      scores[questionnaire.symbol][:assessments] = Response.all(:joins => :map,
                                                                :conditions => {:response_maps => {:reviewee_id => self.id, :type => 'TeamReviewResponseMap'}})
      scores[questionnaire.symbol][:scores] = Score.compute_scores_statistics(scores[questionnaire.symbol][:assessments], questions[questionnaire.symbol])
    end
    scores[:total_score] = assignment.compute_total_score(scores)<commit_msg>Fix usage of old(?) method name, which prevented reviews from being saved.<commit_after>class AssignmentTeam < Team

  belongs_to  :assignment, :class_name => 'Assignment', :foreign_key => 'parent_id'
  has_many    :review_mappings, :class_name => 'TeamReviewResponseMap', :foreign_key => 'reviewee_id'
  has_many    :responses, :finder_sql => 'SELECT r.* FROM responses r, response_maps m, teams t WHERE r.map_id = m.id AND m.type = \'TeamReviewResponseMap\' AND m.reviewee_id = t.id AND t.id = #{id}'

# START of contributor methods, shared with AssignmentParticipant

# Whether this team includes a given participant or not
  def includes?(participant)
    return participants.include?(participant)
  end

  def assign_reviewer(reviewer)
    TeamReviewResponseMap.create(:reviewee_id => self.id, :reviewer_id => reviewer.id,
                                 :reviewed_object_id => assignment.id)
  end

  # Evaluates whether any contribution by this team was reviewed by reviewer
  # @param[in] reviewer AssignmentParticipant object 
  def reviewed_by?(reviewer)
    return TeamReviewResponseMap.count(:conditions => ['reviewee_id = ? AND reviewer_id = ? AND reviewed_object_id = ?',
                                                       self.id, reviewer.id, assignment.id]) > 0
  end

  # Topic picked by the team
  def topic
    team_topic = nil

    participants.each do |participant|
      team_topic = participant.topic
      break if team_topic
    end

    team_topic
  end

  # Whether the team has submitted work or not
  def has_submissions?
    participants.each do |participant|
      return true if participant.has_submissions?
    end
    return false
  end

  def reviewed_contributor?(contributor)
    return TeamReviewResponseMap.find(:all,
                                      :conditions => ['reviewee_id = ? AND reviewer_id = ? AND reviewed_object_id = ?',
                                                      contributor.id, self.id, assignment.id]).empty? == false
  end

# END of contributor methods

  def participants
    @participants ||= AssignmentParticipant.find(:all, :conditions => ['parent_id = ? and user_id IN (?)', parent_id, users])
  end

  def delete
    if read_attribute(:type) == 'AssignmentTeam'
      signup = SignedUpUser.find_team_participants(parent_id.to_s).select{|p| p.creator_id == self.id}
      signup.each &:destroy
    end

    super
  end

  def self.get_first_member(team_id)
    participant = nil
    begin
      team = Team.find(team_id)
      user_id = team.teams_users.first.user_id
      participant = Participant.find_by_user_id_and_parent_id(user_id,team.parent_id)
    rescue NoMethodError => e
      puts "Ignoring error: #{e}"
    rescue ActiveRecord::RecordNotFound => e
      puts "Ignoring error: #{e}"
    end
    return participant
  end

  def get_hyperlinks
    links = Array.new
    for team_member in self.get_participants
      links.concat(team_member.get_hyperlinks_array)
    end
    return links
  end

  def get_path
    self.get_participants.first.get_path
  end

  def get_submitted_files
    self.get_participants.first.get_submitted_files
  end

  def get_review_map_type
    return 'TeamReviewResponseMap'
  end

  def self.handle_duplicate(team, name, assgt_id, handle_dups)
    puts ">>>at beginning of handle_duplicate, name = "+ name
    puts ">>>at beginning of handle_duplicate, handle_dups = "+ handle_dups
    if team.nil? #no duplicate
      return name
    end
    if handle_dups == "ignore" #ignore: do not create the new team
      p '>>>setting name to nil ...'
      return nil
    end
    if handle_dups == "rename" #rename: rename new team
      return self.generate_team_name(Assignment.find(assgt_id).name)
    end
    if handle_dups == "replace" #replace: delete old team
      team.delete
      return name
    else # handle_dups = "insert"
      return nil
    end
  end

  def self.import(row,session,assgt_id,options)
    if (row.length < 2 and options[:has_column_names] == "true") or (row.length < 1 and options[:has_column_names] != "true")
      raise ArgumentError, "Not enough fields on this line"
    end

    if Assignment.find(assgt_id) == nil
      raise ImportError, "The assignment with id \""+assgt_id.to_s+"\" was not found. <a href='/assignment/new'>Create</a> this assignment?"
    end

    if options[:has_column_names] == "true"
      name = row[0].to_s.strip
      team = find(:first, :conditions => ["name =? and parent_id =?", name, assgt_id])
      team_exists = !team.nil?
      name = handle_duplicate(team, name, assgt_id, options[:handle_dups])
      index = 1
    else
      name = self.generate_team_name(Assignment.find(assgt_id).name)
      index = 0
    end

    # create new team for the team to be inserted
    # do not create new team if we choose 'ignore' or 'insert' duplicate teams
    if name
      team=AssignmentTeam.create_team_and_node(assgt_id)
      team.name = name
      team.save
    end

    # insert team members into team unless team was pre-existing & we ignore duplicate teams
    if !(team_exists && options[:handle_dups] == "ignore")
      team.import_team_members(index, row)
    end
  end

  def email
    self.get_team_users.first.email
  end

  def get_participant_type
    "AssignmentParticipant"
  end

  def get_parent_model
    "Assignment"
  end

  def fullname
    self.name
  end

  def get_participants
    users = self.users
    participants = Array.new
    users.each{
        | user |
      participant = AssignmentParticipant.find_by_user_id_and_parent_id(user.id,self.parent_id)
      if participant != nil
        participants << participant
      end
    }
    return participants
  end

  def copy(course_id)
    new_team = CourseTeam.create_team_and_node(course_id)
    new_team.name = name
    new_team.save
    #new_team = CourseTeam.create({:name => self.name, :parent_id => course_id})
    copy_members(new_team)
  end

  def add_participant(assignment_id, user)
    if AssignmentParticipant.find_by_parent_id_and_user_id(assignment_id, user.id) == nil
      AssignmentParticipant.create(:parent_id => assignment_id, :user_id => user.id, :permission_granted => user.master_permission_granted)
    end
  end

  def assignment
    Assignment.find(self.parent_id)
  end

  # return a hash of scores that the team has received for the questions
  def get_scores(questions)
    scores = Hash.new
    scores[:team] = self # This doesn't appear to be used anywhere
    assignment.questionnaires.each do |questionnaire|
      scores[questionnaire.symbol] = Hash.new
      scores[questionnaire.symbol][:assessments] = Response.all(:joins => :map,
                                                                :conditions => {:response_maps => {:reviewee_id => self.id, :type => 'TeamReviewResponseMap'}})
      scores[questionnaire.symbol][:scores] = Score.compute_scores(scores[questionnaire.symbol][:assessments], questions[questionnaire.symbol])
    end
    scores[:total_score] = assignment.compute_total_score(scores)
{'fraction_non_alphanumeric': 0.0914101646385111, 'fraction_numerical': 0.0008589835361488905, 'mean_word_length': 3.0577984316003484, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7364573', 'n_tokens_mistral': 4348, 'n_tokens_neox': 4234, 'n_words': 1096}