Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
/backup/ -> copy.php (source)

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * This script is used to configure and execute the course copy proccess.
  19   *
  20   * @package    core_backup
  21   * @copyright  2020 onward The Moodle Users Association <https://moodleassociation.org/>
  22   * @author     Matt Porritt <mattp@catalyst-au.net>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  require_once('../config.php');
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  $courseid = required_param('id', PARAM_INT);
  31  $returnto = optional_param('returnto', 'course', PARAM_ALPHANUM); // Generic navigation return page switch.
  32  $returnurl = optional_param('returnurl', '', PARAM_LOCALURL); // A return URL. returnto must also be set to 'url'.
  33  
  34  $url = new moodle_url('/backup/copy.php', array('id' => $courseid));
  35  $course = get_course($courseid);
  36  $coursecontext = context_course::instance($course->id);
  37  
  38  // Security and access checks.
  39  require_login($course, false);
  40  $copycaps = \core_course\management\helper::get_course_copy_capabilities();
  41  require_all_capabilities($copycaps, $coursecontext);
  42  
  43  if ($returnurl != '') {
  44      $returnurl = new moodle_url($returnurl);
  45  } else if ($returnto == 'catmanage') {
  46      // Redirect to category management page.
  47      $returnurl = new moodle_url('/course/management.php', array('categoryid' => $course->category));
  48  } else {
  49      // Redirect back to course page if we came from there.
  50      $returnurl = new moodle_url('/course/view.php', array('id' => $courseid));
  51  }
  52  
  53  // Setup the page.
  54  $title = get_string('copycoursetitle', 'backup', $course->shortname);
  55  $heading = get_string('copycourseheading', 'backup');
  56  $PAGE->set_url($url);
  57  $PAGE->set_pagelayout('admin');
  58  $PAGE->set_title($title);
  59  $PAGE->set_heading($heading);
  60  
  61  // Get data ready for mform.
  62  $mform = new \core_backup\output\copy_form(
  63      $url,
  64      array('course' => $course, 'returnto' => $returnto, 'returnurl' => $returnurl));
  65  
  66  if ($mform->is_cancelled()) {
  67      // The form has been cancelled, take them back to what ever the return to is.
  68      redirect($returnurl);
  69  
  70  } else if ($mdata = $mform->get_data()) {
  71  
  72      // Process the form and create the copy task.
  73      $backupcopy = new \core_backup\copy\copy($mdata);
  74      $backupcopy->create_copy();
  75  
  76      if (!empty($mdata->submitdisplay)) {
  77          // Redirect to the copy progress overview.
  78          $progressurl = new moodle_url('/backup/copyprogress.php', array('id' => $courseid));
  79          redirect($progressurl);
  80      } else {
  81          // Redirect to the course view page.
  82          $coursesurl = new moodle_url('/course/view.php', array('id' => $courseid));
  83          redirect($coursesurl);
  84      }
  85  
  86  } else {
  87      // This branch is executed if the form is submitted but the data doesn't validate,
  88      // or on the first display of the form.
  89  
  90      // Build the page output.
  91      echo $OUTPUT->header();
  92      echo $OUTPUT->heading($title);
  93      $mform->display();
  94      echo $OUTPUT->footer();
  95  }