diff --git a/mod/hotpot/restorelib.php b/mod/hotpot/restorelib.php index 20faec8b6d1..500648ad77a 100644 --- a/mod/hotpot/restorelib.php +++ b/mod/hotpot/restorelib.php @@ -283,6 +283,15 @@ function hotpot_restore_record(&$restore, $status, &$xml, $table, $foreign_keys, // the name of the secondary key field, if any, in the current $record. // If this field is specified, then the current record will only be added // if the $record->$secondarykey value does not already exist in $table + + // maintain a cache of info on table columns + static $table_columns = array(); + if (empty($table_columns[$table])) { + global $CFG, $db; + $table_columns[$table] = $db->MetaColumns("$CFG->prefix$table"); + } + + // get values for fields in this record unset($record); $TAGS = array_keys($xml); foreach ($TAGS as $TAG) { @@ -292,39 +301,63 @@ function hotpot_restore_record(&$restore, $status, &$xml, $table, $foreign_keys, $record->$tag = backup_todb($value); } } + + // update secondary keys, if any $ok = true; foreach ($foreign_keys as $key=>$value) { if (is_numeric($value)) { $record->$key = $value; } else { - if (empty($record->$key)) { - $record->$key = NULL; - } else { - $key_table = $value; - $new_ids = array(); + $key_table = $value; + $new_ids = array(); + if (isset($record->$key)) { $old_ids = explode(',', $record->$key); foreach ($old_ids as $old_id) { - $key_record = backup_getid($restore->backup_unique_code, $key_table, $old_id); - if ($key_record) { - $new_ids[] = $key_record->new_id; + if (empty($old_id)) { + // do nothing } else { - // foreign key could not be updated - if (!defined('RESTORE_SILENTLY')) { - print ""; + $ok = false; } - $ok = false; } } - $record->$key = implode(',', $new_ids); + } + $record->$key = implode(',', $new_ids); + } + } + + // check all "not null" fields have been set + foreach ($table_columns[$table] as $column) { + if ($column->not_null) { + $name = $column->name; + if ($name<>'id' && empty($record->$name)) { + if (isset($column->default_value)) { + $default = $column->default_value; + } else { + if (preg_match('/int|decimal|double|float|time|year/i', $column->type)) { + $default = 0; + } else { + $default = ''; + } + } + $record->$name = $default; } } } + // check everything is OK so far if ($ok && isset($record)) { // store old record id, if necessary @@ -343,7 +376,7 @@ function hotpot_restore_record(&$restore, $status, &$xml, $table, $foreign_keys, } if (empty($record->id)) { // add the $record (and get new id) - $record->id = insert_record ($table, $record); + $record->id = insert_record($table, $record); } // check $record was added (or found) if (is_numeric($record->id)) {