Skip to content

Commit

Permalink
basic functionality of reusing files based on xml_id.
Browse files Browse the repository at this point in the history
Use xml_id_path to recreate proforma structure with tests and model_solutions containing multiple files
  • Loading branch information
kkoehn committed Sep 23, 2024
1 parent b30aaa2 commit a7affb4
Show file tree
Hide file tree
Showing 8 changed files with 199 additions and 92 deletions.
94 changes: 60 additions & 34 deletions app/services/proforma_service/convert_exercise_to_task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,62 +77,85 @@ def uuid
end

def model_solutions
@exercise.files.filter {|file| file.role == 'reference_implementation' }.map do |file|
@exercise.files.filter {|file| file.role == 'reference_implementation' }.group_by {|file| file.xml_id_path&.split('/')&.first || "co-ms-#{file.id}" }.map do |xml_id, files|
ProformaXML::ModelSolution.new(
id: "ms-#{file.id}",
files: model_solution_file(file)
id: xml_id || "ms-#{files.first.id}",
files: files.map {|file| model_solution_file(file) }
)
end
end

def model_solution_file(file)
[
task_file(file).tap do |ms_file|
ms_file.used_by_grader = false
ms_file.usage_by_lms = 'display'
end,
]
task_file(file).tap do |ms_file|
ms_file.used_by_grader = false
ms_file.usage_by_lms = 'display'
end
end

def tests
@exercise.files.filter(&:teacher_defined_assessment?).map do |file|
@exercise.files.filter(&:teacher_defined_assessment?).group_by {|file| xml_id_from_file(file).first }.map do |xml_id, files|
ProformaXML::Test.new(
id: file.id,
title: file.name,
files: test_file(file),
meta_data: test_meta_data(file)
id: xml_id || files.first.id,
title: files.first.name,
files: files.map {|file| test_file(file) },
meta_data: test_meta_data(files)
)
end
end

def test_meta_data(file)
def xml_id_from_file(file)
type = if file.teacher_defined_assessment?
'test'
elsif file.reference_implementation?
'ms'
else
'file'
end
xml_id_path_parts = file.xml_id_path&.split('/')
xml_id_path = []

xml_id_path << (xml_id_path_parts&.first || "co-#{type}-#{file.id}") unless type == 'file'
xml_id_path << (xml_id_path_parts&.last || file.id)

xml_id_path
end

def test_meta_data(files)
{
'@@order' => %w[test-meta-data],
'test-meta-data' => {
'@@order' => %w[CodeOcean:feedback-message CodeOcean:weight CodeOcean:hidden-feedback],
'@@order' => %w[CodeOcean:test-file],
'@xmlns' => {'CodeOcean' => 'codeocean.openhpi.de'},
'CodeOcean:feedback-message' => {
'@@order' => %w[$1],
'$1' => file.feedback_message,
},
'CodeOcean:weight' => {
'@@order' => %w[$1],
'$1' => file.weight,
},
'CodeOcean:hidden-feedback' => {
'@@order' => %w[$1],
'$1' => file.hidden_feedback,
},
'CodeOcean:test-file' => files.to_h do |file|
[
"CodeOcean:#{xml_id_from_file(file).last}", {
'@@order' => %w[CodeOcean:feedback-message CodeOcean:weight CodeOcean:hidden-feedback],
'@xmlns' => {'CodeOcean' => 'codeocean.openhpi.de'},
'@id' => xml_id_from_file(file).last,
'@name' => file.name,
'CodeOcean:feedback-message' => {
'@@order' => %w[$1],
'$1' => file.feedback_message,
},
'CodeOcean:weight' => {
'@@order' => %w[$1],
'$1' => file.weight,
},
'CodeOcean:hidden-feedback' => {
'@@order' => %w[$1],
'$1' => file.hidden_feedback,
},
}
]
end,
},
}
end

def test_file(file)
[
task_file(file).tap do |t_file|
t_file.used_by_grader = true
end,
]
task_file(file).tap do |t_file|
t_file.used_by_grader = true
end
end

def exercise_files
Expand Down Expand Up @@ -168,8 +191,11 @@ def task_files
end

def task_file(file)
file.update(xml_id_path: xml_id_from_file(file).join('/')) if file.xml_id_path.blank?

