socialforge/app/models/exercise.rb

34 lines
1.4 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class Exercise < ActiveRecord::Base
#exercise_status: 1,新建2发布3关闭
include Redmine::SafeAttributes
belongs_to :user
belongs_to :course ,:touch => true
has_many :exercise_questions, :dependent => :destroy,:order => "#{ExerciseQuestion.table_name}.question_number"
has_many :exercise_users, :dependent => :destroy
has_many :users, :through => :exercise_users #该测试被哪些用户提交答案过
# 课程消息
has_many :course_messages, :class_name =>'CourseMessage', :as => :course_message, :dependent => :destroy
after_save :acts_as_course_message
def acts_as_course_message
if self.course
if self.exercise_status == 2 && self.course_messages.where(:status => 2).blank? #已发布
self.delay.send_exercise_message_delay
#self.course.members.each do |m|
#self.course_messages << CourseMessage.create(:user_id => User.current.id, :course_id => self.course_id, :viewed => false,:status=>2)
#end
else
#self.course_messages.destroy_all #这里的destory_all值得商榷。因为我这里是通过status来控制不同的status的
end
end
end
#测验通知delay
def send_exercise_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 ,:status=>2)
end
end
end
end