You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
educoder/app/controllers/item_banks_controller.rb

75 lines
2.3 KiB

5 years ago
class ItemBanksController < ApplicationController
5 years ago
include PaginateHelper
5 years ago
before_action :require_login
5 years ago
before_action :find_item, except: [:index, :create]
before_action :edit_auth, only: [:update, :destroy, :set_public]
5 years ago
def index
5 years ago
items = ItemBankQuery.call(params)
@items_count = items.size
5 years ago
@items = paginate items.includes(:item_analysis, :user, :container)
exam = ExaminationBank.find_by(id: params[:exam_id]) if params[:exam_id].present?
5 years ago
exam_setting = ExaminationIntelligentSetting.find_by(id: params[:exam_setting_id]) if params[:exam_setting_id].present?
@item_basket_ids = if exam
exam.examination_items.pluck(:item_bank_id)
elsif exam_setting
exam_setting.item_baskets.pluck(:item_bank_id)
else
current_user.item_baskets.pluck(:item_bank_id)
end
5 years ago
end
def create
5 years ago
item = ItemBank.new(user: current_user)
5 years ago
ItemBanks::SaveItemService.call(item, form_params)
render_ok
rescue ApplicationService::Error => ex
render_error(ex.message)
end
def edit
end
def update
ItemBanks::SaveItemService.call(@item, form_params)
render_ok
rescue ApplicationService::Error => ex
render_error(ex.message)
end
def destroy
ActiveRecord::Base.transaction do
ApplyAction.where(container_type: "ItemBank", container_id: @item.id).destroy_all
if @item.item_type == "PROGRAM"
@item.container&.destroy!
5 years ago
else
@item.destroy!
end
render_ok
end
5 years ago
end
def set_public
tip_exception(-1, "该试题已公开") if @item.public?
5 years ago
tip_exception(-1, "请勿重复提交申请") if ApplyAction.where(container_id: @item.id, container_type: 'ItemBank').exists?
ApplyAction.create!(container_id: @item.id, container_type: 'ItemBank', user_id: current_user.id)
# @item.update_attributes!(public: 1)
5 years ago
render_ok
5 years ago
end
5 years ago
5 years ago
private
5 years ago
def find_item
@item = ItemBank.find_by!(id: params[:id])
end
def edit_auth
current_user.admin_or_business? || @item.user == current_user
end
5 years ago
def form_params
5 years ago
params.permit(:discipline_id, :sub_discipline_id, :item_type, :difficulty, :name, :analysis, tag_discipline_id: [], choices: %i[choice_text is_answer])
5 years ago
end
5 years ago
end