====== Backup And Restore Modules ======
The backup and restore process for modules is actually pretty well documented:
* [[http://docs.moodle.org/dev/Backup_2.0_for_developers|Backup]]
* [[http://docs.moodle.org/dev/Restore_2.0_for_developers|Restore]]
There are a few potential pitfalls however.
===== Backup =====
If you want to backup the files associated with the module instance, you have to use the ''annotate_files'' function. For instance:
$rainmaker->annotate_files('mod_rainmaker', 'rainmaker_docs', 'id');
The 3rd argument of the function is documented as 'elementname'. This suggests you can name the resulting xml element whatever you want. But that is not actually the case: you should use the table column that contains the id which is also used in the mdl_files.itemid column for that particular module.
For instance, if your rainmaker module's instance id is stored in the mdl_rainmaker table, and that is used to identify the file, then mdl_rainmaker.id is the column name you need.
===== Restore =====
To restore files associated with your module's instance, use the ''after_execute'' function. There, make a call to ''add_related_files'':
protected function after_execute() {
$this->add_related_files($component = 'mod_rainmaker', $filearea = 'rainmaker_docs', $mappingitemname = 'rainmaker');
}
Here, the ''$mappingitemname'' refers to the xml element containing the ''id'' attribute that you're interested in.
===== Duplicate of an existing module =====
When you duplicate an existing module and replace everything to the new modulename, you have to make sure the definition is uppercase.
public static function define_decode_rules() {
$rules = array();
$rules[] = new restore_decode_rule('FREETRAININGVIEWBYID',
'/mod/freetraining/view.php?id=$1', 'course_module');
$rules[] = new restore_decode_rule('FREETRAININGVIEWBYQ',
'/mod/freetraining/view.php?q=$1', 'freetraining');
$rules[] = new restore_decode_rule('FREETRAININGINDEX',
'/mod/freetraining/index.php?id=$1', 'course');
return $rules;
}
Take the ''FREETRAININGVIEWBYID'' as example, eveything has to be in UPPERCASE, otherwise the backup/duplicate function will not work.