实践课程复制功能

dev_home
daiao 5 years ago
parent c6ec2d32fc
commit 94d11db377

@ -85,8 +85,8 @@ $(document).on('turbolinks:load', function() {
$subjectSelect.select2({ $subjectSelect.select2({
theme: 'bootstrap4', theme: 'bootstrap4',
placeholder: '请输入课程名称/创建者检索', placeholder: '请输入课程名称/创建者检索',
multiple: true, multiple: false,
closeOnSelect: false, closeOnSelect: true,
ajax: { ajax: {
delay: 500, delay: 500,
url: '/admins/laboratories/' + laboratoryId + '/subjects_for_select', url: '/admins/laboratories/' + laboratoryId + '/subjects_for_select',
@ -132,7 +132,7 @@ $(document).on('turbolinks:load', function() {
method: 'POST', method: 'POST',
dataType: 'json', dataType: 'json',
url: '/admins/laboratories/' + laboratoryId + '/laboratory_subjects', url: '/admins/laboratories/' + laboratoryId + '/laboratory_subjects',
data: { subject_ids: subjectIds }, data: { subject_id: subjectIds },
success: function(){ success: function(){
show_success_flash(); show_success_flash();
window.location.reload(); window.location.reload();

@ -9,15 +9,8 @@ class Admins::LaboratorySubjectsController < Admins::BaseController
end end
def create def create
subject_ids = Array.wrap(params[:subject_ids]) subject = Subject.find(params[:subject_id])
subject_ids = Subject.where(id: subject_ids).pluck(:id) Subjects::CopySubjectService.call(subject, current_user.id, current_laboratory)
exist_subject_id = current_laboratory.laboratory_subjects.where(subject_id: subject_ids).pluck(:subject_id)
LaboratorySubject.bulk_insert(*%i[subject_id laboratory_id created_at updated_at]) do |worker|
(subject_ids - exist_subject_id).each do |subject_id|
worker.add(subject_id: subject_id, laboratory_id: current_laboratory.id)
end
end
render_ok render_ok
end end

@ -18,6 +18,7 @@ module Base::ErrorRescueHandler
render_unprocessable_entity(ex.model.errors.full_messages.join(',')) render_unprocessable_entity(ex.model.errors.full_messages.join(','))
end end
rescue_from ActiveRecord::RecordInvalid do |ex| rescue_from ActiveRecord::RecordInvalid do |ex|
ex.backtrace.each { |msg| Rails.logger.error(msg) }
render_unprocessable_entity(ex.record.errors.full_messages.join(',')) render_unprocessable_entity(ex.record.errors.full_messages.join(','))
end end
end end

@ -6,6 +6,7 @@ class SubjectsController < ApplicationController
before_action :allowed, only: [:update, :edit, :destroy, :publish, :cancel_publish, :cancel_has_publish, before_action :allowed, only: [:update, :edit, :destroy, :publish, :cancel_publish, :cancel_has_publish,
:search_members, :add_subject_members, :statistics, :shixun_report, :school_report, :search_members, :add_subject_members, :statistics, :shixun_report, :school_report,
:up_member_position, :down_member_position, :update_team_title] :up_member_position, :down_member_position, :update_team_title]
before_action :require_admin, only: [:copy_subject]
include ApplicationHelper include ApplicationHelper
include SubjectsHelper include SubjectsHelper
@ -460,6 +461,13 @@ class SubjectsController < ApplicationController
end end
end end
# 复制实践课程
def copy
end
private private
def subject_params def subject_params
tip_exception("实训路径名称不能为空") if params[:name].blank? tip_exception("实训路径名称不能为空") if params[:name].blank?
@ -510,4 +518,6 @@ class SubjectsController < ApplicationController
end end
end end

@ -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

@ -137990,8 +137990,8 @@ $(document).on('turbolinks:load', function() {
$subjectSelect.select2({ $subjectSelect.select2({
theme: 'bootstrap4', theme: 'bootstrap4',
placeholder: '请输入课程名称/创建者检索', placeholder: '请输入课程名称/创建者检索',
multiple: true, multiple: false,
closeOnSelect: false, closeOnSelect: true,
ajax: { ajax: {
delay: 500, delay: 500,
url: '/admins/laboratories/' + laboratoryId + '/subjects_for_select', url: '/admins/laboratories/' + laboratoryId + '/subjects_for_select',
@ -138037,7 +138037,7 @@ $(document).on('turbolinks:load', function() {
method: 'POST', method: 'POST',
dataType: 'json', dataType: 'json',
url: '/admins/laboratories/' + laboratoryId + '/laboratory_subjects', url: '/admins/laboratories/' + laboratoryId + '/laboratory_subjects',
data: { subject_ids: subjectIds }, data: { subject_id: subjectIds },
success: function(){ success: function(){
show_success_flash(); show_success_flash();
window.location.reload(); window.location.reload();
Loading…
Cancel
Save