#学生提交作品表
class StudentWork < ActiveRecord::Base
  attr_accessible :name, :description, :homework_common_id, :user_id, :final_score, :teacher_score, :student_score, :teaching_asistant_score, :project_id, :is_test

  belongs_to :homework_common
  belongs_to :user
  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'

  before_destroy :delete_praise
  before_save :set_program_score, :set_src

  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 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 == 1 #匿评作业
        if homework.teacher_priority == 1 #教师优先
          if student_work.teacher_score
            student_work.final_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
          end
        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
        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
          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
          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
        end
      end
    end
  end
end
