Difference between revisions of "Backup 1.9 conversion for developers"

Jump to: navigation, search
m (Getting know the structure choice.xml)
m (Added a template for the module handler library)
Line 67: Line 67:
 
Great! Now we have all the information needed to start with coding.
 
Great! Now we have all the information needed to start with coding.
  
 +
=== The handler library template ===
 +
 +
Create a new file mod/choice/backup/moodle1/lib.php with the following contents
 +
 +
<code php>
 +
<?php
 +
 +
// This file is part of Moodle - http://moodle.org/
 +
//
 +
// Moodle is free software: you can redistribute it and/or modify
 +
// it under the terms of the GNU General Public License as published by
 +
// the Free Software Foundation, either version 3 of the License, or
 +
// (at your option) any later version.
 +
//
 +
// Moodle is distributed in the hope that it will be useful,
 +
// but WITHOUT ANY WARRANTY; without even the implied warranty of
 +
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 +
// GNU General Public License for more details.
 +
//
 +
// You should have received a copy of the GNU General Public License
 +
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 +
 +
/**
 +
* Provides support for the conversion of moodle1 backup to the moodle2 format
 +
*
 +
* @package    mod
 +
* @subpackage choice
 +
* @copyright  2011 Your Name <your@email>
 +
* @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 +
*/
 +
 +
defined('MOODLE_INTERNAL') || die();
 +
 +
/**
 +
* Choice conversion handler
 +
*/
 +
class moodle1_mod_choice_handler extends moodle1_mod_handler {
 +
 +
    /**
 +
    * Declare the paths in moodle.xml we are able to convert
 +
    *
 +
    * The method returns list of {@link convert_path} instances.
 +
    * For each path returned, the corresponding conversion method must be
 +
    * defined.
 +
    *
 +
    * Note that the path /MOODLE_BACKUP/COURSE/MODULES/MOD/CHOICE does not
 +
    * actually exist in the file. The last element with the module name was
 +
    * appended by the moodle1_converter class.
 +
    *
 +
    * @return array of {@link convert_path} instances
 +
    */
 +
    public function get_paths() {
 +
        return array(
 +
            new convert_path('choice', '/MOODLE_BACKUP/COURSE/MODULES/MOD/CHOICE'),
 +
            new convert_path('choice_option', '/MOODLE_BACKUP/COURSE/MODULES/MOD/CHOICE/OPTIONS/OPTION'),
 +
        );
 +
    }
 +
 +
    /**
 +
    * This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/CHOICE
 +
    * data available
 +
    */
 +
    public function process_choice($data) {
 +
    }
 +
 +
    /**
 +
    * This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/CHOICE/OPTIONS/OPTION
 +
    * data available
 +
    */
 +
    public function process_choice_option($data) {
 +
    }
 +
}
 +
</code>
 +
 +
Let us look at this code template closer.
 
(to be continued)
 
(to be continued)

Revision as of 17:02, 12 May 2011

Introduction

This page follows up the tutorial at Backup 2.0 for developers and describes how to implement support of Moodle 1.9 backup conversion into the new Moodle 2.x format.

In short, since Moodle 2.1 there is a new type of core component available called backup converters. Converter is a tool that takes a directory containing some Moodle course data and converts it into another format. At the moment we are working on converter called moodle1 that supports 1.9 => 2.x conversion path. In the future, more converters can be written (eg supporting custom formats, Blackboard, IMS CC etc). Converters can be chained so if there is a converter that supports IMS => 1.9 conversion and another one converting 1.9 => 2.x, Moodle can automatically restore IMS backups. The core of the moodle1 converter is implemented in backup/converter/moodle1/.

The moodle1 converter is under a heavy development at the moment still. The first milestone is to be able to convert all activity modules without user data.

How to convert one module

For the purpose of this tutorial, the Choice module is used as an example as it allows to demonstrate the basic workflow of the conversion. Let us start with performing a backup in both 1.9 and 2.1. Create an empty course with a single simple Choice module instance inside in both 1.9 and 2.1. Choose backup mode without any user data, without roles, files etc and include just the instance of the module you created.

In case of Moodle 1.9, you will end up having once monolithic file moodle.xml. In Moodle 2.1, the module data will be stored in a file like activities/choice_x/choice.xml. The task of the converter you are going to write is to convert data contained in moodle.xml into choice.xml.

Getting the list of XML paths

Looking at moodle.xml in 1.9 backup, you can see that the module data are stored in XML nodes at /MOODLE_BACKUP/COURSE/MODULES/MOD and that you are interested only to those MODs having <MODTYPE>choice</MODTYPE>. To make your life easier, the core of the moodle1 converter injects one virtual node into the path so that to our module, it appears as if its data were in /MOODLE_BACKUP/COURSE/MODULES/MOD/CHOICE. That is as if all Choice data were wrapped by yet another tag in moodle.xml.

