From 37065c2e062dcb76813cf194a1ef20d415f247b1 Mon Sep 17 00:00:00 2001 From: David Mudrak Date: Tue, 8 Nov 2011 23:33:40 +0100 Subject: [PATCH] MDL-29793 Advanced grading forms attached to activity modules can be restored now --- .../moodle2/restore_activity_task.class.php | 3 + .../restore_gradingform_plugin.class.php | 42 +++++++ backup/moodle2/restore_plan_builder.class.php | 2 + backup/moodle2/restore_stepslib.php | 113 +++++++++++++++++ ...estore_gradingform_rubric_plugin.class.php | 114 ++++++++++++++++++ .../moodle2/restore_assignment_stepslib.php | 1 + 6 files changed, 275 insertions(+) create mode 100644 backup/moodle2/restore_gradingform_plugin.class.php create mode 100644 grade/grading/form/rubric/backup/moodle2/restore_gradingform_rubric_plugin.class.php diff --git a/backup/moodle2/restore_activity_task.class.php b/backup/moodle2/restore_activity_task.class.php index ae273754ffe..50f305dbdc2 100644 --- a/backup/moodle2/restore_activity_task.class.php +++ b/backup/moodle2/restore_activity_task.class.php @@ -150,6 +150,9 @@ abstract class restore_activity_task extends restore_task { // Grades (module-related, rest of gradebook is restored later if possible: cats, calculations...) $this->add_step(new restore_activity_grades_structure_step('activity_grades', 'grades.xml')); + // Advanced grading methods attached to the module + $this->add_step(new restore_activity_grading_structure_step('activity_grading', 'grading.xml')); + // Userscompletion (conditionally) if ($this->get_setting_value('userscompletion')) { $this->add_step(new restore_userscompletion_structure_step('activity_userscompletion', 'completion.xml')); diff --git a/backup/moodle2/restore_gradingform_plugin.class.php b/backup/moodle2/restore_gradingform_plugin.class.php new file mode 100644 index 00000000000..ff7f9985a34 --- /dev/null +++ b/backup/moodle2/restore_gradingform_plugin.class.php @@ -0,0 +1,42 @@ +. + +/** + * @package core + * @subpackage backup-moodle2 + * @copyright 2011 David Mudrak + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * Base class for all advanced grading form plugins + */ +abstract class restore_gradingform_plugin extends restore_plugin { + + /** + * Helper method returning the mapping identifierto use for + * grading form instance's itemid field + * + * @param array $areaname the name of the area the form is defined for + * @return string the mapping identifier + */ + public static function itemid_mapping($areaname) { + return 'grading_item_'.$areaname; + } +} diff --git a/backup/moodle2/restore_plan_builder.class.php b/backup/moodle2/restore_plan_builder.class.php index 5232b5b48b5..a58039692d3 100644 --- a/backup/moodle2/restore_plan_builder.class.php +++ b/backup/moodle2/restore_plan_builder.class.php @@ -37,12 +37,14 @@ require_once($CFG->dirroot . '/backup/moodle2/restore_format_plugin.class.php'); require_once($CFG->dirroot . '/backup/moodle2/restore_theme_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/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'); require_once($CFG->dirroot . '/backup/moodle2/backup_theme_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/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 71416404506..3306ae70d2b 100644 --- a/backup/moodle2/restore_stepslib.php +++ b/backup/moodle2/restore_stepslib.php @@ -1868,6 +1868,119 @@ class restore_activity_logs_structure_step extends restore_course_logs_structure } } + +/** + * Defines the restore step for advanced grading methods attached to the activity module + */ +class restore_activity_grading_structure_step extends restore_structure_step { + + /** + * Declares paths in the grading.xml file we are interested in + */ + protected function define_structure() { + + $paths = array(); + $userinfo = $this->get_setting_value('userinfo'); + + $paths[] = new restore_path_element('grading_area', '/areas/area'); + + $definition = new restore_path_element('grading_definition', '/areas/area/definitions/definition'); + $paths[] = $definition; + $this->add_plugin_structure('gradingform', $definition); + + if ($userinfo) { + $instance = new restore_path_element('grading_instance', + '/areas/area/definitions/definition/instances/instance'); + $paths[] = $instance; + $this->add_plugin_structure('gradingform', $instance); + } + + return $paths; + } + + /** + * Processes one grading area element + * + * @param array $data element data + */ + protected function process_grading_area($data) { + global $DB; + + $task = $this->get_task(); + $data = (object)$data; + $oldid = $data->id; + $data->component = 'mod_'.$task->get_modulename(); + $data->contextid = $task->get_contextid(); + + $newid = $DB->insert_record('grading_areas', $data); + $this->set_mapping('grading_area', $oldid, $newid); + } + + /** + * Processes one grading definition element + * + * @param array $data element data + */ + protected function process_grading_definition($data) { + global $DB; + + $task = $this->get_task(); + $data = (object)$data; + $oldid = $data->id; + $data->areaid = $this->get_new_parentid('grading_area'); + $data->copiedfromid = null; + $data->timecreated = time(); + $data->usercreated = $task->get_userid(); + $data->timemodified = $data->timecreated; + $data->usermodified = $data->usercreated; + + $newid = $DB->insert_record('grading_definitions', $data); + $this->set_mapping('grading_definition', $oldid, $newid, true); + } + + /** + * Processes one grading form instance element + * + * @param array $data element data + */ + protected function process_grading_instance($data) { + global $DB; + + $data = (object)$data; + + // new form definition id + $newformid = $this->get_new_parentid('grading_definition'); + + // get the name of the area we are restoring to + $sql = "SELECT ga.areaname + FROM {grading_definitions} gd + JOIN {grading_areas} ga ON gd.areaid = ga.id + WHERE gd.id = ?"; + $areaname = $DB->get_field_sql($sql, array($newformid), MUST_EXIST); + + // get the mapped itemid - the activity module is expected to define the mappings + // for each gradable area + $newitemid = $this->get_mappingid(restore_gradingform_plugin::itemid_mapping($areaname), $data->itemid); + + $oldid = $data->id; + $data->formid = $newformid; + $data->raterid = $this->get_mappingid('user', $data->raterid); + $data->itemid = $newitemid; + + $newid = $DB->insert_record('grading_instances', $data); + $this->set_mapping('grading_instance', $oldid, $newid); + } + + /** + * Final operations when the database records are inserted + */ + protected function after_execute() { + // Add files embedded into the definition description + $this->add_related_files('grading', 'description', 'grading_definition'); + } +} + + /** * This structure step restores the grade items associated with one activity * All the grade items are made child of the "course" grade item but the original diff --git a/grade/grading/form/rubric/backup/moodle2/restore_gradingform_rubric_plugin.class.php b/grade/grading/form/rubric/backup/moodle2/restore_gradingform_rubric_plugin.class.php new file mode 100644 index 00000000000..dab07f3c3c0 --- /dev/null +++ b/grade/grading/form/rubric/backup/moodle2/restore_gradingform_rubric_plugin.class.php @@ -0,0 +1,114 @@ +. + +/** + * Support for restore API + * + * @package gradingform + * @subpackage rubric + * @copyright 2011 David Mudrak + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * Restores the rubric specific data from grading.xml file + */ +class restore_gradingform_rubric_plugin extends restore_gradingform_plugin { + + /** + * Declares the rubric XML paths attached to the form definition element + * + * @return array of {@link restore_path_element} + */ + protected function define_definition_plugin_structure() { + + $paths = array(); + + $paths[] = new restore_path_element('gradingform_rubric_criterion', + $this->get_pathfor('/criteria/criterion')); + + $paths[] = new restore_path_element('gradingform_rubric_level', + $this->get_pathfor('/criteria/criterion/levels/level')); + + return $paths; + } + + /** + * Declares the rubric XML paths attached to the form instance element + * + * @return array of {@link restore_path_element} + */ + protected function define_instance_plugin_structure() { + + $paths = array(); + + $paths[] = new restore_path_element('gradinform_rubric_filling', + $this->get_pathfor('/fillings/filling')); + + return $paths; + } + + /** + * Processes criterion element data + * + * Sets the mapping 'gradingform_rubric_criterion' to be used later by + * {@link self::process_gradinform_rubric_filling()} + */ + public function process_gradingform_rubric_criterion($data) { + global $DB; + + $data = (object)$data; + $oldid = $data->id; + $data->formid = $this->get_new_parentid('grading_definition'); + + $newid = $DB->insert_record('gradingform_rubric_criteria', $data); + $this->set_mapping('gradingform_rubric_criterion', $oldid, $newid); + } + + /** + * Processes level element data + * + * Sets the mapping 'gradingform_rubric_level' to be used later by + * {@link self::process_gradinform_rubric_filling()} + */ + public function process_gradingform_rubric_level($data) { + global $DB; + + $data = (object)$data; + $oldid = $data->id; + $data->criterionid = $this->get_new_parentid('gradingform_rubric_criterion'); + + $newid = $DB->insert_record('gradingform_rubric_levels', $data); + $this->set_mapping('gradingform_rubric_level', $oldid, $newid); + } + + /** + * Processes filling element data + */ + public function process_gradinform_rubric_filling($data) { + global $DB; + + $data = (object)$data; + $data->forminstanceid = $this->get_new_parentid('grading_instance'); + $data->criterionid = $this->get_mappingid('gradingform_rubric_criterion', $data->criterionid); + $data->levelid = $this->get_mappingid('gradingform_rubric_level', $data->levelid); + + $DB->insert_record('gradingform_rubric_fillings', $data); + } +} diff --git a/mod/assignment/backup/moodle2/restore_assignment_stepslib.php b/mod/assignment/backup/moodle2/restore_assignment_stepslib.php index 3af067416b6..0d95d76d6e2 100644 --- a/mod/assignment/backup/moodle2/restore_assignment_stepslib.php +++ b/mod/assignment/backup/moodle2/restore_assignment_stepslib.php @@ -90,6 +90,7 @@ class restore_assignment_activity_structure_step extends restore_activity_struct $newitemid = $DB->insert_record('assignment_submissions', $data); $this->set_mapping('assignment_submission', $oldid, $newitemid, true); // Going to have files + $this->set_mapping(restore_gradingform_plugin::itemid_mapping('submission'), $oldid, $newitemid); } protected function after_execute() {