Allow random selection for ProxyExercise

This commit is contained in:
Sebastian Serth
2023-01-20 20:56:20 +01:00
parent d05d69756d
commit 2679f5fa56
9 changed files with 45 additions and 10 deletions

View File

@ -42,7 +42,7 @@ class ProxyExercisesController < ApplicationController
def proxy_exercise_params
if params[:proxy_exercise].present?
params[:proxy_exercise].permit(:description, :title, :public, exercise_ids: []).merge(user_id: current_user.id,
params[:proxy_exercise].permit(:description, :title, :algorithm, :public, exercise_ids: []).merge(user_id: current_user.id,
user_type: current_user.class.name)
end
end

View File

@ -4,6 +4,11 @@ class ProxyExercise < ApplicationRecord
include Creation
include DefaultValues
enum algorithm: {
best_match: 0,
random: 1,
}, _default: :write, _prefix: true
after_initialize :generate_token
after_initialize :set_reason
after_initialize :set_default_values
@ -47,16 +52,25 @@ class ProxyExercise < ApplicationRecord
Rails.logger.debug { "retrieved assigned exercise for user #{user.id}: Exercise #{assigned_user_proxy_exercise.exercise}" }
assigned_user_proxy_exercise.exercise
else
Rails.logger.debug { "find new matching exercise for user #{user.id}" }
matching_exercise =
begin
find_matching_exercise(user)
rescue StandardError => e # fallback
Rails.logger.error("finding matching exercise failed. Fall back to random exercise! Error: #{$ERROR_INFO}")
@reason[:reason] = 'fallback because of error'
@reason[:error] = "#{$ERROR_INFO}:\n\t#{e.backtrace.join("\n\t")}"
exercises.where('expected_difficulty > 1').sample # difficulty should be > 1 to prevent dummy exercise from being chosen.
case algorithm
when 'best_match'
Rails.logger.debug { "find new matching exercise for user #{user.id}" }
begin
find_matching_exercise(user)
rescue StandardError => e # fallback
Rails.logger.error("finding matching exercise failed. Fall back to random exercise! Error: #{$ERROR_INFO}")
@reason[:reason] = 'fallback because of error'
@reason[:error] = "#{$ERROR_INFO}:\n\t#{e.backtrace.join("\n\t")}"
exercises.where('expected_difficulty > 1').sample # difficulty should be > 1 to prevent dummy exercise from being chosen.
end
when 'random'
@reason[:reason] = 'random exercise requested'
exercises.sample
else
raise "Unknown algorithm #{algorithm}"
end
user.user_proxy_exercise_exercises << UserProxyExerciseExercise.create(user:,
exercise: matching_exercise, proxy_exercise: self, reason: @reason.to_json)
matching_exercise

View File

@ -6,6 +6,9 @@
.mb-3
= f.label(:description, class: 'form-label')
= f.pagedown :description, input_html: { preview: true, rows: 10 }
.mb-3
= f.label(:algorithm, class: 'form-label')
= f.collection_select(:algorithm, ProxyExercise.algorithms.map { |algorithm, _id| [t("activerecord.attributes.proxy_exercise.algorithm_type.#{algorithm}"), algorithm] }, :second, :first, {}, class: 'form-control form-control-sm')
.form-check.mb-3
label.form-check-label
= f.check_box(:public, class: 'form-check-input')

View File

@ -5,6 +5,7 @@ h1
= row(label: 'exercise.title', value: @proxy_exercise.title)
= row(label: 'exercise.user', value: link_to_if(policy(@proxy_exercise.author).show?, @proxy_exercise.author, @proxy_exercise.author))
= row(label: 'proxy_exercise.files_count', value: @exercises.count)
= row(label: 'proxy_exercise.algorithm', value: t("activerecord.attributes.proxy_exercise.algorithm_type.#{@proxy_exercise.algorithm}"))
= row(label: 'exercise.public', value: @proxy_exercise.public?)
= row(label: 'exercise.description', value: @proxy_exercise.description)
= row(label: 'exercise.embedding_parameters', class: 'mb-4') do