From: stronk7 Date: Sat, 31 May 2003 15:21:39 +0000 (+0000) Subject: Now readjusting course_modules instance and course modinfo X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=5146bf9f54060e6ca9bba390a2550d3641959046;p=moodle.git Now readjusting course_modules instance and course modinfo os implemented and working --- diff --git a/backup/restore_execute.html b/backup/restore_execute.html index 9b11d106df..256cdfc0d1 100644 --- a/backup/restore_execute.html +++ b/backup/restore_execute.html @@ -23,6 +23,19 @@ error("Site not found!"); } + //Checks for the required files/functions to restore every module + //and include them + if ($allmods = get_records("modules") ) { + foreach ($allmods as $mod) { + $modname = $mod->name; + $modfile = "$mods_home/$modname/restorelib.php"; + //If file exists and we have selected to restore that type of module + if ((file_exists($modfile)) and ($restore->mods[$modname]->restore)) { + include_once($modfile); + } + } + } + //Start the main table echo ""; echo "
"; @@ -183,12 +196,22 @@ //Now create log entries as needed if ($status and ($restore->logs)) { - echo "
  • Creating Log Entries (not implemented). Execute after everything..."; + echo "
  • Creating Log Entries (not implemented!!)"; } //Now, if all is OK, adjust the instance field in course_modules !! + if ($status) { + echo "
  • Checking Instances"; + $status = restore_check_instances($restore); + } //Now if all is OK, update course modinfo field !! + if ($status) { + echo "
  • Checking Course"; + rebuild_course_cache($restore->course_id); + } + + //Cleanup temps (files and db) //End the main ul echo ""; diff --git a/backup/restorelib.php b/backup/restorelib.php index 7d66ffe3ab..3feb9b69b6 100644 --- a/backup/restorelib.php +++ b/backup/restorelib.php @@ -368,13 +368,18 @@ $course_module->deleted = $mod->deleted; $course_module->score = $mod->score; $course_module->visible = $mod->visible; - //NOTE: The instance is calculated and updaed in db in the + $course_module->instance = null; + //NOTE: The instance (new) is calculated and updated in db in the // final step of the restore. We don't know it yet. + //print_object($course_module); //Debug //Save it to db $newidmod = insert_record("course_modules",$course_module); if ($newidmod) { //save old and new module id - backup_putid ($restore->backup_unique_code,"course_modules",$keym,$newidmod); + //In the info field, we save the original instance of the module + //to use it later + backup_putid ($restore->backup_unique_code,"course_modules", + $keym,$newidmod,$mod->instance); } else { $status = false; } @@ -665,14 +670,69 @@ //Now, if we have anything in info, we have to restore that mods //from backup_ids (calling every mod restore function) if ($info) { -print_object($info); + //Iterate over each module + foreach ($info as $mod) { + $modrestore = $mod->modtype."_restore_mods"; + if (function_exists($modrestore)) { + $status = $modrestore($mod,$restore); + } else { + //Something was wrong. Function should exist. + $status = false; + } + } + } else { + $status = false; + } + return $status; + } + + //This function adjusts the instance field into course_modules. It's executed after + //modules restore. There, we KNOW the new instance id !! + function restore_check_instances($restore) { + + global $CFG; + + $status = true; + //We are going to iterate over each course_module saved in backup_ids + $course_modules = get_records_sql("SELECT new_id,info as instance + FROM {$CFG->prefix}backup_ids + WHERE backup_code = '$restore->backup_unique_code' AND + table_name = 'course_modules'"); + if ($course_modules) { + foreach($course_modules as $cm) { + //Now we are going to the REAL course_modules to get its type (field module) + $module = get_record("course_modules","id",$cm->new_id); + if ($module) { + //We know the module type id. Get the name from modules + $type = get_record("modules","id",$module->module); + if ($type) { + //We know the type name and the old_id. Get its new_id + //from backup_ids. It's the instance !!! + $instance = get_record("backup_ids","backup_code",$restore->backup_unique_code, + "table_name",$type->name, + "old_id",$cm->instance); + if ($instance) { + //We have the new instance, so update the record in course_modules + $module->instance = $instance->new_id; + //print_object ($module); //Debug + $status = update_record("course_modules",$module); + } else { + $status = false; + } + } else { + $status = false; + } + } else { + $status = false; + } + } } else { $status = false; } - return $status; + return $status; } //===================================================================================== @@ -1213,8 +1273,8 @@ print_object($info); // echo "C".str_repeat(" ",($this->level+2)*2).$this->getContents()."
    \n"; //Debug //echo $this->level.str_repeat(" ",$this->level*2)."</".$tagName.">
    \n"; //Debug //Acumulate data to info (content + close tag) - //Reconvert it to utf & htmlchars and trim to generate xml data - $this->temp .= utf8_encode(htmlspecialchars(trim($this->content))).""; + //Reconvert: strip htmlchars again and trim to generate xml data + $this->temp .= htmlspecialchars(trim($this->content)).""; //If we've finished a mod, xmlize it an save to db if (($this->level == 4) and ($tagName == "MOD")) { //Prepend XML standard header to info gathered @@ -1238,7 +1298,9 @@ print_object($info); backup_putid($this->preferences->backup_unique_code,$mod_type,$mod_id, null,$sla_mod_temp); //Create returning info - $this->info[] = array(id => $mod_id,modtype => $mod_type); + $ret_info->id = $mod_id; + $ret_info->modtype = $mod_type; + $this->info[] = $ret_info; } //Reset info to empty $this->temp = "";