From ba11884a60c43fe33d0cac5d744fdd22fdf0b25f Mon Sep 17 00:00:00 2001 From: p31729568 Date: Fri, 2 Aug 2019 10:32:41 +0800 Subject: [PATCH 1/5] modify competition course --- app/controllers/competition_teams_controller.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/controllers/competition_teams_controller.rb b/app/controllers/competition_teams_controller.rb index 72732a1a..4570366f 100644 --- a/app/controllers/competition_teams_controller.rb +++ b/app/controllers/competition_teams_controller.rb @@ -41,8 +41,14 @@ class CompetitionTeamsController < ApplicationController # 课堂 + 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('count(compelete_status !=0 ) as finish, count(*) as total') + .having('finish > (total / 2)').to_sql course_ids = Course.where('courses.created_at > ?', Time.parse('2018-06-01')) .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.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)') .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()') @@ -359,7 +365,7 @@ class CompetitionTeamsController < ApplicationController end 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') .having('finish > (total / 2)').to_sql From 57588e33f0ec3e2e5f6b39590ee86990cb14df50 Mon Sep 17 00:00:00 2001 From: p31729568 Date: Fri, 2 Aug 2019 10:55:43 +0800 Subject: [PATCH 2/5] fix --- app/controllers/competition_teams_controller.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/controllers/competition_teams_controller.rb b/app/controllers/competition_teams_controller.rb index 4570366f..c6b80e73 100644 --- a/app/controllers/competition_teams_controller.rb +++ b/app/controllers/competition_teams_controller.rb @@ -43,12 +43,12 @@ class CompetitionTeamsController < ApplicationController # 课堂 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('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 course_ids = Course.where('courses.created_at > ?', Time.parse('2018-06-01')) .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.homework_type = 4 and exists(#{subquery}))") + .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)') .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()') @@ -366,8 +366,8 @@ class CompetitionTeamsController < ApplicationController def get_valid_course_count(ids) 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') - .having('finish > (total / 2)').to_sql + .select('sum(compelete_status !=0 ) as finish, count(*) as total') + .having('total != 0 && finish > (total / 2)').to_sql Course.joins(shixun_homework_commons: :homework_commons_shixuns) .where('shixun_id in (?)', ids) @@ -376,8 +376,8 @@ class CompetitionTeamsController < ApplicationController end def get_valid_shixun_count(ids) - percentage_sql = StudentWork.where('homework_common_id = homework_commons.id') - .select('count(compelete_status !=0 ) as finish, count(*) as total') + 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('sum(compelete_status !=0 ) as finish, count(*) as total') .having('finish > (total / 2)').to_sql Shixun.joins(homework_commons_shixuns: :homework_common) .where(homework_commons: { homework_type: 3 }) From 6822506bdf4200d12d6e62ec6a8b26103110fa85 Mon Sep 17 00:00:00 2001 From: p31729568 Date: Fri, 2 Aug 2019 11:20:56 +0800 Subject: [PATCH 3/5] fix --- app/controllers/competition_teams_controller.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/competition_teams_controller.rb b/app/controllers/competition_teams_controller.rb index c6b80e73..e559253b 100644 --- a/app/controllers/competition_teams_controller.rb +++ b/app/controllers/competition_teams_controller.rb @@ -44,7 +44,7 @@ class CompetitionTeamsController < ApplicationController 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('finish > (total / 2)').to_sql + .having('total != 0 and finish >= (total / 2)').to_sql course_ids = Course.where('courses.created_at > ?', Time.parse('2018-06-01')) .where('courses.created_at <= ?', @competition.end_time) .where("(#{student_count_subquery}) >= 10") @@ -367,7 +367,7 @@ class CompetitionTeamsController < ApplicationController def get_valid_course_count(ids) 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('sum(compelete_status !=0 ) as finish, count(*) as total') - .having('total != 0 && finish > (total / 2)').to_sql + .having('total != 0 and finish >= (total / 2)').to_sql Course.joins(shixun_homework_commons: :homework_commons_shixuns) .where('shixun_id in (?)', ids) @@ -378,7 +378,7 @@ class CompetitionTeamsController < ApplicationController def get_valid_shixun_count(ids) 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('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) .where(homework_commons: { homework_type: 3 }) .where('course_id in (?)', ids) From be11a48ea82bfd84760b68e330a7c84fe736bcf0 Mon Sep 17 00:00:00 2001 From: p31729568 Date: Fri, 2 Aug 2019 17:21:41 +0800 Subject: [PATCH 4/5] fix --- app/controllers/competition_teams_controller.rb | 6 ++++-- app/views/competition_teams/show.html.erb | 10 ++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/app/controllers/competition_teams_controller.rb b/app/controllers/competition_teams_controller.rb index e559253b..cfa3a281 100644 --- a/app/controllers/competition_teams_controller.rb +++ b/app/controllers/competition_teams_controller.rb @@ -25,11 +25,12 @@ class CompetitionTeamsController < ApplicationController shixun_ids = @shixuns.map(&:id) @myshixun_count_map = get_valid_myshixun_count(shixun_ids) + @original_myshixun_count_map = @myshixun_count_map.clone # forked shixun valid myshixun count 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_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 = get_valid_myshixun_count(forked_shixun_map.keys) + @forked_myshixun_count_map.each { |k, v| @myshixun_count_map[forked_shixun_map[k]] += v } @course_count_map = get_valid_course_count(shixun_ids) forked_map = get_valid_course_count(forked_shixun_map.keys) @@ -38,6 +39,7 @@ class CompetitionTeamsController < ApplicationController @forked_course_count_map[forked_shixun_map[forked_id]] ||= 0 @forked_course_count_map[forked_shixun_map[forked_id]] += course_count end + @forked_shixun_map = forked_shixun_map # 课堂 diff --git a/app/views/competition_teams/show.html.erb b/app/views/competition_teams/show.html.erb index f8e1f1c5..f8c974b7 100644 --- a/app/views/competition_teams/show.html.erb +++ b/app/views/competition_teams/show.html.erb @@ -28,14 +28,20 @@ total_forked_myshixun_count += shixun['forked_myshixun_count'].to_i valid_course_count = @course_count_map.fetch(shixun.id, 0) - valid_student_count = @myshixun_count_map.fetch(shixun.id, 0) + valid_student_count = @original_myshixun_count_map.fetch(shixun.id, 0) score = if shixun.fork_from.blank? 500 + 50 * valid_course_count + 10 * valid_student_count else 100 + 10 * valid_course_count + 5 * valid_student_count end - score += @forked_course_count_map.fetch(shixun.id, 0) + + @forked_shixun_map.each do |shixun_id, fork_from_id| + next if fork_from_id != shixun.id + + score += 100 + 10 * @forked_shixun_map.fetch(shixun_id, 0) + 5 * @forked_myshixun_count_map.fetch(shixun_id, 0) + end + total_shixun_score += score %> From c49983e224e78488b164648aa831d723c3effb82 Mon Sep 17 00:00:00 2001 From: p31729568 Date: Fri, 2 Aug 2019 17:27:27 +0800 Subject: [PATCH 5/5] fix --- app/controllers/competition_teams_controller.rb | 4 ++-- app/views/competition_teams/show.html.erb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/competition_teams_controller.rb b/app/controllers/competition_teams_controller.rb index cfa3a281..f115a610 100644 --- a/app/controllers/competition_teams_controller.rb +++ b/app/controllers/competition_teams_controller.rb @@ -33,9 +33,9 @@ class CompetitionTeamsController < ApplicationController @forked_myshixun_count_map.each { |k, v| @myshixun_count_map[forked_shixun_map[k]] += v } @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_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]] += course_count end diff --git a/app/views/competition_teams/show.html.erb b/app/views/competition_teams/show.html.erb index f8c974b7..caef09a5 100644 --- a/app/views/competition_teams/show.html.erb +++ b/app/views/competition_teams/show.html.erb @@ -98,7 +98,7 @@ total_members_count += course.members_count.to_i total_shixun_homework_count += course['shixun_homework_count'].to_i - score = 500 + 5 * @course_shixun_count_map.fetch(course.id, 0) * @course_myshixun_map.fetch(course.id, 0) + score = 500 + 5 * @forked_map.fetch(course.id, 0) * @course_myshixun_map.fetch(course.id, 0) total_course_score += score %>