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/homework_banks_controller.rb

68 lines
2.8 KiB

class HomeworkBanksController < ApplicationController
before_action :require_login
5 years ago
before_action :find_bank, :bank_visit_auth
before_action :bank_params, only: [:update]
before_action :bank_admin, only: [:update, :destroy, :set_public]
def show
@bank_attachments = @bank.attachments.where(attachtype: 1)
@reference_attachments = @bank.attachments.where(attachtype: 2)
end
def update
ActiveRecord::Base.transaction do
5 years ago
if @bank.homework_type == 1
@bank.update_attributes(name: params[:name], description: params[:description], reference_answer: params[:reference_answer])
elsif @bank.homework_type == 3
@bank.update_attributes(name: params[:name], description: params[:description], reference_answer: params[:reference_answer],
base_on_project: params[:base_on_project], min_num: params[:min_num], max_num: params[:max_num])
end
# 作业描述的附件
Attachment.associate_container(params[:attachment_ids], @bank.id, @bank.class) if params[:attachment_ids]
# 作业参考答案的附件
Attachment.associate_container(params[:reference_attachment_ids], @bank.id, @bank.class, 2) if params[:reference_attachment_ids]
normal_status(0, "更新成功")
end
end
def destroy
ActiveRecord::Base.transaction do
@bank.homework_commons.update_all(homework_bank_id: nil)
@bank.destroy!
normal_status("删除成功")
end
end
def set_public
@bank.update_attributes(is_public: 1)
normal_status("更新成功")
end
private
def find_bank
@bank = HomeworkBank.find_by!(id: params[:id])
end
def bank_admin
tip_exception(403, "无权限") unless @bank.user_id == current_user.id || current_user.admin_or_business?
end
def bank_params
tip_exception("name参数不能为空") if params[:homework_bank][:name].blank?
tip_exception("description参数不能为空") if params[:homework_bank][:description].blank?
if @bank.homework_type == 3
tip_exception("base_on_project参数不能为空") if params[:homework_bank][:base_on_project].nil?
5 years ago
tip_exception("最小人数不能为空") if params[:homework_bank][:min_num].blank?
tip_exception("最大人数参数不能为空") if params[:homework_bank][:max_num].blank?
tip_exception("最小人数不能小于1") if params[:homework_bank][:min_num].to_i < 1
tip_exception("最大人数不能小于最小人数") if params[:homework_bank][:max_num].to_i < params[:homework_bank][:min_num].to_i
end
params.require(:homework_bank).permit(:name, :description, :reference_answer) if @bank.homework_type == 1
params.require(:homework_bank).permit(:name, :description, :reference_answer, :min_num, :max_num, :base_on_project) if @bank.homework_type == 3
end
end