Sha256: c01a717d4dffa7394c49fc2a9baa396561249a84649cdb174000457ad0520d12

Contents?: true

Size: 1.53 KB

Versions: 9

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

require 'zip'

module Lcms
  module Engine
    class LtiExporter
      class << self
        include Rails.application.routes.url_helpers

        def perform(resource)
          cartridge = create_cartridge resource

          files = [{ name: 'imsmanifest.xml', data: cartridge.manifest }]
          files.concat cartridge.links
          create_zip_stream(files).read
        end

        private

        def build_items(resource)
          resource.children.map do |c|
            {
              children: build_items(c),
              title: c.title,
              url: (lti_document_url(c.document.id, protocol: :https) if c.lesson? && c.document.present?)
            }
          end
        end

        def create_cartridge(resource)
          # NOTE: For now work only with Modules
          # Subject - Grade - Module
          parent = {
            children: [{
              children: [{
                children: build_items(resource),
                identifier: SecureRandom.hex(17),
                title: resource.title
              }],
              title: resource.metadata['grade']
            }],
            title: resource.metadata['subject']
          }

          Lti::ThinCommonCartridge.new parent
        end

        def create_zip_stream(files)
          zip = Zip::OutputStream.write_buffer do |z|
            files.each do |f|
              z.put_next_entry f[:name]
              z.write f[:data]
            end
          end
          zip.rewind
          zip
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
lcms-engine-0.5.5 app/services/lcms/engine/lti_exporter.rb
lcms-engine-0.5.4 app/services/lcms/engine/lti_exporter.rb
lcms-engine-0.5.3 app/services/lcms/engine/lti_exporter.rb
lcms-engine-0.5.2 app/services/lcms/engine/lti_exporter.rb
lcms-engine-0.5.1 app/services/lcms/engine/lti_exporter.rb
lcms-engine-0.5.0 app/services/lcms/engine/lti_exporter.rb
lcms-engine-0.4.2 app/services/lcms/engine/lti_exporter.rb
lcms-engine-0.4.1 app/services/lcms/engine/lti_exporter.rb
lcms-engine-0.4.0 app/services/lcms/engine/lti_exporter.rb