@ -1,7 +1,7 @@
class CompetitionTeamsController < ApplicationController
include ApplicationHelper
before_filter :find_team , :except = > [ :new , :create , :join_team , :search_non_user , :personal_enroll , :check_team_identity , :search_teacher ]
before_filter :find_competition , :only = > [ :new , :create , :join_team , :search_ non_user, :personal_enroll ]
before_filter :find_competition , :only = > [ :new , :create , :join_team , :search_ teacher, :search_ non_user, :personal_enroll ]
before_filter :require_login
skip_before_filter :verify_authenticity_token , :only = > [ :edit_rule ]
layout 'base_competition'
@ -13,6 +13,10 @@ class CompetitionTeamsController < ApplicationController
end
def search_teacher
if params [ :team ] && params [ :team ] != " "
@team = @competition . competition_teams . where ( :id = > params [ :team ] ) . first
end
condition = " % #{ params [ :search ] . strip } % " . gsub ( " " , " " )
@teachers = User . joins ( :user_extensions ) . where ( " status = 1 and LOWER(concat(lastname, firstname, login, nickname)) LIKE ' #{ condition } ' and user_extensions.identity = 0 " )
end
@ -38,9 +42,13 @@ class CompetitionTeamsController < ApplicationController
end
def create
teacher_limited = @competition . teacher_enroll_mutiple_limited
member_limited = @competition . member_enroll_mutiple_limited
# 判断用户是否已创建过战队
if @competition . competition_teams . where ( :user_id = > User . current . id ) . count > 0 && ! User . current . is_teacher
@status , @message = - 1 , '您已创建过战队,不能重复创建'
if @competition . team_members . where ( user_id : User . current . id ) . count > 0 &&
( ( User . current . is_teacher && teacher_limited ) || ( ! User . current . is_teacher && member_limited ) )
@status , @message = - 1 , '您已经在战队中了'
return
end
@ -52,6 +60,11 @@ class CompetitionTeamsController < ApplicationController
is_teacher = User . current . user_extensions . identity == 0
return unless member_and_teacher_count_valid? ( is_teacher )
# 检查老师多次报名限制
return unless check_teacher_enroll_limited? ( @competition , ( params [ :teacher_ids ] . presence || [ ] ) . map ( & :to_i ) - [ User . current . id ] )
# 检查成员多次报名限制
return unless check_member_enroll_limited? ( @competition , ( params [ :member_ids ] . presence || [ ] ) . map ( & :to_i ) - [ User . current . id ] )
ActiveRecord :: Base . transaction do
invite_code = generate_team_code
new_team = CompetitionTeam . create ( :competition_id = > @competition . id , :name = > params [ :name ] ,
@ -104,8 +117,12 @@ class CompetitionTeamsController < ApplicationController
# 删除的成员
delete_member_ids = team_member_ids - new_member_ids
@team . team_members . where ( role : 2 , user_id : delete_member_ids ) . delete_all
# 新增加的成员
( new_member_ids - team_member_ids ) . each do | user_id |
ids = new_member_ids - team_member_ids
raise @message unless check_member_enroll_limited? ( @competition , ids ) # 有成员已经加入其他战队,并且只能一次报名
ids . each do | user_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
@ -117,12 +134,19 @@ class CompetitionTeamsController < ApplicationController
# 删除的老师
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 |
ids = new_teacher_ids - teacher_ids
raise @message unless check_teacher_enroll_limited? ( @competition , ids ) # 有老师已经加入其他战队,并且只能一次报名
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
end
rescue = > ex
@status = - 1
@message = ex . message
end
# @status:提示语标志( 0: 加入成功; 1: 邀请码错误; 2: 已经加入了其他队, 3: 超过人数限制, 4: 已有指导老师, 5: 只有学生和老师身份的用户才能加入战队)
@ -133,7 +157,8 @@ class CompetitionTeamsController < ApplicationController
return
end
if TeamMember . where ( :user_id = > User . current . id , :competition_team_id = > @competition . competition_teams . map ( & :id ) , :is_teacher = > 0 ) . count > 0
enroll_limited = ( User . current . is_teacher ? @competition . teacher_enroll_mutiple_limited : @competition . member_enroll_mutiple_limited )
if enroll_limited && @competition . team_members . exists? ( user_id : User . current . id )
@status , @message = - 1 , '您已加入战队,不能重复加'
return
end
@ -243,4 +268,30 @@ class CompetitionTeamsController < ApplicationController
true
end
def check_teacher_enroll_limited? ( competition , user_ids )
teacher_limited = competition . teacher_enroll_mutiple_limited
return true unless teacher_limited && user_ids . present?
repeat_teachers = competition . team_members . where ( user_id : user_ids ) . includes ( :user ) . to_a
if repeat_teachers . size > 0
@status , @message = - 1 , " 导师 #{ repeat_teachers . map { | t | t . user . show_real_name } . join ( ', ' ) } 已经加入其它战队了 "
return false
end
true
end
def check_member_enroll_limited? ( competition , user_ids )
member_limited = competition . member_enroll_mutiple_limited
return true unless member_limited && user_ids . present?
repeat_members = competition . team_members . where ( user_id : user_ids ) . includes ( :user ) . to_a
if repeat_members . size > 0
@status , @message = - 1 , " 成员 #{ repeat_members . map { | t | t . user . show_real_name } . join ( ', ' ) } 已经加入其它战队了 "
return false
end
true
end
end