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.
pgfqe6ch8/app/models/exercise.rb

77 lines
2.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

class Exercise < ActiveRecord::Base
#exercise_status: 1,新建2发布3关闭
include Redmine::SafeAttributes
belongs_to :user
belongs_to :course ,:touch => true
belongs_to :exercise_bank
has_many :exercise_group_settings, :dependent => :destroy
has_many :exercise_questions, :dependent => :destroy
has_many :exercise_users, :dependent => :destroy, :conditions => "exercise_users.is_delete = 0"
has_many :users, :through => :exercise_users #该测试被哪些用户提交答案过
has_many :course_acts, :class_name => 'CourseActivity',:as =>:course_act ,:dependent => :destroy
# 课程消息
has_many :tidings, as: :container, dependent: :destroy
has_many :course_messages, :class_name =>'CourseMessage', :as => :course_message, :dependent => :destroy
has_many :exercise_level_settings, :dependent => :destroy
has_many :exercise_user_questions
after_create :acts_as_course_message
def acts_as_course_message
if self.course
if self.exercise_status == 2 #未发布
#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
def create_user_question_list exercise_user_id
self.exercise_questions.where("question_type != 5").each do |question|
ExerciseUserQuestion.create(exercise_id: self.id, exercise_user_id: exercise_user_id, exercise_question_id: question.id)
end
self.exercise_level_settings.each do |setting|
# 每个级别中随机挑选num个实训题
setting.exercise_questions.pluck(:id).sample(setting.num).each do |question_id|
ExerciseUserQuestion.create(exercise_id: self.id, exercise_user_id: exercise_user_id, exercise_question_id: question_id)
end
end
end
def user_question_list exercise_user_id
if self.exercise_user_questions.where(exercise_user_id: exercise_user_id).count == 0
self.exercise_questions
else
self.exercise_questions.where(id: self.exercise_user_questions.where(exercise_user_id: exercise_user_id).
pluck(:exercise_question_id))
end
end
def level_setting level
self.exercise_level_settings.where(level: level).first
end
def shixun_question_count
self.exercise_level_settings.sum(:num)
end
def shixun_question_score
score = 0
self.exercise_level_settings.each do |setting|
score += setting.num * setting.score.to_i
end
score
end
def has_shixun_questions?
self.exercise_questions.where("question_type = 5 and exercise_level_setting_id != 0").count > 0
end
def exercise_question_size
self.exercise_questions.where("question_type != 5").count + self.shixun_question_count
end
end