diff --git a/app/controllers/admins/competition_settings_controller.rb b/app/controllers/admins/competition_settings_controller.rb index c905c4644..d062e4c46 100644 --- a/app/controllers/admins/competition_settings_controller.rb +++ b/app/controllers/admins/competition_settings_controller.rb @@ -20,7 +20,7 @@ class Admins::CompetitionSettingsController < Admins::BaseController end def basic_form_params - params.permit(:identifier, :name, :sub_title, :start_time, :end_time, :mode, :identifier, :bonus, :awards_count, :description, :course_id, :teach_start_time, :teach_end_time) + params.permit(:identifier, :name, :sub_title, :sponsor_schools, :region_schools, :start_time, :end_time, :mode, :identifier, :bonus, :awards_count, :description, :course_id, :teach_start_time, :teach_end_time) end def nav_form_params diff --git a/app/controllers/competitions/students_controller.rb b/app/controllers/competitions/students_controller.rb index 8fd235fd1..9d0b40754 100644 --- a/app/controllers/competitions/students_controller.rb +++ b/app/controllers/competitions/students_controller.rb @@ -7,6 +7,7 @@ class Competitions::StudentsController < Competitions::BaseController end students = User.joins(:user_extension).where(status: 1, user_extensions: { identity: 1 }) + students = students.where(user_extensions: { school_id: current_competition.region_schools.pluck(:school_id) }) if current_competition.region_schools.size > 0 students = students.where.not(id: params[:student_ids]) if params[:student_ids].present? students = students.where('LOWER(CONCAT(lastname, firstname, login, nickname)) LIKE ?', "%#{keyword}%") @students = students.includes(user_extension: :school).limit(20) diff --git a/app/models/competition.rb b/app/models/competition.rb index 694f1ca57..845c5c00f 100644 --- a/app/models/competition.rb +++ b/app/models/competition.rb @@ -22,6 +22,9 @@ class Competition < ApplicationRecord has_many :attachments, as: :container, dependent: :destroy has_many :competition_awards, dependent: :destroy + has_many :competition_schools, dependent: :destroy + has_one :sponsor_schools, -> { where(source: :sponsor) }, class_name: 'CompetitionSchool' # 主办方 + has_one :region_schools, -> { where(source: :region) }, class_name: 'CompetitionSchool' # 开放范围 after_create :create_competition_modules @@ -79,6 +82,12 @@ class Competition < ApplicationRecord team_members.exists?(user_id: user.id) end + # 是否开放 + def open?(user) + user_school_id = user.user_extension&.school_id.to_i + competition.region_schools.size == 0 || competition.region_schools.exists?(school_id: user_school_id) + end + # 是否禁止教师报名 def teacher_enroll_forbidden? teacher_staff.blank? || teacher_staff.maximum.zero? diff --git a/app/models/competition_school.rb b/app/models/competition_school.rb new file mode 100644 index 000000000..c1a5decbe --- /dev/null +++ b/app/models/competition_school.rb @@ -0,0 +1,5 @@ +class CompetitionSchool < ApplicationRecord + # source sponsor: 主办方, region:开放范围 + belongs_to :competition + belongs_to :school +end diff --git a/app/services/admins/competition_basic_setting_service.rb b/app/services/admins/competition_basic_setting_service.rb index d943b1cb0..dc3a9cdc5 100644 --- a/app/services/admins/competition_basic_setting_service.rb +++ b/app/services/admins/competition_basic_setting_service.rb @@ -20,6 +20,7 @@ class Admins::CompetitionBasicSettingService < ApplicationService competition.save! + # 竞赛模式相关设置 if competition.mode == 1 || competition.mode == 4 competition.competition_mode_setting&.destroy else @@ -33,6 +34,23 @@ class Admins::CompetitionBasicSettingService < ApplicationService setting.save! end + # 主办方设置 + new_school_ids = (params[:sponsor_schools] || []) - competition.sponsor_schools.pluck(:school_id) + delete_school_ids = competition.sponsor_schools.pluck(:school_id) - (params[:sponsor_schools] || []) + new_school_ids.each do |school_id| + CompetitionSchool.create!(competition_id: competition.id, school_id: school_id, source: 'sponsor') + end + competition.sponsor_schools.where(school_id: delete_school_ids).destroy_all + + # 开放范围设置 + new_region_school_ids = (params[:region_schools] || []) - competition.region_schools.pluck(:school_id) + delete_region_school_ids = competition.region_schools.pluck(:school_id) - (params[:region_schools] || []) + new_region_school_ids.each do |school_id| + CompetitionSchool.create!(competition_id: competition.id, school_id: school_id, source: 'region') + end + competition.region_schools.where(school_id: delete_region_school_ids).destroy_all + + competition end end diff --git a/app/services/competitions/create_personal_team_service.rb b/app/services/competitions/create_personal_team_service.rb index 907c59dc0..82adec0cf 100644 --- a/app/services/competitions/create_personal_team_service.rb +++ b/app/services/competitions/create_personal_team_service.rb @@ -11,6 +11,8 @@ class Competitions::CreatePersonalTeamService < ApplicationService def call raise Error, '个人赛才能报名' unless competition.personal? + raise Error, '本竞赛只对指定单位/学校开放' unless competition.open?(user) + is_teacher = user.is_teacher? raise Error, '本竞赛的参赛者限定为:学生' if is_teacher && competition.teacher_enroll_forbidden? raise Error, '本竞赛的参赛者限定为:教师' if !is_teacher && competition.member_enroll_forbidden? diff --git a/app/services/competitions/join_team_service.rb b/app/services/competitions/join_team_service.rb index df889abe2..cb0253fa2 100644 --- a/app/services/competitions/join_team_service.rb +++ b/app/services/competitions/join_team_service.rb @@ -13,6 +13,8 @@ class Competitions::JoinTeamService < ApplicationService invite_code = params[:invite_code].to_s.strip raise Error, '战队邀请码不能为空' if invite_code.blank? + raise Error, '本竞赛只对指定单位/学校开放' unless competition.open?(user) + is_teacher = user.is_teacher? raise Error, '本竞赛的参赛者限定为:学生' if is_teacher && competition.teacher_enroll_forbidden? raise Error, '本竞赛的参赛者限定为:教师' if !is_teacher && competition.member_enroll_forbidden? diff --git a/db/migrate/20191024105504_create_competition_schools.rb b/db/migrate/20191024105504_create_competition_schools.rb new file mode 100644 index 000000000..27a425abc --- /dev/null +++ b/db/migrate/20191024105504_create_competition_schools.rb @@ -0,0 +1,11 @@ +class CreateCompetitionSchools < ActiveRecord::Migration[5.2] + def change + create_table :competition_schools do |t| + t.references :competition, index: true + t.references :school, index: true + t.string :source + + t.timestamps + end + end +end diff --git a/spec/models/competition_school_spec.rb b/spec/models/competition_school_spec.rb new file mode 100644 index 000000000..dd90d6088 --- /dev/null +++ b/spec/models/competition_school_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe CompetitionSchool, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end