class Moodle2CC::Moodle2::Models::Label

Constants

DEFAULT_PAGE_TITLE

Attributes

id[RW]
intro[RW]
intro_format[RW]
module_id[RW]
name[RW]
visible[RW]

Public Instance Methods

convert_to_header?() click to toggle source
# File lib/moodle2cc/moodle2/models/label.rb, line 29
def convert_to_header?
  process_for_conversion!
  @convert_to_header
end
convert_to_page?() click to toggle source
# File lib/moodle2cc/moodle2/models/label.rb, line 24
def convert_to_page?
  process_for_conversion!
  @convert_to_page
end
converted_title() click to toggle source
# File lib/moodle2cc/moodle2/models/label.rb, line 19
def converted_title
  process_for_conversion!
  @converted_title
end
intro_html() click to toggle source
# File lib/moodle2cc/moodle2/models/label.rb, line 11
def intro_html
  @intro_html ||= (Nokogiri::HTML(@intro.to_s) rescue Nokogiri::HTML(''))
end
intro_text() click to toggle source
# File lib/moodle2cc/moodle2/models/label.rb, line 15
def intro_text
  @intro_text ||= (intro_html.text.strip rescue "")
end
name_text() click to toggle source
# File lib/moodle2cc/moodle2/models/label.rb, line 7
def name_text
  @name_text ||= (Nokogiri::HTML(@name.to_s).text.strip rescue "")
end
process_for_conversion!() click to toggle source
# File lib/moodle2cc/moodle2/models/label.rb, line 35
def process_for_conversion!
  return unless @converted_title.nil? || @convert_to_page.nil? || @convert_to_header.nil?

  @converted_title = name_text

  if @converted_title == "Label" || @converted_title.end_with?("...") || @converted_title.length == 0
    if intro_text.length > Moodle2CC::Moodle2Converter::ConverterHelper::MAX_TITLE_LENGTH
      @truncate = true
      @converted_title = intro_text # we will truncate in the label_converter
      @convert_to_page = true
    else
      if intro_text.length > 0
        @converted_title = intro_text
      else
        @converted_title = DEFAULT_PAGE_TITLE
        @convert_to_header = false # if we're not going to convert it to a page, don't convert it at all
      end
    end
  end

  # if the intro has no text or the text is identical, then convert to page only if it has tags
  if (@converted_title == intro_text && !@truncate) || intro_text.length == 0
    @convert_to_page = intro_html.search('img[src]').length > 0 ||
        intro_html.search('a[href]').length > 0 ||
        intro_html.search('iframe[src]').length > 0
  elsif intro_text.length > 0
    @convert_to_page = true
  else
    @convert_to_page = false
  end

  # do the opposite if we haven't already explicitly decided not to convert to a header
  if @convert_to_header.nil?
    @convert_to_header = !@convert_to_page
  end
end