Now look into 1.9 Moodle code and locate the file mod/choice/backuplib.php. Reading its code you can see that the MOD element in moodle.xml (that will be presented as MOD/CHOICE element to our code) contains the following tags holding the corresponding fields from mdl_choice table:

   ID              $choice->id
   NAME            $choice->name
   TEXT            $choice->text
   FORMAT          $choice->format
   PUBLISH         $choice->publish
   SHOWRESULTS     $choice->showresults
   DISPLAY         $choice->display
   ALLOWUPDATE     $choice->allowupdate
   SHOWUNANSWERED  $choice->showunanswered
   LIMITANSWERS    $choice->limitanswers
   TIMEOPEN        $choice->timeopen
   TIMECLOSE       $choice->timeclose
   TIMEMODIFIED    $choice->timemodified

Below these, the choice options data are dumped into the OPTIONS section, with each option details being wrapped by the OPTION tag:

   ID              $cho_opt->id
   TEXT            $cho_opt->text
   MAXANSWERS      $cho_opt->maxanswers
   TIMEMODIFIED    $cho_opt->timemodified

The file moodle.xml will be parsed by a progressive parser. That basically means it will be read in a sequential order and each time some interesting path is reached, the data contained by that element are dispatched to a handler (on contrary to DOM like parsers where the whole file would be converted into a huge in-memory tree structure). To catch the choice data in moodle.xml we will have to handle /MOODLE_BACKUP/COURSE/MODULES/MOD/CHOICE and /MOODLE_BACKUP/COURSE/MODULES/MOD/CHOICE/OPTIONS/OPTION paths

Getting know how data change during 1.9 => 2.x upgrade

Now let us open mod/choice/db/upgrade.php in your Moodle 2.1 code. It contains all the upgrade logic that is happening during the upgrade of 1.9 site to 2.x. Reading the code, we realize that:

  • the database field text in the {choice} table is renamed to intro
  • the database field format in the {choice} table is renamed to introformat
  • a new field completionsubmit is added to the {choice} table with the default value 0

Getting know the structure of choice.xml

Finally look at mod/choice/backup/moodle2/ in your Moodle 2.1, particularly the file backup_choice_stepslib.php. Here you can see how the structure of the choice.xml is defined and what data it contains:

   $choice = new backup_nested_element('choice', array('id'), array(
       'name', 'intro', 'introformat', 'publish',
       'showresults', 'display', 'allowupdate', 'allowunanswered',
       'limitanswers', 'timeopen', 'timeclose', 'timemodified',
       'completionsubmit'));

The <choice> element has a child element <options> that in turn contains set of <option> elements:

   $option = new backup_nested_element('option', array('id'), array(
       'text', 'maxanswers', 'timemodified'));

Great! Now we have all the information needed to start with coding.

The handler library template

Create a new file mod/choice/backup/moodle1/lib.php with the following contents

<?php
 
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 
/**
 * Provides support for the conversion of moodle1 backup to the moodle2 format
 *
 * @package    mod
 * @subpackage choice
 * @copyright  2011 Your Name <your@email>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
 
defined('MOODLE_INTERNAL') || die();
 
/**
 * Choice conversion handler
 */
class moodle1_mod_choice_handler extends moodle1_mod_handler {
 
    /**
     * Declare the paths in moodle.xml we are able to convert
     *
     * The method returns list of {@link convert_path} instances.
     * For each path returned, the corresponding conversion method must be
     * defined.
     *
     * Note that the path /MOODLE_BACKUP/COURSE/MODULES/MOD/CHOICE does not
     * actually exist in the file. The last element with the module name was
     * appended by the moodle1_converter class.
     *
     * @return array of {@link convert_path} instances
     */
    public function get_paths() {
        return array(
            new convert_path('choice', '/MOODLE_BACKUP/COURSE/MODULES/MOD/CHOICE'),
            new convert_path('choice_option', '/MOODLE_BACKUP/COURSE/MODULES/MOD/CHOICE/OPTIONS/OPTION'),
        );
    }
 
    /**
     * This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/CHOICE
     * data available
     */
    public function process_choice($data) {
    }
 
    /**
     * This is executed every time we have one /MOODLE_BACKUP/COURSE/MODULES/MOD/CHOICE/OPTIONS/OPTION
     * data available
     */
    public function process_choice_option($data) {
    }
}

Let us look at this code template closer. (to be continued)