Class: Moodle2CC::Moodle2::Parsers::CourseParser

Inherits:
Object
  • Object
show all
Includes:
ParserHelper
Defined in:
lib/moodle2cc/moodle2/parsers/course_parser.rb

Constant Summary collapse

COURSE_XML_PATH =
'course/course.xml'
SCALES_XML_PATH =
'scales.xml'

Constants included from ParserHelper

ParserHelper::FILES_XML, ParserHelper::IMS_FILEBASE_TOKEN, ParserHelper::MODULE_XML, ParserHelper::MOODLE_FILEBASE_TOKEN, ParserHelper::SLASH_TOKEN, ParserHelper::XML_NULL_VALUE

Instance Method Summary collapse

Methods included from ParserHelper

#activity_directories, #parse_boolean, #parse_module, #parse_text

Constructor Details

#initialize(backup_folder) ⇒ CourseParser

Returns a new instance of CourseParser.



8
9
10
# File 'lib/moodle2cc/moodle2/parsers/course_parser.rb', line 8

def initialize(backup_folder)
  @backup_folder = backup_folder
end

Instance Method Details

#parseObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/moodle2cc/moodle2/parsers/course_parser.rb', line 12

def parse
  course = Moodle2CC::Moodle2::Models::Course.new
  File.open(File.join(@backup_folder, COURSE_XML_PATH)) do |f|
    course_doc = Nokogiri::XML(f)
    course.course_id = course_doc.at_xpath('/course/@id').value
    course.id_number = parse_text(course_doc, '/course/idnumber')
    course.fullname = parse_text(course_doc, '/course/fullname')
    course.shortname = parse_text(course_doc, '/course/shortname')
    course.summary = parse_text(course_doc, '/course/summary')
    course.show_grades = parse_boolean(course_doc, '/course/showgrades')
    if unix_start_date = parse_text(course_doc, '/course/startdate')
      course.startdate = Time.at(unix_start_date.to_i)
    end
  end

  File.open(File.join(@backup_folder, SCALES_XML_PATH)) do |f|
    scales_doc = Nokogiri::XML(f)
    scales_doc.search('scales_definition/scale').each do |node|
      course.grading_scales[node.attributes['id'].value.to_i] = parse_text(node, 'scale').split(',')
    end
  end

  course
end