Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403]

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * Import backup file or select existing backup file from moodle
  20   * @package   moodlecore
  21   * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require_once('../config.php');
  26  require_once (__DIR__ . '/restorefile_form.php');
  27  require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
  28  
  29  // current context
  30  $contextid = required_param('contextid', PARAM_INT);
  31  $filecontextid = optional_param('filecontextid', 0, PARAM_INT);
  32  // action
  33  $action = optional_param('action', '', PARAM_ALPHA);
  34  // file parameters
  35  // non js interface may require these parameters
  36  $component  = optional_param('component', null, PARAM_COMPONENT);
  37  $filearea   = optional_param('filearea', null, PARAM_AREA);
  38  $itemid     = optional_param('itemid', null, PARAM_INT);
  39  $filepath   = optional_param('filepath', null, PARAM_PATH);
  40  $filename   = optional_param('filename', null, PARAM_FILE);
  41  
  42  list($context, $course, $cm) = get_context_info_array($contextid);
  43  
  44  // will be used when restore
  45  if (!empty($filecontextid)) {
  46      $filecontext = context::instance_by_id($filecontextid);
  47  }
  48  
  49  $url = new moodle_url('/backup/restorefile.php', array('contextid'=>$contextid));
  50  
  51  $PAGE->set_url($url);
  52  $PAGE->set_context($context);
  53  
  54  switch ($context->contextlevel) {
  55      case CONTEXT_COURSECAT:
  56          core_course_category::page_setup();
  57          break;
  58      case CONTEXT_MODULE:
  59          $PAGE->set_heading(get_string('restoreactivity', 'backup'));
  60          break;
  61      case CONTEXT_COURSE:
  62          $course = get_course($context->instanceid);
  63          $PAGE->set_heading($course->fullname);
  64          $PAGE->set_secondary_active_tab('coursereuse');
  65          break;
  66      default:
  67          $PAGE->set_heading($SITE->fullname);
  68  }
  69  
  70  
  71  require_login($course, false, $cm);
  72  require_capability('moodle/restore:restorecourse', $context);
  73  
  74  if (is_null($course)) {
  75      $courseid = 0;
  76      $coursefullname = $SITE->fullname;
  77  } else {
  78      $courseid = $course->id;
  79      $coursefullname = $course->fullname;
  80  }
  81  
  82  $browser = get_file_browser();
  83  
  84  // check if tmp dir exists
  85  $tmpdir = make_backup_temp_directory('', false);
  86  if (!check_dir_exists($tmpdir, true, true)) {
  87      throw new restore_controller_exception('cannot_create_backup_temp_dir');
  88  }
  89  
  90  // choose the backup file from backup files tree
  91  if ($action == 'choosebackupfile') {
  92      if ($filearea == 'automated') {
  93          require_capability('moodle/restore:viewautomatedfilearea', $context);
  94      }
  95      if ($fileinfo = $browser->get_file_info($filecontext, $component, $filearea, $itemid, $filepath, $filename)) {
  96          if (is_a($fileinfo, 'file_info_stored')) {
  97              // Use the contenthash rather than copying the file where possible,
  98              // to improve performance and avoid timeouts with large files.
  99              $fs = get_file_storage();
 100              $params = $fileinfo->get_params();
 101              $file = $fs->get_file($params['contextid'], $params['component'], $params['filearea'],
 102                      $params['itemid'], $params['filepath'], $params['filename']);
 103              $restore_url = new moodle_url('/backup/restore.php', array('contextid' => $contextid,
 104                      'pathnamehash' => $file->get_pathnamehash(), 'contenthash' => $file->get_contenthash()));
 105          } else {
 106              // If it's some weird other kind of file then use old code.
 107              $filename = restore_controller::get_tempdir_name($courseid, $USER->id);
 108              $pathname = $tmpdir . '/' . $filename;
 109              if (!$fileinfo->copy_to_pathname($pathname)) {
 110                  throw new restore_ui_exception('errorcopyingbackupfile', null, $pathname);
 111              }
 112              $restore_url = new moodle_url('/backup/restore.php', array(
 113                      'contextid' => $contextid, 'filename' => $filename));
 114          }
 115          redirect($restore_url);
 116      } else {
 117          redirect($url, get_string('filenotfound', 'error'));
 118      }
 119      die;
 120  }
 121  
 122  $PAGE->set_title(get_string('course') . ': ' . $coursefullname);
 123  $PAGE->set_pagelayout('admin');
 124  $PAGE->activityheader->disable();
 125  $PAGE->requires->js_call_amd('core_backup/async_backup', 'asyncBackupAllStatus', array($context->id));
 126  
 127  $form = new course_restore_form(null, array('contextid'=>$contextid));
 128  $data = $form->get_data();
 129  if ($data && has_capability('moodle/restore:uploadfile', $context)) {
 130      $filename = restore_controller::get_tempdir_name($courseid, $USER->id);
 131      $pathname = $tmpdir . '/' . $filename;
 132      if (!$form->save_file('backupfile', $pathname)) {
 133          throw new restore_ui_exception('errorcopyingbackupfile', null, $pathname);
 134      }
 135      $restore_url = new moodle_url('/backup/restore.php', array('contextid'=>$contextid, 'filename'=>$filename));
 136      redirect($restore_url);
 137      die;
 138  }
 139  
 140  echo $OUTPUT->header();
 141  
 142  // require uploadfile cap to use file picker
 143  if (has_capability('moodle/restore:uploadfile', $context)) {
 144      echo $OUTPUT->heading(get_string('importfile', 'backup'));
 145      echo $OUTPUT->container_start();
 146      $form->display();
 147      echo $OUTPUT->container_end();
 148  }
 149  
 150  if ($context->contextlevel == CONTEXT_MODULE) {
 151      echo $OUTPUT->heading_with_help(get_string('choosefilefromactivitybackup', 'backup'), 'choosefilefromuserbackup', 'backup');
 152      echo $OUTPUT->container_start();
 153      $treeview_options = array();
 154      $user_context = context_user::instance($USER->id);
 155      $treeview_options['filecontext'] = $context;
 156      $treeview_options['currentcontext'] = $context;
 157      $treeview_options['component']   = 'backup';
 158      $treeview_options['context']     = $context;
 159      $treeview_options['filearea']    = 'activity';
 160      $renderer = $PAGE->get_renderer('core', 'backup');
 161      echo $renderer->backup_files_viewer($treeview_options);
 162      echo $OUTPUT->container_end();
 163      // Update the course context with the proper value, because $context contains the module context.
 164      $coursecontext = \context_course::instance($course->id);
 165  } else {
 166      $coursecontext = $context;
 167  }
 168  
 169  echo $OUTPUT->heading_with_help(get_string('choosefilefromcoursebackup', 'backup'), 'choosefilefromcoursebackup', 'backup');
 170  echo $OUTPUT->container_start();
 171  $treeview_options = array();
 172  $treeview_options['filecontext'] = $coursecontext;
 173  $treeview_options['currentcontext'] = $context;
 174  $treeview_options['component']   = 'backup';
 175  $treeview_options['context']     = $context;
 176  $treeview_options['filearea']    = 'course';
 177  $renderer = $PAGE->get_renderer('core', 'backup');
 178  echo $renderer->backup_files_viewer($treeview_options);
 179  echo $OUTPUT->container_end();
 180  
 181  echo $OUTPUT->heading_with_help(get_string('choosefilefromuserbackup', 'backup'), 'choosefilefromuserbackup', 'backup');
 182  echo $OUTPUT->container_start();
 183  $treeview_options = array();
 184  $user_context = context_user::instance($USER->id);
 185  $treeview_options['filecontext'] = $user_context;
 186  $treeview_options['currentcontext'] = $context;
 187  $treeview_options['component']   = 'user';
 188  $treeview_options['context']     = 'backup';
 189  $treeview_options['filearea']    = 'backup';
 190  $renderer = $PAGE->get_renderer('core', 'backup');
 191  echo $renderer->backup_files_viewer($treeview_options);
 192  echo $OUTPUT->container_end();
 193  
 194  $automatedbackups = get_config('backup', 'backup_auto_active');
 195  if (!empty($automatedbackups)) {
 196      echo $OUTPUT->heading_with_help(get_string('choosefilefromautomatedbackup', 'backup'), 'choosefilefromautomatedbackup', 'backup');
 197      echo $OUTPUT->container_start();
 198      $treeview_options = array();
 199      $user_context = context_user::instance($USER->id);
 200      $treeview_options['filecontext'] = $context;
 201      $treeview_options['currentcontext'] = $context;
 202      $treeview_options['component']   = 'backup';
 203      $treeview_options['context']     = $context;
 204      $treeview_options['filearea']    = 'automated';
 205      $renderer = $PAGE->get_renderer('core', 'backup');
 206      echo $renderer->backup_files_viewer($treeview_options);
 207      echo $OUTPUT->container_end();
 208  }
 209  
 210  // In progress course restores.
 211  if (async_helper::is_async_enabled()) {
 212      echo $OUTPUT->heading_with_help(get_string('asyncrestoreinprogress', 'backup'), 'asyncrestoreinprogress', 'backup');
 213      echo $OUTPUT->container_start();
 214      $renderer = $PAGE->get_renderer('core', 'backup');
 215      echo $renderer->restore_progress_viewer($USER->id, $context);
 216      echo $OUTPUT->container_end();
 217  }
 218  
 219  echo $OUTPUT->footer();