You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							147 lines
						
					
					
						
							4.9 KiB
						
					
					
				
			
		
		
	
	
							147 lines
						
					
					
						
							4.9 KiB
						
					
					
				#encoding: utf-8
 | 
						|
#老师布置的作业表
 | 
						|
#homework_type: 0:普通作业;1:匿评作业;2:编程作业
 | 
						|
class HomeworkCommon < ActiveRecord::Base
 | 
						|
  # attr_accessible :name, :user_id, :description, :publish_time, :end_time, :homework_type, :late_penalty, :course_id
 | 
						|
  require 'net/http'
 | 
						|
  require 'json'
 | 
						|
  include Redmine::SafeAttributes
 | 
						|
  include ApplicationHelper
 | 
						|
 | 
						|
  belongs_to :course
 | 
						|
  belongs_to :user
 | 
						|
  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 :student_works, :dependent => :destroy, :conditions => "is_test=0"
 | 
						|
  has_many :student_works_evaluation_distributions, :through => :student_works #一个作业的分配的匿评列表
 | 
						|
  has_many :journals_for_messages, :as => :jour, :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, :send_mail, :act_as_course_message
 | 
						|
  after_update :update_activity, :wechat_message
 | 
						|
  after_save :act_as_course_activity
 | 
						|
  after_destroy :delete_kindeditor_assets
 | 
						|
 | 
						|
  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)
 | 
						|
          # 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 , options = {})
 | 
						|
    homework = HomeworkCommon.find(id)
 | 
						|
    if options.count == 0
 | 
						|
      jfm = homework.journals_for_messages.build(:user_id => user.id, :notes => notes, :reply_id => 0)
 | 
						|
    else
 | 
						|
      jfm = homework.journals_for_messages.build(options)
 | 
						|
    end
 | 
						|
    jfm.save
 | 
						|
    jfm
 | 
						|
  end
 | 
						|
 | 
						|
  #修改作业后发送微信模板消息
 | 
						|
  def wechat_message
 | 
						|
    self.course.members.each do |member|
 | 
						|
      uw = UserWechat.where("user_id=?", member.user_id).first
 | 
						|
      unless uw.nil?
 | 
						|
        data = {
 | 
						|
            touser:uw.openid,
 | 
						|
            template_id:"3e5Dj2GIx8MOcMyRKpTUEQnM7Tg0ASSCNc01NS9HCGI",
 | 
						|
            url:"http://weixin.qq.com/download",
 | 
						|
            topcolor:"#FF0000",
 | 
						|
            data:{
 | 
						|
                first: {
 | 
						|
                    value:"您的作业已被修改",
 | 
						|
                    color:"#173177"
 | 
						|
                },
 | 
						|
                keyword1:{
 | 
						|
                    value:self.course.name,
 | 
						|
                    color:"#173177"
 | 
						|
                },
 | 
						|
                keyword2:{
 | 
						|
                    value:self.name,
 | 
						|
                    color:"#173177"
 | 
						|
                },
 | 
						|
                keyword3:{
 | 
						|
                    value:self.end_time.to_s + "23:59:59",
 | 
						|
                    color:"#173177"
 | 
						|
                },
 | 
						|
                remark:{
 | 
						|
                    value:"具体内容请点击详情查看网站",
 | 
						|
                    color:"#173177"
 | 
						|
                }
 | 
						|
            }
 | 
						|
        }
 | 
						|
        logger.info "start send template message: #{data}"
 | 
						|
        req = Wechat.api.template_message_send Wechat::Message.to(uw.openid).template(data)
 | 
						|
        logger.info "send over. #{req}"
 | 
						|
      end
 | 
						|
    end
 | 
						|
  end
 | 
						|
 | 
						|
  delegate :language_name, :language, :to => :homework_detail_programing
 | 
						|
 | 
						|
end
 |