From e84afb2f4133fe9c4c61924c6fbfd82ff1a25906 Mon Sep 17 00:00:00 2001 From: p31729568 Date: Thu, 27 Jun 2019 16:07:53 +0800 Subject: [PATCH] competition setting && competition team --- .../competition_teams_controller.rb | 201 +++++++------ app/controllers/competitions_controller.rb | 3 +- app/models/competition_team.rb | 3 + app/models/team_member.rb | 2 +- .../_competition_team_form.html.erb | 263 ++++++++++-------- .../_search_teacher_list.html.erb | 2 +- app/views/competition_teams/create.js.erb | 10 +- app/views/competition_teams/join_team.js.erb | 16 +- app/views/competition_teams/update.js.erb | 6 +- app/views/competitions/enroll.html.erb | 2 +- public/stylesheets/educoder/edu-all.css | 2 +- 11 files changed, 282 insertions(+), 228 deletions(-) diff --git a/app/controllers/competition_teams_controller.rb b/app/controllers/competition_teams_controller.rb index 5cac9ef0..0d78fb58 100644 --- a/app/controllers/competition_teams_controller.rb +++ b/app/controllers/competition_teams_controller.rb @@ -38,32 +38,34 @@ class CompetitionTeamsController < ApplicationController end def create - if params[:name] - # 判断用户是否已创建过战队 - if @competition.competition_teams.where(:user_id => User.current.id).count > 0 && !User.current.is_teacher - @status = 1 - else - invite_code = generate_team_code - teacher_id = User.current.user_extensions.identity == 0 ? User.current.id : params[:teacher] - - new_team = CompetitionTeam.create(:competition_id => @competition.id, :name => params[:name], - :invite_code => invite_code, :user_id => User.current.id, :teacher_id => teacher_id) - new_team.team_members << TeamMember.new(:user_id => User.current.id, :role => 1, :competition_id => @competition.id, :is_teacher => User.current.user_extensions.identity == 0 ? 1 : 0) - if params[:member_ids] - params[:member_ids].each do |user_id| - new_team.team_members << TeamMember.new(:user_id => user_id, :role => 2, :competition_id => @competition.id) - end - end - if params[:teacher] && params[:teacher].strip != "" - new_team.team_members << TeamMember.new(:user_id => params[:teacher], :role => 2, :competition_id => @competition.id, :is_teacher => 1) - # 创建成功且配置了指导老师 - @status = 0 - elsif User.current.user_extensions.identity != 0 - # 创建成功未配置指导老师 - @status = 2 - elsif User.current.user_extensions.identity == 0 - @status = 0 - end + # 判断用户是否已创建过战队 + if @competition.competition_teams.where(:user_id => User.current.id).count > 0 && !User.current.is_teacher + @status, @message = -1, '您已创建过战队,不能重复创建' + return + end + + if params[:name].blank? + @status, @message = -1, '战队名称不能为空' + return + end + + is_teacher = User.current.user_extensions.identity == 0 + return unless member_and_teacher_count_valid?(is_teacher) + + ActiveRecord::Base.transaction do + invite_code = generate_team_code + new_team = CompetitionTeam.create(:competition_id => @competition.id, :name => params[:name], + :invite_code => invite_code, :user_id => User.current.id) + new_team.team_members.create!(user_id: User.current.id, role: 1, competition_id: @competition.id, is_teacher: User.current.user_extensions.identity == 0 ? 1 : 0) + + params[:member_ids].try(:each) do |user_id| + next if user_id.to_i == User.current.id + new_team.team_members.create!(user_id: user_id, role: 2, competition_id: @competition.id) + end + + params[:teacher_ids].try(:each) do |user_id| + next if user_id.to_i == User.current.id + new_team.team_members.create!(user_id: user_id, role: 3, competition_id: @competition.id, is_teacher: 1) end end end @@ -89,79 +91,80 @@ class CompetitionTeamsController < ApplicationController end def update - unless @competition.enroll_end_time.present? && @competition.enroll_end_time < Time.now - if params[:name] - teacher_id = User.current.user_extensions.identity == 0 ? User.current.id : params[:teacher] - - @team.update_attributes(:name => params[:name], :teacher_id => teacher_id) - @team.team_members.update_all(:is_teacher => 0) - if @team.user.user_extensions.identity == 0 - @team.team_members.where(:user_id => @team.user_id).update_all(:is_teacher => 1) - end + is_teacher = @team.user.user_extensions.identity == 0 + return unless member_and_teacher_count_valid?(is_teacher) + + ActiveRecord::Base.transaction do + unless @competition.enroll_end_time.present? && @competition.enroll_end_time < Time.now + @team.update_attributes(name: params[:name]) if params[:name].present? - team_member_ids = @team.team_members.where(:role => 2, :is_teacher => 0).map(&:user_id) - new_member_ids = (params[:member_ids] || []).collect(&:to_i) + team_member_ids = @team.team_members.where(role: 2, is_teacher: 0).pluck(:user_id) + new_member_ids = (params[:member_ids].presence || []).map(&:to_i) + new_member_ids << @team.user_id unless is_teacher # 删除的成员 delete_member_ids = team_member_ids - new_member_ids - @team.team_members.where(:role => 2, :user_id => delete_member_ids).destroy_all + @team.team_members.where(role: 2, user_id: delete_member_ids).delete_all # 新增加的成员 (new_member_ids - team_member_ids).each do |user_id| - @team.team_members << TeamMember.new(:user_id => user_id, :role => 2, :competition_id => @competition.id) + next if user_id.to_i == @team.user_id + @team.team_members.create!(user_id: user_id, role: 2, competition_id: @competition.id) end end - end - # 判断之前是否已存在指导老师,不存在则创建 否则更新 - if params[:teacher] && params[:teacher].strip != "" - o_teacher = @team.team_members.where(:is_teacher => 1).first - unless o_teacher.present? && o_teacher.user_id == params[:teacher].to_i - if o_teacher.present? - o_teacher.update_attributes(:user_id => params[:teacher]) - else - teacher = @team.team_members.where(:user_id => params[:teacher]).first - if teacher.present? - teacher.update_column("is_teacher", 1) - else - @team.team_members << TeamMember.new(:user_id => params[:teacher], :role => 2, :competition_id => @competition.id, :is_teacher => 1) - end - end + teacher_ids = @team.teachers.pluck(:user_id) + new_teacher_ids = (params[:teacher_ids].presence || []).map(&:to_i) + new_teacher_ids << @team.user_id if is_teacher + # 删除的老师 + delete_teacher_ids = teacher_ids - new_teacher_ids + @team.team_members.where(role: 3, user_id: delete_teacher_ids).delete_all + # 新增加的老师 + (new_teacher_ids - teacher_ids).each do |user_id| + next if user_id.to_i == @team.user_id + @team.team_members.create!(user_id: user_id, role: 3, competition_id: @competition.id, is_teacher: true) end - @team.update_attributes(:teacher_id => params[:teacher]) - elsif @team.user.user_extensions.identity != 0 - @team.update_attributes(:teacher_id => nil) - @team.team_members.where(:is_teacher => 1).destroy_all end end # @status:提示语标志(0:加入成功;1:邀请码错误;2:已经加入了其他队, 3:超过人数限制,4:已有指导老师,5:只有学生和老师身份的用户才能加入战队) def join_team - team = @competition.competition_teams.where(:invite_code => params[:code]).first - if team.present? - if TeamMember.where(:user_id => User.current.id, :competition_team_id => @competition.competition_teams.map(&:id), :is_teacher => 0).count > 0 - @status = 2 - else - # 老师身份加入战队判断是否已有指导老师 - if User.current.user_extensions.identity == 0 - if team.teacher_id.present? - @status = 4 - else - @status = 0 - team.update_attributes(:teacher_id => User.current.id) - team.team_members << TeamMember.new(:user_id => User.current.id, :role => 2, :competition_id => @competition.id, :is_teacher => 1) - end - elsif User.current.user_extensions.identity == 1 - if team.team_members.count < @competition.max_num.to_i - team.team_members << TeamMember.new(:user_id => User.current.id, :role => 2, :competition_id => @competition.id) - @status = 0 - else - @status = 3 - end - else - @status = 5 - end + team = @competition.competition_teams.where(invite_code: params[:code]).first + if team.blank? + @status, @message = -1, '战队邀请码错误' + return + end + + if TeamMember.where(:user_id => User.current.id, :competition_team_id => @competition.competition_teams.map(&:id), :is_teacher => 0).count > 0 + @status, @message = -1, '您已加入战队,不能重复加' + return + end + + if team.team_members.where(user_id: User.current.id).exists? + @status, @message = -1, '您已加入该战队' + return + end + + # 老师身份加入战队 + if User.current.user_extensions.identity == 0 + teacher_staff = @competition.where(category: 'teacher').first + + if teacher_staff.blank? + @status, @message = -1, '该竞赛不能配备导师' + return end + + if team.teachers.count + 1 > teacher_staff.maximum + @status, @message = -1, '该战队导师人数已满' + return + end + team.team_members.create!(user_id: User.current.id, role: 3, competition_id: @competition.id, is_teacher: true) else - @status = 1 + max_member_count = @competition.where('category != "teacher"').sum(:maximum) + if team.members.count + 1 > max_member_count + @status, @message = -1, '该战队成员人数已满' + return + end + + team.team_members.create!(user_id: User.current.id, role: 2, competition_id: @competition.id) end end @@ -169,9 +172,6 @@ class CompetitionTeamsController < ApplicationController def exit_team team_member = @team.team_members.where(:user_id => User.current.id).first if team_member.present? - if team_member.is_teacher - @team.update_attributes(:teacher_id => nil) - end if team_member.user_id != @team.user_id team_member.destroy @status = 1 @@ -214,4 +214,33 @@ class CompetitionTeamsController < ApplicationController rescue ActiveRecord::RecordNotFound render_404 end + + def member_and_teacher_count_valid?(is_teacher) + teacher_staff = @competition.competition_staffs.where(category: 'teacher').first + teacher_count = params[:teacher_ids].try(:size) || 0 + teacher_count += 1 if is_teacher + # 检查老师数量 + if teacher_staff.blank? && teacher_count > 0 + @status, @message = -1, '该竞赛不能配备导师' + return false + elsif teacher_staff.present? && (teacher_staff.minimum > teacher_count || teacher_staff.maximum < teacher_count) + @status = -1 + @message = teacher_staff.maximum == teacher_staff.maximum ? "导师数量应为#{teacher_staff.minimum}人" : "导师数量应为#{teacher_staff.minimum}-#{teacher_staff.maximum}人" + return false + end + + member_relations = @competition.competition_staffs.where('category != "teacher"') + min_member_count = member_relations.sum(:minimum) + max_member_count = member_relations.sum(:maximum) + member_count = params[:member_ids].try(:size) || 0 + member_count += 1 unless is_teacher + # 检查成员数据 + if min_member_count > member_count || member_count > max_member_count + @status = -1 + @message = min_member_count == max_member_count ? "成员数量应为#{max_member_count}人" : "成员数量应为#{min_member_count}-#{max_member_count}人" + return false + end + + true + end end diff --git a/app/controllers/competitions_controller.rb b/app/controllers/competitions_controller.rb index 75a06fd3..bd3134a2 100644 --- a/app/controllers/competitions_controller.rb +++ b/app/controllers/competitions_controller.rb @@ -110,7 +110,8 @@ class CompetitionsController < ApplicationController @team_members_count = TeamMember.where(:competition_team_id => @teams.pluck(:id)).count @is_enroll = CompetitionTeam.where(:id => TeamMember.where(:user_id => @user, :competition_team_id => @competition.competition_teams.map(&:id)).pluck(:competition_team_id)).reorder("created_at desc") - @show_notice = (@competition.identifier == "gcc-dev-2018" || @competition.identifier == "gcc-annotation-2018") && @competition.competition_teams.where(:user_id => User.current.id, :teacher_id => nil).count > 0 + @show_notice = (@competition.identifier == "gcc-dev-2018" || @competition.identifier == "gcc-annotation-2018") && + @competition.competition_teams.joins(:team_members).where(:user_id => User.current.id).group('competition_teams.id').sum('IF(team_members.is_teacher=1, 1, 0)').values.any?(&:zero?) @teams = paginateHelper @teams, 50 respond_to do |format| format.js diff --git a/app/models/competition_team.rb b/app/models/competition_team.rb index 804854f4..84dcac81 100644 --- a/app/models/competition_team.rb +++ b/app/models/competition_team.rb @@ -2,7 +2,10 @@ class CompetitionTeam < ActiveRecord::Base belongs_to :user belongs_to :competition + has_many :team_members, :dependent => :destroy + has_many :members, conditions: 'is_teacher = 0', class_name: 'TeamMember' + has_many :teachers, conditions: 'is_teacher = 1', class_name: 'TeamMember' has_many :competition_scores, :dependent => :destroy # team_type 0:组队 1:个人 # attr_accessible :invite_code, :name, :team_type diff --git a/app/models/team_member.rb b/app/models/team_member.rb index d722f7c8..aef358da 100644 --- a/app/models/team_member.rb +++ b/app/models/team_member.rb @@ -3,5 +3,5 @@ class TeamMember < ActiveRecord::Base belongs_to :competition belongs_to :user # role 1:创建者 2:成员 3:指导老师 - # attr_accessible :role + attr_accessible :role, :user_id, :competition_id, :is_teacher end diff --git a/app/views/competition_teams/_competition_team_form.html.erb b/app/views/competition_teams/_competition_team_form.html.erb index dc845c59..c182c138 100644 --- a/app/views/competition_teams/_competition_team_form.html.erb +++ b/app/views/competition_teams/_competition_team_form.html.erb @@ -1,7 +1,12 @@ <% none_edit = @team.try(:id).present? && @competition.enroll_end_time.present? && @competition.enroll_end_time < Time.now %> <%= labelled_form_for @team, :html => {:id => 'competition_team_form', :multipart => true, :remote => true} do |f| %> - - + <% teacher_staff = @competition.competition_staffs.where(category: 'teacher').first %> + <% min_member_staff = @competition.competition_staffs.where('category != "teacher"').sum(:minimum) %> + <% max_member_staff = @competition.competition_staffs.where('category != "teacher"').sum(:maximum) %> + + + + @@ -15,7 +20,8 @@

