diff --git a/backup/moodle2/backup_course_task.class.php b/backup/moodle2/backup_course_task.class.php index 11f245f20aa..5ed54e91cfc 100644 --- a/backup/moodle2/backup_course_task.class.php +++ b/backup/moodle2/backup_course_task.class.php @@ -80,6 +80,9 @@ class backup_course_task extends backup_task { $this->add_step(new backup_enrolments_structure_step('course_enrolments', 'enrolments.xml')); } + // Annotate enrolment custom fields. + $this->add_step(new backup_enrolments_execution_step('annotate_enrol_custom_fields')); + // Annotate all the groups and groupings belonging to the course $this->add_step(new backup_annotate_course_groups_and_groupings('annotate_course_groups')); diff --git a/backup/moodle2/backup_enrol_plugin.class.php b/backup/moodle2/backup_enrol_plugin.class.php new file mode 100644 index 00000000000..a9ede547310 --- /dev/null +++ b/backup/moodle2/backup_enrol_plugin.class.php @@ -0,0 +1,39 @@ +. + +/** + * Defines backup_enrol_plugin class. + * + * @package core_backup + * @subpackage moodle2 + * @category backup + * @copyright 2014 University of Wisconsin + * @author Matt petro + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * Base class for enrol backup plugins. + * + * @package core_backup + * @copyright 2014 University of Wisconsin + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +abstract class backup_enrol_plugin extends backup_plugin { + // Use default parent behaviour. +} diff --git a/backup/moodle2/backup_plan_builder.class.php b/backup/moodle2/backup_plan_builder.class.php index b78fbad5063..c359096e44f 100644 --- a/backup/moodle2/backup_plan_builder.class.php +++ b/backup/moodle2/backup_plan_builder.class.php @@ -44,6 +44,7 @@ require_once($CFG->dirroot . '/backup/moodle2/backup_theme_plugin.class.php'); require_once($CFG->dirroot . '/backup/moodle2/backup_report_plugin.class.php'); require_once($CFG->dirroot . '/backup/moodle2/backup_coursereport_plugin.class.php'); require_once($CFG->dirroot . '/backup/moodle2/backup_plagiarism_plugin.class.php'); +require_once($CFG->dirroot . '/backup/moodle2/backup_enrol_plugin.class.php'); require_once($CFG->dirroot . '/backup/moodle2/backup_subplugin.class.php'); require_once($CFG->dirroot . '/backup/moodle2/backup_settingslib.php'); require_once($CFG->dirroot . '/backup/moodle2/backup_stepslib.php'); diff --git a/backup/moodle2/backup_stepslib.php b/backup/moodle2/backup_stepslib.php index de35cc37cfb..7830271a0cc 100644 --- a/backup/moodle2/backup_stepslib.php +++ b/backup/moodle2/backup_stepslib.php @@ -555,7 +555,8 @@ class backup_enrolments_structure_step extends backup_structure_step { $enrol->annotate_ids('role', 'roleid'); - //TODO: let plugins annotate custom fields too and add more children + // Add enrol plugin structure. + $this->add_plugin_structure('enrol', $enrol, false); return $enrolments; } @@ -1875,6 +1876,47 @@ class backup_activity_grade_items_to_ids extends backup_execution_step { } } + +/** + * This step allows enrol plugins to annotate custom fields. + * + * @package core_backup + * @copyright 2014 University of Wisconsin + * @author Matt Petro + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class backup_enrolments_execution_step extends backup_execution_step { + + /** + * Function that will contain all the code to be executed. + */ + protected function define_execution() { + global $DB; + + $plugins = enrol_get_plugins(true); + $enrols = $DB->get_records('enrol', array( + 'courseid' => $this->task->get_courseid())); + + // Allow each enrol plugin to add annotations. + foreach ($enrols as $enrol) { + if (isset($plugins[$enrol->enrol])) { + $plugins[$enrol->enrol]->backup_annotate_custom_fields($this, $enrol); + } + } + } + + /** + * Annotate a single name/id pair. + * This can be called from {@link enrol_plugin::backup_annotate_custom_fields()}. + * + * @param string $itemname + * @param int $itemid + */ + public function annotate_id($itemname, $itemid) { + backup_structure_dbops::insert_backup_ids_record($this->get_backupid(), $itemname, $itemid); + } +} + /** * This step will annotate all the groups and groupings belonging to the course */ diff --git a/backup/moodle2/restore_enrol_plugin.class.php b/backup/moodle2/restore_enrol_plugin.class.php new file mode 100644 index 00000000000..92d945e4bda --- /dev/null +++ b/backup/moodle2/restore_enrol_plugin.class.php @@ -0,0 +1,39 @@ +. + +/** + * Defines restore_enrol_plugin class. + * + * @package core_backup + * @subpackage moodle2 + * @category backup + * @copyright 2014 University of Wisconsin + * @author Matt petro + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * Base class for enrol backup plugins. + * + * @package core_backup + * @copyright 2014 University of Wisconsin + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +abstract class restore_enrol_plugin extends restore_plugin { + // Use default parent behaviour. +} diff --git a/backup/moodle2/restore_plan_builder.class.php b/backup/moodle2/restore_plan_builder.class.php index b7c65ef29b0..ae48ec93dfc 100644 --- a/backup/moodle2/restore_plan_builder.class.php +++ b/backup/moodle2/restore_plan_builder.class.php @@ -43,6 +43,7 @@ require_once($CFG->dirroot . '/backup/moodle2/restore_report_plugin.class.php'); require_once($CFG->dirroot . '/backup/moodle2/restore_coursereport_plugin.class.php'); require_once($CFG->dirroot . '/backup/moodle2/restore_plagiarism_plugin.class.php'); require_once($CFG->dirroot . '/backup/moodle2/restore_gradingform_plugin.class.php'); +require_once($CFG->dirroot . '/backup/moodle2/restore_enrol_plugin.class.php'); require_once($CFG->dirroot . '/backup/moodle2/backup_plugin.class.php'); require_once($CFG->dirroot . '/backup/moodle2/backup_qtype_plugin.class.php'); require_once($CFG->dirroot . '/backup/moodle2/backup_format_plugin.class.php'); @@ -52,6 +53,7 @@ require_once($CFG->dirroot . '/backup/moodle2/backup_report_plugin.class.php'); require_once($CFG->dirroot . '/backup/moodle2/backup_coursereport_plugin.class.php'); require_once($CFG->dirroot . '/backup/moodle2/backup_plagiarism_plugin.class.php'); require_once($CFG->dirroot . '/backup/moodle2/backup_gradingform_plugin.class.php'); +require_once($CFG->dirroot . '/backup/moodle2/backup_enrol_plugin.class.php'); require_once($CFG->dirroot . '/backup/moodle2/restore_subplugin.class.php'); require_once($CFG->dirroot . '/backup/moodle2/restore_settingslib.php'); require_once($CFG->dirroot . '/backup/moodle2/restore_stepslib.php'); diff --git a/backup/moodle2/restore_stepslib.php b/backup/moodle2/restore_stepslib.php index ee4fa9d2c83..9bdee8a4011 100644 --- a/backup/moodle2/restore_stepslib.php +++ b/backup/moodle2/restore_stepslib.php @@ -1884,12 +1884,12 @@ class restore_enrolments_structure_step extends restore_structure_step { protected function define_structure() { - $paths = array(); + $enrol = new restore_path_element('enrol', '/enrolments/enrols/enrol'); + $enrolment = new restore_path_element('enrolment', '/enrolments/enrols/enrol/user_enrolments/enrolment'); + // Attach local plugin stucture to enrol element. + $this->add_plugin_structure('enrol', $enrol); - $paths[] = new restore_path_element('enrol', '/enrolments/enrols/enrol'); - $paths[] = new restore_path_element('enrolment', '/enrolments/enrols/enrol/user_enrolments/enrolment'); - - return $paths; + return array($enrol, $enrolment); } /** diff --git a/lib/enrollib.php b/lib/enrollib.php index 2df68628087..75072e23389 100644 --- a/lib/enrollib.php +++ b/lib/enrollib.php @@ -2278,6 +2278,16 @@ abstract class enrol_plugin { force_current_language($oldforcelang); } + /** + * Backup execution step hook to annotate custom fields. + * + * @param backup_enrolments_execution_step $step + * @param stdClass $enrol + */ + public function backup_annotate_custom_fields(backup_enrolments_execution_step $step, stdClass $enrol) { + // Override as necessary to annotate custom fields in the enrol table. + } + /** * Automatic enrol sync executed during restore. * Useful for automatic sync by course->idnumber or course category.