Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

Defines Moodle 1.9 backup conversion handlers Handlers are classes responsible for the actual conversion work. Their logic is similar to the functionality provided by steps in plan based restore process.

Copyright: 2011 David Mudrak <david@moodle.com>
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 2173 lines (83 kb)
Included or required: 1 time
Referenced: 0 times
Includes or requires: 0 files

Defines 20 classes

moodle1_handlers_factory:: (2 methods):
  get_handlers()
  get_plugin_handlers()

moodle1_handler:: (3 methods):
  __construct()
  get_converter()
  log()

moodle1_xml_handler:: (5 methods):
  open_xml_writer()
  close_xml_writer()
  has_xml_writer()
  write_xml()
  make_sure_xml_exists()

moodle1_root_handler:: (3 methods):
  get_paths()
  on_root_element_start()
  on_root_element_end()

moodle1_files_handler:: (2 methods):
  process()
  migrate_course_files()

moodle1_info_handler:: (6 methods):
  get_paths()
  process_info()
  process_info_details_mod()
  process_info_details_mod_instance()
  on_info_details_mod_end()
  on_info_details_end()

moodle1_course_header_handler:: (4 methods):
  get_paths()
  process_course_header()
  process_course_header_category()
  on_course_header_end()

moodle1_course_outline_handler:: (6 methods):
  get_paths()
  process_course_section()
  process_course_module()
  on_course_section_end()
  on_course_sections_end()
  on_course_modules_end()

moodle1_roles_definition_handler:: (3 methods):
  get_paths()
  process_roles_role()
  on_roles_end()

moodle1_question_bank_handler:: (12 methods):
  get_file_manager()
  get_current_category_context()
  get_paths()
  on_question_categories_start()
  on_question_category_start()
  process_question_category()
  process_question_category_context()
  process_question()
  on_questions_end()
  on_question_category_end()
  on_question_categories_end()
  get_qtype_handler()

moodle1_scales_handler:: (3 methods):
  get_paths()
  on_scales_start()
  process_scale()

moodle1_outcomes_handler:: (4 methods):
  get_paths()
  on_gradebook_grade_outcomes_start()
  process_gradebook_grade_outcome()
  on_gradebook_grade_outcomes_end()

moodle1_gradebook_handler:: (13 methods):
  get_paths()
  on_gradebook_start()
  process_gradebook_grade_letter()
  process_gradebook_grade_category()
  process_gradebook_grade_item()
  process_mod_grade_item()
  process_nonmod_grade_item()
  on_gradebook_grade_item_grades_start()
  on_gradebook_end()
  write_grade_categories()
  calculate_category_path()
  write_grade_items()
  write_grade_letters()

moodle1_plugin_handler:: (2 methods):
  __construct()
  get_component_name()

moodle1_qtype_handler:: (14 methods):
  get_question_subpaths()
  process_question()
  write_answers()
  migrate_files()
  write_numerical_units()
  write_numerical_options()
  get_default_numerical_options()
  write_dataset_definitions()
  __construct()
  get_paths()
  open_xml_writer()
  close_xml_writer()
  use_xml_writer()
  convert_answer()

moodle1_mod_handler:: (2 methods):
  get_modname()
  get_cminfo()

moodle1_resource_successor_handler:: (3 methods):
  get_paths()
  process_legacy_resource()
  on_legacy_resource_end()

moodle1_block_handler:: (7 methods):
  get_paths()
  process_block()
  convert_common_block_data()
  convert_configdata()
  write_block_xml()
  write_inforef_xml()
  write_roles_xml()

moodle1_block_generic_handler:: (0 methods):

moodle1_submod_handler:: (2 methods):
  __construct()
  get_paths()


Class: moodle1_handlers_factory  - X-Ref

Handlers factory class

get_handlers(moodle1_converter $converter)   X-Ref

param: moodle1_converter the converter requesting the converters
return: list of all available conversion handlers

get_plugin_handlers($type, moodle1_converter $converter)   X-Ref
Runs through all plugins of a specific type and instantiates their handlers

param: string $type the plugin type
param: moodle1_converter $converter the converter requesting the handler
return: array of {@link moodle1_handler} instances

Class: moodle1_handler  - X-Ref

Base backup conversion handler

__construct(moodle1_converter $converter)   X-Ref

param: moodle1_converter $converter the converter that requires us

get_converter()   X-Ref

return: moodle1_converter the converter that required this handler

log($message, $level, $a = null, $depth = null, $display = false)   X-Ref
Log a message using the converter's logging mechanism

