Difference between revisions of "Restore 2.0 for developers"

Jump to: navigation, search
(Introduction)
(restore_choice_activity_task.class.php)
Line 134: Line 134:
 
         return $rules;
 
         return $rules;
  
 +
    }
 +
 +
    /**
 +
    * Define the restore log rules that will be applied
 +
    * by the {@link restore_logs_processor} when restoring
 +
    * choice logs. It must return one array
 +
    * of {@link restore_log_rule} objects
 +
    */
 +
    static public function define_restore_log_rules() {
 +
        $rules = array();
 +
 +
        $rules[] = new restore_log_rule('choice', 'add', 'view.php?id={course_module}', '{choice}');
 +
        $rules[] = new restore_log_rule('choice', 'update', 'view.php?id={course_module}', '{choice}');
 +
        $rules[] = new restore_log_rule('choice', 'view', 'view.php?id={course_module}', '{choice}');
 +
        $rules[] = new restore_log_rule('choice', 'choose', 'view.php?id={course_module}', '{choice}');
 +
        $rules[] = new restore_log_rule('choice', 'choose again', 'view.php?id={course_module}', '{choice}');
 +
        $rules[] = new restore_log_rule('choice', 'report', 'report.php?id={course_module}', '{choice}');
 +
 +
        return $rules;
 +
    }
 +
 +
    /**
 +
    * Define the restore log rules that will be applied
 +
    * by the {@link restore_logs_processor} when restoring
 +
    * course logs. It must return one array
 +
    * of {@link restore_log_rule} objects
 +
    *
 +
    * Note this rules are applied when restoring course logs
 +
    * by the restore final task, but are defined here at
 +
    * activity level. All them are rules not linked to any module instance (cmid = 0)
 +
    */
 +
    static public function define_restore_log_rules_for_course() {
 +
        $rules = array();
 +
 +
        // Fix old wrong uses (missing extension)
 +
        $rules[] = new restore_log_rule('choice', 'view all', 'index?id={course}', null,
 +
                                        null, null, 'index.php?id={course}');
 +
        $rules[] = new restore_log_rule('choice', 'view all', 'index.php?id={course}', null);
 +
 +
        return $rules;
 
     }
 
     }
  
 
}
 
}
 +
</code>
 +
 +
'''define_restore_log_rules''': it covers all the "mandatory" log actions, performing {course_module} and {choice} mappings properly. Note that names between curly brackets instruct the log processor to look in the mapping tables for item = name between brackets. Of course, other modules like forum, has a lot of rules, with more complex mappings and so on. Choice is pretty basic.
  
</code>
+
'''define_restore_log_rules_for_course''': choice module has the 1st rule because, while implementing restore, we discovered one bug causing URLs to be written in the logs table WITHOUT the .php in the URL, hence choice has that extra rule (that uses the 5-8 params, to REWRITE the URL to be inserted on restore). But that was one problem with choices only, afaik, so you probably would not need it in your own module.

Revision as of 03:12, 10 December 2010

Template:Development:Backup 2.0Moodle 2.0

Note: This page is a work-in-progress. Feedback and suggested improvements are welcome. Please join the discussion on moodle.org or use the page comments.


Introduction

This page tries to explain, from a development perspective, how to implement the restore feature for various Moodle 2.x plugins, mainly, modules and blocks.

In order to work with Restore 2.0 you must have read the Backup 2.0 documentation. Everything will be crystal clear once you have read it.

In this documentation you will learn about some points that you can not easily deduce from Backup 2.0 documentation concerning the Choice module.

Coding

the restore is composed by two main php files: restore_choice_stepslib.php and restore_choice_activity_task.class.php

restore_choice_stepslib.php

/**
 * Structure step to restore one choice activity
 */
class restore_choice_activity_structure_step extends restore_activity_structure_step {
 
    protected function define_structure() {
 
        $paths = array();
        $userinfo = $this->get_setting_value('userinfo');
 
        $paths[] = new restore_path_element('choice', '/activity/choice');
        $paths[] = new restore_path_element('choice_option', '/activity/choice/options/option');
        if ($userinfo) {
            $paths[] = new restore_path_element('choice_answer', '/activity/choice/answers/answer');
        }
 
        // Return the paths wrapped into standard activity structure
        return $this->prepare_activity_structure($paths);
    }
 
    protected function process_choice($data) {
        global $DB;
 
        $data = (object)$data;
        $oldid = $data->id;
        $data->course = $this->get_courseid();
 
        $data->timeopen = $this->apply_date_offset($data->timeopen);
        $data->timeclose = $this->apply_date_offset($data->timeclose);
        $data->timemodified = $this->apply_date_offset($data->timemodified);
 
        // insert the choice record
        $newitemid = $DB->insert_record('choice', $data);
        // immediately after inserting "activity" record, call this
        $this->apply_activity_instance($newitemid);
    }
 
