parent
6fcfac707d
commit
d206e1c37e
@ -0,0 +1,11 @@
|
|||||||
|
class ExaminationIntelligentSettings::SaveExamSettingForm
|
||||||
|
include ActiveModel::Model
|
||||||
|
|
||||||
|
attr_accessor :discipline_id, :sub_discipline_id, :source, :difficulty, :tag_discipline_id, :question_settings
|
||||||
|
|
||||||
|
validates :discipline_id, presence: true
|
||||||
|
validates :sub_discipline_id, presence: true
|
||||||
|
validates :source, presence: true
|
||||||
|
validates :difficulty, presence: true, inclusion: {in: 1..3}, numericality: { only_integer: true }
|
||||||
|
validates :question_settings, presence: true
|
||||||
|
end
|
@ -1,4 +1,7 @@
|
|||||||
class ExaminationIntelligentSetting < ApplicationRecord
|
class ExaminationIntelligentSetting < ApplicationRecord
|
||||||
belongs_to :sub_discipline
|
belongs_to :sub_discipline
|
||||||
|
belongs_to :user
|
||||||
has_many :examination_type_settings, dependent: :destroy
|
has_many :examination_type_settings, dependent: :destroy
|
||||||
|
has_many :tag_discipline_containers, as: :container, dependent: :destroy
|
||||||
|
has_many :item_baskets, dependent: :destroy
|
||||||
end
|
end
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
class ExaminationTypeSetting < ApplicationRecord
|
class ExaminationTypeSetting < ApplicationRecord
|
||||||
|
enum item_type: { SINGLE: 0, MULTIPLE: 1, JUDGMENT: 2, COMPLETION: 3, SUBJECTIVE: 4, PRACTICAL: 5, PROGRAM: 6 }
|
||||||
belongs_to :examination_intelligent_setting
|
belongs_to :examination_intelligent_setting
|
||||||
end
|
end
|
||||||
|
@ -1,32 +1,35 @@
|
|||||||
class OptionalItemQuery < ApplicationQuery
|
class OptionalItemQuery < ApplicationQuery
|
||||||
attr_reader :params
|
attr_reader :sub_discipline_id, :tag_discipline_id, :difficulty, :source
|
||||||
|
|
||||||
def initialize(params)
|
def initialize(sub_discipline_id, tag_discipline_id, difficulty, source)
|
||||||
@params = params
|
@sub_discipline_id = sub_discipline_id
|
||||||
|
@tag_discipline_id = tag_discipline_id
|
||||||
|
@difficulty = difficulty
|
||||||
|
@source = source
|
||||||
end
|
end
|
||||||
|
|
||||||
def call
|
def call
|
||||||
items = ItemBank.all
|
items = ItemBank.all
|
||||||
if params[:tag_discipline_id].present? && params[:sub_discipline_id].present?
|
if tag_discipline_id.present? && sub_discipline_id.present?
|
||||||
items = items.joins(:tag_discipline_containers).where(tag_discipline_containers: {tag_discipline_id: params[:tag_discipline_id]})
|
items = items.joins(:tag_discipline_containers).where(tag_discipline_containers: {tag_discipline_id: tag_discipline_id})
|
||||||
hacks = Hack.joins(:tag_discipline_containers).where(tag_discipline_containers: {tag_discipline_id: params[:tag_discipline_id]})
|
hacks = Hack.joins(:tag_discipline_containers).where(tag_discipline_containers: {tag_discipline_id: tag_discipline_id})
|
||||||
elsif params[:sub_discipline_id].present?
|
elsif sub_discipline_id.present?
|
||||||
items = items.where(sub_discipline_id: params[:sub_discipline_id])
|
items = items.where(sub_discipline_id: sub_discipline_id)
|
||||||
hacks = Hack.where(sub_discipline_id: params[:sub_discipline_id])
|
hacks = Hack.where(sub_discipline_id: sub_discipline_id)
|
||||||
|
end
|
||||||
|
|
||||||
|
if hacks.present?
|
||||||
|
items = ItemBank.where(container_id: hacks.pluck(:id), container_type: "Hack").or(ItemBank.where(id: items.pluck(:id)))
|
||||||
end
|
end
|
||||||
|
|
||||||
# 来源
|
# 来源
|
||||||
source = params[:source].present? ? params[:source].to_i : 1
|
public = source.present? ? source.to_i : 1
|
||||||
public = source == 3 ? [0, 1] : source
|
public = public == 2 ? [0, 1] : public
|
||||||
items = items.where(public: public)
|
items = items.where(public: public)
|
||||||
|
|
||||||
# 难度
|
# 难度
|
||||||
difficulty = params[:difficulty] ? params[:difficulty].to_i : 1
|
difficulty = difficulty ? difficulty.to_i : 1
|
||||||
items = items.where(difficulty: difficulty)
|
items = items.where(difficulty: difficulty)
|
||||||
single_question_count = items.select{ |item| item.item_type == "SINGLE" }.size
|
items
|
||||||
multiple_question_count = items.select{ |item| item.item_type == "MULTIPLE" }.size
|
|
||||||
judgement_question_count = items.select{ |item| item.item_type == "JUDGMENT" }.size
|
|
||||||
program_question_count = hacks.present? ? hacks.pluck(:item_bank_id).reject(&:blank?).size : items.select{ |item| item.item_type == "PROGRAM" }.size
|
|
||||||
{single_question_count: single_question_count, multiple_question_count: multiple_question_count, judgement_question_count: judgement_question_count, program_question_count: program_question_count}
|
|
||||||
end
|
end
|
||||||
end
|
end
|
@ -0,0 +1,63 @@
|
|||||||
|
class ExaminationIntelligentSettings::SaveSettingService < ApplicationService
|
||||||
|
attr_reader :exam, :params
|
||||||
|
|
||||||
|
def initialize(exam, params)
|
||||||
|
@exam = exam
|
||||||
|
@params = params
|
||||||
|
end
|
||||||
|
|
||||||
|
def call
|
||||||
|
ExaminationIntelligentSettings::SaveExamSettingForm.new(params).validate!
|
||||||
|
items = OptionalItemQuery.call(params[:sub_discipline_id], params[:tag_discipline_id], params[:difficulty], params[:source])
|
||||||
|
params[:question_settings].each do |setting|
|
||||||
|
raise "超出可选题数范围" if items.select{ |item| item.item_type == setting[:item_type] }.size.to_i < setting[:count].to_i
|
||||||
|
end
|
||||||
|
|
||||||
|
exam.difficulty = params[:difficulty]
|
||||||
|
exam.sub_discipline_id = params[:sub_discipline_id]
|
||||||
|
exam.public = params[:source].present? ? params[:source].to_i : 1
|
||||||
|
exam.save!
|
||||||
|
|
||||||
|
# 知识点的创建
|
||||||
|
params[:tag_discipline_id].each do |tag_id|
|
||||||
|
exam.tag_discipline_containers << TagDisciplineContainer.new(tag_discipline_id: tag_id)
|
||||||
|
end
|
||||||
|
|
||||||
|
# 智能选题的设置
|
||||||
|
params[:question_settings].each do |setting|
|
||||||
|
if setting[:count].to_i > 0
|
||||||
|
exam.examination_type_settings << ExaminationTypeSetting.new(item_type: setting[:item_type], count: setting[:count].to_i)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# 选题
|
||||||
|
choose_question items
|
||||||
|
|
||||||
|
exam
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def choose_question items
|
||||||
|
exam.examination_type_settings.each do |setting|
|
||||||
|
questions = items.select{ |item| item.item_type == setting.item_type }
|
||||||
|
questions.pluck(:id).sample(setting.count).each_with_index do |item_id, index|
|
||||||
|
item = ItemBank.find item_id
|
||||||
|
exam.item_baskets << ItemBasket.new(item_bank_id: item.id, position: index+1, score: item_score(item.item_type), item_type: item.item_type)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def item_score item_type
|
||||||
|
score =
|
||||||
|
case item_type
|
||||||
|
when "SINGLE", "MULTIPLE", "JUDGMENT"
|
||||||
|
5
|
||||||
|
when "PROGRAM"
|
||||||
|
10
|
||||||
|
else
|
||||||
|
5
|
||||||
|
end
|
||||||
|
score
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,5 @@
|
|||||||
|
class AddUserIdToIntelligentSettings < ActiveRecord::Migration[5.2]
|
||||||
|
def change
|
||||||
|
add_column :examination_intelligent_settings, :user_id, :integer, index: true
|
||||||
|
end
|
||||||
|
end
|
@ -0,0 +1,5 @@
|
|||||||
|
class AddIntelligentSettingIdToItemBaskets < ActiveRecord::Migration[5.2]
|
||||||
|
def change
|
||||||
|
add_column :item_baskets, :examination_intelligent_setting_id, :integer, index: true
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in new issue