Search moodle.org's
Developer Documentation


/backup/ -> backup.php (source)
   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  require_once('../config.php');
  19  require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
  20  require_once($CFG->dirroot . '/backup/moodle2/backup_plan_builder.class.php');
  21  
  22  
  23  $courseid = required_param('id', PARAM_INT);
  24  $sectionid = optional_param('section', null, PARAM_INT);
  25  $cmid = optional_param('cm', null, PARAM_INT);
  26  /**
  27   * Part of the forms in stages after initial, is POST never GET
  28   */
  29  $backupid = optional_param('backup', false, PARAM_ALPHANUM);
  30  
  31  $url = new moodle_url('/backup/backup.php', array('id'=>$courseid));
  32  if ($sectionid !== null) {
  33      $url->param('section', $sectionid);
  34  }
  35  if ($cmid !== null) {
  36      $url->param('cm', $cmid);
  37  }
  38  $PAGE->set_url($url);
  39  $PAGE->set_pagelayout('admin');
  40  
  41  $id = $courseid;
  42  $cm = null;
  43  $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST);
  44  $type = backup::TYPE_1COURSE;
  45  if (!is_null($sectionid)) {
  46      $section = $DB->get_record('course_sections', array('course'=>$course->id, 'id'=>$sectionid), '*', MUST_EXIST);
  47      $type = backup::TYPE_1SECTION;
  48      $id = $sectionid;
  49  }
  50  if (!is_null($cmid)) {
  51      $cm = get_coursemodule_from_id(null, $cmid, $course->id, false, MUST_EXIST);
  52      $type = backup::TYPE_1ACTIVITY;
  53      $id = $cmid;
  54  }
  55  require_login($course, false, $cm);
  56  
  57  switch ($type) {
  58      case backup::TYPE_1COURSE :
  59          require_capability('moodle/backup:backupcourse', context_course::instance($course->id));
  60          $heading = get_string('backupcourse', 'backup', $course->shortname);
  61          break;
  62      case backup::TYPE_1SECTION :
  63          $coursecontext = context_course::instance($course->id);
  64          require_capability('moodle/backup:backupsection', $coursecontext);
  65          if ((string)$section->name !== '') {
  66              $sectionname = format_string($section->name, true, array('context' => $coursecontext));
  67              $heading = get_string('backupsection', 'backup', $sectionname);
  68              $PAGE->navbar->add($sectionname);
  69          } else {
  70              $heading = get_string('backupsection', 'backup', $section->section);
  71              $PAGE->navbar->add(get_string('section').' '.$section->section);
  72          }
  73          break;
  74      case backup::TYPE_1ACTIVITY :
  75          require_capability('moodle/backup:backupactivity', context_module::instance($cm->id));
  76          $heading = get_string('backupactivity', 'backup', $cm->name);
  77          break;
  78      default :
  79          print_error('unknownbackuptype');
  80  }
  81  
  82  // Backup of large courses requires extra memory. Use the amount configured
  83  // in admin settings.
  84  raise_memory_limit(MEMORY_EXTRA);
  85  
  86  if (!($bc = backup_ui::load_controller($backupid))) {
  87      $bc = new backup_controller($type, $id, backup::FORMAT_MOODLE,
  88                              backup::INTERACTIVE_YES, backup::MODE_GENERAL, $USER->id);
  89  }
  90  $backup = new backup_ui($bc);
  91  
  92  $PAGE->set_title($heading);
  93  $PAGE->set_heading($heading);
  94  
  95  $renderer = $PAGE->get_renderer('core','backup');
  96  echo $OUTPUT->header();
  97  
  98  // Prepare a progress bar which can display optionally during long-running
  99  // operations while setting up the UI.
 100  $slowprogress = new \core\progress\display_if_slow(get_string('preparingui', 'backup'));
 101  
 102  $previous = optional_param('previous', false, PARAM_BOOL);
 103  if ($backup->get_stage() == backup_ui::STAGE_SCHEMA && !$previous) {
 104      // After schema stage, we are probably going to get to the confirmation stage,
 105      // The confirmation stage has 2 sets of progress, so this is needed to prevent
 106      // it showing 2 progress bars.
 107      $twobars = true;
 108      $slowprogress->start_progress('', 2);
 109  } else {
 110      $twobars = false;
 111  }
 112  $backup->get_controller()->set_progress($slowprogress);
 113  $backup->process();
 114  
 115  if ($backup->enforce_changed_dependencies()) {
 116      debugging('Your settings have been altered due to unmet dependencies', DEBUG_DEVELOPER);
 117  }
 118  
 119  $loghtml = '';
 120  if ($backup->get_stage() == backup_ui::STAGE_FINAL) {
 121      // Display an extra backup step bar so that we can show the 'processing' step first.
 122      echo html_writer::start_div('', array('id' => 'executionprogress'));
 123      echo $renderer->progress_bar($backup->get_progress_bar());
 124      $backup->get_controller()->set_progress(new \core\progress\display());
 125  
 126      // Prepare logger and add to end of chain.
 127      $logger = new core_backup_html_logger($CFG->debugdeveloper ? backup::LOG_DEBUG : backup::LOG_INFO);
 128      $backup->get_controller()->add_logger($logger);
 129  
 130      // Carry out actual backup.
 131      $backup->execute();
 132  
 133      // Backup controller gets saved/loaded so the logger object changes and we
 134      // have to retrieve it.
 135      $logger = $backup->get_controller()->get_logger();
 136      while (!is_a($logger, 'core_backup_html_logger')) {
 137          $logger = $logger->get_next();
 138      }
 139  
 140      // Get HTML from logger.
 141      if ($CFG->debugdisplay) {
 142          $loghtml = $logger->get_html();
 143      }
 144  
 145      // Hide the progress display and first backup step bar (the 'finished' step will show next).
 146      echo html_writer::end_div();
 147      echo html_writer::script('document.getElementById("executionprogress").style.display = "none";');
 148  } else {
 149      $backup->save_controller();
 150  }
 151  
 152  // Displaying UI can require progress reporting, so do it here before outputting
 153  // the backup stage bar (as part of the existing progress bar, if required).
 154  $ui = $backup->display($renderer);
 155  if ($twobars) {
 156      $slowprogress->end_progress();
 157  }
 158  
 159  echo $renderer->progress_bar($backup->get_progress_bar());
 160  
 161  echo $ui;
 162  $backup->destroy();
 163  unset($backup);
 164  
 165  // Display log data if there was any.
 166  if ($loghtml != '') {
 167      echo $renderer->log_display($loghtml);
 168  }
 169  
 170  echo $OUTPUT->footer();

Search This Site: