2017-01-12 17:27:04 +08:00
#encoding: utf-8
2013-09-12 10:41:15 +08:00
class StudentsForCourse < ActiveRecord :: Base
attr_accessible :course_id , :student_id
2014-06-16 17:14:05 +08:00
belongs_to :course , :class_name = > 'Course' , :foreign_key = > :course_id
2013-09-12 10:41:15 +08:00
belongs_to :student , :class_name = > 'User' , :foreign_key = > :student_id
validates_presence_of :course_id , :student_id
validates_uniqueness_of :student_id , :scope = > :course_id
2016-11-11 13:58:07 +08:00
2017-01-20 10:16:51 +08:00
after_destroy :delete_student_works , :delete_course_homework_statistic
2017-02-10 17:04:10 +08:00
after_create :recovery_student_works , :create_student_works , :create_course_homework_statistic , :create_course_contribute_score
2016-11-11 13:58:07 +08:00
2016-11-17 10:23:13 +08:00
#退出班级或删除学生时隐藏学生的作品
2016-11-11 13:58:07 +08:00
def delete_student_works
course = self . course
homework_ids = course . homework_commons . blank? ? " (-1) " : " ( " + course . homework_commons . map { | hw | hw . id } . join ( " , " ) + " ) "
student_works = StudentWork . where ( " user_id = #{ self . student_id } && homework_common_id in #{ homework_ids } " )
2016-11-17 10:23:13 +08:00
student_works . update_all ( :is_delete = > 1 )
end
#加入班级时还原作品
def recovery_student_works
course = self . course
homework_ids = course . homework_commons . blank? ? " (-1) " : " ( " + course . homework_commons . map { | hw | hw . id } . join ( " , " ) + " ) "
student_works = StudentWork . where ( " user_id = #{ self . student_id } && homework_common_id in #{ homework_ids } " )
student_works . update_all ( :is_delete = > 0 )
2016-11-11 13:58:07 +08:00
end
2017-01-12 17:27:04 +08:00
#加入班级时创建已发布作业的作品
def create_student_works
course = self . course
course . homework_commons . each do | hw |
if hw . homework_type != 3 && hw . student_works . where ( " user_id = #{ self . student_id } " ) . count == 0
hw . student_works << StudentWork . new ( :user_id = > self . student_id , :name = > hw . name . to_s + " 的作品提交 " , :work_status = > 0 )
end
end
end
2017-01-20 10:16:51 +08:00
#加入班级时创建一条记录
def create_course_homework_statistic
2017-02-10 16:11:33 +08:00
if CourseHomeworkStatistics . where ( :user_id = > self . student_id , :course_id = > self . course_id ) . count == 0
2017-02-10 14:35:43 +08:00
course = self . course
user = self . student
hw_count = course . homework_commons . includes ( :homework_detail_manual ) . where ( " homework_detail_manuals.comment_status > 0 " ) . count
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 " )
is_eva_homeworks = course . homework_commons . includes ( :homework_detail_manual ) . where ( " homework_commons.anonymous_comment = 0 and homework_detail_manuals.comment_status = 2 " )
is_eva_student_works = StudentWork . where ( :homework_common_id = > is_eva_homeworks . map { | hw | hw . id } )
has_eva_homeworks = course . homework_commons . includes ( :homework_detail_manual ) . where ( " homework_commons.anonymous_comment = 0 and homework_detail_manuals.comment_status = 3 " )
has_eva_student_works = StudentWork . where ( :homework_common_id = > has_eva_homeworks . map { | hw | hw . id } )
committed_work_num = user . student_works . where ( " homework_common_id in #{ homework_ids } and work_status != 0 " ) . count
un_commit_work_num = ( hw_count - committed_work_num ) < 0 ? 0 : ( hw_count - committed_work_num )
late_commit_work_num = user . student_works . where ( " homework_common_id in #{ homework_ids } and work_status = 2 " ) . count
absence_evaluation_work_num = user . student_works_evaluation_distributions . where ( :student_work_id = > has_eva_student_works . map ( & :id ) ) . count -
user . student_works_scores . where ( :reviewer_role = > 3 , :student_work_id = > has_eva_student_works . map ( & :id ) ) . group_by ( & :student_work_id ) . count
absence_evaluation_work_num = absence_evaluation_work_num < 0 ? 0 : absence_evaluation_work_num
un_evaluation_work_num = user . student_works_evaluation_distributions . where ( :student_work_id = > is_eva_student_works . map ( & :id ) ) . count -
user . student_works_scores . where ( :reviewer_role = > 3 , :student_work_id = > is_eva_student_works . map ( & :id ) ) . group_by ( & :student_work_id ) . count
un_evaluation_work_num = un_evaluation_work_num < 0 ? 0 : un_evaluation_work_num
appeal_num = user . student_works_scores . where ( :student_work_id = > student_works . map ( & :id ) , :appeal_status = > 3 ) . count
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
2017-02-10 16:11:33 +08:00
CourseHomeworkStatistics . create ( :course_id = > course . id , :user_id = > user . id , :committed_work_num = > committed_work_num , :un_commit_work_num = > un_commit_work_num ,
2017-02-10 14:35:43 +08:00
:late_commit_work_num = > late_commit_work_num , :absence_evaluation_work_num = > absence_evaluation_work_num , :un_evaluation_work_num = > un_evaluation_work_num ,
:appeal_num = > appeal_num , :average_score = > average_score , :total_score = > total_score )
end
2017-01-20 10:16:51 +08:00
end
def delete_course_homework_statistic
2017-02-10 16:11:33 +08:00
CourseHomeworkStatistics . where ( :user_id = > self . student_id , :course_id = > self . course_id ) . destroy_all
2017-01-20 10:16:51 +08:00
end
2017-02-10 17:04:10 +08:00
def create_course_contribute_score
ccs = CourseContributorScore . where ( " course_id =? and user_id =? " , self . course_id , self . student_id ) . first
unless ccs
course = self . course
user_id = self . student_id
message_num = Message . find_by_sql ( " select DISTINCT me.* from messages me, boards b where b.id = me.board_id and b.course_id = #{ course . id } and b.project_id = '-1' and me.author_id = #{ user_id } and me.parent_id is null; " ) . count
message_reply_num = Message . find_by_sql ( " select DISTINCT me.* from messages me, boards b where b.id = me.board_id and b.course_id = #{ course . id } and b.project_id = '-1' and me.author_id = #{ user_id } and me.parent_id is not null " ) . count
news_reply_num = Comment . find_by_sql ( " select cm.* from comments cm, news n where cm.author_id = #{ user_id } and n.course_id = #{ course . id } and cm.commented_id = n.id and cm.commented_type ='News' " ) . count
resource_num = Attachment . find_by_sql ( " SELECT * FROM `attachments` where container_id = #{ course . id } and author_id = #{ user_id } and container_type ='Course' " ) . count
journal_num = JournalsForMessage . where ( " user_id =? and jour_id =? and jour_type =? " , user_id , course . id , " Course " ) . count
journal_reply_num = JournalsForMessage . where ( " user_id =? and jour_id =? and jour_type =? and m_parent_id is not null " , user_id , course . id , " Course " ) . count
homework_ids = course . homework_commons . empty? ? " (-1) " : " ( " + course . homework_commons . map ( & :id ) . join ( ',' ) + " ) "
homework_journal_num = JournalsForMessage . where ( " user_id =? and jour_id in #{ homework_ids } and jour_type =? " , user_id , " HomeworkCommon " ) . count
news_num = News . where ( " author_id = #{ user_id } and course_id = #{ course . id } " ) . count
total = message_num * 2 + message_reply_num * 1 + news_reply_num * 1 + news_num * 1 + resource_num * 5 + journal_num * 1 + homework_journal_num * 1
CourseContributorScore . create ( :course_id = > self . course_id , :user_id = > self . student_id , :message_num = > message_num , :message_reply_num = > message_reply_num , :news_reply_num = > news_reply_num ,
:resource_num = > resource_num , :journal_num = > journal_num , :journal_reply_num = > journal_reply_num , :total_score = > total , :homework_journal_num = > homework_journal_num , :news_num = > news_num )
end
end
2013-09-12 10:41:15 +08:00
end