Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/backup/util/helper/backup_file_manager.class.php
Go to the documentation of this file.
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 
00038 class backup_file_manager {
00039 
00043     public static function get_backup_storage_base_dir($backupid) {
00044         global $CFG;
00045 
00046         return $CFG->tempdir . '/backup/' . $backupid . '/files';
00047     }
00048 
00053     public static function get_backup_content_file_location($contenthash) {
00054         $l1 = $contenthash[0].$contenthash[1];
00055         return "$l1/$contenthash";
00056     }
00057 
00061     public static function copy_file_moodle2backup($backupid, $filerecorid) {
00062         global $DB;
00063 
00064         // Normalise param
00065         if (!is_object($filerecorid)) {
00066             $filerecorid = $DB->get_record('files', array('id' => $filerecorid));
00067         }
00068 
00069         // Directory, nothing to do
00070         if ($filerecorid->filename === '.') {
00071             return;
00072         }
00073 
00074         $fs = get_file_storage();
00075         $file = $fs->get_file_instance($filerecorid);
00076 
00077         // Calculate source and target paths (use same subdirs strategy for both)
00078         $targetfilepath = self::get_backup_storage_base_dir($backupid) . '/' .
00079                           self::get_backup_content_file_location($filerecorid->contenthash);
00080 
00081         // Create target dir if necessary
00082         if (!file_exists(dirname($targetfilepath))) {
00083             if (!check_dir_exists(dirname($targetfilepath), true, true)) {
00084                 throw new backup_helper_exception('cannot_create_directory', dirname($targetfilepath));
00085             }
00086         }
00087 
00088         // And copy the file (if doesn't exist already)
00089         if (!file_exists($targetfilepath)) {
00090             $file->copy_content_to($targetfilepath);
00091         }
00092     }
00093 }
 All Data Structures Namespaces Files Functions Variables Enumerations