Class: AtomicAssessmentsImport::ExamSoft::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/atomic_assessments_import/exam_soft/converter.rb

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Converter

Returns a new instance of Converter.



22
23
24
# File 'lib/atomic_assessments_import/exam_soft/converter.rb', line 22

def initialize(file)
  @file = file
end

Instance Method Details

#convertObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/atomic_assessments_import/exam_soft/converter.rb', line 26

def convert
  html = normalize_to_html
  doc = Nokogiri::HTML.fragment(html)
  normalize_html_structure(doc)

  # Chunk the document
  chunk_result = Chunker.chunk(doc)
  all_warnings = chunk_result[:warnings].map { |w| build_warning(w) }

  if chunk_result[:chunks].length == 1
    all_warnings << build_warning("Only 1 chunk detected — document may not be in a recognized format")
  end

  # Log header info if present
  header_text = nil
  unless chunk_result[:header_nodes].empty?
    header_text = chunk_result[:header_nodes].map { |n| n.text.strip }.join(" ")
    all_warnings << build_warning("Exam header detected: #{header_text}") unless header_text.empty?
  end
  activity_title = header_text&.strip&.presence || fallback_title

  items = []
  questions = []

  chunk_result[:chunks].each_with_index do |chunk_nodes, index|
    # Extract fields from this chunk
    extraction = Extractor.extract(chunk_nodes)
    extraction[:warnings].each do |w|
      all_warnings << build_warning("Question #{index + 1}: #{w}", index: index, question_type: extraction[:row]["question type"])
    end

    row = extraction[:row]
    status = extraction[:status]

    # Skip completely unparseable chunks
    if row["question text"].nil? && row["option a"].nil?
      all_warnings << build_warning("Question #{index + 1}: Skipped — no usable content found", index: index)
      next
    end

    next unless status == "published"

    begin
      item, question_widgets = convert_row(row, "published")
      items << item
      questions += question_widgets
    rescue StandardError => e
      title = row["title"] || "Question #{index + 1}"
      all_warnings << build_warning("#{title}: #{e.message}", index: index, question_type: row["question type"])
    end
  end

  {
    activities: items.any? ? [build_activity(activity_title, items)] : [],
    items: items,
    questions: questions,
    features: [],
    errors: all_warnings,
  }
end