Class: Moodle2CC::Moodle2::Models::Label

Inherits:
Object
  • Object
show all
Defined in:
lib/moodle2cc/moodle2/models/label.rb

Constant Summary collapse

DEFAULT_PAGE_TITLE =
"Untitled Page"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/moodle2cc/moodle2/models/label.rb', line 3

def id
  @id
end

#introObject

Returns the value of attribute intro.



3
4
5
# File 'lib/moodle2cc/moodle2/models/label.rb', line 3

def intro
  @intro
end

#intro_formatObject

Returns the value of attribute intro_format.



3
4
5
# File 'lib/moodle2cc/moodle2/models/label.rb', line 3

def intro_format
  @intro_format
end

#module_idObject

Returns the value of attribute module_id.



3
4
5
# File 'lib/moodle2cc/moodle2/models/label.rb', line 3

def module_id
  @module_id
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/moodle2cc/moodle2/models/label.rb', line 3

def name
  @name
end

#visibleObject

Returns the value of attribute visible.



3
4
5
# File 'lib/moodle2cc/moodle2/models/label.rb', line 3

def visible
  @visible
end

Instance Method Details

#convert_to_header?Boolean

Returns:

  • (Boolean)


29
30
31
32
# File 'lib/moodle2cc/moodle2/models/label.rb', line 29

def convert_to_header?
  process_for_conversion!
  @convert_to_header
end

#convert_to_page?Boolean

Returns:

  • (Boolean)


24
25
26
27
# File 'lib/moodle2cc/moodle2/models/label.rb', line 24

def convert_to_page?
  process_for_conversion!
  @convert_to_page
end

#converted_titleObject



19
20
21
22
# File 'lib/moodle2cc/moodle2/models/label.rb', line 19

def converted_title
  process_for_conversion!
  @converted_title
end

#intro_htmlObject



11
12
13
# File 'lib/moodle2cc/moodle2/models/label.rb', line 11

def intro_html
  @intro_html ||= (Nokogiri::HTML(@intro.to_s) rescue Nokogiri::HTML(''))
end

#intro_textObject



15
16
17
# File 'lib/moodle2cc/moodle2/models/label.rb', line 15

def intro_text
  @intro_text ||= (intro_html.text.strip rescue "")
end

#name_textObject



7
8
9
# 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!Object



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
# 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