Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   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   * Provides {@link imscc11_export_converter} class
  20   *
  21   * @package    core
  22   * @subpackage backup-convert
  23   * @copyright  2011 Darko Miletic <dmiletic@moodlerooms.com>
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  require_once($CFG->dirroot . '/backup/converter/convertlib.php');
  30  
  31  class imscc11_export_converter extends base_converter {
  32      static public function get_deps() {
  33          global $CFG;
  34          require_once($CFG->dirroot . '/backup/util/settings/setting_dependency.class.php');
  35          return array(
  36                  'users'   => setting_dependency::DISABLED_VALUE,
  37                  'filters' => setting_dependency::DISABLED_VALUE,
  38                  'blocks'  => setting_dependency::DISABLED_VALUE
  39          );
  40  
  41      }
  42      protected function execute() {
  43  
  44      }
  45      public static function description() {
  46  
  47          return array(
  48                  'from'  => backup::FORMAT_MOODLE,
  49                  'to'    => backup::FORMAT_IMSCC11,
  50                  'cost'  => 10
  51          );
  52      }
  53  
  54  }
  55  
  56  
  57  class imscc11_store_backup_file extends backup_execution_step {
  58  
  59      protected function define_execution() {
  60  
  61          // Get basepath
  62          $basepath = $this->get_basepath();
  63  
  64          // Calculate the zip fullpath (in OS temp area it's always backup.imscc)
  65          $zipfile = $basepath . '/backup.imscc';
  66  
  67          // Perform storage and return it (TODO: shouldn't be array but proper result object)
  68          // Let's send the file to file storage, everything already defined
  69          // First of all, get some information from the backup_controller to help us decide
  70          list($dinfo, $cinfo, $sinfo) = backup_controller_dbops::get_moodle_backup_information($this->get_backupid());
  71  
  72          // Extract useful information to decide
  73          $file      = $sinfo['filename']->value;
  74          $filename  = basename($file,'.'.pathinfo($file, PATHINFO_EXTENSION)).'.imscc';        // Backup filename
  75          $userid    = $dinfo[0]->userid;                // User->id executing the backup
  76          $id        = $dinfo[0]->id;                    // Id of activity/section/course (depends of type)
  77          $courseid  = $dinfo[0]->courseid;              // Id of the course
  78  
  79          $ctxid     = context_user::instance($userid)->id;
  80          $component = 'user';
  81          $filearea  = 'backup';
  82          $itemid    = 0;
  83          $fs = get_file_storage();
  84          $fr = array(
  85                      'contextid'   => $ctxid,
  86                      'component'   => $component,
  87                      'filearea'    => $filearea,
  88                      'itemid'      => $itemid,
  89                      'filepath'    => '/',
  90                      'filename'    => $filename,
  91                      'userid'      => $userid,
  92                      'timecreated' => time(),
  93                      'timemodified'=> time());
  94          // If file already exists, delete if before
  95          // creating it again. This is BC behaviour - copy()
  96          // overwrites by default
  97          if ($fs->file_exists($fr['contextid'], $fr['component'], $fr['filearea'], $fr['itemid'], $fr['filepath'], $fr['filename'])) {
  98              $pathnamehash = $fs->get_pathname_hash($fr['contextid'], $fr['component'], $fr['filearea'], $fr['itemid'], $fr['filepath'], $fr['filename']);
  99              $sf = $fs->get_file_by_hash($pathnamehash);
 100              $sf->delete();
 101          }
 102  
 103          return array('backup_destination' => $fs->create_file_from_pathname($fr, $zipfile));
 104      }
 105  }
 106  
 107  class imscc11_zip_contents extends backup_execution_step {
 108  
 109      protected function define_execution() {
 110  
 111          // Get basepath
 112          $basepath = $this->get_basepath();
 113  
 114          // Get the list of files in directory
 115          $filestemp = get_directory_list($basepath, '', false, true, true);
 116          $files = array();
 117          foreach ($filestemp as $file) {
 118              // Add zip paths and fs paths to all them
 119              $files[$file] = $basepath . '/' . $file;
 120          }
 121  
 122          // Calculate the zip fullpath (in OS temp area it's always backup.mbz)
 123          $zipfile = $basepath . '/backup.imscc';
 124  
 125          // Get the zip packer
 126          $zippacker = get_file_packer('application/zip');
 127  
 128          // Zip files
 129          $zippacker->archive_to_pathname($files, $zipfile);
 130      }
 131  }
 132  
 133  class imscc11_backup_convert extends backup_execution_step {
 134  
 135      protected function define_execution() {
 136          global $CFG;
 137          // Get basepath
 138          $basepath = $this->get_basepath();
 139  
 140          require_once($CFG->dirroot . '/backup/cc/cc_includes.php');
 141  
 142          $tempdir = $CFG->backuptempdir . '/' . uniqid('', true);
 143  
 144          if (mkdir($tempdir, $CFG->directorypermissions, true)) {
 145  
 146              cc_convert_moodle2::convert($basepath, $tempdir);
 147              //Switch the directories
 148              if (empty($CFG->keeptempdirectoriesonbackup)) {
 149                  fulldelete($basepath);
 150              } else {
 151                  if (!rename($basepath, $basepath  . '_moodle2_source')) {
 152                      throw new backup_task_exception('failed_rename_source_tempdir');
 153                  }
 154              }
 155  
 156              if (!rename($tempdir, $basepath)) {
 157                  throw new backup_task_exception('failed_move_converted_into_place');
 158              }
 159  
 160          }
 161      }
 162  }
 163  
 164  
 165