@ -25,24 +25,32 @@ class CompetitionTeamsController < ApplicationController
shixun_ids = @shixuns . map ( & :id )
shixun_ids = @shixuns . map ( & :id )
@myshixun_count_map = get_valid_myshixun_count ( shixun_ids )
@myshixun_count_map = get_valid_myshixun_count ( shixun_ids )
@original_myshixun_count_map = @myshixun_count_map . clone
# forked shixun valid myshixun count
# forked shixun valid myshixun count
forked_shixun_map = Shixun . where ( status : 2 , fork_from : shixun_ids ) . select ( 'id, fork_from' )
forked_shixun_map = Shixun . where ( status : 2 , fork_from : shixun_ids ) . select ( 'id, fork_from' )
forked_shixun_map = forked_shixun_map . each_with_object ( { } ) { | sx , obj | obj [ sx . id ] = sx . fork_from }
forked_shixun_map = forked_shixun_map . each_with_object ( { } ) { | sx , obj | obj [ sx . id ] = sx . fork_from }
forked_myshixun_count_map = get_valid_myshixun_count ( forked_shixun_map . keys )
@ forked_myshixun_count_map = get_valid_myshixun_count ( forked_shixun_map . keys )
forked_myshixun_count_map. each { | k , v | @myshixun_count_map [ forked_shixun_map [ k ] ] += v }
@ forked_myshixun_count_map. each { | k , v | @myshixun_count_map [ forked_shixun_map [ k ] ] += v }
@course_count_map = get_valid_course_count ( shixun_ids )
@course_count_map = get_valid_course_count ( shixun_ids )
forked_map = get_valid_course_count ( forked_shixun_map . keys )
@ forked_map = get_valid_course_count ( forked_shixun_map . keys )
@forked_course_count_map = { }
@forked_course_count_map = { }
forked_map. each do | forked_id , course_count |
@ forked_map. each do | forked_id , course_count |
@forked_course_count_map [ forked_shixun_map [ forked_id ] ] || = 0
@forked_course_count_map [ forked_shixun_map [ forked_id ] ] || = 0
@forked_course_count_map [ forked_shixun_map [ forked_id ] ] += course_count
@forked_course_count_map [ forked_shixun_map [ forked_id ] ] += course_count
end
end
@forked_shixun_map = forked_shixun_map
# 课堂
# 课堂
student_count_subquery = CourseMember . where ( 'course_id = courses.id AND role = 5' ) . select ( 'count(*)' ) . to_sql
subquery = StudentWork . where ( 'homework_common_id = hcs.id' )
. select ( 'sum(compelete_status !=0 ) as finish, count(*) as total' )
. having ( 'total != 0 and finish >= (total / 2)' ) . to_sql
course_ids = Course . where ( 'courses.created_at > ?' , Time . parse ( '2018-06-01' ) )
course_ids = Course . where ( 'courses.created_at > ?' , Time . parse ( '2018-06-01' ) )
. where ( 'courses.created_at <= ?' , @competition . end_time )
. where ( 'courses.created_at <= ?' , @competition . end_time )
. where ( " ( #{ student_count_subquery } ) >= 10 " )
. where ( " exists(select 1 from homework_commons hcs where hcs.course_id = courses.id and hcs.publish_time is not null and hcs.publish_time < NOW() and hcs.homework_type = 4 and exists( #{ subquery } )) " )
. joins ( 'join course_members on course_members.course_id = courses.id and course_members.role in (1,2,3)' )
. joins ( 'join course_members on course_members.course_id = courses.id and course_members.role in (1,2,3)' )
. where ( course_members : { user_id : @team_user_ids } ) . pluck ( :id )
. where ( course_members : { user_id : @team_user_ids } ) . pluck ( :id )
courses = Course . where ( id : course_ids ) . joins ( :shixun_homework_commons ) . where ( 'homework_commons.publish_time < now()' )
courses = Course . where ( id : course_ids ) . joins ( :shixun_homework_commons ) . where ( 'homework_commons.publish_time < now()' )
@ -359,9 +367,9 @@ class CompetitionTeamsController < ApplicationController
end
end
def get_valid_course_count ( ids )
def get_valid_course_count ( ids )
percentage_sql = StudentWork . where ( 'homework_common_id = homework_commons.id ')
percentage_sql = StudentWork . where ( 'homework_common_id = homework_commons.id and homework_commons.publish_time is not null and homework_commons.publish_time < NOW() ')
. select ( ' count (compelete_status !=0 ) as finish, count(*) as total')
. select ( ' sum (compelete_status !=0 ) as finish, count(*) as total')
. having ( ' finish > (total / 2)') . to_sql
. having ( ' total != 0 and finish >= (total / 2)') . to_sql
Course . joins ( shixun_homework_commons : :homework_commons_shixuns )
Course . joins ( shixun_homework_commons : :homework_commons_shixuns )
. where ( 'shixun_id in (?)' , ids )
. where ( 'shixun_id in (?)' , ids )
@ -370,9 +378,9 @@ class CompetitionTeamsController < ApplicationController
end
end
def get_valid_shixun_count ( ids )
def get_valid_shixun_count ( ids )
percentage_sql = StudentWork . where ( 'homework_common_id = homework_commons.id ')
percentage_sql = StudentWork . where ( 'homework_common_id = homework_commons.id and homework_commons.publish_time is not null and homework_commons.publish_time < NOW() ')
. select ( ' count (compelete_status !=0 ) as finish, count(*) as total')
. select ( ' sum (compelete_status !=0 ) as finish, count(*) as total')
. having ( ' finish > (total / 2)') . to_sql
. having ( ' total != 0 and finish >= (total / 2)') . to_sql
Shixun . joins ( homework_commons_shixuns : :homework_common )
Shixun . joins ( homework_commons_shixuns : :homework_common )
. where ( homework_commons : { homework_type : 3 } )
. where ( homework_commons : { homework_type : 3 } )
. where ( 'course_id in (?)' , ids )
. where ( 'course_id in (?)' , ids )