param: string $message message text
param: int $level message level {@example backup::LOG_WARNING}
param: null|mixed $a additional information
param: null|int $depth the message depth
param: bool $display whether the message should be sent to the output, too

Class: moodle1_xml_handler  - X-Ref

Base backup conversion handler that generates an XML file

open_xml_writer($filename)   X-Ref
Opens the XML writer - after calling, one is free to use $xmlwriter

param: string $filename XML file name to write into
return: void

close_xml_writer()   X-Ref
Close the XML writer

At the moment, the caller must close all tags before calling

return: void

has_xml_writer()   X-Ref
Checks if the XML writer has been opened by {@link self::open_xml_writer()}

return: bool

write_xml($element, array $data, array $attribs = array()   X-Ref
Writes the given XML tree data into the currently opened file

param: string $element the name of the root element of the tree
param: array $data the associative array of data to write
param: array $attribs list of additional fields written as attributes instead of nested elements
param: string $parent used internally during the recursion, do not set yourself

make_sure_xml_exists($filename, $rootelement = false, $content = array()   X-Ref
Makes sure that a new XML file exists, or creates it itself

This is here so we can check that all XML files that the restore process relies on have
been created by an executed handler. If the file is not found, this method can create it
using the given $rootelement as an empty root container in the file.

param: string $filename relative file name like 'course/course.xml'
param: string|bool $rootelement root element to use, false to not create the file
param: array $content content of the root element
return: bool true is the file existed, false if it did not

Class: moodle1_root_handler  - X-Ref

Process the root element of the backup file

get_paths()   X-Ref
No description

on_root_element_start()   X-Ref
Converts course_files and site_files


on_root_element_end()   X-Ref
This is executed at the end of the moodle.xml parsing


Class: moodle1_files_handler  - X-Ref

The class responsible for course and site files migration

process()   X-Ref
Migrates course_files and site_files in the converter workdir


migrate_course_files()   X-Ref
Migrates course_files in the converter workdir


Class: moodle1_info_handler  - X-Ref

Handles the conversion of /MOODLE_BACKUP/INFO paths

We do not produce any XML file here, just storing the data in the temp
table so thay can be used by a later handler.
get_paths()   X-Ref
No description

process_info($data)   X-Ref
Stashes the backup info for later processing by {@link moodle1_root_handler}


process_info_details_mod($data)   X-Ref
Initializes the in-memory cache for the current mod


process_info_details_mod_instance($data)   X-Ref
Appends the current instance data to the temporary in-memory cache


on_info_details_mod_end($data)   X-Ref
Stashes the backup info for later processing by {@link moodle1_root_handler}


on_info_details_end()   X-Ref
Stashes the list of activity module types for later processing by {@link moodle1_root_handler}


Class: moodle1_course_header_handler  - X-Ref

Handles the conversion of /MOODLE_BACKUP/COURSE/HEADER paths

get_paths()   X-Ref
No description

process_course_header($data, $raw)   X-Ref
Because there is the CATEGORY branch in the middle of the COURSE/HEADER
branch, this is dispatched twice. We use $this->coursecooked to merge
the result. Once the parser is fixed, it can be refactored.


process_course_header_category($data)   X-Ref
No description

on_course_header_end()   X-Ref
No description

Class: moodle1_course_outline_handler  - X-Ref

Handles the conversion of course sections and course modules

get_paths()   X-Ref
This handler is interested in course sections and course modules within them


process_course_section($data)   X-Ref
No description

process_course_module($data, $raw)   X-Ref
Populates the section sequence field (order of course modules) and stashes the
course module info so that is can be dumped to activities/xxxx_x/module.xml later


on_course_section_end()   X-Ref
Writes sections/section_xxx/section.xml file and stashes it, too


on_course_sections_end()   X-Ref
Stashes the course contents


on_course_modules_end()   X-Ref
Writes the information collected by mod handlers


Class: moodle1_roles_definition_handler  - X-Ref

Handles the conversion of the defined roles

get_paths()   X-Ref
Where the roles are defined in the source moodle.xml


process_roles_role($data)   X-Ref
If there are any roles defined in moodle.xml, convert them to roles.xml


on_roles_end()   X-Ref
Finishes writing roles.xml


Class: moodle1_question_bank_handler  - X-Ref

Handles the conversion of the question bank included in the moodle.xml file

get_file_manager()   X-Ref
Return the file manager instance used.

return: moodle1_file_manager

get_current_category_context()   X-Ref
Returns the information about the question category context being currently parsed

return: array with keys contextid, contextlevel and contextinstanceid

get_paths()   X-Ref
Registers path that are not qtype-specific


on_question_categories_start()   X-Ref
Starts writing questions.xml and prepares the file manager instance


on_question_category_start()   X-Ref
Initializes the current category cache


process_question_category($data, $raw)   X-Ref
Populates the current question category data

Bacuse of the known subpath-in-the-middle problem (CONTEXT in this case), this is actually
called twice for both halves of the data. We merge them here into the currentcategory array.

process_question_category_context($data)   X-Ref
Inject the context related information into the current category


process_question(array $data, array $raw)   X-Ref
Writes the common <question> data and re-dispateches the whole grouped
<QUESTION> data to the qtype for appending its qtype specific data processing

param: array $data
param: array $raw
return: array

on_questions_end()   X-Ref
Closes the questions wrapper


on_question_category_end()   X-Ref
Closes the question_category and annotates the category id
so that it can be dumped into course/inforef.xml


on_question_categories_end()   X-Ref
Stops writing questions.xml


get_qtype_handler($qtype)   X-Ref
Provides access to the qtype handlers

Returns either list of all qtype handler instances (if passed '*') or a particular handler
for the given qtype or false if the qtype is not supported.

param: string $qtype the name of the question type or '*' for returning all
return: array|moodle1_qtype_handler|bool

Class: moodle1_scales_handler  - X-Ref

Handles the conversion of the scales included in the moodle.xml file

get_paths()   X-Ref
Registers paths


on_scales_start()   X-Ref
Prepare the file manager for the files embedded in the scale description field


process_scale(array $data, array $raw)   X-Ref
This is executed every time we have one <SCALE> data available

param: array $data
param: array $raw
return: array

Class: moodle1_outcomes_handler  - X-Ref

Handles the conversion of the outcomes

get_paths()   X-Ref
Registers paths


on_gradebook_grade_outcomes_start()   X-Ref
Prepares the file manager and starts writing outcomes.xml


process_gradebook_grade_outcome(array $data, array $raw)   X-Ref
Processes GRADE_OUTCOME tags progressively


on_gradebook_grade_outcomes_end()   X-Ref
Closes outcomes.xml


Class: moodle1_gradebook_handler  - X-Ref

Handles the conversion of the gradebook structures in the moodle.xml file

get_paths()   X-Ref
Registers paths


on_gradebook_start()   X-Ref
Initializes the in-memory structures

This should not be needed actually as the moodle.xml contains just one GRADEBOOK
element. But who knows - maybe someone will want to write a mass conversion
tool in the future (not me definitely ;-)

process_gradebook_grade_letter(array $data, array $raw)   X-Ref
Processes one GRADE_LETTER data

In Moodle 1.9, all grade_letters are from course context only. Therefore
we put them here.

process_gradebook_grade_category(array $data, array $raw)   X-Ref
Processes one GRADE_CATEGORY data


process_gradebook_grade_item(array $data, array $raw)   X-Ref
Processes one GRADE_ITEM data


process_mod_grade_item(array $data, array $raw)   X-Ref
Processes one GRADE_ITEM of the type 'mod'


process_nonmod_grade_item(array $data, array $raw)   X-Ref
Processes one GRADE_ITEM of te type 'manual' or 'course' or 'category'


on_gradebook_grade_item_grades_start()   X-Ref


on_gradebook_end()   X-Ref
Writes the collected information into gradebook.xml


write_grade_categories()   X-Ref
Writes grade_categories


calculate_category_path($categoryid)   X-Ref
Calculates the path to the grade_category

Moodle 1.9 backup does not store the grade_category's depth and path. This method is used
to repopulate this information using the $this->categoryparent values.

param: int $categoryid
return: array of ids including the categoryid

write_grade_items()   X-Ref
Writes grade_items


write_grade_letters()   X-Ref
Writes grade_letters


Class: moodle1_plugin_handler  - X-Ref

Shared base class for activity modules, blocks and qtype handlers

__construct(moodle1_converter $converter, $plugintype, $pluginname)   X-Ref

param: moodle1_converter $converter the converter that requires us
param: string $plugintype
param: string $pluginname

get_component_name()   X-Ref
Returns the normalized name of the plugin, eg mod_workshop

return: string

Class: moodle1_qtype_handler  - X-Ref

Base class for all question type handlers

get_question_subpaths()   X-Ref
Returns the list of paths within one <QUESTION> that this qtype needs to have included
in the grouped question structure

return: array of strings

process_question(array $data, array $raw)   X-Ref
Gives the qtype handler a chance to write converted data into questions.xml

param: array $data grouped question data
param: array $raw grouped raw QUESTION data

write_answers(array $answers, $qtype)   X-Ref
Converts the answers and writes them into the questions.xml

The structure "answers" is used by several qtypes. It contains data from {question_answers} table.

param: array $answers as parsed by the grouped parser in moodle.xml
param: string $qtype containing the answers

migrate_files($text, $component, $filearea, $itemid)   X-Ref
Migrate files belonging to one qtype plugin text field.

param: array $text the html fragment containing references to files
param: string $component the component for restored files
param: string $filearea the file area for restored files
param: int $itemid the itemid for restored files
return: string the text for this field, after files references have been processed

write_numerical_units(array $numericalunits)   X-Ref
Writes the grouped numerical_units structure

param: array $numericalunits

write_numerical_options(array $numericaloption)   X-Ref
Writes the numerical_options structure

param: array $numericaloption

get_default_numerical_options($oldquestiontextformat, $units)   X-Ref
Returns default numerical_option structure

This structure is not present in moodle.xml, we create a new artificial one here.

param: int $oldquestiontextformat
return: array

write_dataset_definitions(array $datasetdefinitions)   X-Ref
Writes the dataset_definitions structure

param: array $datasetdefinitions array of dataset_definition structures

__construct(moodle1_question_bank_handler $qbankhandler, $qtype)   X-Ref
No description

get_paths()   X-Ref


open_xml_writer($filename)   X-Ref
Question type handlers cannot open the xml_writer


close_xml_writer()   X-Ref
Question type handlers cannot close the xml_writer


use_xml_writer(xml_writer $xmlwriter)   X-Ref
Provides a xml_writer instance to this qtype converter

param: xml_writer $xmlwriter

convert_answer(array $old, $qtype)   X-Ref
Converts <ANSWER> structure into the new <answer> one

See question_backup_answers() in 1.9 and add_question_question_answers() in 2.0

param: array $old the parsed answer array in moodle.xml
param: string $qtype the question type the answer is part of
return: array

Class: moodle1_mod_handler  - X-Ref

Base class for activity module handlers

get_modname()   X-Ref
Returns the name of the module, eg. 'forum'

return: string

get_cminfo($instance, $modname = null)   X-Ref
Returns course module information for the given instance id

The information for this instance id has been stashed by
{@link moodle1_course_outline_handler::process_course_module()}

param: int $instance the module instance id
param: string $modname the module type, defaults to $this->pluginname
return: int

Class: moodle1_resource_successor_handler  - X-Ref

Base class for all modules that are successors of the 1.9 resource module

get_paths()   X-Ref
Resource successors do not attach to paths themselves, they are called explicitely
by moodle1_mod_resource_handler

return: array

process_legacy_resource(array $data, array $raw = null)   X-Ref
Converts /MOODLE_BACKUP/COURSE/MODULES/MOD/RESOURCE data

Called by {@link moodle1_mod_resource_handler::process_resource()}

param: array $data pre-cooked legacy resource data
param: array $raw raw legacy resource data

on_legacy_resource_end(array $data)   X-Ref
Called when the parses reaches the end </MOD> resource tag

param: array $data the data returned by {@link self::process_resource} or just pre-cooked

Class: moodle1_block_handler  - X-Ref

Base class for block handlers

get_paths()   X-Ref
No description

process_block(array $data)   X-Ref
No description

convert_common_block_data(array $olddata)   X-Ref
No description

convert_configdata(array $olddata)   X-Ref
No description

write_block_xml($newdata, $data)   X-Ref
No description

write_inforef_xml($newdata, $data)   X-Ref
No description

write_roles_xml($newdata, $data)   X-Ref
No description

Class: moodle1_block_generic_handler  - X-Ref

Base class for block generic handler

Class: moodle1_submod_handler  - X-Ref

Base class for the activity modules' subplugins

__construct(moodle1_mod_handler $parenthandler, $subplugintype, $subpluginname)   X-Ref

param: moodle1_mod_handler $parenthandler the handler of a module we are subplugin of
param: string $subplugintype the type of the subplugin
param: string $subpluginname the name of the subplugin

get_paths()   X-Ref
Activity module subplugins can't declare any paths to handle

The paths must be registered by the parent module and then re-dispatched to the
relevant subplugins for eventual processing.

return: array empty array