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/challenge.rb

150 lines
4.0 KiB

6 years ago
#coding=utf-8
# st 0实践任务1选择题任务
# show_type: [1, "图片"], [2, "apk/exe"], [3, "txt"], [4, "html"]
# modify_time 与games表的modify_time联合使用2个字段一致则表示测试集未修改反之被修改
class Challenge < ActiveRecord::Base
default_scope :order => 'position'
belongs_to :shixun, :touch=> true, counter_cache: 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
has_many :homework_challenge_settings
has_many :praise_tread, as: :praise_tread_object, dependent: :destroy
has_one :praise_tread_cache, as: :object, dependent: :destroy
has_many :tidings
acts_as_attachable
validates_presence_of :subject
# validates_presence_of :score
validates_presence_of :task_pass
validates_length_of :subject, :maximum => 255
scope :min, lambda { select([:id, :subject, :position, :shixun_id, :st, :score, :path, :task_pass, :modify_time, :web_route, :answer]) }
scope :choose_type, lambda{where(st: 1)}
scope :practice_type, lambda{where(st: 0)}
def game_difficulty
str = "简单"
case self.difficulty
when 1
str = "简单"
when 2
str = "中等"
when 3
str = "困难"
end
str
end
## 用户关卡状态 0: 不能开启实训; 1:直接开启; 2表示已完成
def user_tpi_status(user_id)
# todo: 以前没加索引导致相同关卡,同一用户有多个games
game = self.games.where(user_id: user_id).last
status =
if game.blank?
self.position == 1 ? 1 : 0
elsif game.status == 2
2
else
1
end
end
def challenge_file_show_type
[[1, "图片"], [2, "apk/exe"], [3, "txt"], [4, "html"]]
end
alias :cfst :challenge_file_show_type
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
if self.st == 1
self.challenge_chooses.each do |choose|
score += choose.score.to_i
end
else
return self.score
end
return score
end
def choose_tags_num
num = 0
self.challenge_chooses.includes(:challenge_tags).each do |choose|
num += choose.challenge_tags.count
end
return num
end
def tags_show
if self.challenge_tags.nil?
"--"
else
self.try(:challenge_tags).map(&:name).join(";")
end
end
def next_challenge
challenge_count = Challenge.where(:shixun_id => self.shixun_id).count
if self.position != challenge_count
Challenge.where(:position => (self.position + 1), :shixun_id => self.shixun).first
end
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
# # 当前用户开启的实训中应进入的game关卡
# # 为了节约性能而写的
# def game
# self.games.where(:user_id => )
# end
def user_myshixun
Myshixun.where(:shixun_id => self.shixun.id, :user_id => User.current.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
# 通关的games
def passed_games
self.games.where(:status => 2)
end
end