class HomeworkAttach < ActiveRecord::Base
  include Redmine::SafeAttributes

  #attr_accessible :name, :description, :state, :user_id, :bid_id

  belongs_to :user
  belongs_to :bid
  has_many :journals_for_messages, :as => :jour, :dependent => :destroy
  has_many :homework_users, :dependent => :destroy
  has_many :users, :through => :homework_users
  seems_rateable :allow_update => true, :dimensions => :quality
  belongs_to :project
  has_many :homework_evaluations, :dependent => :destroy
  
  safe_attributes "bid_id",
				  "user_id"
  acts_as_attachable

  def addjours user_id,message,status = 0,is_comprehensive_evaluation = 0,reply_id = 0
    jfm = self.journals_for_messages.build(:user_id => user_id,:notes =>message,:status => status,:is_comprehensive_evaluation => is_comprehensive_evaluation,:reply_id => reply_id)
    jfm.save
    jfm
  end

  def score
    stars_reates = self.rates(:quality)
    percent = 0
    stars_reates.each do |star_reates|
      percent = percent + star_reates.stars
    end
    result = percent * 1.0 /  stars_reates.count
    result
  end

  def project_for_homework
    work = HomeworkForCourse.find_by_bid_id(self.bid_id)
    if work
        work.project
    end
  end

  def add_jours options
    jfm = self.journals_for_messages.build(options)
    jfm.save
    jfm
  end
end
