[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/backup/converter/imscc11/ -> lib.php (source)

   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          global $CFG;
  42  
  43          $filepath = $CFG->dataroot . '/temp/backup/' . $tempdir;
  44          $manifest = cc112moodle::get_manifest($filepath);
  45          if (file_exists($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/imsccv1p1/imscp_v1p1";
  59              if (strpos($xml_snippet, $search_string) !== false) {
  60                  return backup::FORMAT_IMSCC11;
  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_IMSCC11,
  80                  'to'    => backup::FORMAT_MOODLE1,
  81                  'cost'  => 10
  82          );
  83      }
  84  
  85      protected function execute() {
  86          global $CFG;
  87  
  88          $manifest = cc112moodle::get_manifest($this->get_tempdir_path());
  89          if (empty($manifest)) {
  90              throw new imscc11_convert_exception('No Manifest detected!');
  91          }
  92  
  93          $this->log('validating manifest', backup::LOG_DEBUG, null, 1);
  94          $validator = new manifest_validator($CFG->dirroot . '/backup/cc/schemas11');
  95          if (!$validator->validate($manifest)) {
  96              $this->log('validation error(s): '.PHP_EOL.error_messages::instance(), backup::LOG_DEBUG, null, 2);
  97              throw new imscc11_convert_exception(error_messages::instance()->to_string(true));
  98          }
  99          $manifestdir = dirname($manifest);
 100          $cc112moodle = new cc112moodle($manifest);
 101          if ($cc112moodle->is_auth()) {
 102              throw new imscc11_convert_exception('protected_cc_not_supported');
 103          }
 104          $status = $cc112moodle->generate_moodle_xml();
 105          // Final cleanup.
 106          $xml_error = new libxml_errors_mgr(true);
 107          $mdoc = new DOMDocument();
 108          $mdoc->preserveWhiteSpace = false;
 109          $mdoc->formatOutput = true;
 110          $mdoc->validateOnParse = false;
 111          $mdoc->strictErrorChecking = false;
 112          if ($mdoc->load($manifestdir.'/moodle.xml', LIBXML_NONET)) {
 113              $mdoc->save($this->get_workdir_path().'/moodle.xml', LIBXML_NOEMPTYTAG);
 114          } else {
 115              $xml_error->collect();
 116              $this->log('validation error(s): '.PHP_EOL.error_messages::instance(), backup::LOG_DEBUG, null, 2);
 117              throw new imscc11_convert_exception(error_messages::instance()->to_string(true));
 118          }
 119          // Move the files to the workdir.
 120          rename($manifestdir.'/course_files', $this->get_workdir_path().'/course_files');
 121      }
 122  
 123  
 124  }
 125  
 126  /**
 127   * Exception thrown by this converter
 128   */
 129  class imscc11_convert_exception extends convert_exception {
 130  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1