2019-04-11 19:42:40 +08:00
#encoding: utf-8
#老师布置的作业表
#homework_type: 0:普通作业;1:匿评作业;2:编程作业;3:分组作业;
class HomeworkCommon < ActiveRecord :: Base
# attr_accessible :name, :user_id, :description, :publish_time, :end_time, :homework_type, :late_penalty, :course_id,:simi_time
require 'net/http'
require 'json'
include Redmine :: SafeAttributes
include ApplicationHelper
belongs_to :course
belongs_to :user
belongs_to :homework_bank
has_one :homework_detail_manual , :dependent = > :destroy
has_one :homework_detail_programing , :dependent = > :destroy
has_one :homework_detail_group , :dependent = > :destroy
has_many :student_work_projects , :dependent = > :destroy
has_many :homework_tests , :dependent = > :destroy
has_many :homework_samples , :dependent = > :destroy
has_many :student_works , :dependent = > :destroy , :conditions = > " is_test=0 and is_delete != 1 "
has_many :student_works_evaluation_distributions , :through = > :student_works #一个作业的分配的匿评列表
has_many :journals_for_messages , :as = > :jour , :dependent = > :destroy
has_many :apply_homeworks , :dependent = > :destroy
has_many :praise_tread , as : :praise_tread_object , dependent : :destroy
has_one :praise_tread_cache , as : :object , dependent : :destroy
has_many :acts , :class_name = > 'Activity' , :as = > :act , :dependent = > :destroy #用户活动
# 课程动态
has_many :course_acts , :class_name = > 'CourseActivity' , :as = > :course_act , :dependent = > :destroy
# 课程消息
has_many :course_messages , :class_name = > 'CourseMessage' , :as = > :course_message , :dependent = > :destroy
acts_as_attachable
acts_as_event :title = > Proc . new { | o | " #{ l ( :label_course_homework ) } # #{ o . id } : #{ o . name } " } ,
:description = > :description ,
:author = > :author ,
:url = > Proc . new { | o | { :controller = > 'student_work' , :action = > 'index' , :homework = > o . id } }
after_create :act_as_activity , :act_as_course_message , :delay_homework_send
after_update :update_activity
after_save :act_as_course_activity
after_destroy :delete_kindeditor_assets
before_destroy :update_homework_bank_quotes , :update_course_statistics
#删除时更新题库中的引用数
def update_homework_bank_quotes
old_banks = HomeworkBank . where ( :id = > self . homework_bank_id )
unless old_banks . blank?
old_banks . first . update_attributes ( :quotes = > ( old_banks . first . quotes - 1 ) > 0 ? ( old_banks . first . quotes - 1 ) : 0 , :homework_common_id = > nil )
end
HomeworkBank . where ( :homework_common_id = > self . id ) . update_all ( :homework_common_id = > nil )
end
#更新CourseHomeworkStatistics中每个学生的未交作品数、已交作品数、迟交作品数
def update_course_statistics
course = self . course
hw_count = course . homework_commons . includes ( :homework_detail_manual ) . where ( " homework_detail_manuals.comment_status > 0 " ) . count
homework_ids = course . homework_commons . includes ( :homework_detail_manual ) . where ( " homework_detail_manuals.comment_status > 0 " ) . empty? ? " (-1) " : " ( " + course . homework_commons . includes ( :homework_detail_manual ) . where ( " homework_detail_manuals.comment_status > 0 " ) . 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 } )
course . student . each do | student |
user = student . student
course_statistics = CourseHomeworkStatistics . find_by_course_id_and_user_id ( course . id , student . student_id )
if user && course_statistics
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
course_statistics . update_attributes ( :committed_work_num = > committed_work_num , :un_commit_work_num = > un_commit_work_num ,
: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
end
end
#作业类型
def homework_type_ch
type = self . homework_type
case type
when 1
type = " 普通 "
when 2
type = " 编程 "
when 3
type = " 分组 "
end
type
end
def act_as_activity
self . acts << Activity . new ( :user_id = > self . user_id )
end
#课程动态公共表记录
def act_as_course_activity
if self . course
if self . homework_detail_manual . comment_status == 0
self . course_acts . destroy_all
else
if self . course_acts . size == 0
self . course_acts << CourseActivity . new ( :user_id = > self . user_id , :course_id = > self . course_id )
end
end
end
end
#课程作业消息记录
def act_as_course_message
if self . course
if self . homework_detail_manual . comment_status == 0
self . course_messages . destroy_all
else
# self.course.members.each do |m|
# # if m.user_id != self.user_id
# #self.course_messages << CourseMessage.new(:user_id => m.user_id, :course_id => self.course_id, :viewed => false)
# rolesids = []
# m.roles.each do |role|
# rolesids << role.id
# end
# if rolesids.include?(10)
# count = ShieldWechatMessage.where("container_type='User' and container_id=#{m.user_id} and shield_type='Course' and shield_id=#{self.course_id}").count
# if count == 0
# ws = WechatService.new
# name = self.course.syllabus.nil? ? self.course.name : self.course.syllabus.title+" • "+self.course.name
# ws.homework_message_notice(m.user_id, "homework", self.id, "#{m.user.show_name}, #{l(:label_new_homework_template)}", name, "新作业:"+ self.name.html_safe, "作业内容请点击“详情”查看。")
# end
# end
# # end
# end
self . delay . homework_wechat_message
end
end
end
def homework_wechat_message
self . course . members . each do | m |
rolesids = [ ]
m . roles . each do | role |
rolesids << role . id
end
if rolesids . include? ( 10 )
count = ShieldWechatMessage . where ( " container_type='User' and container_id= #{ m . user_id } and shield_type='Course' and shield_id= #{ self . course_id } " ) . count
if count == 0
ws = WechatService . new
name = self . course . syllabus . nil? ? self . course . name : self . course . syllabus . title + " • " + self . course . name
ws . homework_message_notice ( m . user_id , " homework " , self . id , " #{ m . user . show_name } , #{ l ( :label_new_homework_template ) } " , name , " 新作业: " + self . name . html_safe , " 作业内容请点击“详情”查看。 " )
end
end
# end
end
end
def delay_homework_send
if self . course
if self . homework_detail_manual . comment_status != 0
vs = [ ]
self . course . members . each do | m |
if m . user_id != self . user_id
vs << { course_message_type : 'HomeworkCommon' , course_message_id : self . id , :user_id = > m . user_id ,
:course_id = > self . course_id , :viewed = > false }
#delayed_job卡住的原因是一次执行的条数太多, 导致超时。
#现在把每次只执行不超过30条, 就不会超了。
if vs . size > = 30
self . delay . contain_homework_message ( vs )
vs . clear
end
end
end
unless vs . empty?
self . delay . contain_homework_message ( vs )
end
end
end
end
def contain_homework_message ( vs )
CourseMessage . create ( vs )
end
#作业微信通知delay
def send_homework_wechat_message_delay
self . course . members . each do | m |
if m . user_id != self . user_id
self . course_messages << CourseMessage . new ( :user_id = > m . user_id , :course_id = > self . course_id , :viewed = > false )
end
rolesids = [ ]
m . roles . each do | role |
rolesids << role . id
end
if rolesids . include? ( 10 )
count = ShieldWechatMessage . where ( " container_type='User' and container_id= #{ m . user_id } and shield_type='Course' and shield_id= #{ self . course_id } " ) . count
if count == 0
ws = WechatService . new
name = self . course . syllabus . nil? ? self . course . name : self . course . syllabus . title + " • " + self . course . name
ws . homework_message_notice ( m . user_id , " homework " , self . id , " #{ m . user . show_name } , #{ l ( :label_new_homework_template ) } " , name , " 新作业: " + self . name . html_safe , " 作业内容请点击“详情”查看。 " )
end
end
# end
end
end
#动态的更新
def update_activity
update_course_activity ( self . class , self . id )
update_user_activity ( self . class , self . id )
update_org_activity ( self . class , self . id )
end
#删除对应的图片
def delete_kindeditor_assets
delete_kindeditor_assets_from_disk self . id , OwnerTypeHelper :: HOMEWORKCOMMON
end
def send_mail
# if self.homework_detail_manual.comment_status != 0
# Mailer.run.homework_added(self)
# end
end
def is_program_homework?
self . homework_type == 2 && self . homework_detail_programing
end
def is_group_homework?
self . homework_type == 3 && self . homework_detail_group
end
###添加回复
def self . add_homework_jour ( user , notes , id , root_id , options = { } )
homework = HomeworkCommon . find ( id )
if options . count == 0
jfm = homework . journals_for_messages . build ( :user_id = > user . id , :notes = > notes , :reply_id = > 0 , :root_id = > root_id )
else
jfm = homework . journals_for_messages . build ( options )
end
jfm . save
jfm
end
#修改作业后发送微信模板消息
# def wechat_message
# self.course.members.each do |member|
# count = ShieldWechatMessage.where("container_type='User' and container_id=#{member.user_id} and shield_type='Course' and shield_id=#{self.course_id}").count
# if count == 0
# ws = WechatService.new
# ws.homework_template(member.user_id, "homework", self.id, "#{l(:label_update_homework_template)}", self.course.name, self.name.html_safe, self.end_time.to_s + " 23:59:59")
# end
# end
# end
def set_jour_viewed
self . journals_for_messages . each do | j |
User . current . at_messages . unviewed ( 'JournalsForMessage' , j . id ) . each { | x | x . viewed! }
end
end
delegate :language_name , :language , :to = > :homework_detail_programing
end