-
+ <% if teacher_staff.present? %> +
autocomplete="off" placeholder="请您输入老师姓名进行搜索" id="teacher_search_input" @@ -31,73 +37,82 @@ 添加导师

-
- -
- autocomplete="off" style="width: 100%" placeholder="请您输入想要添加的成员搜索" id="user_search_input"> - - - - - - -
    -
+ <% end %> + <% if min_member_staff > 0 %> +
+ +
+ autocomplete="off" style="width: 100%" placeholder="请您输入想要添加的成员搜索" id="user_search_input"> + + + + + + +
    +
+
+ 添加成员
- 添加成员 -

+ <% end %> -
-

- 姓名 - 角色 - 单位 - 其他 - 操作 +

+

+ 姓名 + 角色 + 单位 + 其他 + 操作 +

+ <% if @team.try(:id).present? %> + <% creator_teacher = @team.user.user_extensions.identity == 0 %> +

+ <%= @team.user.show_name %> + <%= @team.user.user_extensions.identity == 0 ? "导师/创建者" : "创建者" %> + <%= @team.user.school_name %> + <%= @team.user.user_extensions.identity == 0 ? "职称:#{@team.user.identity}" : "学号:#{@team.user.user_extensions.student_id.present? ? @team.user.user_extensions.student_id : "--"}" %> +

