Refactor user redirect after submission

This commit is contained in:
Maximilian Grundke
2017-11-08 15:39:04 +01:00
parent ac14e2d0ca
commit c4cf11f299
3 changed files with 36 additions and 17 deletions

View File

@ -24,10 +24,6 @@ class ExercisesController < ApplicationController
3 3
end end
MAX_EXERCISE_FEEDBACKS = 20
MAX_COMMENTS_PER_REQUEST = 5
def java_course_token def java_course_token
"702cbd2a-c84c-4b37-923a-692d7d1532d0" "702cbd2a-c84c-4b37-923a-692d7d1532d0"
end end
@ -380,15 +376,18 @@ class ExercisesController < ApplicationController
def redirect_after_submit def redirect_after_submit
Rails.logger.debug('Redirecting user with score:s ' + @submission.normalized_score.to_s) Rails.logger.debug('Redirecting user with score:s ' + @submission.normalized_score.to_s)
if @submission.normalized_score == 1.0 if submission.normalized_score == 1.0
# if user is external and has an own rfc, redirect to it and message him to clean up and accept the answer. (we need to check that the user is external, # if user is external and has an own rfc, redirect to it and message him to clean up and accept the answer. (we need to check that the user is external,
# otherwise an internal user could be shown a false rfc here, since current_user.id is polymorphic, but only makes sense for external users when used with rfcs.) # otherwise an internal user could be shown a false rfc here, since current_user.id is polymorphic, but only makes sense for external users when used with rfcs.)
# redirect 10 percent pseudorandomly to the feedback page # redirect 10 percent pseudorandomly to the feedback page
if current_user.respond_to? :external_id if current_user.respond_to? :external_id
if (((current_user.id + @submission.exercise.created_at.to_i) % 10 == 1) && @exercise.user_exercise_feedbacks.size <= MAX_EXERCISE_FEEDBACKS) if @submission.redirect_to_feedback?
redirect_to_user_feedback redirect_to_user_feedback
return return
elsif rfc = RequestForComment.unsolved.where(exercise_id: @submission.exercise, user_id: current_user.id).first end
rfc = @submission.own_unsolved_rfc
if rfc
# set a message that informs the user that his own RFC should be closed. # 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') flash[:notice] = I18n.t('exercises.submit.full_score_redirect_to_own_rfc')
flash.keep(:notice) flash.keep(:notice)
@ -398,20 +397,20 @@ class ExercisesController < ApplicationController
format.json { render(json: {redirect: url_for(rfc)}) } format.json { render(json: {redirect: url_for(rfc)}) }
end end
return return
end
# else: show open rfc for same exercise if available # else: show open rfc for same exercise if available
elsif rfc = RequestForComment.unsolved.where(exercise_id: @submission.exercise).where.not(question: nil).order("RANDOM()").find { | rfc_element |(rfc_element.comments_count < MAX_COMMENTS_PER_REQUEST) } rfc = @submission.unsolved_rfc
if(not rfc.nil?) unless rfc.nil?
# set a message that informs the user that his score was perfect and help in RFC is greatly appreciated. # 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') flash[:notice] = I18n.t('exercises.submit.full_score_redirect_to_rfc')
flash.keep(:notice) flash.keep(:notice)
respond_to do |format| respond_to do |format|
format.html { redirect_to(rfc) } format.html {redirect_to(rfc)}
format.json { render(json: {redirect: url_for(rfc)}) } format.json {render(json: {redirect: url_for(rfc)})}
end
return
end end
return
end end
end end
else else

View File

@ -37,6 +37,8 @@ class Exercise < ActiveRecord::Base
@working_time_statistics = nil @working_time_statistics = nil
MAX_EXERCISE_FEEDBACKS = 20
def average_percentage def average_percentage
if average_score and maximum_score != 0.0 and submissions.exists?(cause: 'submit') if average_score and maximum_score != 0.0 and submissions.exists?(cause: 'submit')
@ -362,4 +364,8 @@ class Exercise < ActiveRecord::Base
end end
private :valid_main_file? private :valid_main_file?
def needs_more_feedback
user_exercise_feedbacks.size <= MAX_EXERCISE_FEEDBACKS
end
end end

View File

@ -18,6 +18,8 @@ class Submission < ActiveRecord::Base
validates :cause, inclusion: {in: CAUSES} validates :cause, inclusion: {in: CAUSES}
validates :exercise_id, presence: true validates :exercise_id, presence: true
MAX_COMMENTS_ON_RECOMMENDED_RFC = 5
def build_files_hash(files, attribute) def build_files_hash(files, attribute)
files.map(&attribute.to_proc).zip(files).to_h files.map(&attribute.to_proc).zip(files).to_h
end end
@ -53,4 +55,16 @@ class Submission < ActiveRecord::Base
def to_s def to_s
Submission.model_name.human Submission.model_name.human
end end
def redirect_to_feedback?
((user_id + exercise.created_at.to_i) % 10 == 1) && exercise.needs_more_feedback
end
def own_unsolved_rfc
RequestForComment.unsolved.where(exercise_id: exercise, user_id: user_id).first
end
def unsolved_rfc
RequestForComment.unsolved.where(exercise_id: exercise).where.not(question: nil).order("RANDOM()").find { | rfc_element |(rfc_element.comments_count < MAX_COMMENTS_ON_RECOMMENDED_RFC) }
end
end end