|
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 00029 require_once($CFG->dirroot . '/backup/converter/convertlib.php'); 00030 00031 class imscc11_export_converter extends base_converter { 00032 public function get_deps() { 00033 global $CFG; 00034 require_once($CFG->dirroot . '/backup/util/settings/setting_dependency.class.php'); 00035 return array( 00036 'users' => setting_dependency::DISABLED_VALUE, 00037 'filters' => setting_dependency::DISABLED_VALUE, 00038 'blocks' => setting_dependency::DISABLED_VALUE 00039 ); 00040 00041 } 00042 protected function execute() { 00043 00044 } 00045 public static function description() { 00046 00047 return array( 00048 'from' => backup::FORMAT_MOODLE, 00049 'to' => backup::FORMAT_IMSCC11, 00050 'cost' => 10 00051 ); 00052 } 00053 00054 } 00055 00056 00057 class imscc11_store_backup_file extends backup_execution_step { 00058 00059 protected function define_execution() { 00060 00061 // Get basepath 00062 $basepath = $this->get_basepath(); 00063 00064 // Calculate the zip fullpath (in OS temp area it's always backup.imscc) 00065 $zipfile = $basepath . '/backup.imscc'; 00066 00067 // Perform storage and return it (TODO: shouldn't be array but proper result object) 00068 // Let's send the file to file storage, everything already defined 00069 // First of all, get some information from the backup_controller to help us decide 00070 list($dinfo, $cinfo, $sinfo) = backup_controller_dbops::get_moodle_backup_information($this->get_backupid()); 00071 00072 // Extract useful information to decide 00073 $file = $sinfo['filename']->value; 00074 $filename = basename($file,'.'.pathinfo($file, PATHINFO_EXTENSION)).'.imscc'; // Backup filename 00075 $userid = $dinfo[0]->userid; // User->id executing the backup 00076 $id = $dinfo[0]->id; // Id of activity/section/course (depends of type) 00077 $courseid = $dinfo[0]->courseid; // Id of the course 00078 00079 $ctxid = get_context_instance(CONTEXT_USER, $userid)->id; 00080 $component = 'user'; 00081 $filearea = 'backup'; 00082 $itemid = 0; 00083 $fs = get_file_storage(); 00084 $fr = array( 00085 'contextid' => $ctxid, 00086 'component' => $component, 00087 'filearea' => $filearea, 00088 'itemid' => $itemid, 00089 'filepath' => '/', 00090 'filename' => $filename, 00091 'userid' => $userid, 00092 'timecreated' => time(), 00093 'timemodified'=> time()); 00094 // If file already exists, delete if before 00095 // creating it again. This is BC behaviour - copy() 00096 // overwrites by default 00097 if ($fs->file_exists($fr['contextid'], $fr['component'], $fr['filearea'], $fr['itemid'], $fr['filepath'], $fr['filename'])) { 00098 $pathnamehash = $fs->get_pathname_hash($fr['contextid'], $fr['component'], $fr['filearea'], $fr['itemid'], $fr['filepath'], $fr['filename']); 00099 $sf = $fs->get_file_by_hash($pathnamehash); 00100 $sf->delete(); 00101 } 00102 00103 return array('backup_destination' => $fs->create_file_from_pathname($fr, $zipfile)); 00104 } 00105 } 00106 00107 class imscc11_zip_contents extends backup_execution_step { 00108 00109 protected function define_execution() { 00110 00111 // Get basepath 00112 $basepath = $this->get_basepath(); 00113 00114 // Get the list of files in directory 00115 $filestemp = get_directory_list($basepath, '', false, true, true); 00116 $files = array(); 00117 foreach ($filestemp as $file) { 00118 // Add zip paths and fs paths to all them 00119 $files[$file] = $basepath . '/' . $file; 00120 } 00121 00122 // Calculate the zip fullpath (in OS temp area it's always backup.mbz) 00123 $zipfile = $basepath . '/backup.imscc'; 00124 00125 // Get the zip packer 00126 $zippacker = get_file_packer('application/zip'); 00127 00128 // Zip files 00129 $zippacker->archive_to_pathname($files, $zipfile); 00130 } 00131 } 00132 00133 class imscc11_backup_convert extends backup_execution_step { 00134 00135 protected function define_execution() { 00136 global $CFG; 00137 // Get basepath 00138 $basepath = $this->get_basepath(); 00139 00140 require_once($CFG->dirroot . '/backup/cc/cc_includes.php'); 00141 00142 $tempdir = $CFG->dataroot . '/temp/backup/' . uniqid('', true); 00143 00144 if (mkdir($tempdir, 0777, true)) { 00145 00146 cc_convert_moodle2::convert($basepath, $tempdir); 00147 //Switch the directories 00148 if (empty($CFG->keeptempdirectoriesonbackup)) { 00149 fulldelete($basepath); 00150 } else { 00151 if (!rename($basepath, $basepath . '_moodle2_source')) { 00152 throw new backup_task_exception('failed_rename_source_tempdir'); 00153 } 00154 } 00155 00156 if (!rename($tempdir, $basepath)) { 00157 throw new backup_task_exception('failed_move_converted_into_place'); 00158 } 00159 00160 } 00161 } 00162 } 00163 00164 00165