parent
c6ec2d32fc
commit
94d11db377
@ -0,0 +1,206 @@
|
|||||||
|
class Subjects::CopySubjectService < ApplicationService
|
||||||
|
attr_reader :subject, :to_subject, :user_id, :laboratory
|
||||||
|
|
||||||
|
def initialize(subject, user_id, laboratory=nil)
|
||||||
|
@subject = subject
|
||||||
|
@user_id = user_id
|
||||||
|
@laboratory = laboratory
|
||||||
|
subject_params = subject.attributes.dup.except('id', 'copy_subject_id', 'user_id', 'homepage_show')
|
||||||
|
@to_subject = Subject.new(subject_params)
|
||||||
|
end
|
||||||
|
|
||||||
|
def call
|
||||||
|
return if subject.blank?
|
||||||
|
ActiveRecord::Base.transaction do
|
||||||
|
copy_subject!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
private
|
||||||
|
# 复制实践课程表
|
||||||
|
def copy_subject!
|
||||||
|
to_subject.copy_subject_id = subject.id
|
||||||
|
to_subject.user_id = user_id
|
||||||
|
to_subject.save!
|
||||||
|
|
||||||
|
copy_stages_data!(subject, to_subject)
|
||||||
|
copy_subject_members_data(to_subject)
|
||||||
|
laboratory.laboratory_subjects.create(subject: to_subject) if laboratory
|
||||||
|
end
|
||||||
|
|
||||||
|
# 复制章节需要的章节
|
||||||
|
def copy_stages_data!(subject, to_subject)
|
||||||
|
subject.stages.each do |stage|
|
||||||
|
to_stage = to_subject.stages.new
|
||||||
|
to_stage.attributes = stage.attributes.dup.except('id', 'subject_id', 'user_id')
|
||||||
|
to_stage.user_id = user_id
|
||||||
|
to_stage.save!
|
||||||
|
|
||||||
|
copy_stage_shixuns_data!(stage, to_stage)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# 创建实践课程关联实训表
|
||||||
|
def copy_stage_shixuns_data!(stage, to_stage)
|
||||||
|
stage.stage_shixuns.each do |stage_shixun|
|
||||||
|
to_shixun = copy_shixun_data!(stage_shixun)
|
||||||
|
to_stage_shixun = to_stage.stage_shixuns.new
|
||||||
|
to_stage_shixun.attributes = stage_shixun.attributes.dup.except('id', 'subject_id', 'stage_id', 'shixun_id')
|
||||||
|
to_stage_shixun.subject_id = to_stage.subject_id
|
||||||
|
to_stage_shixun.shixun_id = to_shixun.id
|
||||||
|
to_stage_shixun.save!
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# 复制实训数据
|
||||||
|
def copy_shixun_data!(stage_shixun)
|
||||||
|
shixun = stage_shixun.shixun
|
||||||
|
to_shixun = Shixun.new
|
||||||
|
to_shixun.attributes = shixun.attributes.dup.except('id', 'user_id', 'identifier', 'homepage_show',
|
||||||
|
'use_scope', 'averge_star', 'myshixuns_count')
|
||||||
|
to_shixun.user_id = user_id
|
||||||
|
to_shixun.save!
|
||||||
|
|
||||||
|
copy_shixun_info_data!(shixun, to_shixun)
|
||||||
|
copy_shixun_mirror_repositories_data!(shixun, to_shixun)
|
||||||
|
copy_shixun_tag_repertoires_data!(shixun, to_shixun)
|
||||||
|
copy_shixun_service_configs_data!(shixun, to_shixun)
|
||||||
|
copy_challenges_data!(shixun, to_shixun)
|
||||||
|
copy_shixun_members_data!(to_shixun)
|
||||||
|
|
||||||
|
# 云上实验室
|
||||||
|
laboratory.laboratory_shixuns.create(shixun: to_shixun) if laboratory
|
||||||
|
to_shixun
|
||||||
|
end
|
||||||
|
|
||||||
|
# 创建实训长字段内容
|
||||||
|
def copy_shixun_info_data!(shixun, to_shixun)
|
||||||
|
to_shixun_info = ShixunInfo.new
|
||||||
|
to_shixun_info.attributes = shixun.shixun_info.attributes.except('id', 'shixun_id')
|
||||||
|
to_shixun_info.shixun_id = to_shixun.id
|
||||||
|
to_shixun_info.save!
|
||||||
|
end
|
||||||
|
|
||||||
|
# 创建实训镜像标签
|
||||||
|
def copy_shixun_mirror_repositories_data!(shixun, to_shixun)
|
||||||
|
shixun.shixun_mirror_repositories.each do |shixun_mirror_repository|
|
||||||
|
to_shixun_mirror_repository = to_shixun.shixun_mirror_repositories.new
|
||||||
|
to_shixun_mirror_repository.attributes = shixun_mirror_repository.attributes.dup.except('id', 'shixun_id')
|
||||||
|
to_shixun_mirror_repository.shixun_id = to_shixun.id
|
||||||
|
to_shixun_mirror_repository.save!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# 创建实训tag标签
|
||||||
|
def copy_shixun_tag_repertoires_data!(shixun, to_shixun)
|
||||||
|
shixun.shixun_tag_repertoires.each do |shixun_tag_repertoire|
|
||||||
|
to_shixun_tag_repertoire = to_shixun.shixun_tag_repertoires.new
|
||||||
|
to_shixun_tag_repertoire.attributes = shixun_tag_repertoire.attributes.dup.except('id', 'shixun_id')
|
||||||
|
to_shixun_tag_repertoire.shixun_id = to_shixun.id
|
||||||
|
to_shixun_tag_repertoire.save!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# 复制实训服务配置
|
||||||
|
def copy_shixun_service_configs_data!(shixun, to_shixun)
|
||||||
|
shixun.shixun_service_configs.each do |shixun_service_config|
|
||||||
|
to_shixun_service_config = to_shixun.shixun_service_configs.new
|
||||||
|
to_shixun_service_config.attributes = shixun_service_config.attributes.dup.except('id', 'shixun_id')
|
||||||
|
to_shixun_service_config.shixun_id = to_shixun.id
|
||||||
|
to_shixun_service_config.save!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# 复制关卡信息
|
||||||
|
def copy_challenges_data!(shixun, to_shixun)
|
||||||
|
shixun.challenges.each do |challenge|
|
||||||
|
to_challenge = to_shixun.challenges.new
|
||||||
|
to_challenge.attributes = challenge.attributes.dup.except('id', 'shixun_id', 'praises_count', 'user_id', 'visits')
|
||||||
|
to_challenge.user_id = user_id
|
||||||
|
to_challenge.shixun_id = to_shixun.id
|
||||||
|
to_challenge.save!
|
||||||
|
|
||||||
|
copy_challenge_answers_data!(challenge, to_challenge)
|
||||||
|
copy_challenge_tags_data!(challenge, to_challenge)
|
||||||
|
copy_test_sets_data!(challenge, to_challenge)
|
||||||
|
copy_challenge_chooses_data!(challenge, to_challenge)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# 复制答案数据
|
||||||
|
def copy_challenge_answers_data!(challenge, to_challenge)
|
||||||
|
challenge.challenge_answers.each do |challenge_answer|
|
||||||
|
to_challenge_answer = to_challenge.challenge_answers.new
|
||||||
|
to_challenge_answer.attributes = challenge_answer.attributes.dup.except('id', 'challenge_id')
|
||||||
|
to_challenge_answer.challenge_id = to_challenge.id
|
||||||
|
to_challenge_answer.save!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# 复制关卡标签数据
|
||||||
|
def copy_challenge_tags_data!(challenge, to_challenge)
|
||||||
|
challenge.challenge_tags.each do |challenge_tag|
|
||||||
|
to_challenge_tag = to_challenge.challenge_tags.new
|
||||||
|
to_challenge_tag.attributes = challenge_tag.attributes.dup.except('id', 'challenge_id')
|
||||||
|
to_challenge_tag.challenge_id = to_challenge.id
|
||||||
|
to_challenge_tag.save!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# 复制测试集
|
||||||
|
def copy_test_sets_data!(challenge, to_challenge)
|
||||||
|
challenge.test_sets.each do |test_set|
|
||||||
|
to_test_set = to_challenge.test_sets.new
|
||||||
|
to_test_set.attributes = test_set.attributes.dup.except('id', 'challenge_id')
|
||||||
|
to_test_set.challenge_id = to_challenge.id
|
||||||
|
to_test_set.save!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# 复制选择题关卡
|
||||||
|
def copy_challenge_chooses_data!(challenge, to_challenge)
|
||||||
|
challenge.challenge_chooses.each do |challenge_choose|
|
||||||
|
to_challenge_choose = to_challenge.challenge_chooses.new
|
||||||
|
to_challenge_choose.attributes = challenge_choose.attributes.dup.except('id', 'challenge_id')
|
||||||
|
to_challenge_choose.challenge_id = to_challenge.id
|
||||||
|
to_challenge_choose.save!
|
||||||
|
|
||||||
|
copy_challenge_questions_data!(challenge_choose, to_challenge_choose)
|
||||||
|
copy_challenge_choose_tags_data!(challenge_choose, to_challenge_choose)
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# 复制选择题问题
|
||||||
|
def copy_challenge_questions_data!(challenge_choose, to_challenge_choose)
|
||||||
|
challenge_choose.challenge_questions.each do |challenge_question|
|
||||||
|
to_challenge_question = to_challenge_choose.challenge_questions.new
|
||||||
|
to_challenge_question.attributes = challenge_question.attributes.dup.except('id,', 'challenge_choose_id')
|
||||||
|
to_challenge_question.challenge_choose_id = to_challenge_choose.id
|
||||||
|
to_challenge_question.save!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# 复制选择题标签
|
||||||
|
def copy_challenge_choose_tags_data!(challenge_choose, to_challenge_choose)
|
||||||
|
challenge_choose.challenge_tags.each do |challenge_tag|
|
||||||
|
to_challenge_tag = to_challenge_choose.challenge_tags.new
|
||||||
|
to_challenge_tag.attributes = challenge_tag.attributes.dup.except('id', 'challenge_choose_id')
|
||||||
|
to_challenge_tag.challenge_choose_id = to_challenge_choose.id
|
||||||
|
to_challenge_tag.save!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# 创建实训成员
|
||||||
|
def copy_shixun_members_data!(to_shixun)
|
||||||
|
to_shixun.shixun_members.create!(user_id: user_id, role: 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
# 创建课程成员
|
||||||
|
def copy_subject_members_data(to_subject)
|
||||||
|
to_subject.subject_members.create!(user_id: user_id, role: 1)
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
@ -0,0 +1,5 @@
|
|||||||
|
class AddCopyFromForSubjects < ActiveRecord::Migration[5.2]
|
||||||
|
def change
|
||||||
|
add_column :subjects, :copy_subject_id, :integer
|
||||||
|
end
|
||||||
|
end
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
Binary file not shown.
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue