Skip to content

Commit

Permalink
First pass at parsing out a Wiris short answer question
Browse files Browse the repository at this point in the history
  • Loading branch information
seanrcollings committed Mar 12, 2024
1 parent 07796be commit b74e93f
Show file tree
Hide file tree
Showing 24 changed files with 191 additions and 103 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ test/version_tmp
tmp
vendor
out
moodle_test.mbz
moodle_test.mbz
.DS_Store
.byebug_history
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby 2.7.5
ruby 3.2.2
2 changes: 2 additions & 0 deletions lib/moodle2aa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class OpenStruct < ::OpenStruct

autoload :ResourceFactory, 'moodle2aa/resource_factory'

autoload :OutputLogger, 'moodle2aa/output_logger'

module CC
autoload :Assessment, 'moodle2aa/cc/assessment'
autoload :Assignment, 'moodle2aa/cc/assignment'
Expand Down
6 changes: 3 additions & 3 deletions lib/moodle2aa/canvas_cc/assessment_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def write_assessment_meta_xml(assessment)
end.to_xml

file_path = File.join(@work_dir, assessment.meta_file_path)
FileUtils.mkdir_p(File.dirname(file_path)) unless File.exists?(File.dirname(file_path))
FileUtils.mkdir_p(File.dirname(file_path)) unless File.exist?(File.dirname(file_path))
File.open(file_path, 'w') { |f| f.write(xml) }
end

Expand Down Expand Up @@ -75,8 +75,8 @@ def write_assessment_non_cc_qti_xml(assessment)
end.to_xml

file_path = File.join(@work_dir, assessment.qti_file_path)
FileUtils.mkdir_p(File.dirname(file_path)) unless File.exists?(File.dirname(file_path))
FileUtils.mkdir_p(File.dirname(file_path)) unless File.exist?(File.dirname(file_path))
File.open(file_path, 'w') { |f| f.write(xml) }
end
end
end
end
4 changes: 2 additions & 2 deletions lib/moodle2aa/canvas_cc/question_bank_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def write_bank(bank)
end.to_xml

file_path = File.join(@work_dir, bank_resource.href)
FileUtils.mkdir_p(File.dirname(file_path)) unless File.exists?(File.dirname(file_path))
FileUtils.mkdir_p(File.dirname(file_path)) unless File.exist?(File.dirname(file_path))
File.open(file_path, 'w') { |f| f.write(xml) }
end
end
end
end
5 changes: 5 additions & 0 deletions lib/moodle2aa/learnosity/converters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,10 @@ module Converters
require_relative 'converters/html_converter'
require_relative 'converters/file_converter'
require_relative 'converters/gapselect_converter'

module Wiris
require_relative 'converters/wiris/multichoice_converter'
require_relative 'converters/wiris/shortanswer_converter'
end
end
end
24 changes: 12 additions & 12 deletions lib/moodle2aa/learnosity/converters/expression_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize(moodle_question, moodle_course, html_converter)
@moodle_course = moodle_course
@html_converter = html_converter
@expressions = {}

# load all variables from question definition
collect_vars(moodle_question)
normalize_vars
Expand All @@ -17,7 +17,7 @@ def initialize(moodle_question, moodle_course, html_converter)

# update @vars with moodle_question variables
def collect_vars(moodle_question)
if moodle_question.type == 'calculatedquestiongroup' ||
if moodle_question.type == 'calculatedquestiongroup' ||
moodle_question.type == 'quizpage'
moodle_question.questions.each do |subquestion|
collect_vars(subquestion)
Expand Down Expand Up @@ -68,7 +68,7 @@ def collect_vars(moodle_question)
# remove empty variables
@vars = @vars.select { |set| set[:values].count != 0 }
end

def normalize_vars # make sure all vars have the same number of values
maxcount = (@vars.map { |set| set[:values].count }).max
mincount = (@vars.map { |set| set[:values].count }).min
Expand Down Expand Up @@ -99,7 +99,7 @@ def shuffle_vars # shuffle vars so different groups aren't synchronized by accid

groups.each do |group|
matching = @vars.select {|set| set[:group] == group}

seed = (rng.rand*1000000).to_i
matching.each do |set|
# deterministic shuffle
Expand Down Expand Up @@ -134,7 +134,7 @@ def convert_expression(expr, moodle_question)
def convert_answer(expr, format, moodle_question)
convert_formula expr, format, moodle_question, 'ans'
end

# convert a single variable, e.g. {A}
def convert_variable var, moodle_question

Expand Down Expand Up @@ -163,7 +163,7 @@ def convert_formula expr, format, moodle_question, type
as_expr = convert_formula_as_formula expr, format, moodle_question
as_var = convert_formula_as_variable expr, format, moodle_question, type
out = [as_expr, as_var]

if m = as_var.match(/^\{\{var:(.*)\}\}$/)
raw_var = m[1]
else
Expand Down Expand Up @@ -226,14 +226,14 @@ def calculate_data_values(expr, format, set, moodle_question)
evalobj.add_variable(var[:name], var[:values])
end
end

Range.new(0,maxcount-1).each do |variant|
evalobj.set_variant variant
set[:values][variant] = evalobj.evaluate expr, format
end
#pp set[:values]
end

# Convert an expression from Moodle to Learnosity syntax.
def formula_to_learnosity(expr, format)
# TODO this isn't complete
Expand Down Expand Up @@ -270,13 +270,13 @@ def has_nan?
def has_truncated_rows?
@truncated_rows
end


def generate_dynamic_content_data
# number of variants = maximum number of values over all variables
maxcount = (@vars.map { |set| set[:values].count }).max
return '' if maxcount == 0 || !maxcount

unique = []
@vars.each do |var|
unique << var unless unique.detect {|var2| var2[:output_name] == var[:output_name]}
Expand All @@ -296,7 +296,7 @@ def generate_dynamic_content_data
def dump_variables
return '' if @vars.count == 0
out = []
if has_shuffled_vars?
if has_shuffled_vars?
out << "SHUFFLED Dataset:"
end
out << (@vars.map {|v| v[:datasetid]?v[:output_name]:v[:output_name]+"="+v[:name]}).join(",").gsub(/[{}]/,'')
Expand All @@ -307,7 +307,7 @@ def dump_variables
end
out.join "\n"
end

def dump_csv
return '' if @vars.count == 0
CSV.generate do |csv|
Expand Down
Loading

0 comments on commit b74e93f

Please sign in to comment.