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.

Differences Between: [Versions 310 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   * Course copy class.
  19   *
  20   * Handles procesing data submitted by UI copy form
  21   * and sets up the course copy process.
  22   *
  23   * @package    core_backup
  24   * @copyright  2020 onward The Moodle Users Association <https://moodleassociation.org/>
  25   * @author     Matt Porritt <mattp@catalyst-au.net>
  26   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   * @deprecated since Moodle 4.1. Use copy_helper instead
  28   */
  29  
  30  namespace core_backup\copy;
  31  
  32  defined('MOODLE_INTERNAL') || die;
  33  
  34  global $CFG;
  35  require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
  36  require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
  37  
  38  /**
  39   * Course copy class.
  40   *
  41   * Handles procesing data submitted by UI copy form
  42   * and sets up the course copy process.
  43   *
  44   * @package    core_backup
  45   * @copyright  2020 onward The Moodle Users Association <https://moodleassociation.org/>
  46   * @author     Matt Porritt <mattp@catalyst-au.net>
  47   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  48   * @deprecated since Moodle 4.1 MDL-74548 - please use copy_helper instead
  49   * @todo MDL-75022 This class will be deleted in Moodle 4.5
  50   * @see copy_helper
  51   */
  52  class copy {
  53  
  54      /**
  55       * The fields required for copy operations.
  56       *
  57       * @var array
  58       */
  59      private $copyfields = array(
  60          'courseid',  // Course id integer.
  61          'fullname', // Fullname of the destination course.
  62          'shortname', // Shortname of the destination course.
  63          'category', // Category integer ID that contains the destination course.
  64          'visible', // Integer to detrmine of the copied course will be visible.
  65          'startdate', // Integer timestamp of the start of the destination course.
  66          'enddate', // Integer timestamp of the end of the destination course.
  67          'idnumber', // ID of the destination course.
  68          'userdata', // Integer to determine if the copied course will contain user data.
  69      );
  70  
  71      /**
  72       * Data required for course copy operations.
  73       *
  74       * @var array
  75       */
  76      private $copydata = array();
  77  
  78      /**
  79       * List of role ids to keep enrolments for in the destination course.
  80       *
  81       * @var array
  82       */
  83      private $roles = array();
  84  
  85      /**
  86       * Constructor for the class.
  87       *
  88       * @param \stdClass $formdata Data from the validated course copy form.
  89       */
  90      public function __construct(\stdClass $formdata) {
  91          debugging('Class \course_backup\copy\copy is deprecated. Please use the copy_helper class instead.');
  92          $this->copydata = $this->get_copy_data($formdata);
  93          $this->roles = $this->get_enrollment_roles($formdata);
  94      }
  95  
  96      /**
  97       * Extract the enrolment roles to keep in the copied course
  98       * from the raw submitted form data.
  99       *
 100       * @param \stdClass $formdata Data from the validated course copy form.
 101       * @return array $keptroles The roles to keep.
 102       */
 103      private function get_enrollment_roles(\stdClass $formdata): array {
 104          $keptroles = array();
 105  
 106          foreach ($formdata as $key => $value) {
 107              if ((substr($key, 0, 5 ) === 'role_') && ($value != 0)) {
 108                  $keptroles[] = $value;
 109              }
 110          }
 111  
 112          return $keptroles;
 113      }
 114  
 115      /**
 116       *  Take the validated form data and extract the required information for copy operations.
 117       *
 118       * @param \stdClass $formdata Data from the validated course copy form.
 119       * @return \stdClass $copydata Data required for course copy operations.
 120       * @throws \moodle_exception If one of the required copy fields is missing
 121       */
 122      private function get_copy_data(\stdClass $formdata): \stdClass {
 123          $copydata = new \stdClass();
 124  
 125          foreach ($this->copyfields as $field) {
 126              if (isset($formdata->{$field})) {
 127                  $copydata->{$field} = $formdata->{$field};
 128              } else {
 129                  throw new \moodle_exception('copyfieldnotfound', 'backup', '', null, $field);
 130              }
 131          }
 132  
 133          return $copydata;
 134      }
 135  
 136      /**
 137       * Creates a course copy.
 138       * Sets up relevant controllers and adhoc task.
 139       *
 140       * @return array $copyids THe backup and restore controller ids.
 141       * @deprecated since Moodle 4.1 MDL-74548 - please use copy_helper instead.
 142       * @todo MDL-75023 This method will be deleted in Moodle 4.5
 143       * @see copy_helper::process_formdata()
 144       * @see copy_helper::create_copy()
 145       */
 146      public function create_copy(): array {
 147          debugging('The method \core_backup\copy\copy::create_copy() is deprecated.
 148              Please use the methods provided by copy_helper instead.', DEBUG_DEVELOPER);
 149          $copydata = clone($this->copydata);
 150          $copydata->keptroles = $this->roles;
 151          return \copy_helper::create_copy($copydata);
 152      }
 153  
 154      /**
 155       * Get the in progress course copy operations for a user.
 156       *
 157       * @param int $userid User id to get the course copies for.
 158       * @param int $courseid The optional source course id to get copies for.
 159       * @return array $copies Details of the inprogress copies.
 160       * @deprecated since Moodle 4.1 MDL-74548 - please use copy_helper::get_copies() instead.
 161       * @todo MDL-75024 This method will be deleted in Moodle 4.5
 162       * @see copy_helper::get_copies()
 163       */
 164      public static function get_copies(int $userid, int $courseid=0): array {
 165          debugging('The method \core_backup\copy\copy::get_copies() is deprecated.
 166              Please use copy_helper::get_copies() instead.', DEBUG_DEVELOPER);
 167  
 168          return \copy_helper::get_copies($userid, $coursied);
 169      }
 170  }