Permalink
Browse files

MDL-34220 condition: Fixed up backup and restore of custom profile fi…

…eld conditions
  • Loading branch information...
Sam Hemelryk
Sam Hemelryk committed Jul 13, 2012
1 parent 904673d commit 8ab1529d8a794f708dff76fe8735a98edb64f1ec
Showing with 40 additions and 16 deletions.
  1. +12 −4 backup/moodle2/backup_stepslib.php
  2. +28 −12 backup/moodle2/restore_stepslib.php
@@ -323,7 +323,7 @@ protected function define_structure() {
$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 @@ protected function define_structure() {
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 @@ protected function define_structure() {
$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');
@@ -1139,18 +1139,26 @@ public function process_availability_field($data) {
$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 @@ protected function process_availability_field($data) {
$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

0 comments on commit 8ab1529

Please sign in to comment.