Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   1  <?php
   2  
   3  /**
   4   * Provides Common Cartridge v1.1 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/cc112moodle.php');
  15  require_once($CFG->dirroot.'/backup/cc/validator.php');
  16  
  17  
  18  class imscc11_converter extends base_converter {
  19  
  20      /**
  21       * Log a message
  22       *
  23       * @see parent::log()
  24       * @param string $message message text
  25       * @param int $level message level {@example backup::LOG_WARNING}
  26       * @param null|mixed $a additional information
  27       * @param null|int $depth the message depth
  28       * @param bool $display whether the message should be sent to the output, too
  29       */
  30      public function log($message, $level, $a = null, $depth = null, $display = false) {
  31          parent::log('(imscc1) '.$message, $level, $a, $depth, $display);
  32      }
  33  
  34      /**
  35       * Detects the Common Cartridge 1.0 format of the backup directory
  36       *
  37       * @param string $tempdir the name of the backup directory
  38       * @return null|string backup::FORMAT_IMSCC11 if the Common Cartridge 1.1 is detected, null otherwise
  39       */
  40      public static function detect_format($tempdir) {
  41          $filepath = make_backup_temp_directory($tempdir, false);
  42          if (!is_dir($filepath)) {
  43              throw new convert_helper_exception('tmp_backup_directory_not_found', $filepath);
  44          }
  45          $manifest = cc112moodle::get_manifest($filepath);
  46          if (file_exists($manifest)) {
  47              // Looks promising, lets load some information.
  48              $handle = fopen($manifest, 'r');
  49              $xml_snippet = fread($handle, 1024);
  50              fclose($handle);
  51  
  52              // Check if it has the required strings.
  53  
  54              $xml_snippet = strtolower($xml_snippet);
  55              $xml_snippet = preg_replace('/\s*/m', '', $xml_snippet);
  56              $xml_snippet = str_replace("'", '', $xml_snippet);
  57              $xml_snippet = str_replace('"', '', $xml_snippet);
  58  
  59              $search_string = "xmlns=http://www.imsglobal.org/xsd/imsccv1p1/imscp_v1p1";
  60              if (strpos($xml_snippet, $search_string) !== false) {
  61                  return backup::FORMAT_IMSCC11;
  62              }
  63          }
  64  
  65          return null;
  66      }
  67  
  68  
  69      /**
  70       * Returns the basic information about the converter
  71       *
  72       * The returned array must contain the following keys:
  73       * 'from' - the supported source format, eg. backup::FORMAT_MOODLE1
  74       * 'to'   - the supported target format, eg. backup::FORMAT_MOODLE
  75       * 'cost' - the cost of the conversion, non-negative non-zero integer
  76       */
  77      public static function description() {
  78  
  79          return array(
  80                  'from'  => backup::FORMAT_IMSCC11,
  81                  'to'    => backup::FORMAT_MOODLE1,
  82                  'cost'  => 10
  83          );
  84      }
  85  
  86      protected function execute() {
  87          global $CFG;
  88  
  89          $manifest = cc112moodle::get_manifest($this->get_tempdir_path());
  90          if (empty($manifest)) {
  91              throw new imscc11_convert_exception('No Manifest detected!');
  92          }
  93  
  94          $this->log('validating manifest', backup::LOG_DEBUG, null, 1);
  95          $validator = new manifest_validator($CFG->dirroot . '/backup/cc/schemas11');
  96          if (!$validator->validate($manifest)) {
  97              $this->log('validation error(s): '.PHP_EOL.error_messages::instance(), backup::LOG_DEBUG, null, 2);
  98              throw new imscc11_convert_exception(error_messages::instance()->to_string(true));
  99          }
 100          $manifestdir = dirname($manifest);
 101          $cc112moodle = new cc112moodle($manifest);
 102          if ($cc112moodle->is_auth()) {
 103              throw new imscc11_convert_exception('protected_cc_not_supported');
 104          }
 105          $status = $cc112moodle->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 imscc11_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 imscc11_convert_exception extends convert_exception {
 131  }