From 8ab1529d8a794f708dff76fe8735a98edb64f1ec Mon Sep 17 00:00:00 2001 From: Sam Hemelryk Date: Fri, 13 Jul 2012 15:19:32 +1200 Subject: [PATCH] MDL-34220 condition: Fixed up backup and restore of custom profile field conditions --- backup/moodle2/backup_stepslib.php | 16 +++++++++--- backup/moodle2/restore_stepslib.php | 40 ++++++++++++++++++++--------- 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/backup/moodle2/backup_stepslib.php b/backup/moodle2/backup_stepslib.php index a9ef11119a9..a73cafa587e 100644 --- a/backup/moodle2/backup_stepslib.php +++ b/backup/moodle2/backup_stepslib.php @@ -323,7 +323,7 @@ class backup_module_structure_step extends backup_structure_step { $availability = new backup_nested_element('availability', array('id'), array( 'sourcecmid', 'requiredcompletion', 'gradeitemid', 'grademin', 'grademax')); $availabilityfield = new backup_nested_element('availability_field', array('id'), array( - 'userfield', 'customfieldid', 'operator', 'value')); + 'userfield', 'customfield', 'customfieldtype', 'operator', 'value')); // attach format plugin structure to $module element, only one allowed $this->add_plugin_structure('format', $module, false); @@ -346,7 +346,11 @@ class backup_module_structure_step extends backup_structure_step { WHERE cm.id = ?', array(backup::VAR_MODID)); $availability->set_source_table('course_modules_availability', array('coursemoduleid' => backup::VAR_MODID)); - $availabilityfield->set_source_table('course_modules_avail_fields', array('coursemoduleid' => backup::VAR_MODID)); + $availabilityfield->set_source_sql(' + SELECT cmaf.*, uif.shortname AS customfield, uif.datatype AS customfieldtype + FROM {course_modules_avail_fields} cmaf + LEFT JOIN {user_info_field} uif ON uif.id = cmaf.customfieldid + WHERE cmaf.coursemoduleid = ?', array(backup::VAR_MODID)); // Define annotations $module->annotate_ids('grouping', 'groupingid'); @@ -377,14 +381,18 @@ class backup_section_structure_step extends backup_structure_step { $avail = new backup_nested_element('availability', array('id'), array( 'sourcecmid', 'requiredcompletion', 'gradeitemid', 'grademin', 'grademax')); $availfield = new backup_nested_element('availability_field', array('id'), array( - 'userfield', 'customfieldid', 'operator', 'value')); + 'userfield', 'operator', 'value', 'customfield', 'customfieldtype')); $section->add_child($avail); $section->add_child($availfield); // Define sources $section->set_source_table('course_sections', array('id' => backup::VAR_SECTIONID)); $avail->set_source_table('course_sections_availability', array('coursesectionid' => backup::VAR_SECTIONID)); - $availfield->set_source_table('course_sections_avail_fields', array('coursesectionid' => backup::VAR_SECTIONID)); + $availfield->set_source_sql(' + SELECT csaf.*, uif.shortname AS customfield, uif.datatype AS customfieldtype + FROM {course_sections_avail_fields} csaf + LEFT JOIN {user_info_field} uif ON uif.id = csaf.customfieldid + WHERE csaf.coursesectionid = ?', array(backup::VAR_SECTIONID)); // Aliases $section->set_source_alias('section', 'number'); diff --git a/backup/moodle2/restore_stepslib.php b/backup/moodle2/restore_stepslib.php index 6bed961172b..1bd5a48b973 100644 --- a/backup/moodle2/restore_stepslib.php +++ b/backup/moodle2/restore_stepslib.php @@ -1139,18 +1139,26 @@ class restore_section_structure_step extends restore_structure_step { $data = (object)$data; // Mark it is as passed by default $passed = true; - // Ok, if it is a profile field we need to check it exists - if (!is_null($data->customfieldid)) { - if (!$DB->record_exists('user_info_field', array('id' => $data->customfieldid))) { - $passed = false; - } + $customfieldid = null; + + // If a customfield has been used in order to pass we must be able to match an existing + // customfield by name (data->customfield) and type (data->customfieldtype) + if (is_null($data->customfield) xor is_null($data->customfieldtype)) { + // xor is sort of uncommon. If either customfield is null or customfieldtype is null BUT not both. + // If one is null but the other isn't something clearly went wrong and we'll skip this condition. + $passed = false; + } else if (!is_null($data->customfield)) { + $params = array('shortname' => $data->customfield, 'datatype' => $data->customfieldtype); + $customfieldid = $DB->get_field('user_info_field', 'id', $params); + $passed = ($customfieldid !== false); } + if ($passed) { // Create the object to insert into the database $availfield = new stdClass(); $availfield->coursesectionid = $this->task->get_sectionid(); $availfield->userfield = $data->userfield; - $availfield->customfieldid = $data->customfieldid; + $availfield->customfieldid = $customfieldid; $availfield->operator = $data->operator; $availfield->value = $data->value; $DB->insert_record('course_sections_avail_fields', $availfield); @@ -2637,18 +2645,26 @@ class restore_module_structure_step extends restore_structure_step { $data = (object)$data; // Mark it is as passed by default $passed = true; - // Ok, if it is a profile field we need to check it exists - if (!is_null($data->customfieldid)) { - if (!$DB->record_exists('user_info_field', array('id' => $data->customfieldid))) { - $passed = false; - } + $customfieldid = null; + + // If a customfield has been used in order to pass we must be able to match an existing + // customfield by name (data->customfield) and type (data->customfieldtype) + if (!empty($data->customfield) xor !empty($data->customfieldtype)) { + // xor is sort of uncommon. If either customfield is null or customfieldtype is null BUT not both. + // If one is null but the other isn't something clearly went wrong and we'll skip this condition. + $passed = false; + } else if (!empty($data->customfield)) { + $params = array('shortname' => $data->customfield, 'datatype' => $data->customfieldtype); + $customfieldid = $DB->get_field('user_info_field', 'id', $params); + $passed = ($customfieldid !== false); } + if ($passed) { // Create the object to insert into the database $availfield = new stdClass(); $availfield->coursemoduleid = $this->task->get_moduleid(); // Lets add the availability cmid $availfield->userfield = $data->userfield; - $availfield->customfieldid = $data->customfieldid; + $availfield->customfieldid = $customfieldid; $availfield->operator = $data->operator; $availfield->value = $data->value; // Insert into the database