xml_id = xml_id_from_file(file).last
task_file = ProformaXML::TaskFile.new(
id: file.id,
id: xml_id,
filename: filename(file),
usage_by_lms: file.read_only ? 'display' : 'edit',
visible: file.hidden ? 'no' : 'yes'
Expand Down
42 changes: 27 additions & 15 deletions app/services/proforma_service/convert_task_to_exercise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,44 +60,56 @@ def string_to_bool(str)
end

def files
model_solution_files + test_files + task_files.values.tap {|array| array.each {|file| file.role ||= 'regular_file' } }
model_solution_files + test_files + task_files
end

def test_files
@task.tests.map do |test_object|
task_files.delete(test_object.files.first.id).tap do |file|
file.weight = extract_meta_data(test_object.meta_data&.dig('test-meta-data'), 'weight').presence || 1.0
file.feedback_message = extract_meta_data(test_object.meta_data&.dig('test-meta-data'), 'feedback-message').presence || 'Feedback'
file.hidden_feedback = extract_meta_data(test_object.meta_data&.dig('test-meta-data'), 'hidden-feedback').presence || false
file.role ||= 'teacher_defined_test'
@task.tests.flat_map do |test|
test.files.map do |task_file|
codeocean_file_from_task_file(task_file, test).tap do |file|
file.weight = extract_meta_data(test.meta_data&.dig('test-meta-data'), 'test-file', task_file.id, 'weight').presence || 1.0
file.feedback_message = extract_meta_data(test.meta_data&.dig('test-meta-data'), 'test-file', task_file.id, 'feedback-message').presence || 'Feedback'
file.hidden_feedback = extract_meta_data(test.meta_data&.dig('test-meta-data'), 'test-file', task_file.id, 'hidden-feedback').presence || false
file.role = 'teacher_defined_test' unless file.teacher_defined_assessment?
end
end
end
end

def model_solution_files
@task.model_solutions.map do |model_solution_object|
task_files.delete(model_solution_object.files.first.id).tap do |file|
file.role ||= 'reference_implementation'
@task.model_solutions.flat_map do |model_solution|
model_solution.files.map do |task_file|
codeocean_file_from_task_file(task_file, model_solution).tap do |file|
file.role ||= 'reference_implementation'
end
end
end
end

def task_files
@task_files ||= @task.all_files.reject {|file| file.id == 'ms-placeholder-file' }.to_h do |task_file|
[task_file.id, codeocean_file_from_task_file(task_file)]
@task.files.reject {|file| file.id == 'ms-placeholder-file' }.map do |task_file|
codeocean_file_from_task_file(task_file).tap do |file|
file.role ||= 'regular_file'
end
end
# @task_files ||= @task.all_files.reject {|file| file.id == 'ms-placeholder-file' }.to_h do |task_file|
# [task_file.id, codeocean_file_from_task_file(task_file)]
# end
end

def codeocean_file_from_task_file(file)
def codeocean_file_from_task_file(file, parent_object = nil)
extension = File.extname(file.filename)
codeocean_file = CodeOcean::File.new(

codeocean_file = CodeOcean::File.where(context: @exercise).where('xml_id_path LIKE ?', "%#{file.id}").first_or_initialize(context: @exercise)
codeocean_file.assign_attributes(
context: @exercise,
file_type: file_type(extension),
hidden: file.visible != 'yes', # hides 'delayed' and 'no'
name: File.basename(file.filename, '.*'),
read_only: file.usage_by_lms != 'edit',
role: extract_meta_data(@task.meta_data&.dig('meta-data'), 'files', "CO-#{file.id}", 'role'),
path: File.dirname(file.filename).in?(['.', '']) ? nil : File.dirname(file.filename)
path: File.dirname(file.filename).in?(['.', '']) ? nil : File.dirname(file.filename),
xml_id_path: (parent_object.nil? ? '' : "#{parent_object.id}/") + file.id.to_s
)
if file.binary
codeocean_file.native_file = FileIO.new(file.content.dup.force_encoding('UTF-8'), File.basename(file.filename))
Expand Down
8 changes: 1 addition & 7 deletions app/services/proforma_service/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ def execute
import_result = importer.perform
@task = import_result

exercise = base_exercise
exercise_files = exercise&.files&.to_a

exercise = ConvertTaskToExercise.call(task: @task, user: @user, exercise:)
exercise_files&.each(&:destroy) # feels suboptimal

exercise
ConvertTaskToExercise.call(task: @task, user: @user, exercise: base_exercise)
else
import_multi
end
Expand Down
7 changes: 7 additions & 0 deletions db/migrate/20240903204319_add_xml_path_to_files.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddXmlPathToFiles < ActiveRecord::Migration[7.1]
def change
add_column :files, :xml_id_path, :string, null: true, default: nil
end
end
29 changes: 15 additions & 14 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2023_12_08_194632) do
ActiveRecord::Schema[7.1].define(version: 2024_09_03_204319) do
# These are extensions that must be enabled in order to support this database
enable_extension "pg_trgm"
enable_extension "pgcrypto"
enable_extension "plpgsql"

create_table "anomaly_notifications", id: :serial, force: :cascade do |t|
t.integer "contributor_id", null: false
t.string "contributor_type", null: false
t.integer "contributor_id", null: false
t.integer "exercise_id", null: false
t.integer "exercise_collection_id", null: false
t.jsonb "reason"
Expand Down Expand Up @@ -46,10 +46,10 @@
t.string "api_key"
t.datetime "created_at"
t.datetime "updated_at"
t.string "user_type"
t.integer "user_id"
t.string "push_url"
t.string "check_uuid_url"
t.string "user_type"
t.index ["user_type", "user_id"], name: "index_codeharbor_links_on_user_type_and_user_id"
end

Expand Down Expand Up @@ -137,8 +137,8 @@
create_table "events", id: :serial, force: :cascade do |t|
t.string "category"
t.string "data"
t.integer "user_id"
t.string "user_type"
t.integer "user_id"
t.integer "exercise_id"
t.integer "file_id"
t.datetime "created_at", null: false
Expand Down Expand Up @@ -207,8 +207,8 @@
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "use_anomaly_detection", default: false
t.integer "user_id"
t.string "user_type"
t.integer "user_id"
t.index ["user_type", "user_id"], name: "index_exercise_collections_on_user_type_and_user_id"
end

Expand Down Expand Up @@ -298,8 +298,8 @@

create_table "files", id: :serial, force: :cascade do |t|
t.text "content"
t.integer "context_id"
t.string "context_type"
t.integer "context_id"
t.integer "file_id"
t.integer "file_type_id"
t.boolean "hidden"
Expand All @@ -315,6 +315,7 @@
t.string "path"
t.integer "file_template_id"
t.boolean "hidden_feedback", default: false, null: false
t.string "xml_id_path"
t.index ["context_id", "context_type"], name: "index_files_on_context_id_and_context_type"
end

Expand Down Expand Up @@ -492,8 +493,8 @@

create_table "searches", id: :serial, force: :cascade do |t|
t.integer "exercise_id", null: false
t.integer "user_id", null: false
t.string "user_type", null: false
t.integer "user_id", null: false
t.string "search"
t.datetime "created_at"
t.datetime "updated_at"
Expand Down Expand Up @@ -523,7 +524,7 @@
t.bigint "user_id"
t.integer "role", limit: 2, default: 0, null: false, comment: "Used as enum in Rails"
t.index ["study_group_id"], name: "index_study_group_memberships_on_study_group_id"
t.index ["user_type", "user_id"], name: "index_study_group_memberships_on_user"
t.index ["user_type", "user_id"], name: "index_study_group_memberships_on_user_type_and_user_id"
end

create_table "study_groups", force: :cascade do |t|
Expand Down Expand Up @@ -551,8 +552,8 @@
end

create_table "subscriptions", id: :serial, force: :cascade do |t|
t.integer "user_id"
t.string "user_type"
t.integer "user_id"
t.integer "request_for_comment_id"
t.string "subscription_type"
t.datetime "created_at", null: false
Expand Down Expand Up @@ -619,13 +620,13 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["file_type_id"], name: "index_tips_on_file_type_id"
t.index ["user_type", "user_id"], name: "index_tips_on_user"
t.index ["user_type", "user_id"], name: "index_tips_on_user_type_and_user_id"
end

create_table "user_exercise_feedbacks", id: :serial, force: :cascade do |t|
t.integer "exercise_id", null: false
t.integer "user_id", null: false
t.string "user_type", null: false
t.integer "user_id", null: false
t.integer "difficulty"
t.integer "working_time_seconds"
t.string "feedback_text"
Expand All @@ -638,8 +639,8 @@
end

create_table "user_exercise_interventions", id: :serial, force: :cascade do |t|
t.integer "contributor_id", null: false
t.string "contributor_type", null: false
t.integer "contributor_id", null: false
t.integer "exercise_id", null: false
t.integer "intervention_id", null: false
t.integer "accumulated_worktime_s"
Expand All @@ -652,16 +653,16 @@
end

create_table "user_proxy_exercise_exercises", id: :serial, force: :cascade do |t|
t.integer "user_id"
t.string "user_type"
t.integer "user_id"
t.integer "proxy_exercise_id"
t.integer "exercise_id"
t.datetime "created_at"
t.datetime "updated_at"
t.string "reason"
t.index ["exercise_id"], name: "index_user_proxy_exercise_exercises_on_exercise_id"
t.index ["proxy_exercise_id"], name: "index_user_proxy_exercise_exercises_on_proxy_exercise_id"
t.index ["user_type", "user_id"], name: "index_user_proxy_exercise_exercises_on_user"
t.index ["user_type", "user_id"], name: "index_user_proxy_exercise_exercises_on_user_type_and_user_id"
end

add_foreign_key "anomaly_notifications", "exercise_collections"
Expand Down
Loading

0 comments on commit a7affb4

Please sign in to comment.