# frozen_string_literal: true # # Exporter in LTI Thin Common Cartridge format # module Lti class ThinCommonCartridge attr_reader :links def initialize(items) @items = Array.wrap items @xml = File.open(File.expand_path MANIFEST_FILEPATH) { |f| Nokogiri::XML(f) } @link_template = File.open(File.expand_path LTI_LINK_FILEPATH) { |f| Nokogiri::XML(f) } @links = [] create_nodes end def create_nodes # Build hierarchy of objects root_item = xml.at 'organizations/organization/item' items.each { |c| add_item c, root_item } end def manifest xml.to_xml end private LTI_LINK_RESOURCE_TYPE = 'imsbasiclti_xmlv1p0' MANIFEST_FILEPATH = ::Lcms::Engine::Engine.root.join 'lib', 'lti', 'xml', 'imsmanifest.xml' LTI_LINK_FILEPATH = ::Lcms::Engine::Engine.root.join 'lib', 'lti', 'xml', 'lti_link.xml' attr_reader :items, :link_template, :xml def add_item(item, parent) item[:identifier] = "ub#{SecureRandom.hex(17)}" item[:identifierref] = "#{item[:identifier]}_link" params = { identifier: item[:identifier], identifierref: (item[:identifierref] if item[:url].present?) } node = create_node 'item', params node.add_child %(