|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00012 require_once($CFG->dirroot . '/backup/converter/convertlib.php'); 00013 require_once($CFG->dirroot . '/backup/cc/includes/constants.php'); 00014 require_once($CFG->dirroot . '/backup/cc/cc2moodle.php'); 00015 require_once($CFG->dirroot . '/backup/cc/validator.php'); 00016 00017 class imscc1_converter extends base_converter { 00018 00029 public function log($message, $level, $a = null, $depth = null, $display = false) { 00030 parent::log('(imscc1) '.$message, $level, $a, $depth, $display); 00031 } 00032 00039 public static function detect_format($tempdir) { 00040 global $CFG; 00041 00042 $filepath = $CFG->dataroot . '/temp/backup/' . $tempdir; 00043 $manifest = cc2moodle::get_manifest($filepath); 00044 if (!empty($manifest)) { 00045 // looks promising, lets load some information 00046 $handle = fopen($manifest, 'r'); 00047 $xml_snippet = fread($handle, 500); 00048 fclose($handle); 00049 00050 // check if it has the required strings 00051 00052 $xml_snippet = strtolower($xml_snippet); 00053 $xml_snippet = preg_replace('/\s*/m', '', $xml_snippet); 00054 $xml_snippet = str_replace("'", '', $xml_snippet); 00055 $xml_snippet = str_replace('"', '', $xml_snippet); 00056 00057 $search_string = "xmlns=http://www.imsglobal.org/xsd/imscc/imscp_v1p1"; 00058 if (strpos($xml_snippet, $search_string) !== false) { 00059 return backup::FORMAT_IMSCC1; 00060 } 00061 } 00062 00063 return null; 00064 } 00065 00066 00075 public static function description() { 00076 00077 return array( 00078 'from' => backup::FORMAT_IMSCC1, 00079 'to' => backup::FORMAT_MOODLE1, 00080 'cost' => 10 00081 ); 00082 } 00083 00084 protected function execute() { 00085 global $CFG; 00086 00087 $manifest = cc2moodle::get_manifest($this->get_tempdir_path()); 00088 if (empty($manifest)) { 00089 throw new imscc1_convert_exception('No Manifest detected!'); 00090 } 00091 00092 $this->log('validating manifest', backup::LOG_DEBUG, null, 1); 00093 $validator = new manifest10_validator($CFG->dirroot . '/backup/cc/schemas'); 00094 if (!$validator->validate($manifest)) { 00095 $this->log('validation error(s): '.PHP_EOL.error_messages::instance(), backup::LOG_DEBUG, null, 2); 00096 throw new imscc1_convert_exception(error_messages::instance()->to_string(true)); 00097 } 00098 00099 $manifestdir = dirname($manifest); 00100 $cc2moodle = new cc2moodle($manifest); 00101 if ($cc2moodle->is_auth()) { 00102 throw new imscc1_convert_exception('protected_cc_not_supported'); 00103 } 00104 $status = $cc2moodle->generate_moodle_xml(); 00105 //Final cleanup 00106 $xml_error = new libxml_errors_mgr(true); 00107 $mdoc = new DOMDocument(); 00108 $mdoc->preserveWhiteSpace = false; 00109 $mdoc->formatOutput = true; 00110 $mdoc->validateOnParse = false; 00111 $mdoc->strictErrorChecking = false; 00112 if ($mdoc->load($manifestdir.'/moodle.xml', LIBXML_NONET)) { 00113 $mdoc->save($this->get_workdir_path().'/moodle.xml', LIBXML_NOEMPTYTAG); 00114 } else { 00115 $xml_error->collect(); 00116 $this->log('validation error(s): '.PHP_EOL.error_messages::instance(), backup::LOG_DEBUG, null, 2); 00117 throw new imscc1_convert_exception(error_messages::instance()->to_string(true)); 00118 } 00119 //Move the files to the workdir 00120 rename($manifestdir.'/course_files', $this->get_workdir_path().'/course_files'); 00121 } 00122 00123 00124 } 00125 00129 class imscc1_convert_exception extends convert_exception { 00130 }