diff --git a/app/controllers/exercises_controller.rb b/app/controllers/exercises_controller.rb index 77b7bd7e..148f0178 100644 --- a/app/controllers/exercises_controller.rb +++ b/app/controllers/exercises_controller.rb @@ -24,10 +24,6 @@ class ExercisesController < ApplicationController 3 end - MAX_EXERCISE_FEEDBACKS = 20 - MAX_COMMENTS_PER_REQUEST = 5 - - def java_course_token "702cbd2a-c84c-4b37-923a-692d7d1532d0" end @@ -380,15 +376,18 @@ class ExercisesController < ApplicationController def redirect_after_submit 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, # 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 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 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. flash[:notice] = I18n.t('exercises.submit.full_score_redirect_to_own_rfc') flash.keep(:notice) @@ -398,20 +397,20 @@ class ExercisesController < ApplicationController format.json { render(json: {redirect: url_for(rfc)}) } end return + end # 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) } - if(not rfc.nil?) - # 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.keep(:notice) + rfc = @submission.unsolved_rfc + unless rfc.nil? + # 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.keep(:notice) - respond_to do |format| - format.html { redirect_to(rfc) } - format.json { render(json: {redirect: url_for(rfc)}) } - end - return + respond_to do |format| + format.html {redirect_to(rfc)} + format.json {render(json: {redirect: url_for(rfc)})} end + return end end else diff --git a/app/models/exercise.rb b/app/models/exercise.rb index b7487399..06ed391f 100644 --- a/app/models/exercise.rb +++ b/app/models/exercise.rb @@ -37,6 +37,8 @@ class Exercise < ActiveRecord::Base @working_time_statistics = nil + MAX_EXERCISE_FEEDBACKS = 20 + def average_percentage if average_score and maximum_score != 0.0 and submissions.exists?(cause: 'submit') @@ -362,4 +364,8 @@ class Exercise < ActiveRecord::Base end private :valid_main_file? + def needs_more_feedback + user_exercise_feedbacks.size <= MAX_EXERCISE_FEEDBACKS + end + end diff --git a/app/models/submission.rb b/app/models/submission.rb index 69a48307..38ea16d6 100644 --- a/app/models/submission.rb +++ b/app/models/submission.rb @@ -18,6 +18,8 @@ class Submission < ActiveRecord::Base validates :cause, inclusion: {in: CAUSES} validates :exercise_id, presence: true + MAX_COMMENTS_ON_RECOMMENDED_RFC = 5 + def build_files_hash(files, attribute) files.map(&attribute.to_proc).zip(files).to_h end @@ -53,4 +55,16 @@ class Submission < ActiveRecord::Base def to_s Submission.model_name.human 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