Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   1  <?php
   2  
   3  /**
   4   * Provides Common Cartridge v1 converter class
   5   *
   6   * @package    core
   7   * @subpackage backup-convert
   8   * @copyright  2011 Darko Miletic <dmiletic@moodlerooms.com>
   9   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  10   */
  11  
  12  require_once($CFG->dirroot.'/backup/converter/convertlib.php');
  13  require_once($CFG->dirroot.'/backup/cc/includes/constants.php');
  14  require_once($CFG->dirroot.'/backup/cc/cc2moodle.php');
  15  require_once($CFG->dirroot.'/backup/cc/validator.php');
  16  
  17  class imscc1_converter extends base_converter {
  18  
  19      /**
  20       * Log a message
  21       *
  22       * @see parent::log()
  23       * @param string $message message text
  24       * @param int $level message level {@example backup::LOG_WARNING}
  25       * @param null|mixed $a additional information
  26       * @param null|int $depth the message depth
  27       * @param bool $display whether the message should be sent to the output, too
  28       */
  29      public function log($message, $level, $a = null, $depth = null, $display = false) {
  30          parent::log('(imscc1) '.$message, $level, $a, $depth, $display);
  31      }
  32  
  33      /**
  34       * Detects the Common Cartridge 1.0 format of the backup directory
  35       *
  36       * @param string $tempdir the name of the backup directory
  37       * @return null|string backup::FORMAT_IMSCC1 if the Common Cartridge 1.0 is detected, null otherwise
  38       */
  39      public static function detect_format($tempdir) {
  40          $filepath = make_backup_temp_directory($tempdir, false);
  41          if (!is_dir($filepath)) {
  42              throw new convert_helper_exception('tmp_backup_directory_not_found', $filepath);
  43          }
  44          $manifest = cc2moodle::get_manifest($filepath);
  45          if (!empty($manifest)) {
  46              // Looks promising, lets load some information.
  47              $handle = fopen($manifest, 'r');
  48              $xml_snippet = fread($handle, 1024);
  49              fclose($handle);
  50  
  51              // Check if it has the required strings.
  52  
  53              $xml_snippet = strtolower($xml_snippet);
  54              $xml_snippet = preg_replace('/\s*/m', '', $xml_snippet);
  55              $xml_snippet = str_replace("'", '', $xml_snippet);
  56              $xml_snippet = str_replace('"', '', $xml_snippet);
  57  
  58              $search_string = "xmlns=http://www.imsglobal.org/xsd/imscc/imscp_v1p1";
  59              if (strpos($xml_snippet, $search_string) !== false) {
  60                  return backup::FORMAT_IMSCC1;
  61              }
  62          }
  63  
  64          return null;
  65      }
  66  
  67  
  68      /**
  69       * Returns the basic information about the converter
  70       *
  71       * The returned array must contain the following keys:
  72       * 'from' - the supported source format, eg. backup::FORMAT_MOODLE1
  73       * 'to'   - the supported target format, eg. backup::FORMAT_MOODLE
  74       * 'cost' - the cost of the conversion, non-negative non-zero integer
  75       */
  76      public static function description() {
  77  
  78          return array(
  79                  'from'  => backup::FORMAT_IMSCC1,
  80                  'to'    => backup::FORMAT_MOODLE1,
  81                  'cost'  => 10
  82          );
  83      }
  84  
  85      protected function execute() {
  86          global $CFG;
  87  
  88          $manifest = cc2moodle::get_manifest($this->get_tempdir_path());
  89          if (empty($manifest)) {
  90              throw new imscc1_convert_exception('No Manifest detected!');
  91          }
  92  
  93          $this->log('validating manifest', backup::LOG_DEBUG, null, 1);
  94          $validator = new manifest10_validator($CFG->dirroot . '/backup/cc/schemas');
  95          if (!$validator->validate($manifest)) {
  96              $this->log('validation error(s): '.PHP_EOL.error_messages::instance(), backup::LOG_DEBUG, null, 2);
  97              throw new imscc1_convert_exception(error_messages::instance()->to_string(true));
  98          }
  99  
 100          $manifestdir = dirname($manifest);
 101          $cc2moodle = new cc2moodle($manifest);
 102          if ($cc2moodle->is_auth()) {
 103              throw new imscc1_convert_exception('protected_cc_not_supported');
 104          }
 105          $status = $cc2moodle->generate_moodle_xml();
 106          // Final cleanup.
 107          $xml_error = new libxml_errors_mgr(true);
 108          $mdoc = new DOMDocument();
 109          $mdoc->preserveWhiteSpace = false;
 110          $mdoc->formatOutput = true;
 111          $mdoc->validateOnParse = false;
 112          $mdoc->strictErrorChecking = false;
 113          if ($mdoc->load($manifestdir.'/moodle.xml', LIBXML_NONET)) {
 114              $mdoc->save($this->get_workdir_path().'/moodle.xml', LIBXML_NOEMPTYTAG);
 115          } else {
 116              $xml_error->collect();
 117              $this->log('validation error(s): '.PHP_EOL.error_messages::instance(), backup::LOG_DEBUG, null, 2);
 118              throw new imscc1_convert_exception(error_messages::instance()->to_string(true));
 119          }
 120          // Move the files to the workdir.
 121          rename($manifestdir.'/course_files', $this->get_workdir_path().'/course_files');
 122      }
 123  
 124  
 125  }
 126  
 127  /**
 128   * Exception thrown by this converter
 129   */
 130  class imscc1_convert_exception extends convert_exception {
 131  }