# encoding: utf-8 class StudentWorkTest < ActiveRecord::Base attr_accessible :student_work_id, :results, :status, :src, :uwait_time belongs_to :student_work serialize :results, Array def status_to_s case self.status.to_i when -1 '编译出错' when -2 '答题错误' when -3 '答案错误' when 1 '运行出错' when 2 '超时' when 3 '内存超出' when 4 '输出超出' when 5 '禁用函数' when 6 '其他错误' when 0 '成功' else '未知错误' end end #必须和测试集个数一样才能得100分 def test_score(testcount) if self.status.to_i == 0 && testcount == self.results.count 100 elsif self.results.empty? 0 else get_success_count * 100 / testcount end end private def get_success_count self.results.inject(0) do |sum, result| sum += (result["status"] && result["status"].class == ::Fixnum && result["status"].to_i == 0 ? 1 : 0) end || 0 end end