2016-04-08 18:52:26 +08:00
#encoding: utf-8
2015-05-19 09:49:26 +08:00
#老师布置的作业表
2015-05-20 16:49:19 +08:00
#homework_type: 0:普通作业;1:匿评作业;2:编程作业
2015-05-19 09:49:26 +08:00
class HomeworkCommon < ActiveRecord :: Base
2016-04-14 11:29:22 +08:00
# attr_accessible :name, :user_id, :description, :publish_time, :end_time, :homework_type, :late_penalty, :course_id,:simi_time
2016-03-31 20:46:36 +08:00
require 'net/http'
require 'json'
2015-05-20 15:05:35 +08:00
include Redmine :: SafeAttributes
2015-06-02 11:59:59 +08:00
include ApplicationHelper
2015-05-19 09:49:26 +08:00
belongs_to :course
belongs_to :user
2015-05-19 10:20:07 +08:00
has_one :homework_detail_manual , :dependent = > :destroy
has_one :homework_detail_programing , :dependent = > :destroy
2015-12-03 09:17:36 +08:00
has_one :homework_detail_group , :dependent = > :destroy
2015-12-11 14:57:09 +08:00
has_many :student_work_projects , :dependent = > :destroy
2015-05-19 10:20:07 +08:00
has_many :homework_tests , :dependent = > :destroy
2016-11-17 10:23:13 +08:00
has_many :student_works , :dependent = > :destroy , :conditions = > " is_test=0 and is_delete != 1 "
2015-05-19 11:18:19 +08:00
has_many :student_works_evaluation_distributions , :through = > :student_works #一个作业的分配的匿评列表
2015-10-22 09:20:19 +08:00
has_many :journals_for_messages , :as = > :jour , :dependent = > :destroy
2016-05-12 09:30:06 +08:00
has_many :apply_homeworks , :dependent = > :destroy
2016-09-23 16:44:49 +08:00
has_many :praise_tread , as : :praise_tread_object , dependent : :destroy
has_one :praise_tread_cache , as : :object , dependent : :destroy
2015-06-02 11:58:21 +08:00
has_many :acts , :class_name = > 'Activity' , :as = > :act , :dependent = > :destroy #用户活动
2015-08-11 15:54:05 +08:00
# 课程动态
has_many :course_acts , :class_name = > 'CourseActivity' , :as = > :course_act , :dependent = > :destroy
2015-08-14 15:48:23 +08:00
# 课程消息
has_many :course_messages , :class_name = > 'CourseMessage' , :as = > :course_message , :dependent = > :destroy
2015-05-19 17:12:43 +08:00
acts_as_attachable
2015-06-02 14:35:17 +08:00
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 } }
2016-05-18 14:49:49 +08:00
after_create :act_as_activity , :send_mail , :act_as_course_message
2016-06-02 13:56:11 +08:00
after_update :update_activity
2015-10-30 13:58:54 +08:00
after_save :act_as_course_activity
2015-06-02 11:58:21 +08:00
after_destroy :delete_kindeditor_assets
2016-08-05 16:12:53 +08:00
#作业类型
def homework_type_ch
type = self . homework_type
case type
when 1
type = " 普通 "
when 2
type = " 编程 "
when 3
type = " 分组 "
end
type
end
2015-06-02 11:58:21 +08:00
def act_as_activity
self . acts << Activity . new ( :user_id = > self . user_id )
end
2015-08-11 15:54:05 +08:00
#课程动态公共表记录
def act_as_course_activity
if self . course
2015-10-30 13:58:54 +08:00
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
2015-08-11 15:54:05 +08:00
end
end
2015-08-14 15:48:23 +08:00
#课程作业消息记录
def act_as_course_message
if self . course
2015-10-30 13:58:54 +08:00
if self . homework_detail_manual . comment_status == 0
self . course_messages . destroy_all
else
2016-11-11 09:28:10 +08:00
# 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 . send_homework_wechat_message_delay
end
end
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)
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 , " 作业内容请点击“详情”查看。 " )
2015-10-30 13:58:54 +08:00
end
2015-08-14 15:48:23 +08:00
end
2016-11-11 09:28:10 +08:00
# end
2015-08-14 15:48:23 +08:00
end
end
2016-11-11 09:28:10 +08:00
2016-01-12 15:53:15 +08:00
#动态的更新
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
2015-06-02 11:58:21 +08:00
#删除对应的图片
def delete_kindeditor_assets
delete_kindeditor_assets_from_disk self . id , OwnerTypeHelper :: HOMEWORKCOMMON
end
2015-06-18 10:23:51 +08:00
def send_mail
2015-10-30 13:58:54 +08:00
if self . homework_detail_manual . comment_status != 0
Mailer . run . homework_added ( self )
end
2015-06-18 10:23:51 +08:00
end
2015-09-09 00:43:54 +08:00
def is_program_homework?
self . homework_type == 2 && self . homework_detail_programing
end
2015-12-03 09:17:36 +08:00
def is_group_homework?
self . homework_type == 3 && self . homework_detail_group
end
2015-10-22 09:20:19 +08:00
###添加回复
2016-10-11 14:08:44 +08:00
def self . add_homework_jour ( user , notes , id , root_id , options = { } )
2015-10-22 09:20:19 +08:00
homework = HomeworkCommon . find ( id )
if options . count == 0
2016-10-11 14:08:44 +08:00
jfm = homework . journals_for_messages . build ( :user_id = > user . id , :notes = > notes , :reply_id = > 0 , :root_id = > root_id )
2015-10-22 09:20:19 +08:00
else
jfm = homework . journals_for_messages . build ( options )
end
jfm . save
jfm
end
2016-03-31 20:46:36 +08:00
#修改作业后发送微信模板消息
2016-06-02 10:39:11 +08:00
# 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
2016-03-31 20:46:36 +08:00
2016-11-10 10:41:35 +08:00
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
2015-09-11 14:59:30 +08:00
delegate :language_name , :language , :to = > :homework_detail_programing
2015-08-28 12:04:49 +08:00
2015-05-19 09:49:26 +08:00
end