Check RfC visibility before redirecting users

This commit is contained in:
Sebastian Serth
2023-02-21 10:01:01 +01:00
parent d0e4fcfa20
commit ddf041ac05
2 changed files with 6 additions and 6 deletions

View File

@ -21,7 +21,7 @@ module RedirectBehavior
return
end
rfc = @submission.own_unsolved_rfc
rfc = @submission.own_unsolved_rfc(current_user)
if rfc
# set a message that informs the user that his own RFC should be closed.
flash[:notice] = I18n.t('exercises.submit.full_score_redirect_to_own_rfc')
@ -36,7 +36,7 @@ module RedirectBehavior
end
# else: show open rfc for same exercise if available
rfc = @submission.unsolved_rfc
rfc = @submission.unsolved_rfc(current_user)
unless rfc.nil? || @embed_options[:disable_redirect_to_rfcs] || @embed_options[:disable_rfc]
# set a message that informs the user that his score was perfect and help in RFC is greatly appreciated.
flash[:notice] = I18n.t('exercises.submit.full_score_redirect_to_rfc')

View File

@ -128,12 +128,12 @@ class Submission < ApplicationRecord
(user_id + exercise.created_at.to_i) % 10 == 1
end
def own_unsolved_rfc
RequestForComment.unsolved.find_by(exercise_id: exercise, user_id:)
def own_unsolved_rfc(user = self.user)
Pundit.policy_scope(user, RequestForComment).unsolved.find_by(exercise_id: exercise, user_id:)
end
def unsolved_rfc
RequestForComment.unsolved.where(exercise_id: exercise).where.not(question: nil).where(created_at: OLDEST_RFC_TO_SHOW.ago..Time.current).order('RANDOM()').find do |rfc_element|
def unsolved_rfc(user = self.user)
Pundit.policy_scope(user, RequestForComment).unsolved.where(exercise_id: exercise).where.not(question: nil).where(created_at: OLDEST_RFC_TO_SHOW.ago..Time.current).order('RANDOM()').find do |rfc_element|
((rfc_element.comments_count < MAX_COMMENTS_ON_RECOMMENDED_RFC) && !rfc_element.question.empty?)
end
end