Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Restore 2.0 for developers

From MoodleDocs
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

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. Everything will be crystal clear once you have undergone the full reading of Backup 2.0.

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

Coding

Similarity with Backup 2.0

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);
   }

}

/**

* 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;
   }

}

Some specificities