Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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  switch ($context->contextlevel) {
  52      case CONTEXT_MODULE:
  53          $heading = get_string('restoreactivity', 'backup');
  54          break;
  55      case CONTEXT_COURSE:
  56      default:
  57          $heading = get_string('restorecourse', 'backup');
  58  }
  59  
  60  
  61  require_login($course, false, $cm);
  62  require_capability('moodle/restore:restorecourse', $context);
  63  
  64  if (is_null($course)) {
  65      $courseid = 0;
  66      $coursefullname = $SITE->fullname;
  67  } else {
  68      $courseid = $course->id;
  69      $coursefullname = $course->fullname;
  70  }
  71  
  72  $browser = get_file_browser();
  73  
  74  // check if tmp dir exists
  75  $tmpdir = make_backup_temp_directory('', false);
  76  if (!check_dir_exists($tmpdir, true, true)) {
  77      throw new restore_controller_exception('cannot_create_backup_temp_dir');
  78  }
  79  
  80  // choose the backup file from backup files tree
  81  if ($action == 'choosebackupfile') {
  82      if ($filearea == 'automated') {
  83          require_capability('moodle/restore:viewautomatedfilearea', $context);
  84      }
  85      if ($fileinfo = $browser->get_file_info($filecontext, $component, $filearea, $itemid, $filepath, $filename)) {
  86          if (is_a($fileinfo, 'file_info_stored')) {
  87              // Use the contenthash rather than copying the file where possible,
  88              // to improve performance and avoid timeouts with large files.
  89              $fs = get_file_storage();
  90              $params = $fileinfo->get_params();
  91              $file = $fs->get_file($params['contextid'], $params['component'], $params['filearea'],
  92                      $params['itemid'], $params['filepath'], $params['filename']);
  93              $restore_url = new moodle_url('/backup/restore.php', array('contextid' => $contextid,
  94                      'pathnamehash' => $file->get_pathnamehash(), 'contenthash' => $file->get_contenthash()));
  95          } else {
  96              // If it's some weird other kind of file then use old code.
  97              $filename = restore_controller::get_tempdir_name($courseid, $USER->id);
  98              $pathname = $tmpdir . '/' . $filename;
  99              if (!$fileinfo->copy_to_pathname($pathname)) {
 100                  throw new restore_ui_exception('errorcopyingbackupfile', null, $pathname);
 101              }
 102              $restore_url = new moodle_url('/backup/restore.php', array(
 103                      'contextid' => $contextid, 'filename' => $filename));
 104          }
 105          redirect($restore_url);
 106      } else {
 107          redirect($url, get_string('filenotfound', 'error'));
 108      }
 109      die;
 110  }
 111  
 112  $PAGE->set_url($url);
 113  $PAGE->set_context($context);
 114  $PAGE->set_title(get_string('course') . ': ' . $coursefullname);
 115  $PAGE->set_heading($heading);
 116  $PAGE->set_pagelayout('admin');
 117  $PAGE->requires->js_call_amd('core_backup/async_backup', 'asyncBackupAllStatus', array($context->id));
 118  
 119  $form = new course_restore_form(null, array('contextid'=>$contextid));
 120  $data = $form->get_data();
 121  if ($data && has_capability('moodle/restore:uploadfile', $context)) {
 122      $filename = restore_controller::get_tempdir_name($courseid, $USER->id);
 123      $pathname = $tmpdir . '/' . $filename;
 124      if (!$form->save_file('backupfile', $pathname)) {
 125          throw new restore_ui_exception('errorcopyingbackupfile', null, $pathname);
 126      }
 127      $restore_url = new moodle_url('/backup/restore.php', array('contextid'=>$contextid, 'filename'=>$filename));
 128      redirect($restore_url);
 129      die;
 130  }
 131  
 132  echo $OUTPUT->header();
 133  
 134  // require uploadfile cap to use file picker
 135  if (has_capability('moodle/restore:uploadfile', $context)) {
 136      echo $OUTPUT->heading(get_string('importfile', 'backup'));
 137      echo $OUTPUT->container_start();
 138      $form->display();
 139      echo $OUTPUT->container_end();
 140  }
 141  
 142  if ($context->contextlevel == CONTEXT_MODULE) {
 143      echo $OUTPUT->heading_with_help(get_string('choosefilefromactivitybackup', 'backup'), 'choosefilefromuserbackup', 'backup');
 144      echo $OUTPUT->container_start();
 145      $treeview_options = array();
 146      $user_context = context_user::instance($USER->id);
 147      $treeview_options['filecontext'] = $context;
 148      $treeview_options['currentcontext'] = $context;
 149      $treeview_options['component']   = 'backup';
 150      $treeview_options['context']     = $context;
 151      $treeview_options['filearea']    = 'activity';
 152      $renderer = $PAGE->get_renderer('core', 'backup');
 153      echo $renderer->backup_files_viewer($treeview_options);
 154      echo $OUTPUT->container_end();
 155  }
 156  
 157  echo $OUTPUT->heading_with_help(get_string('choosefilefromcoursebackup', 'backup'), 'choosefilefromcoursebackup', 'backup');
 158  echo $OUTPUT->container_start();
 159  $treeview_options = array();
 160  $treeview_options['filecontext'] = $context;
 161  $treeview_options['currentcontext'] = $context;
 162  $treeview_options['component']   = 'backup';
 163  $treeview_options['context']     = $context;
 164  $treeview_options['filearea']    = 'course';
 165  $renderer = $PAGE->get_renderer('core', 'backup');
 166  echo $renderer->backup_files_viewer($treeview_options);
 167  echo $OUTPUT->container_end();
 168  
 169  echo $OUTPUT->heading_with_help(get_string('choosefilefromuserbackup', 'backup'), 'choosefilefromuserbackup', 'backup');
 170  echo $OUTPUT->container_start();
 171  $treeview_options = array();
 172  $user_context = context_user::instance($USER->id);
 173  $treeview_options['filecontext'] = $user_context;
 174  $treeview_options['currentcontext'] = $context;
 175  $treeview_options['component']   = 'user';
 176  $treeview_options['context']     = 'backup';
 177  $treeview_options['filearea']    = 'backup';
 178  $renderer = $PAGE->get_renderer('core', 'backup');
 179  echo $renderer->backup_files_viewer($treeview_options);
 180  echo $OUTPUT->container_end();
 181  
 182  $automatedbackups = get_config('backup', 'backup_auto_active');
 183  if (!empty($automatedbackups)) {
 184      echo $OUTPUT->heading_with_help(get_string('choosefilefromautomatedbackup', 'backup'), 'choosefilefromautomatedbackup', 'backup');
 185      echo $OUTPUT->container_start();
 186      $treeview_options = array();
 187      $user_context = context_user::instance($USER->id);
 188      $treeview_options['filecontext'] = $context;
 189      $treeview_options['currentcontext'] = $context;
 190      $treeview_options['component']   = 'backup';
 191      $treeview_options['context']     = $context;
 192      $treeview_options['filearea']    = 'automated';
 193      $renderer = $PAGE->get_renderer('core', 'backup');
 194      echo $renderer->backup_files_viewer($treeview_options);
 195      echo $OUTPUT->container_end();
 196  }
 197  
 198  // In progress course restores.
 199  if (async_helper::is_async_enabled()) {
 200      echo $OUTPUT->heading_with_help(get_string('asyncrestoreinprogress', 'backup'), 'asyncrestoreinprogress', 'backup');
 201      echo $OUTPUT->container_start();
 202      $renderer = $PAGE->get_renderer('core', 'backup');
 203      echo $renderer->restore_progress_viewer($USER->id, $context);
 204      echo $OUTPUT->container_end();
 205  }
 206  
 207  echo $OUTPUT->footer();