    protected function process_choice_option($data) {
        global $DB;
 
        $data = (object)$data;
        $oldid = $data->id;
 
        $data->choiceid = $this->get_new_parentid('choice');
        $data->timemodified = $this->apply_date_offset($data->timemodified);
 
        $newitemid = $DB->insert_record('choice_options', $data);
        $this->set_mapping('choice_option', $oldid, $newitemid);
    }
 
    protected function process_choice_answer($data) {
        global $DB;
 
        $data = (object)$data;
        $oldid = $data->id;
 
        $data->choiceid = $this->get_new_parentid('choice');
        $data->optionid = $this->get_mappingid('choice_option', $oldid);
        $data->userid = $this->get_mappingid('user', $data->userid);
        $data->timemodified = $this->apply_date_offset($data->timemodified);
 
        $newitemid = $DB->insert_record('choice_answers', $data);
        // No need to save this mapping as far as nothing depend on it
        // (child paths, file areas nor links decoder)
    }
 
    protected function after_execute() {
        // Add choice related files, no need to match by itemname (just internally handled context)
        $this->add_related_files('mod_choice', 'intro', null);
    }
}

restore_choice_activity_task.class.php

/**
 * choice restore task that provides all the settings and steps to perform one
 * complete restore of the activity
 */
class restore_choice_activity_task extends restore_activity_task {
 
    /**
     * Define (add) particular settings this activity can have
     */
    protected function define_my_settings() {
        // No particular settings for this activity
    }
 
    /**
     * Define (add) particular steps this activity can have
     */
    protected function define_my_steps() {
        // Choice only has one structure step
        $this->add_step(new restore_choice_activity_structure_step('choice_structure', 'choice.xml'));
    }
 
    /**
     * Define the contents in the activity that must be
     * processed by the link decoder
     */
    static public function define_decode_contents() {
        $contents = array();
 
        $contents[] = new restore_decode_content('choice', array('intro'), 'choice');
 
        return $contents;
    }
 
    /**
     * Define the decoding rules for links belonging
     * to the activity to be executed by the link decoder
     */
    static public function define_decode_rules() {
        $rules = array();
 
        $rules[] = new restore_decode_rule('CHOICEVIEWBYID', '/mod/choice/view.php?id=$1', 'course_module');
        $rules[] = new restore_decode_rule('CHOICEINDEX', '/mod/choice/index.php?id=$1', 'course');
 
        return $rules;
 
    }
 
    /**
     * Define the restore log rules that will be applied
     * by the {@link restore_logs_processor} when restoring
     * choice logs. It must return one array
     * of {@link restore_log_rule} objects
     */
    static public function define_restore_log_rules() {
        $rules = array();
 
        $rules[] = new restore_log_rule('choice', 'add', 'view.php?id={course_module}', '{choice}');
        $rules[] = new restore_log_rule('choice', 'update', 'view.php?id={course_module}', '{choice}');
        $rules[] = new restore_log_rule('choice', 'view', 'view.php?id={course_module}', '{choice}');
        $rules[] = new restore_log_rule('choice', 'choose', 'view.php?id={course_module}', '{choice}');
        $rules[] = new restore_log_rule('choice', 'choose again', 'view.php?id={course_module}', '{choice}');
        $rules[] = new restore_log_rule('choice', 'report', 'report.php?id={course_module}', '{choice}');
 
        return $rules;
    }
 
    /**
     * Define the restore log rules that will be applied
     * by the {@link restore_logs_processor} when restoring
     * course logs. It must return one array
     * of {@link restore_log_rule} objects
     *
     * Note this rules are applied when restoring course logs
     * by the restore final task, but are defined here at
     * activity level. All them are rules not linked to any module instance (cmid = 0)
     */
    static public function define_restore_log_rules_for_course() {
        $rules = array();
 
        // Fix old wrong uses (missing extension)
        $rules[] = new restore_log_rule('choice', 'view all', 'index?id={course}', null,
                                        null, null, 'index.php?id={course}');
        $rules[] = new restore_log_rule('choice', 'view all', 'index.php?id={course}', null);
 
        return $rules;
    }
 
}

define_restore_log_rules: it covers all the "mandatory" log actions, performing {course_module} and {choice} mappings properly. Note that names between curly brackets instruct the log processor to look in the mapping tables for item = name between brackets. Of course, other modules like forum, has a lot of rules, with more complex mappings and so on. Choice is pretty basic.

define_restore_log_rules_for_course: choice module has the 1st rule because, while implementing restore, we discovered one bug causing URLs to be written in the logs table WITHOUT the .php in the URL, hence choice has that extra rule (that uses the 5-8 params, to REWRITE the URL to be inserted on restore). But that was one problem with choices only, afaik, so you probably would not need it in your own module.