diff --git a/app/controllers/subjects_controller.rb b/app/controllers/subjects_controller.rb index e8192ae72..694595d17 100644 --- a/app/controllers/subjects_controller.rb +++ b/app/controllers/subjects_controller.rb @@ -82,13 +82,15 @@ class SubjectsController < ApplicationController @is_creator = current_user.creator_of_subject?(@subject) # 合作团队 @members = @subject.subject_members.includes(:user) - @shixuns = @subject.shixuns.published.pluck(:id) - challenge_ids = Challenge.where(shixun_id: @shixuns).pluck(:id) + # @shixuns = @subject.shixuns.published.pluck(:id) + challenge_ids = @subject.challenge_ids # 实训路径中的所有实训标签 @tags = ChallengeTag.where(challenge_id: challenge_ids).pluck(:name).uniq # 用户获取的实训标签 # @user_tags = @subject.shixuns.map(&:user_tags_name).flatten.uniq @user_tags = user_shixun_tags challenge_ids, @user.id + @my_subject_progress = @subject.my_subject_progress + @challenge_count = challenge_ids.size # 访问数变更 @subject.increment!(:visits) diff --git a/app/helpers/subjects_helper.rb b/app/helpers/subjects_helper.rb index c464c7a7e..27495f326 100644 --- a/app/helpers/subjects_helper.rb +++ b/app/helpers/subjects_helper.rb @@ -1,10 +1,10 @@ module SubjectsHelper # 实训路径的发布状态 - def publish_status subject, is_manager, user, shixuns + def publish_status subject, is_manager, user, count status = -1 if is_manager - status = 0 if subject.status == 0 && shixuns.count > 0 + status = 0 if subject.status == 0 && count > 0 status = 1 if subject.status == 1 status = 2 if subject.status == 2 && user.admin? end @@ -13,6 +13,7 @@ module SubjectsHelper # 实训路径的所有用户获得的标签 def user_shixun_tags challenge_ids, user_id - ChallengeTag.joins(challenge: [:games]).where(games: {status: 2, user_id: user_id}, challenges: {id:challenge_ids}).pluck("challenge_tags.name").uniq + ChallengeTag.joins("join games on challenge_tags.challenge_id = games.challenge_id"). + where(challenge_id: challenge_ids, games: {status: 2, user_id: user_id}).pluck("challenge_tags.name").uniq end end diff --git a/app/models/subject.rb b/app/models/subject.rb index eb8930e3b..7a1253f9f 100644 --- a/app/models/subject.rb +++ b/app/models/subject.rb @@ -31,6 +31,12 @@ class Subject < ApplicationRecord self.tidings << Tiding.new(user_id: self.user_id, trigger_user_id: self.user_id, belong_container_id: self.id, belong_container_type: 'Subject', tiding_type: "System", viewed: 0) end + + def challenge_ids + sub_shixuns = shixuns.published.pluck(:id) + challenge_ids = Challenge.where(shixun_id: sub_shixuns).pluck(:id) + end + # 挑战过路径的成员数 def member_count shixuns.pluck(:myshixuns_count).sum @@ -54,8 +60,8 @@ class Subject < ApplicationRecord def my_subject_score shixuns_id = shixuns.published_closed.pluck(:id) - my_shixun_score = Challenge.joins(:games).where(games: {status: 2, user_id: User.current.id}, shixun_id: shixuns_id).pluck(:score).sum - my_choose_score = ChallengeChoose.joins(challenge: :games).where(games: {status: 2, user_id: User.current.id}, challenges: {shixun_id: shixuns_id}).pluck(:score).sum + my_shixun_score = Challenge.joins(:games).where(games: {status: 2, user_id: User.current.id}, shixun_id: shixuns_id).sum(:score) + my_choose_score = ChallengeChoose.joins(challenge: :games).where(games: {status: 2, user_id: User.current.id}, challenges: {shixun_id: shixuns_id}).sum(:score) return my_shixun_score.to_i + my_choose_score.to_i end @@ -68,8 +74,7 @@ class Subject < ApplicationRecord end def my_subject_progress - my_challenge_count = Game.joins(:challenge).where(user_id: User.current.id, status: 2, challenges: {shixun_id: shixuns.published_closed}). - pluck(:challenge_id).uniq.size + my_challenge_count = Game.where(user_id: User.current.id, status: 2, challenge_id: challenge_ids).pluck(:challenge_id).uniq.size count = self.subject_challenge_count == 0 ? 0 : ((my_challenge_count.to_f / self.subject_challenge_count).round(2) * 100).to_i end diff --git a/app/views/subjects/show.json.jbuilder b/app/views/subjects/show.json.jbuilder index cafb08942..7ea92a547 100644 --- a/app/views/subjects/show.json.jbuilder +++ b/app/views/subjects/show.json.jbuilder @@ -6,7 +6,7 @@ json.subject_score @subject.all_score json.member_count @subject.member_count json.allow_delete @is_creator && (@subject.status != 2 || @user.admin?) -json.publish_status publish_status(@subject, @is_creator, @user, @shixuns) +json.publish_status publish_status(@subject, @is_creator, @user, @challenge_count) json.allow_statistics @user.manager_of_subject?(@subject) json.allow_send @user.logged? json.allow_visit @user.manager_of_subject?(@subject) || @user.admin? || @subject.status > 1