educoder/app/models/challenge.rb

93 lines
2.3 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选择题任务
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_chooses, :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 choose_correct_num
num = 0
self.challenge_chooses.each do |choose|
outputs = ChooseOutputs.where(:challenge_choose_id => choose.id, :user_id => User.current.id).first
if outputs.nil?
num = nil
else
num += (outputs.correct ? 1 : 0)
end
end
return num
end
def choose_score
score = 0
self.challenge_chooses.each do |choose|
score += choose.score.to_i
end
return score
end
def choose_tags_num
num = 0
self.challenge_chooses.each do |choose|
num += choose.challenge_tags.count
end
return num
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 prev_challenge
render_404 if self.position == 1
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