educoder/app/models/challenge.rb

59 lines
1.6 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.

#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