parent
4dafb533e4
commit
d7a47239fb
@ -0,0 +1,51 @@
|
||||
class ExaminationBanksController < ApplicationController
|
||||
before_action :require_login
|
||||
|
||||
def index
|
||||
exams = ExaminationBankQuery.call(params)
|
||||
@exams_count = exams.size
|
||||
@exams = paginate exams.includes(:user, :examination_items)
|
||||
end
|
||||
|
||||
def show
|
||||
|
||||
end
|
||||
|
||||
def create
|
||||
ActiveRecord::Base.transaction do
|
||||
exam = ExaminationBank.new(user: current_user)
|
||||
# 保存试卷基础信息
|
||||
exam = ExaminationBanks::SaveExaminationBankService.call(exam, form_params)
|
||||
|
||||
# 将试题篮中的试题发送到试卷,试卷的题目与试题独立
|
||||
current_user.item_baskets.includes(:item_bank).each do |basket|
|
||||
item = basket.item_bank
|
||||
if item.present?
|
||||
new_item = ExaminationItem.new(examination_bank: exam, item_bank: item, name: item.name, item_type: item.item_type,
|
||||
difficulty: item.difficulty, score: basket.score, position: basket.position)
|
||||
if new_item.save!
|
||||
item.increment!(:quotes)
|
||||
if item.item_choices.present?
|
||||
item.item_choices.each do |choice|
|
||||
new_item.examination_item_choices << ExaminationItemChoice.new(choice_text: choice.choice_text, is_answer: choice.is_answer)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
current_user.item_baskets.destroy_all
|
||||
end
|
||||
render_ok
|
||||
end
|
||||
|
||||
def edit
|
||||
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def form_params
|
||||
params.permit(:discipline_id, :sub_discipline_id, :difficulty, :name, :duration, tag_discipline_id: [])
|
||||
end
|
||||
end
|
@ -0,0 +1,12 @@
|
||||
class ExaminationBanks::SaveExamForm
|
||||
include ActiveModel::Model
|
||||
|
||||
attr_accessor :discipline_id, :sub_discipline_id, :difficulty, :name, :duration, :tag_discipline_id
|
||||
|
||||
validates :discipline_id, presence: true
|
||||
validates :sub_discipline_id, presence: true
|
||||
validates :difficulty, presence: true, inclusion: {in: 1..3}, numericality: { only_integer: true }
|
||||
validates :name, presence: true, length: { maximum: 60 }
|
||||
validates :duration, numericality: { only_integer: true, greater_than: 0 }
|
||||
|
||||
end
|
@ -0,0 +1,17 @@
|
||||
class ExaminationBank < ApplicationRecord
|
||||
belongs_to :user
|
||||
belongs_to :sub_discipline
|
||||
|
||||
has_many :tag_discipline_containers, as: :container, dependent: :destroy
|
||||
has_many :tag_disciplines, through: :tag_discipline_containers
|
||||
|
||||
has_many :examination_items, dependent: :destroy
|
||||
|
||||
def question_count
|
||||
examination_items.size
|
||||
end
|
||||
|
||||
def total_score
|
||||
examination_items.pluck(:score).sum
|
||||
end
|
||||
end
|
@ -0,0 +1,6 @@
|
||||
class ExaminationItem < ApplicationRecord
|
||||
belongs_to :examination_bank, touch: true
|
||||
belongs_to :item_bank, optional: true
|
||||
|
||||
has_many :examination_item_choices, dependent: :destroy
|
||||
end
|
@ -0,0 +1,3 @@
|
||||
class ExaminationItemChoice < ApplicationRecord
|
||||
belongs_to :examination_item
|
||||
end
|
@ -0,0 +1,32 @@
|
||||
class ExaminationBankQuery < ApplicationQuery
|
||||
include CustomSortable
|
||||
attr_reader :params
|
||||
|
||||
sort_columns :updated_at, default_by: :updated_at, default_direction: :desc, default_table: 'examination_banks'
|
||||
|
||||
def initialize(params)
|
||||
@params = params
|
||||
end
|
||||
|
||||
def call
|
||||
if params[:public].to_i == 1
|
||||
exams = ExaminationBank.where(public: 1)
|
||||
elsif params[:public].to_i == 0
|
||||
exams = ExaminationBank.where(user_id: User.current.id)
|
||||
end
|
||||
|
||||
if params[:tag_discipline_id].present?
|
||||
exams = exams.joins(:tag_discipline_containers).where(tag_discipline_containers: {tag_discipline_id: params[:tag_discipline_id]})
|
||||
elsif params[:sub_discipline_id].present?
|
||||
exams = exams.where(sub_discipline_id: params[:sub_discipline_id])
|
||||
elsif params[:discipline_id].present?
|
||||
exams = exams.joins(:sub_discipline).where(sub_disciplines: {discipline_id: params[:discipline_id]})
|
||||
end
|
||||
|
||||
exams = exams.where(difficulty: params[:difficulty].to_i) if params[:difficulty].present?
|
||||
|
||||
exams = exams.where("name like ?", "%#{params[:keyword].strip}%") if params[:keyword].present?
|
||||
|
||||
custom_sort(exams, params[:sort_by], params[:sort_direction])
|
||||
end
|
||||
end
|
@ -0,0 +1,30 @@
|
||||
class ExaminationBanks::SaveExaminationBankService < ApplicationService
|
||||
attr_reader :exam, :params
|
||||
|
||||
def initialize(exam, params)
|
||||
@exam = exam
|
||||
@params = params
|
||||
end
|
||||
|
||||
def call
|
||||
ExaminationBanks::SaveExamForm.new(params).validate!
|
||||
|
||||
exam.name = params[:name].to_s.strip
|
||||
exam.difficulty = params[:difficulty]
|
||||
exam.duration = params[:duration]
|
||||
exam.sub_discipline_id = params[:sub_discipline_id]
|
||||
exam.save!
|
||||
|
||||
# 知识点的创建
|
||||
new_tag_discipline_ids = params[:tag_discipline_id] || []
|
||||
old_tag_discipline_ids = exam.tag_discipline_containers.pluck(:tag_discipline_id)
|
||||
delete_tag_discipline_ids = old_tag_discipline_ids - new_tag_discipline_ids
|
||||
create_tag_discipline_ids = new_tag_discipline_ids - old_tag_discipline_ids
|
||||
exam.tag_discipline_containers.where(tag_discipline_id: delete_tag_discipline_ids).destroy_all
|
||||
create_tag_discipline_ids.each do |tag_id|
|
||||
exam.tag_discipline_containers << TagDisciplineContainer.new(tag_discipline_id: tag_id)
|
||||
end
|
||||
|
||||
exam
|
||||
end
|
||||
end
|
@ -0,0 +1,11 @@
|
||||
json.exams @exams.each do |exam|
|
||||
json.(exam, :id, :name, :difficulty, :quotes)
|
||||
json.question_count exam.question_count
|
||||
json.total_score exam.total_score
|
||||
json.update_time exam.updated_at&.strftime("%Y-%m-%d %H:%M")
|
||||
json.author do
|
||||
json.login exam.user&.login
|
||||
json.name exam.user&.full_name
|
||||
end
|
||||
end
|
||||
json.exam_count @exams_count
|
@ -0,0 +1,3 @@
|
||||
json.status 0
|
||||
json.questions_score @questions_score
|
||||
json.all_score @all_score
|
@ -0,0 +1,3 @@
|
||||
json.status 0
|
||||
json.questions_score @questions_score
|
||||
json.all_score @all_score
|
@ -0,0 +1,15 @@
|
||||
class CreateExaminationBanks < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
create_table :examination_banks do |t|
|
||||
t.string :name
|
||||
t.integer :difficulty, default: 1
|
||||
t.references :user, index: true
|
||||
t.boolean :public, default: 0
|
||||
t.integer :quotes, default: 0
|
||||
t.references :sub_discipline, index: true
|
||||
t.integer :duration
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,15 @@
|
||||
class CreateExaminationItems < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
create_table :examination_items do |t|
|
||||
t.references :examination_bank, index: true
|
||||
t.references :item_bank, index: true
|
||||
t.string :name
|
||||
t.integer :item_type, default: 0
|
||||
t.integer :difficulty, default: 1
|
||||
t.float :score, default: 0
|
||||
t.integer :position, default: 0
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,11 @@
|
||||
class CreateExaminationItemChoices < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
create_table :examination_item_choices do |t|
|
||||
t.references :examination_item, index: true
|
||||
t.text :choice_text
|
||||
t.boolean :is_answer, default: 0
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,5 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe ExaminationBank, type: :model do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
@ -0,0 +1,5 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe ExaminationItemChoice, type: :model do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
@ -0,0 +1,5 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe ExaminationItem, type: :model do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
Loading…
Reference in new issue