<commit_before>require 'digest/sha1'

class User < ActiveRecord::Base

  belongs_to :course

  has_many :announcements
  has_many :documents
  has_many :grades
  has_many :question_responses
  has_many :correct_responses, :class_name => 'QuestionResponse', :conditions => { :correct => true }
  has_many :audits

  # Virtual attribute for the unencrypted password
  attr_accessor :password

  validates_presence_of     :login, :email, :first_name, :last_name
  validates_presence_of     :password,                   :if => :password_required?
  validates_presence_of     :password_confirmation,      :if => :password_required?
  validates_length_of       :password, :within => 4..40, :if => :password_required?
  validates_confirmation_of :password,                   :if => :password_required?
  validates_length_of       :login,    :within => 3..40
  validates_length_of       :email,    :within => 3..100
  validates_uniqueness_of   :login, :case_sensitive => false
  validates_uniqueness_of   :blackboard_username, :case_sensitive => false

  before_save :encrypt_password
  before_create :make_activation_code 
  before_save :set_course

  # prevents a user from submitting a crafted form that bypasses activation
  # anything else you want your user to change should be added here.
  attr_accessible :login, :email, :password, :password_confirmation, :first_name, :last_name,
                  :blackboard_username, :registration_code, :instructor, :remember_me

  def full_name
    "#{first_name} #{last_name}"
  end

  def name
    "#{first_name.first.capitalize} #{last_name.capitalize}"
  end

  def mean_score
    return if question_responses.empty?
    return if course.quizzes.detect(&:open?)

    correct_responses.size / question_responses.size.to_f
  end

  def adjusted_mean_score(adjustment)
    return if question_responses.empty?
    return if course.quizzes.detect(&:open?)

    ams = (correct_responses.size + adjustment)/ question_responses.size.to_f
    ams > 100.0 ? 100.0 : ams
  end

  def exam_mean_score
    return if grades.empty?
    grades.sum(:value) / grades.length.to_f
  end

  # Activates the user in the database.
  def activate
    @activated = true
    self.activated_at = Time.now
    self.activation_code = nil
    save(false)
  end

  def activated?
    # the existence of an activation code means they have not activated yet
    activation_code.nil?
  end

  # Returns true if the user has just been activated.
  def recently_activated?
    @activated
  end

  # Authenticates a user by their login name and unencrypted password.  Returns the user or nil.
  def self.authenticate(login, password)
    u = find :first, :conditions => ['login = ? and activated_at IS NOT NULL', login] # need to get the salt
    u && u.authenticated?(password) ? u : nil
  end

  # Encrypts some data with the salt.
  def self.encrypt(password, salt)
    Digest::SHA1.hexdigest("--#{salt}--#{password}--")<commit_msg>updated exam_mean_score calculation to not include the final exam<commit_after>require 'digest/sha1'

class User < ActiveRecord::Base

  belongs_to :course

  has_many :announcements
  has_many :documents
  has_many :grades
  has_many :question_responses
  has_many :correct_responses, :class_name => 'QuestionResponse', :conditions => { :correct => true }
  has_many :audits

  # Virtual attribute for the unencrypted password
  attr_accessor :password

  validates_presence_of     :login, :email, :first_name, :last_name
  validates_presence_of     :password,                   :if => :password_required?
  validates_presence_of     :password_confirmation,      :if => :password_required?
  validates_length_of       :password, :within => 4..40, :if => :password_required?
  validates_confirmation_of :password,                   :if => :password_required?
  validates_length_of       :login,    :within => 3..40
  validates_length_of       :email,    :within => 3..100
  validates_uniqueness_of   :login, :case_sensitive => false
  validates_uniqueness_of   :blackboard_username, :case_sensitive => false

  before_save :encrypt_password
  before_create :make_activation_code 
  before_save :set_course

  # prevents a user from submitting a crafted form that bypasses activation
  # anything else you want your user to change should be added here.
  attr_accessible :login, :email, :password, :password_confirmation, :first_name, :last_name,
                  :blackboard_username, :registration_code, :instructor, :remember_me

  def full_name
    "#{first_name} #{last_name}"
  end

  def name
    "#{first_name.first.capitalize} #{last_name.capitalize}"
  end

  def mean_score
    return if question_responses.empty?
    return if course.quizzes.detect(&:open?)

    correct_responses.size / question_responses.size.to_f
  end

  def adjusted_mean_score(adjustment)
    return if question_responses.empty?
    return if course.quizzes.detect(&:open?)

    ams = (correct_responses.size + adjustment)/ question_responses.size.to_f
    ams > 100.0 ? 100.0 : ams
  end

  def exam_mean_score
    return if grades.empty?
    grades.inject(0.0) {|sum, grade| sum += grade.value unless grade.exam.final; sum } / grades.length.to_f
  end

  # Activates the user in the database.
  def activate
    @activated = true
    self.activated_at = Time.now
    self.activation_code = nil
    save(false)
  end

  def activated?
    # the existence of an activation code means they have not activated yet
    activation_code.nil?
  end

  # Returns true if the user has just been activated.
  def recently_activated?
    @activated
  end

  # Authenticates a user by their login name and unencrypted password.  Returns the user or nil.
  def self.authenticate(login, password)
    u = find :first, :conditions => ['login = ? and activated_at IS NOT NULL', login] # need to get the salt
    u && u.authenticated?(password) ? u : nil
  end

  # Encrypts some data with the salt.
  def self.encrypt(password, salt)
    Digest::SHA1.hexdigest("--#{salt}--#{password}--")
{'fraction_non_alphanumeric': 0.08883333333333333, 'fraction_numerical': 0.007, 'mean_word_length': 3.609062980030722, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, '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': '1501981', 'n_tokens_mistral': 1942, 'n_tokens_neox': 1827, 'n_words': 516}