- <% if @team.try(:id).present? %> -

- <%= @team.user.show_name %> - <%= @team.user.user_extensions.identity == 0 ? "导师/创建者" : "创建者" %> - <%= @team.user.school_name %> - <%= @team.user.user_extensions.identity == 0 ? "职称:#{@team.user.identity}" : "学号:#{@team.user.user_extensions.student_id.present? ? @team.user.user_extensions.student_id : "--"}" %> - + + <% @team.teachers.includes(user: :user_extensions).each do |teacher| %> + <% next if teacher.user_id == @team.user_id %> +

+ + <%= teacher.user.show_name %> + 导师 + <%= teacher.user.school_name %> + 职称:<%= teacher.user.identity %> +

- <% if @team.teacher.present? && @team.teacher != @team.user %> -

- - <%= @team.teacher.show_name %> - 导师 - <%= @team.teacher.school_name %> - 职称:<%= @team.teacher.identity %> - -

- <% end %> - <% @team.team_members.where(:role => 2, :is_teacher => 0).each do |team_user| %> -

- - <%= team_user.user.show_name %> - 成员 - <%= team_user.user.school_name %> - 学号:<%= team_user.user.user_extensions.student_id.present? ? team_user.user.user_extensions.student_id : "--" %> - - <% unless none_edit %> - - <% end %> - -

