103 lines
2.7 KiB
Ruby
103 lines
2.7 KiB
Ruby
# status 1:完成实训
|
|
class Myshixun < ActiveRecord::Base
|
|
attr_accessible :description, :name, :parent_id, :user_id, :gpid, :forked_from, :visits, :is_public, :identifier, :shixun_id
|
|
# has_many :users, :through => :myshixun_members
|
|
has_many :myshixun_members, :dependent => :destroy
|
|
has_one :repository, :dependent => :destroy
|
|
has_many :games, :dependent => :destroy, :order => "games.id ASC"
|
|
belongs_to :shixun
|
|
has_one :shixun_modify, :dependent => :destroy
|
|
has_one :webssh, :dependent => :destroy
|
|
has_many :student_works
|
|
|
|
def total_stage
|
|
self.games.count
|
|
end
|
|
|
|
def current_stage
|
|
game = self.games.where(:status => 0).first
|
|
position = game.blank? ? games.count : game.challenge.try(:position)
|
|
return position
|
|
end
|
|
|
|
def current_stages
|
|
game = self.games.where(:status => 1).first
|
|
position = game.blank? ? 1 : game.challenge.try(:position)
|
|
return position
|
|
end
|
|
|
|
def done_time
|
|
time = ""
|
|
if self.status == 1
|
|
time = self.games.order("end_time desc").first.try(:end_time)
|
|
end
|
|
time
|
|
end
|
|
|
|
def total_spend_time
|
|
time = 0
|
|
self.games.each do |game|
|
|
if game.status == 2
|
|
time += (game.end_time.to_i - game.open_time.to_i) > 0 ? (game.end_time.to_i - game.open_time.to_i) : 0
|
|
end
|
|
end
|
|
time
|
|
end
|
|
|
|
def total_score
|
|
score = 0
|
|
self.games.each do |game|
|
|
if game.status == 2
|
|
score += game.final_score.to_i
|
|
end
|
|
end
|
|
score
|
|
end
|
|
|
|
def total_gold
|
|
score = 0
|
|
self.games.each do |game|
|
|
if game.status == 2
|
|
total_gold =game.answer_open? ? -game.challenge.score.to_i : game.final_score.to_i
|
|
score += total_gold
|
|
end
|
|
end
|
|
score
|
|
end
|
|
|
|
|
|
# id 转换成 identifier
|
|
def to_param
|
|
identifier
|
|
end
|
|
|
|
# 当前任务:一个实训中只可能一个未完成任务(status 0或1只会存在一条记录)
|
|
# status:0 可以测评的,正在测评的
|
|
# 如果都完成,则当前任务为最后一个任务
|
|
def current_task
|
|
games = self.games
|
|
current_game = games.select{|game| game.status == 1 || game.status == 0}.first
|
|
if current_game.blank?
|
|
game_id = '(' + games.map(&:id).join(',') + ')'
|
|
passed_games = games.find_by_sql("SELECT *, (SELECT c.position from `challenges` c where g.challenge_id = c.id) as pos FROM `games` g where g.status = 2 and g.id in #{game_id} ORDER BY pos desc")
|
|
current_game = passed_games.first
|
|
end
|
|
return current_game
|
|
end
|
|
|
|
def owner
|
|
User.find(self.user_id)
|
|
rescue ActiveRecord::RecordNotFound
|
|
render_404
|
|
end
|
|
|
|
def score
|
|
game = self.games.where(:status => [0, 1]).first
|
|
unless game.blank?
|
|
score = game.challenge.try(:score)
|
|
end
|
|
return score
|
|
end
|
|
|
|
end
|