#学生提交作品表 #work_status :0 未提交  1 已提交  2 迟交  3 分组作品复制的组员作品
class StudentWork < ActiveRecord::Base
  attr_accessible :name, :description, :homework_common_id, :user_id, :final_score, :teacher_score, :student_score, :teaching_asistant_score, :system_score, :work_score, :project_id, :is_test, :simi_id, :simi_value, :work_status, :commit_time, :late_penalty, :absence_penalty

  belongs_to :homework_common
  belongs_to :user
  has_many :student_work_projects
  has_many :student_works_evaluation_distributions, :dependent => :destroy
  has_many :student_works_scores, :dependent => :destroy
  belongs_to :project
  has_many :student_work_tests, order: 'id desc'
  # course's message
  has_many :course_messages, :class_name =>'CourseMessage', :as => :course_message, :dependent => :destroy
  has_many :attachments, :dependent => :destroy

  scope :has_committed, lambda{where("work_status != 0 and work_status != 3")}
  scope :no_copy, lambda{where("work_status != 3")}

  before_destroy :delete_praise
  before_save :set_program_score, :set_src

  after_create :act_as_message
  acts_as_attachable

  def delete_praise
    PraiseTread.where("praise_tread_object_id = #{self.id} AND praise_tread_object_type = 'StudentWork'").destroy_all
  end

  def last_test
    student_work_tests.order('id desc').first
  end

  private 
  def set_program_score
    if self.homework_common.is_program_homework?       #编程作业，学生提交作品后计算系统得分
        #根据最后一次测试计算得分
        unless last_test
          self.system_score = 0
        else
          self.system_score = last_test.test_score
        end
    end
    set_final_score self.homework_common,self
  end
  def set_src
    self.description = last_test.src if last_test
  end

  #成绩计算
  def set_final_score homework,student_work
    if homework && homework.homework_detail_manual
      if homework.homework_type != 2 #匿评作业
        if homework.teacher_priority == 1 #教师优先
          if student_work.teacher_score
            student_work.final_score = student_work.teacher_score
            student_work.work_score = student_work.teacher_score
          else
            if student_work.teaching_asistant_score.nil?
              student_work.final_score = student_work.student_score
            elsif student_work.student_score.nil?
              student_work.final_score = student_work.teaching_asistant_score
            else
              ta_proportion = homework.homework_detail_manual.ta_proportion
              final_ta_score = BigDecimal.new("#{student_work.teaching_asistant_score}") * BigDecimal.new("#{ta_proportion}")
              final_s_score = BigDecimal.new("#{student_work.student_score}") * (BigDecimal.new('1.0')  - BigDecimal.new("#{ta_proportion}"))
              final_score = final_ta_score + final_s_score
              student_work.final_score = format("%.2f",final_score.to_f)
            end
            if student_work.final_score
              score = student_work.final_score - student_work.absence_penalty - student_work.late_penalty
              student_work.work_score = format("%.2f",(score < 0 ? 0 : score).to_f) if score
            else
              student_work.work_score = nil
            end
          end
        else  #不考虑教师评分
          if student_work.student_score.nil? && student_work.teaching_asistant_score.nil?
            student_work.final_score = student_work.teacher_score
          elsif student_work.teaching_asistant_score.nil?
            student_work.final_score = student_work.student_score
          elsif student_work.student_score.nil?
            student_work.final_score = student_work.teaching_asistant_score
          else
            ta_proportion = homework.homework_detail_manual.ta_proportion
            final_ta_score = BigDecimal.new("#{student_work.teaching_asistant_score}") * BigDecimal.new("#{ta_proportion}")
            final_s_score = BigDecimal.new("#{student_work.student_score}") * (BigDecimal.new('1.0')  - BigDecimal.new("#{ta_proportion}"))
            final_score = final_ta_score + final_s_score
            student_work.final_score = format("%.2f",final_score.to_f)
          end
          if student_work.final_score
            score = student_work.final_score - student_work.absence_penalty - student_work.late_penalty
            student_work.work_score = format("%.2f",(score < 0 ? 0 : score).to_f) if score
          else
            student_work.work_score = nil
          end
        end
      elsif homework.homework_type == 2 && homework.homework_detail_programing #编程作业-----设定：系统评分必定不为空
        if homework.teacher_priority == 1 #教师优先
          if student_work.teacher_score
            student_work.final_score = student_work.teacher_score
            student_work.work_score = student_work.teacher_score
          else
            if student_work.teaching_asistant_score.nil? #教辅未评分
              if student_work.student_score.nil?
                student_work.final_score = student_work.system_score
              else
                ta_proportion = homework.homework_detail_programing.ta_proportion + homework.homework_detail_manual.ta_proportion / 2
                final_sy_score = BigDecimal.new("#{student_work.system_score || 0}") * BigDecimal.new("#{ta_proportion}")
                final_st_score = BigDecimal.new("#{student_work.student_score}") * (BigDecimal.new('1.0')  - BigDecimal.new("#{ta_proportion}"))
                final_score = final_sy_score + final_st_score
                student_work.final_score = format("%.2f",final_score.to_f)
              end
            elsif student_work.student_score.nil? #学生未评分
              if student_work.teaching_asistant_score.nil?
                student_work.final_score = student_work.system_score
              else
                ta_proportion = homework.homework_detail_programing.ta_proportion + (1.0 - homework.homework_detail_manual.ta_proportion - homework.homework_detail_programing.ta_proportion) / 2
                final_sy_score = BigDecimal.new("#{student_work.system_score || 0}") * BigDecimal.new("#{ta_proportion}")
                final_ts_score = BigDecimal.new("#{student_work.teaching_asistant_score}") * (BigDecimal.new('1.0')  - BigDecimal.new("#{ta_proportion}"))
                final_score = final_sy_score + final_ts_score
                student_work.final_score = format("%.2f",final_score.to_f)
              end
            else
              final_sy_score = BigDecimal.new("#{student_work.system_score || 0}") * BigDecimal.new("#{homework.homework_detail_programing.ta_proportion}")
              final_ts_score = BigDecimal.new("#{student_work.teaching_asistant_score}") * BigDecimal.new("#{homework.homework_detail_manual.ta_proportion}")
              final_st_score = BigDecimal.new("#{student_work.student_score}") * (BigDecimal.new('1.0')  - BigDecimal.new("#{homework.homework_detail_programing.ta_proportion}") - BigDecimal.new("#{homework.homework_detail_manual.ta_proportion}"))
              final_score = final_sy_score + final_ts_score + final_st_score
              student_work.final_score = format("%.2f",final_score.to_f)
            end
            if student_work.final_score
              score = student_work.final_score - student_work.absence_penalty - student_work.late_penalty
              student_work.work_score = format("%.2f",(score < 0 ? 0 : score).to_f) if score
            else
              student_work.work_score = nil
            end
            end
        else  #不考虑教师评分
          if student_work.teaching_asistant_score.nil? #教辅未评分
            if student_work.student_score.nil?
              student_work.final_score = student_work.system_score
            else
              ta_proportion = homework.homework_detail_programing.ta_proportion + homework.homework_detail_manual.ta_proportion / 2
              final_sy_score = BigDecimal.new("#{student_work.system_score || 0}") * BigDecimal.new("#{ta_proportion}")
              final_st_score = BigDecimal.new("#{student_work.student_score}") * (BigDecimal.new('1.0')  - BigDecimal.new("#{ta_proportion}"))
              final_score = final_sy_score + final_st_score
              student_work.final_score = format("%.2f",final_score.to_f)
            end
          elsif student_work.student_score.nil? #学生未评分
            if student_work.teaching_asistant_score.nil?
              student_work.final_score = student_work.system_score
            else
              ta_proportion = homework.homework_detail_programing.ta_proportion + (1.0 - homework.homework_detail_manual.ta_proportion - homework.homework_detail_programing.ta_proportion) / 2
              final_sy_score = BigDecimal.new("#{student_work.system_score || 0}") * BigDecimal.new("#{ta_proportion}")
              final_ts_score = BigDecimal.new("#{student_work.teaching_asistant_score}") * (BigDecimal.new('1.0')  - BigDecimal.new("#{ta_proportion}"))
              final_score = final_sy_score + final_ts_score
              student_work.final_score = format("%.2f",final_score.to_f)
            end
          else
            final_sy_score = BigDecimal.new("#{student_work.system_score || 0}") * BigDecimal.new("#{homework.homework_detail_programing.ta_proportion}")
            final_ts_score = BigDecimal.new("#{student_work.teaching_asistant_score}") * BigDecimal.new("#{homework.homework_detail_manual.ta_proportion}")
            final_st_score = BigDecimal.new("#{student_work.student_score}") * (BigDecimal.new('1.0')  - BigDecimal.new("#{homework.homework_detail_programing.ta_proportion}") - BigDecimal.new("#{homework.homework_detail_manual.ta_proportion}"))
            final_score = final_sy_score + final_ts_score + final_st_score
            student_work.final_score = format("%.2f",final_score.to_f)
          end
          if student_work.final_score
            score = student_work.final_score - student_work.absence_penalty - student_work.late_penalty
            student_work.work_score = format("%.2f",(score < 0 ? 0 : score).to_f) if score
          else
            student_work.work_score = nil
          end
        end
      end
    end
  end

  # status == 0 : delay
  def act_as_message
    if self.work_status != 0 && self.created_at > self.homework_common.end_time + 1
      self.course_messages << CourseMessage.new(:user_id => self.user_id, :course_id => self.homework_common.course_id, :viewed => false, :status => false)
    end
  end
end
