@ -1,7 +1,7 @@
class CompetitionTeamsController < ApplicationController
class CompetitionTeamsController < ApplicationController
include ApplicationHelper
include ApplicationHelper
before_filter :find_team , :except = > [ :new , :create , :join_team , :search_non_user , :personal_enroll , :check_team_identity , :search_teacher ]
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
before_filter :require_login
skip_before_filter :verify_authenticity_token , :only = > [ :edit_rule ]
skip_before_filter :verify_authenticity_token , :only = > [ :edit_rule ]
layout 'base_competition'
layout 'base_competition'
@ -12,7 +12,38 @@ class CompetitionTeamsController < ApplicationController
@team_user = User . current
@team_user = User . current
end
end
def show
return render_404 if @competition . identifier != 'gcc-course-2019'
@team_user_ids = @team . team_members . pluck ( :user_id )
shixuns = Shixun . where ( user_id : @team_user_ids , status : 2 )
shixuns = shixuns . joins ( 'left join shixuns forked_shixuns on forked_shixuns.fork_from = shixuns.id and forked_shixuns.status = 2' )
shixuns = shixuns . joins ( 'left join myshixuns on myshixuns.shixun_id = shixuns.id and exists(select 1 from games where games.myshixun_id = myshixuns.id and games.status = 2)' )
shixuns = shixuns . select ( 'shixuns.id, shixuns.identifier, shixuns.user_id, shixuns.myshixuns_count, shixuns.name, shixuns.fork_from, sum(forked_shixuns.myshixuns_count) forked_myshixun_count' )
@shixuns = shixuns . group ( 'shixuns.id' ) . order ( 'shixuns.myshixuns_count desc' ) . includes ( :creator )
@myshixun_count_map = Myshixun . where ( shixun_id : @shixuns . map ( & :id ) )
. where ( 'exists(select 1 from games where games.myshixun_id = myshixuns.id and games.status = 2)' )
. group ( 'shixun_id' ) . count
course_ids = Course . joins ( 'join members on members.course_id = courses.id' )
. joins ( 'join member_roles on member_roles.member_id = members.id and member_roles.role_id in (3,7,9)' )
. where ( members : { user_id : @team_user_ids } ) . pluck ( :id )
courses = Course . where ( id : course_ids ) . joins ( :shixun_homework_commons )
@courses = courses . select ( 'courses.id, courses.name, courses.members_count, count(*) shixun_homework_count' )
. group ( 'courses.id' ) . order ( 'shixun_homework_count desc' )
@course_myshixun_map = Myshixun . joins ( student_works : :homework_common )
. where ( homework_commons : { course_id : @courses . map ( & :id ) } )
. where ( 'exists(select 1 from games where games.myshixun_id = myshixuns.id and games.status = 2)' )
. group ( 'homework_commons.course_id' ) . count
end
def search_teacher
def search_teacher
if params [ :team ] && params [ :team ] != " "
@team = @competition . competition_teams . where ( :id = > params [ :team ] ) . first
end
condition = " % #{ params [ :search ] . strip } % " . gsub ( " " , " " )
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 " )
@teachers = User . joins ( :user_extensions ) . where ( " status = 1 and LOWER(concat(lastname, firstname, login, nickname)) LIKE ' #{ condition } ' and user_extensions.identity = 0 " )
end
end
@ -38,9 +69,13 @@ class CompetitionTeamsController < ApplicationController
end
end
def create
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
if @competition . team_members . where ( user_id : User . current . id ) . count > 0 &&
@status , @message = - 1 , '您已创建过战队,不能重复创建'
( ( User . current . is_teacher && teacher_limited ) || ( ! User . current . is_teacher && member_limited ) )
@status , @message = - 1 , '您已经在战队中了'
return
return
end
end
@ -52,6 +87,11 @@ class CompetitionTeamsController < ApplicationController
is_teacher = User . current . user_extensions . identity == 0
is_teacher = User . current . user_extensions . identity == 0
return unless member_and_teacher_count_valid? ( is_teacher )
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
ActiveRecord :: Base . transaction do
invite_code = generate_team_code
invite_code = generate_team_code
new_team = CompetitionTeam . create ( :competition_id = > @competition . id , :name = > params [ :name ] ,
new_team = CompetitionTeam . create ( :competition_id = > @competition . id , :name = > params [ :name ] ,
@ -104,8 +144,12 @@ class CompetitionTeamsController < ApplicationController
# 删除的成员
# 删除的成员
delete_member_ids = team_member_ids - new_member_ids
delete_member_ids = team_member_ids - new_member_ids
@team . team_members . where ( role : 2 , user_id : delete_member_ids ) . delete_all
@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
next if user_id . to_i == @team . user_id
@team . team_members . create! ( user_id : user_id , role : 2 , competition_id : @competition . id )
@team . team_members . create! ( user_id : user_id , role : 2 , competition_id : @competition . id )
end
end
@ -117,12 +161,19 @@ class CompetitionTeamsController < ApplicationController
# 删除的老师
# 删除的老师
delete_teacher_ids = teacher_ids - new_teacher_ids
delete_teacher_ids = teacher_ids - new_teacher_ids
@team . team_members . where ( role : 3 , user_id : delete_teacher_ids ) . delete_all
@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
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 )
@team . team_members . create! ( user_id : user_id , role : 3 , competition_id : @competition . id , is_teacher : true )
end
end
end
end
rescue = > ex
@status = - 1
@message = ex . message
end
end
# @status:提示语标志( 0: 加入成功; 1: 邀请码错误; 2: 已经加入了其他队, 3: 超过人数限制, 4: 已有指导老师, 5: 只有学生和老师身份的用户才能加入战队)
# @status:提示语标志( 0: 加入成功; 1: 邀请码错误; 2: 已经加入了其他队, 3: 超过人数限制, 4: 已有指导老师, 5: 只有学生和老师身份的用户才能加入战队)
@ -133,7 +184,8 @@ class CompetitionTeamsController < ApplicationController
return
return
end
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 , '您已加入战队,不能重复加'
@status , @message = - 1 , '您已加入战队,不能重复加'
return
return
end
end
@ -243,4 +295,30 @@ class CompetitionTeamsController < ApplicationController
true
true
end
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
end