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

View File

@ -54,6 +54,10 @@ de:
proxy_exercise:
title: Title
files_count: Anzahl der Aufgaben
algorithm: Algorithmus
algorithm_type:
best_match: Beste Übereinstimmung
random: Zufällige Aufgabe
external_user:
consumer: Konsument
email: E-Mail

View File

@ -54,6 +54,10 @@ en:
proxy_exercise:
title: Title
files_count: Exercises Count
algorithm: Algorithm
algorithm_type:
best_match: Best Match
random: Random Exercise
external_user:
consumer: Consumer
email: Email

View File

@ -0,0 +1,7 @@
# frozen_string_literal: true
class AddAlgorithmToProxyExercise < ActiveRecord::Migration[7.0]
def change
add_column :proxy_exercises, :algorithm, :integer, limit: 1, null: false, default: 0, comment: 'Used as enum in Rails'
end
end

View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.0].define(version: 2022_12_06_221333) do
ActiveRecord::Schema[7.0].define(version: 2023_01_20_185807) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
enable_extension "pgcrypto"
@ -358,6 +358,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_12_06_221333) do
t.string "user_type"
t.bigint "user_id"
t.boolean "public", default: false, null: false
t.integer "algorithm", limit: 2, default: 0, null: false, comment: "Used as enum in Rails"
t.index ["user_type", "user_id"], name: "index_proxy_exercises_on_user_type_and_user_id"
end

View File

@ -5,5 +5,6 @@ FactoryBot.define do
created_by_teacher
token { 'dummytoken' }
title { 'Dummy' }
algorithm { 'best_match' }
end
end