2016-09-06 15:07:43 +08:00
#学生提交作品表 #work_status :0 未提交 1 已提交 2 迟交 3 分组作品复制的组员作品
2015-05-19 10:36:30 +08:00
class StudentWork < ActiveRecord :: Base
2016-10-19 18:09:29 +08:00
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
2015-05-19 10:36:30 +08:00
belongs_to :homework_common
belongs_to :user
2016-11-14 17:32:18 +08:00
has_many :student_work_projects , :dependent = > :destroy
2015-05-19 10:56:41 +08:00
has_many :student_works_evaluation_distributions , :dependent = > :destroy
2015-05-19 11:18:19 +08:00
has_many :student_works_scores , :dependent = > :destroy
2015-05-21 10:54:02 +08:00
belongs_to :project
2015-08-28 12:04:49 +08:00
has_many :student_work_tests , order : 'id desc'
2015-10-20 16:19:32 +08:00
# course's message
has_many :course_messages , :class_name = > 'CourseMessage' , :as = > :course_message , :dependent = > :destroy
2015-12-04 16:21:11 +08:00
has_many :attachments , :dependent = > :destroy
2015-05-19 17:12:43 +08:00
2016-09-06 15:07:43 +08:00
scope :has_committed , lambda { where ( " work_status != 0 and work_status != 3 " ) }
scope :no_copy , lambda { where ( " work_status != 3 " ) }
2016-07-08 13:17:14 +08:00
2015-07-17 14:48:20 +08:00
before_destroy :delete_praise
2015-09-09 00:43:54 +08:00
before_save :set_program_score , :set_src
2015-07-17 14:48:20 +08:00
2017-02-10 14:35:43 +08:00
after_save :update_avg_sum_score
2015-10-19 15:45:24 +08:00
after_create :act_as_message
2015-05-19 17:12:43 +08:00
acts_as_attachable
2015-07-17 14:48:20 +08:00
def delete_praise
PraiseTread . where ( " praise_tread_object_id = #{ self . id } AND praise_tread_object_type = 'StudentWork' " ) . destroy_all
end
2015-09-09 00:43:54 +08:00
def last_test
student_work_tests . order ( 'id desc' ) . first
end
2016-10-19 18:09:29 +08:00
private
2015-09-09 00:43:54 +08:00
def set_program_score
2016-09-06 16:38:29 +08:00
if self . homework_common . is_program_homework? #编程作业,学生提交作品后计算系统得分
2016-10-19 18:09:29 +08:00
#根据最后一次测试计算得分
unless last_test
self . system_score = 0
else
self . system_score = last_test . test_score self . homework_common . homework_tests . size
end
2015-09-09 00:43:54 +08:00
end
2015-09-18 17:16:31 +08:00
set_final_score self . homework_common , self
2015-09-09 00:43:54 +08:00
end
def set_src
self . description = last_test . src if last_test
end
2015-09-18 11:05:21 +08:00
#成绩计算
def set_final_score homework , student_work
if homework && homework . homework_detail_manual
2016-11-04 09:23:23 +08:00
if homework . homework_type != 2 #非编程作业
2015-09-18 11:05:21 +08:00
if homework . teacher_priority == 1 #教师优先
if student_work . teacher_score
student_work . final_score = student_work . teacher_score
2016-12-15 10:19:16 +08:00
student_work . work_score = student_work . teacher_score - student_work . absence_penalty - student_work . late_penalty - student_work . appeal_penalty
2015-09-18 11:05:21 +08:00
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
2016-05-03 15:12:56 +08:00
if student_work . final_score
2016-12-15 10:19:16 +08:00
score = student_work . final_score - student_work . absence_penalty - student_work . late_penalty - student_work . appeal_penalty
2016-05-03 15:12:56 +08:00
student_work . work_score = format ( " %.2f " , ( score < 0 ? 0 : score ) . to_f ) if score
else
student_work . work_score = nil
end
2015-09-18 11:05:21 +08:00
end
else #不考虑教师评分
2016-05-03 15:12:56 +08:00
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?
2015-09-18 11:05:21 +08:00
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
2016-05-03 15:12:56 +08:00
if student_work . final_score
2016-12-15 10:19:16 +08:00
score = student_work . final_score - student_work . absence_penalty - student_work . late_penalty - student_work . appeal_penalty
2016-05-03 15:12:56 +08:00
student_work . work_score = format ( " %.2f " , ( score < 0 ? 0 : score ) . to_f ) if score
else
student_work . work_score = nil
end
2015-09-18 11:05:21 +08:00
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
2016-12-15 10:19:16 +08:00
student_work . work_score = student_work . teacher_score - student_work . absence_penalty - student_work . late_penalty - student_work . appeal_penalty
2015-09-18 11:05:21 +08:00
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
2016-05-03 15:12:56 +08:00
if student_work . final_score
2016-12-15 10:19:16 +08:00
score = student_work . final_score - student_work . absence_penalty - student_work . late_penalty - student_work . appeal_penalty
2016-05-03 15:12:56 +08:00
student_work . work_score = format ( " %.2f " , ( score < 0 ? 0 : score ) . to_f ) if score
else
student_work . work_score = nil
end
2016-10-19 18:09:29 +08:00
end
2015-09-18 11:05:21 +08:00
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
2016-05-03 15:12:56 +08:00
if student_work . final_score
2016-12-15 10:19:16 +08:00
score = student_work . final_score - student_work . absence_penalty - student_work . late_penalty - student_work . appeal_penalty
2016-05-03 15:12:56 +08:00
student_work . work_score = format ( " %.2f " , ( score < 0 ? 0 : score ) . to_f ) if score
else
student_work . work_score = nil
end
2015-09-18 11:05:21 +08:00
end
end
end
end
2015-10-19 15:45:24 +08:00
2017-02-10 14:35:43 +08:00
#更新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
2015-10-20 16:19:32 +08:00
# status == 0 : delay
2015-10-19 15:45:24 +08:00
def act_as_message
2016-07-08 13:17:14 +08:00
if self . work_status != 0 && self . created_at > self . homework_common . end_time + 1
2015-10-20 16:19:32 +08:00
self . course_messages << CourseMessage . new ( :user_id = > self . user_id , :course_id = > self . homework_common . course_id , :viewed = > false , :status = > false )
2015-10-19 15:45:24 +08:00
end
end
2015-05-19 10:36:30 +08:00
end