diff --git a/app/assets/javascripts/homework_banks.js b/app/assets/javascripts/homework_banks.js new file mode 100644 index 000000000..dee720fac --- /dev/null +++ b/app/assets/javascripts/homework_banks.js @@ -0,0 +1,2 @@ +// Place all the behaviors and hooks related to the matching controller here. +// All this logic will automatically be available in application.js. diff --git a/app/assets/stylesheets/homework_banks.scss b/app/assets/stylesheets/homework_banks.scss new file mode 100644 index 000000000..0cfbbd32d --- /dev/null +++ b/app/assets/stylesheets/homework_banks.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the homework_banks controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/homework_banks_controller.rb b/app/controllers/homework_banks_controller.rb new file mode 100644 index 000000000..8c4d607a9 --- /dev/null +++ b/app/controllers/homework_banks_controller.rb @@ -0,0 +1,63 @@ +class HomeworkBanksController < ApplicationController + before_action :require_login + before_action :find_bank + 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 + @bank.update_attributes(name: params[:name], description: params[:description], reference_answer: params[:reference_answer]) + + # 作业描述的附件 + 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]) + tip_exception(403, "无权限") unless (current_user.certification_teacher? && (@bank.is_public || @bank.user_id == current_user.id)) || current_user.admin? + end + + def bank_admin + tip_exception(403, "无权限") unless (current_user.certification_teacher? && @bank.user_id == current_user.id) || current_user.admin? + 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? + tip_exception("min_num参数不能为空") if params[:homework_bank][:min_num].blank? + tip_exception("max_num参数不能为空") 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 diff --git a/app/helpers/homework_banks_helper.rb b/app/helpers/homework_banks_helper.rb new file mode 100644 index 000000000..805479c79 --- /dev/null +++ b/app/helpers/homework_banks_helper.rb @@ -0,0 +1,2 @@ +module HomeworkBanksHelper +end diff --git a/app/views/homework_banks/show.json.jbuilder b/app/views/homework_banks/show.json.jbuilder new file mode 100644 index 000000000..34ca5ec25 --- /dev/null +++ b/app/views/homework_banks/show.json.jbuilder @@ -0,0 +1,9 @@ +json.(@bank, :id, :name, :description, :homework_type, :is_public, :min_num, :max_num, :base_on_project, :reference_answer) + +json.attachments @bank_attachments do |attachment| + json.partial! "attachments/attachment_simple", locals: {attachment: attachment} +end + +json.reference_attachments @reference_attachments do |attachment| + json.partial! "attachments/attachment_simple", locals: {attachment: attachment} +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index de4f46a40..61d59a1a8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -568,14 +568,6 @@ Rails.application.routes.draw do resource :poll_votes,only:[:create,:destroy] end - resources :question_banks do - collection do - get :bank_list - post :save_banks - end - end - - resources :exercises do member do get :choose_shixun @@ -630,6 +622,20 @@ Rails.application.routes.draw do end end + + resources :question_banks do + collection do + get :bank_list + post :save_banks + end + end + + resources :homework_banks do + member do + post :set_public + end + end + resources :attachments resources :schools do diff --git a/spec/controllers/homework_banks_controller_spec.rb b/spec/controllers/homework_banks_controller_spec.rb new file mode 100644 index 000000000..ada01ad8b --- /dev/null +++ b/spec/controllers/homework_banks_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe HomeworkBanksController, type: :controller do + +end diff --git a/spec/helpers/homework_banks_helper_spec.rb b/spec/helpers/homework_banks_helper_spec.rb new file mode 100644 index 000000000..bd54e5d8f --- /dev/null +++ b/spec/helpers/homework_banks_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the HomeworkBanksHelper. For example: +# +# describe HomeworkBanksHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe HomeworkBanksHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end