@ -1,26 +1,71 @@
class MigrationShixunStudentWork < ActiveRecord :: Migration
def up
works = StudentWork . where ( " myshixun_id != 0 and myshixun_id not in (select id from myshixuns) and (work_score is null or work_score = 0) " )
works . update_all ( :myshixun_id = > 0 , :work_status = > 0 , :work_score = > nil , :final_score = > nil ,
:cost_time = > 0 , :update_time = > nil , :compelete_status = > 0 , :commit_time = > nil )
ActiveRecord :: Base . transaction do
begin
works = StudentWork . where ( " myshixun_id != 0 and myshixun_id not in (select id from myshixuns) and (work_score is null or work_score = 0) " )
works . update_all ( :myshixun_id = > 0 , :work_status = > 0 , :work_score = > nil , :final_score = > nil ,
:cost_time = > 0 , :update_time = > nil , :compelete_status = > 0 , :commit_time = > nil )
shixun_works = StudentWork . where ( " myshixun_id != 0 and myshixun_id not in (select id from myshixuns) and work_score > 0 " )
shixun_works . each do | work |
if work . homework_common && work . homework_common . homework_commons_shixuns . try ( :shixun )
shixun = work . homework_common . homework_commons_shixuns . try ( :shixun )
shixun_works = StudentWork . where ( " myshixun_id != 0 and myshixun_id not in (select id from myshixuns) and work_score > 0 " )
shixun_works . each do | work |
shixun = work . homework_common . try ( :homework_commons_shixuns ) . try ( :shixun )
user = work . user
if shixun . present? && user . present?
# 创建新的myshixun和games
# fork版本库, 如果用户没有同步, 则先同步用户
g = Gitlab . client
if user . gid . nil?
s = Trustie :: Gitlab :: Sync . new
s . sync_user ( user )
end
gshixun = g . fork ( shixun . gpid , user . gid )
shixun_tomcat = Redmine :: Configuration [ 'shixun_tomcat' ]
code = down_generate_identifier ( " myshixun " )
# 一般通过默认分支是否存在来判断一个项目是否fork成功
if gshixun . try ( :id ) . present?
commit_id = g . commits ( shixun . gpid ) . first . try ( :id )
# educoder 加入到myshixun中
myshixun_admin_gid = User . where ( :login = > " educoder " ) . first . try ( :gid )
g . add_team_member ( gshixun . id , myshixun_admin_gid , 40 ) # 40代表角色master
# 创建新的myshixun和games
myshixun = nil
myshixun = Myshixun . create! ( :shixun_id = > shixun . id , :user_id = > user . id , :identifier = > code ,
:modify_time = > shixun . modify_time , :reset_time = > shixun . reset_time ,
:onclick_time = > Time . now , :gpid = > gshixun . id ,
:git_url = > gshixun . try ( :path_with_namespace ) , :commit_id = > commit_id )
work_score = work . final_score
work . homework_common . homework_challenge_settings . each do | setting |
if work_score > 1
myshixun . games . where ( :challenge_id = > setting . challenge_id ) . first . update_attributes ( :status = > 2 )
work_score -= setting . score
rep_url = Base64 . urlsafe_encode64 ( gitlab_url shixun ) # 注意: educoder为默认给实训创建版本库的用户, 如果换成别的用户, 名字要相应的修改
uri = " #{ shixun_tomcat } /bridge/game/openGameInstance "
params = { tpiID : " #{ myshixun . id } " , tpmGitURL : rep_url , tpiRepoName : gshixun . try ( :name ) }
logger . info ( " openGameInstance params is #{ params } " )
res = uri_exec uri , params
if ( res && res [ 'code' ] . to_i != 0 )
raise ( " 实训云平台繁忙( 繁忙等级: 83) " )
end
# 其它创建关卡等操作
challenges = shixun . challenges
# 之所以增加user_id是为了方便统计查询性能
challenges . each_with_index do | challenge , index |
status = ( index == 0 ? 0 : 3 )
code = down_generate_identifier ( " game " )
Game . create! ( :challenge_id = > challenge . id , :myshixun_id = > myshixun . id , :status = > status , :user_id = > myshixun . user_id ,
:open_time = > Time . now , :identifier = > code , :modify_time = > challenge . modify_time )
end
work_score = work . final_score
work . homework_common . homework_challenge_settings . each do | setting |
if work_score > 1
games = myshixun . games . where ( :challenge_id = > setting . challenge_id ) . first
games . update_attributes ( :status = > 2 , :end_time = > Time . now , :final_score = > setting . challenge . score )
work_score -= setting . score
end
end
work . update_column ( " myshixun_id " , myshixun . id )
end
end
end
work . update_column ( " myshixun_id " , myshixun . id )
rescue Exception = > e
puts ( " # # # failed to exec shixun: current task id is #{ e } " )
g . delete_project ( gshixun . id ) if gshixun . try ( :id ) . present?
raise ActiveRecord :: Rollback
end
end
end