[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

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

   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          global $CFG;
  41  
  42          $filepath = $CFG->dataroot . '/temp/backup/' . $tempdir;
  43          $manifest = cc2moodle::get_manifest($filepath);
  44          if (!empty($manifest)) {
  45              // Looks promising, lets load some information.
  46              $handle = fopen($manifest, 'r');
  47              $xml_snippet = fread($handle, 1024);
  48              fclose($handle);
  49  
  50              // Check if it has the required strings.
  51  
  52              $xml_snippet = strtolower($xml_snippet);
  53              $xml_snippet = preg_replace('/\s*/m', '', $xml_snippet);
  54              $xml_snippet = str_replace("'", '', $xml_snippet);
  55              $xml_snippet = str_replace('"', '', $xml_snippet);
  56  
  57              $search_string = "xmlns=http://www.imsglobal.org/xsd/imscc/imscp_v1p1";
  58              if (strpos($xml_snippet, $search_string) !== false) {
  59                  return backup::FORMAT_IMSCC1;
  60              }
  61          }
  62  
  63          return null;
  64      }
  65  
  66  
  67      /**
  68       * Returns the basic information about the converter
  69       *
  70       * The returned array must contain the following keys:
  71       * 'from' - the supported source format, eg. backup::FORMAT_MOODLE1
  72       * 'to'   - the supported target format, eg. backup::FORMAT_MOODLE
  73       * 'cost' - the cost of the conversion, non-negative non-zero integer
  74       */
  75      public static function description() {
  76  
  77          return array(
  78                  'from'  => backup::FORMAT_IMSCC1,
  79                  'to'    => backup::FORMAT_MOODLE1,
  80                  'cost'  => 10
  81          );
  82      }
  83  
  84      protected function execute() {
  85          global $CFG;
  86  
  87          $manifest = cc2moodle::get_manifest($this->get_tempdir_path());
  88          if (empty($manifest)) {
  89              throw new imscc1_convert_exception('No Manifest detected!');
  90          }
  91  
  92          $this->log('validating manifest', backup::LOG_DEBUG, null, 1);
  93          $validator = new manifest10_validator($CFG->dirroot . '/backup/cc/schemas');
  94          if (!$validator->validate($manifest)) {
  95              $this->log('validation error(s): '.PHP_EOL.error_messages::instance(), backup::LOG_DEBUG, null, 2);
  96              throw new imscc1_convert_exception(error_messages::instance()->to_string(true));
  97          }
  98  
  99          $manifestdir = dirname($manifest);
 100          $cc2moodle = new cc2moodle($manifest);
 101          if ($cc2moodle->is_auth()) {
 102              throw new imscc1_convert_exception('protected_cc_not_supported');
 103          }
 104          $status = $cc2moodle->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 imscc1_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 imscc1_convert_exception extends convert_exception {
 130  }


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