|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00027 defined('MOODLE_INTERNAL') || die(); 00028 00033 function imscp_20_migrate() { 00034 global $CFG, $DB, $OUTPUT; 00035 require_once("$CFG->libdir/filelib.php"); 00036 require_once("$CFG->dirroot/course/lib.php"); 00037 require_once("$CFG->dirroot/mod/imscp/locallib.php"); 00038 00039 if (!file_exists("$CFG->dirroot/mod/resource/db/upgradelib.php")) { 00040 // bad luck, somebody deleted resource module 00041 return; 00042 } 00043 00044 require_once("$CFG->dirroot/mod/resource/db/upgradelib.php"); 00045 00046 // create resource_old table and copy resource table there if needed 00047 if (!resource_20_prepare_migration()) { 00048 // no modules or fresh install 00049 return; 00050 } 00051 00052 $candidates = $DB->get_recordset('resource_old', array('type'=>'ims', 'migrated'=>0)); 00053 if (!$candidates->valid()) { 00054 $candidates->close(); // Not going to iterate (but exit), close rs 00055 return; 00056 } 00057 00058 $fs = get_file_storage(); 00059 00060 foreach ($candidates as $candidate) { 00061 upgrade_set_timeout(60); 00062 00063 if ($CFG->texteditors !== 'textarea') { 00064 $intro = text_to_html($candidate->intro, false, false, true); 00065 $introformat = FORMAT_HTML; 00066 } else { 00067 $intro = $candidate->intro; 00068 $introformat = FORMAT_MOODLE; 00069 } 00070 00071 $imscp = new stdClass(); 00072 $imscp->course = $candidate->course; 00073 $imscp->name = $candidate->name; 00074 $imscp->intro = $intro; 00075 $imscp->introformat = $introformat; 00076 $imscp->revision = 1; 00077 $imscp->keepold = 1; 00078 $imscp->timemodified = time(); 00079 00080 if (!$imscp = resource_migrate_to_module('imscp', $candidate, $imscp)) { 00081 continue; 00082 } 00083 00084 $context = get_context_instance(CONTEXT_MODULE, $candidate->cmid); 00085 $root = "$CFG->dataroot/$candidate->course/$CFG->moddata/resource/$candidate->oldid"; 00086 00087 // migrate package backup file 00088 if ($candidate->reference) { 00089 $package = basename($candidate->reference); 00090 $fullpath = $root.'/'.$package; 00091 if (file_exists($fullpath)) { 00092 $file_record = array('contextid' => $context->id, 00093 'component' => 'mod_imscp', 00094 'filearea' => 'backup', 00095 'itemid' => 1, 00096 'filepath' => '/', 00097 'filename' => $package); 00098 $fs->create_file_from_pathname($file_record, $fullpath); 00099 } 00100 } 00101 00102 // migrate extracted package data 00103 $files = imsc_migrate_get_old_files($root, ''); 00104 if (empty($files)) { 00105 // if ims package doesn't exist, continue loop 00106 echo $OUTPUT->notification("IMS package data cannot be found, failed migrating activity: \"$candidate->name\", please fix it manually"); 00107 continue; 00108 } 00109 00110 $file_record = array('contextid'=>$context->id, 'component'=>'mod_imscp', 'filearea'=>'content', 'itemid'=>1); 00111 $error = false; 00112 foreach ($files as $relname=>$fullpath) { 00113 $parts = explode('/', $relname); 00114 $file_record['filename'] = array_pop($parts); 00115 $parts[] = ''; // keep trailing slash 00116 $file_record['filepath'] = implode('/', $parts); 00117 00118 try { 00119 $fs->create_file_from_pathname($file_record, $fullpath); 00120 } catch (Exception $e) { 00121 //continue on error, we can not recover anyway 00122 $error = true; 00123 echo $OUTPUT->notification("IMSCP: failed migrating file: $relname"); 00124 } 00125 } 00126 unset($files); 00127 00128 // parse manifest 00129 $structure = imscp_parse_structure($imscp, $context); 00130 $imscp->structure = is_array($structure) ? serialize($structure) : null; 00131 $DB->update_record('imscp', $imscp); 00132 00133 // remove old moddata dir only if no error and manifest ok 00134 if (!$error and is_array($structure)) { 00135 fulldelete($root); 00136 } 00137 } 00138 $candidates->close(); 00139 00140 // clear all course modinfo caches 00141 rebuild_course_cache(0, true); 00142 } 00143 00147 function imsc_migrate_get_old_files($path, $relative) { 00148 global $OUTPUT; 00149 $result = array(); 00150 if (!file_exists($path)) { 00151 echo $OUTPUT->notification("File path doesn't exist: $path <br/> Please fix it manually."); 00152 return array(); 00153 } 00154 $items = new DirectoryIterator($path); 00155 foreach ($items as $item) { 00156 if ($item->isDot() or $item->isLink()) { 00157 // symbolic links could create infinite loops or cause unintended file migration, sorry 00158 continue; 00159 } 00160 $pathname = $item->getPathname(); 00161 $relname = $relative.'/'.$item->getFilename(); 00162 if ($item->isFile()) { 00163 $result[$relname] = $pathname; 00164 } else if ($item->isDir()) { 00165 $result = array_merge($result, imsc_migrate_get_old_files($pathname, $relname)); 00166 } 00167 unset($item); 00168 } 00169 unset($items); 00170 return $result; 00171 }