socialforge/app/models/student_work.rb

191 lines
12 KiB
Ruby

#学生提交作品表 #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, :re_commit
belongs_to :homework_common
belongs_to :user
has_many :student_work_projects, :dependent => :destroy
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_save :update_avg_sum_score
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 self.homework_common.homework_tests.size
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
tea_ass_proportion = homework.homework_detail_manual.ta_proportion
tea_proportion = homework.homework_detail_manual.te_proportion
if homework.homework_type != 2 #非编程作业
if student_work.teacher_score
if student_work.teaching_asistant_score.nil?
if student_work.student_score.nil?
student_work.final_score = student_work.teacher_score
else
te_proportion = tea_proportion + tea_ass_proportion / 2
final_te_score = BigDecimal.new("#{student_work.teacher_score}") * BigDecimal.new("#{te_proportion}")
final_s_score = BigDecimal.new("#{student_work.student_score}") * (BigDecimal.new('1.0') - BigDecimal.new("#{te_proportion}"))
final_score = final_te_score + final_s_score
student_work.final_score = format("%.2f",final_score.to_f)
end
else
if student_work.student_score.nil?
te_proportion = tea_proportion + (1.0 - tea_proportion - tea_ass_proportion) / 2
final_te_score = BigDecimal.new("#{student_work.teacher_score}") * BigDecimal.new("#{te_proportion}")
final_ta_score = BigDecimal.new("#{student_work.teaching_asistant_score}") * (BigDecimal.new('1.0') - BigDecimal.new("#{te_proportion}"))
final_score = final_te_score + final_ta_score
student_work.final_score = format("%.2f",final_score.to_f)
else
final_te_score = BigDecimal.new("#{student_work.teacher_score}") * BigDecimal.new("#{tea_proportion}")
final_ta_score = BigDecimal.new("#{student_work.teaching_asistant_score}") * BigDecimal.new("#{tea_ass_proportion}")
final_s_score = BigDecimal.new("#{student_work.student_score}") * (BigDecimal.new('1.0') - BigDecimal.new("#{tea_proportion}") - BigDecimal.new("#{tea_ass_proportion}"))
final_score = final_te_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 = tea_ass_proportion + tea_proportion / 2
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 #教师优先
sy_proportion = homework.homework_detail_programing.ta_proportion
if student_work.teacher_score
if student_work.teaching_asistant_score.nil? #教辅未评分
if student_work.student_score.nil?
ta_proportion = tea_proportion + (1 - tea_proportion - sy_proportion) / 2
final_te_score = BigDecimal.new("#{student_work.teacher_score}") * BigDecimal.new("#{ta_proportion}")
final_sy_score = BigDecimal.new("#{student_work.system_score || 0}") * (BigDecimal.new('1.0') - BigDecimal.new("#{ta_proportion}"))
final_score = final_sy_score + final_te_score
student_work.final_score = format("%.2f",final_score.to_f)
else
rest_proportion = tea_ass_proportion / 3
final_sy_score = BigDecimal.new("#{student_work.system_score || 0}") * BigDecimal.new("#{sy_proportion + rest_proportion}")
final_te_score = BigDecimal.new("#{student_work.teacher_score}") * BigDecimal.new("#{tea_proportion + rest_proportion}")
final_st_score = BigDecimal.new("#{student_work.student_score}") * (BigDecimal.new('1.0') - BigDecimal.new("#{sy_proportion + rest_proportion}") - BigDecimal.new("#{tea_proportion + rest_proportion}"))
final_score = final_sy_score + final_te_score + final_st_score
student_work.final_score = format("%.2f",final_score.to_f)
end
elsif student_work.student_score.nil? #学生未评分
rest_proportion = (1 - tea_proportion - sy_proportion - tea_ass_proportion) / 3
final_sy_score = BigDecimal.new("#{student_work.system_score || 0}") * BigDecimal.new("#{sy_proportion + rest_proportion}")
final_te_score = BigDecimal.new("#{student_work.teacher_score}") * BigDecimal.new("#{tea_proportion + rest_proportion}")
final_ta_score = BigDecimal.new("#{student_work.teaching_asistant_score}") * (BigDecimal.new('1.0') - BigDecimal.new("#{sy_proportion + rest_proportion}") - BigDecimal.new("#{tea_proportion + rest_proportion}"))
final_score = final_sy_score + final_te_score + final_ta_score
student_work.final_score = format("%.2f",final_score.to_f)
else
final_sy_score = BigDecimal.new("#{student_work.system_score || 0}") * BigDecimal.new("#{sy_proportion}")
final_te_score = BigDecimal.new("#{student_work.teacher_score}") * BigDecimal.new("#{tea_proportion}")
final_ta_score = BigDecimal.new("#{student_work.teaching_asistant_score}") * BigDecimal.new("#{tea_ass_proportion}")
final_s_score = BigDecimal.new("#{student_work.student_score}") * (BigDecimal.new('1.0') - BigDecimal.new("#{sy_proportion}") - BigDecimal.new("#{tea_proportion}") - BigDecimal.new("#{tea_ass_proportion}"))
final_score = final_sy_score + final_ta_score + final_te_score + final_st_score
student_work.final_score = format("%.2f",final_score.to_f)
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 = sy_proportion + (tea_ass_proportion + tea_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 = sy_proportion + (1.0 - tea_ass_proportion - sy_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
rest_proportion = tea_proportion / 3
final_sy_score = BigDecimal.new("#{student_work.system_score || 0}") * BigDecimal.new("#{sy_proportion + rest_proportion}")
final_ts_score = BigDecimal.new("#{student_work.teaching_asistant_score}") * BigDecimal.new("#{tea_ass_proportion + rest_proportion}")
final_st_score = BigDecimal.new("#{student_work.student_score}") * (BigDecimal.new('1.0') - BigDecimal.new("#{sy_proportion + rest_proportion}") - BigDecimal.new("#{tea_ass_proportion + rest_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
if student_work.final_score
score = student_work.final_score - student_work.absence_penalty - student_work.late_penalty - student_work.appeal_penalty
student_work.work_score = format("%.2f",(score < 0 ? 0 : score).to_f) if score
else
student_work.work_score = nil
end
end
end
#更新course_homework_statistics中的avg_score和total_score
def update_avg_sum_score
course_homework_statistics = CourseHomeworkStatistics.where(:user_id => self.user_id, :course_id => self.homework_common.course_id).first
if course_homework_statistics
course = self.homework_common.course
homework_ids = course.homework_commons.empty? ? "(-1)" : "(" + course.homework_commons.map{|hw| hw.id}.join(",") + ")"
student_works = StudentWork.where("homework_common_id in #{homework_ids} and work_status !=0")
user = self.user
average_score = user.student_works.where(:id => student_works.map(&:id)).select("AVG(student_works.work_score) as score").first ? user.student_works.where(:id => student_works.map(&:id)).select("AVG(student_works.work_score) as score").first.score : 0
total_score = user.student_works.where(:id => student_works.map(&:id)).select("SUM(student_works.work_score) as score").first ? user.student_works.where(:id => student_works.map(&:id)).select("SUM(student_works.work_score) as score").first.score : 0
course_homework_statistics.update_attributes(:average_score => average_score, :total_score => total_score)
end
end
# status == 0 : delay
def act_as_message
if self.work_status != 0 && self.created_at > self.homework_common.end_time
self.course_messages << CourseMessage.new(:user_id => self.user_id, :course_id => self.homework_common.course_id, :viewed => false, :status => false)
end
end
end