- <% end %> - <% else %> -

- <%= @team_user.show_name %> - <%= @team_user.user_extensions.identity == 0 ? "导师/创建者" : "创建者" %> - <%= @team_user.school_name %> - <%= @team_user.user_extensions.identity == 0 ? "职称:#{@team_user.identity}" : "学号:#{@team_user.user_extensions.student_id.present? ? @team_user.user_extensions.student_id : "--"}" %> - + <% end %> + + <% @team.members.includes(user: :user_extensions).each do |team_user| %> + <% next if team_user.user_id == @team.user_id %> +

+ + <%= team_user.user.show_name %> + 成员 + <%= team_user.user.school_name %> + 学号:<%= team_user.user.user_extensions.student_id.present? ? team_user.user.user_extensions.student_id : "--" %> + + <% unless none_edit %> + + <% end %> +

<% end %> -
+ <% else %> + <% creator_teacher = @team_user.user_extensions.identity == 0 %> +

+ <%= @team_user.show_name %> + <%= @team_user.user_extensions.identity == 0 ? "导师/创建者" : "创建者" %> + <%= @team_user.school_name %> + <%= @team_user.user_extensions.identity == 0 ? "职称:#{@team_user.identity}" : "学号:#{@team_user.user_extensions.student_id.present? ? @team_user.user_extensions.student_id : "--"}" %> + +

+ <% end %> +

@@ -111,69 +126,81 @@