Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-42498

Improve backup/restore within Moodle (eg. course and activity duplication) for 2.4 and 2.5

    XMLWordPrintable

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Won't Fix
    • 2.4.3, 2.5
    • None
    • Backup, Performance
    • None
    • Any
    • MOODLE_24_STABLE, MOODLE_25_STABLE
    • Hide

      On backup/moodle2/backup_custom_fields.php change fill_values function of file_nested_element class

          public function fill_values($values) {
              // Fill values
              parent::fill_values($values);
              // Do our own tasks (copy file from moodle to backup)
       
              //ADD THIS!
              $bc = backup_ui::load_controller($backupid);
                  if($bc && $bc->get_plan()->get_mode() != backup::MODE_IMPORT){
                  try {
                      backup_file_manager::copy_file_moodle2backup($this->backupid, $values);
                  } catch (file_exception $e) {
                      $this->add_result(array('missing_files_in_pool' => true));
                      $this->add_log('missing file in pool: ' . $e->debuginfo, backup::LOG_WARNING);
                  }
              }
          }
      

      On backup/util/dbops/restore_dbops.class.php on funcion send_files_to_pool change the contents inside "if (empty($file->repositoryid)) {"

      if (empty($file->repositoryid)) {
                      // create the file in the filepool if it does not exist yet
                      if (!$fs->file_exists($newcontextid, $component, $filearea, $rec->newitemid, $file->filepath, $file->filename)) {
                          //ADD THIS
                          if($copy_fileid = $DB->get_field('files','id',array('contenthash'=>$file->contenthash))){
                              $file_record = array(
                                  'contextid'   => $newcontextid,
                                  'component'   => $component,
                                  'filearea'    => $filearea,
                                  'itemid'      => $rec->newitemid,
                                  'filepath'    => $file->filepath,
                                  'filename'    => $file->filename,
                                  'timecreated' => $file->timecreated,
                                  'timemodified'=> $file->timemodified,
                                  'userid'      => $mappeduserid,
                                  'author'      => $file->author,
                                  'license'     => $file->license,
                                  'sortorder'   => $file->sortorder
                              );
                              $fs->create_file_from_storedfile($file_record, $copy_fileid);
                          } else {
                              // this is a regular file, it must be present in the backup pool
                              $backuppath = $basepath . backup_file_manager::get_backup_content_file_location($file->contenthash);
       
                              // The file is not found in the backup.
                              if (!file_exists($backuppath)) {
                                  $result = new stdClass();
                                  $result->code = 'file_missing_in_backup';
                                  $result->message = sprintf('missing file %s%s in backup', $file->filepath, $file->filename);
                                  $result->level = backup::LOG_WARNING;
                                  $results[] = $result;
                                  continue;
                              }
                              
                              $file_record = array(
                                  'contextid'   => $newcontextid,
                                  'component'   => $component,
                                  'filearea'    => $filearea,
                                  'itemid'      => $rec->newitemid,
                                  'filepath'    => $file->filepath,
                                  'filename'    => $file->filename,
                                  'timecreated' => $file->timecreated,
                                  'timemodified'=> $file->timemodified,
                                  'userid'      => $mappeduserid,
                                  'author'      => $file->author,
                                  'license'     => $file->license,
                                  'sortorder'   => $file->sortorder
                              );
                              $fs->create_file_from_pathname($file_record, $backuppath);
                          }
      }
      
      

      Show
      On backup/moodle2/backup_custom_fields.php change fill_values function of file_nested_element class public function fill_values($values) { // Fill values parent::fill_values($values); // Do our own tasks (copy file from moodle to backup)   //ADD THIS! $bc = backup_ui::load_controller($backupid); if($bc && $bc->get_plan()->get_mode() != backup::MODE_IMPORT){ try { backup_file_manager::copy_file_moodle2backup($this->backupid, $values); } catch (file_exception $e) { $this->add_result(array('missing_files_in_pool' => true)); $this->add_log('missing file in pool: ' . $e->debuginfo, backup::LOG_WARNING); } } } On backup/util/dbops/restore_dbops.class.php on funcion send_files_to_pool change the contents inside "if (empty($file->repositoryid)) {" if (empty($file->repositoryid)) { // create the file in the filepool if it does not exist yet if (!$fs->file_exists($newcontextid, $component, $filearea, $rec->newitemid, $file->filepath, $file->filename)) { //ADD THIS if($copy_fileid = $DB->get_field('files','id',array('contenthash'=>$file->contenthash))){ $file_record = array( 'contextid' => $newcontextid, 'component' => $component, 'filearea' => $filearea, 'itemid' => $rec->newitemid, 'filepath' => $file->filepath, 'filename' => $file->filename, 'timecreated' => $file->timecreated, 'timemodified'=> $file->timemodified, 'userid' => $mappeduserid, 'author' => $file->author, 'license' => $file->license, 'sortorder' => $file->sortorder ); $fs->create_file_from_storedfile($file_record, $copy_fileid); } else { // this is a regular file, it must be present in the backup pool $backuppath = $basepath . backup_file_manager::get_backup_content_file_location($file->contenthash);   // The file is not found in the backup. if (!file_exists($backuppath)) { $result = new stdClass(); $result->code = 'file_missing_in_backup'; $result->message = sprintf('missing file %s%s in backup', $file->filepath, $file->filename); $result->level = backup::LOG_WARNING; $results[] = $result; continue; } $file_record = array( 'contextid' => $newcontextid, 'component' => $component, 'filearea' => $filearea, 'itemid' => $rec->newitemid, 'filepath' => $file->filepath, 'filename' => $file->filename, 'timecreated' => $file->timecreated, 'timemodified'=> $file->timemodified, 'userid' => $mappeduserid, 'author' => $file->author, 'license' => $file->license, 'sortorder' => $file->sortorder ); $fs->create_file_from_pathname($file_record, $backuppath); } }

    Description

      MDL-37761 has resolved this issue in master, but was not backported due to numerous API changes. We should consider introducing the same improvement in 2.4 and 2.5 using another method where no new API is introduced.

      Attachments

        Issue Links

          Activity

            People

              markn Mark Nelson
              pferre22 Pau Ferrer
              Adrian Greeve, Jake Dallimore, Mathew May, Mihail Geshoski, Matteo Scaramuccia, Andrew Lyons, Huong Nguyen, Jun Pataleta, Michael Hawkins, Shamim Rezaie, Simey Lameze, Stevani Andolo
              Votes:
              1 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: