59 lines
1.6 KiB
Ruby
59 lines
1.6 KiB
Ruby
#coding=utf-8
|
||
# st 0:实践任务;1:多选任务;2:单选任务
|
||
class Challenge < ActiveRecord::Base
|
||
default_scope :order => 'position'
|
||
belongs_to :shixun,:touch=> true
|
||
belongs_to :user
|
||
has_many :challenge_samples, :dependent => :destroy
|
||
has_many :test_sets, :dependent => :destroy
|
||
has_many :challenge_tags, :dependent => :destroy
|
||
has_many :games, :dependent => :destroy
|
||
has_many :challenge_questions, :dependent => :destroy
|
||
|
||
validates_presence_of :subject
|
||
# validates_presence_of :score
|
||
validates_presence_of :task_pass
|
||
validates_length_of :subject, :maximum => 255
|
||
|
||
def game_difficulty
|
||
str = "简单"
|
||
case self.difficulty
|
||
when 1
|
||
str = "简单"
|
||
when 2
|
||
str = "中等"
|
||
when 3
|
||
str = "困难"
|
||
end
|
||
str
|
||
end
|
||
|
||
def next_challenge
|
||
challenge_count = Challenge.where(:shixun_id => self.shixun_id).count
|
||
render_404 if self.position ==challenge_count
|
||
Challenge.where(:position => (self.position + 1), :shixun_id => self.shixun).first
|
||
end
|
||
|
||
def last_challenge
|
||
render_404 if self.position < 2
|
||
position = self.position - 1
|
||
Challenge.where(:position => position, :shixun_id => self.shixun).first
|
||
end
|
||
|
||
def current_user_game
|
||
myshixun = Myshixun.where(:shixun_id => self.shixun.id, :user_id => User.current.id).first
|
||
game = Game.where(:myshixun_id => myshixun, :challenge_id => self.id).first
|
||
end
|
||
|
||
# 正确答案
|
||
def right_answers
|
||
answer = ""
|
||
self.challenge_questions.each do |ans|
|
||
if ans.right_key
|
||
answer << (ans.position + 65).chr
|
||
end
|
||
end
|
||
return answer
|
||
end
|
||
end
|