Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
/backup/ -> copy.php (source)

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401]

   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  require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  $courseid = required_param('id', PARAM_INT);
  32  $returnto = optional_param('returnto', 'course', PARAM_ALPHANUM); // Generic navigation return page switch.
  33  $returnurl = optional_param('returnurl', '', PARAM_LOCALURL); // A return URL. returnto must also be set to 'url'.
  34  
  35  $url = new moodle_url('/backup/copy.php', array('id' => $courseid));
  36  $course = get_course($courseid);
  37  $coursecontext = context_course::instance($course->id);
  38  
  39  // Security and access checks.
  40  require_login($course, false);
  41  $copycaps = \core_course\management\helper::get_course_copy_capabilities();
  42  require_all_capabilities($copycaps, $coursecontext);
  43  
  44  if ($returnurl != '') {
  45      $returnurl = new moodle_url($returnurl);
  46  } else if ($returnto == 'catmanage') {
  47      // Redirect to category management page.
  48      $returnurl = new moodle_url('/course/management.php', array('categoryid' => $course->category));
  49  } else {
  50      // Redirect back to course page if we came from there.
  51      $returnurl = new moodle_url('/course/view.php', array('id' => $courseid));
  52  }
  53  
  54  // Setup the page.
  55  $title = get_string('copycoursetitle', 'backup', $course->shortname);
  56  $heading = get_string('copycourseheading', 'backup');
  57  $PAGE->set_url($url);
  58  $PAGE->set_pagelayout('admin');
  59  $PAGE->set_title($title);
  60  $PAGE->set_heading($heading);
  61  $PAGE->set_secondary_active_tab('coursereuse');
  62  
  63  // Get data ready for mform.
  64  $mform = new \core_backup\output\copy_form(
  65      $url,
  66      array('course' => $course, 'returnto' => $returnto, 'returnurl' => $returnurl));
  67  
  68  if ($mform->is_cancelled()) {
  69      // The form has been cancelled, take them back to what ever the return to is.
  70      redirect($returnurl);
  71  
  72  } else if ($mdata = $mform->get_data()) {
  73  
  74      // Process the form and create the copy task.
  75      $copydata = \copy_helper::process_formdata($mdata);
  76      \copy_helper::create_copy($copydata);
  77  
  78      if (!empty($mdata->submitdisplay)) {
  79          // Redirect to the copy progress overview.
  80          $progressurl = new moodle_url('/backup/copyprogress.php', array('id' => $courseid));
  81          redirect($progressurl);
  82      } else {
  83          // Redirect to the course view page.
  84          $coursesurl = new moodle_url('/course/view.php', array('id' => $courseid));
  85          redirect($coursesurl);
  86      }
  87  
  88  } else {
  89      // This branch is executed if the form is submitted but the data doesn't validate,
  90      // or on the first display of the form.
  91  
  92      // Build the page output.
  93      echo $OUTPUT->header();
  94      echo $OUTPUT->heading($title);
  95      $mform->display();
  96      echo $OUTPUT->footer();
  97  }