Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/backup/util/helper/restore_prechecks_helper.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 
00039 abstract class restore_prechecks_helper {
00040 
00046     public static function execute_prechecks($controller, $droptemptablesafter = false) {
00047         global $CFG;
00048 
00049         $errors = array();
00050         $warnings = array();
00051 
00052         // Some handy vars to be used along the prechecks
00053         $samesite = $controller->is_samesite();
00054         $restoreusers = $controller->get_plan()->get_setting('users')->get_value();
00055         $hasmnetusers = (int)$controller->get_info()->mnet_remoteusers;
00056         $restoreid = $controller->get_restoreid();
00057         $courseid = $controller->get_courseid();
00058         $userid = $controller->get_userid();
00059         $rolemappings = $controller->get_info()->role_mappings;
00060         // Load all the included tasks to look for inforef.xml files
00061         $inforeffiles = array();
00062         $tasks = restore_dbops::get_included_tasks($restoreid);
00063         foreach ($tasks as $task) {
00064             // Add the inforef.xml file if exists
00065             $inforefpath = $task->get_taskbasepath() . '/inforef.xml';
00066             if (file_exists($inforefpath)) {
00067                 $inforeffiles[] = $inforefpath;
00068             }
00069         }
00070 
00071         // Create temp tables
00072         restore_controller_dbops::create_restore_temp_tables($controller->get_restoreid());
00073 
00074         // Check we are restoring one backup >= $min20version (very first ok ever)
00075         $min20version = 2010072300;
00076         if ($controller->get_info()->backup_version < $min20version) {
00077             $message = new stdclass();
00078             $message->backup = $controller->get_info()->backup_version;
00079             $message->min    = $min20version;
00080             $errors[] = get_string('errorminbackup20version', 'backup', $message);
00081         }
00082 
00083         // Compare Moodle's versions
00084         if ($CFG->version < $controller->get_info()->moodle_version) {
00085             $message = new stdclass();
00086             $message->serverversion = $CFG->version;
00087             $message->serverrelease = $CFG->release;
00088             $message->backupversion = $controller->get_info()->moodle_version;
00089             $message->backuprelease = $controller->get_info()->moodle_release;
00090             $warnings[] = get_string('noticenewerbackup','',$message);
00091         }
00092 
00093         // Error if restoring over frontpage
00094         // TODO: Review the whole restore process in order to transform this into one warning (see 1.9)
00095         if ($controller->get_courseid() == SITEID) {
00096             $errors[] = get_string('errorrestorefrontpage', 'backup');
00097         }
00098 
00099         // If restoring to different site and restoring users and backup has mnet users warn/error
00100         if (!$samesite && $restoreusers && $hasmnetusers) {
00101             // User is admin (can create users at sysctx), warn
00102             if (has_capability('moodle/user:create', get_context_instance(CONTEXT_SYSTEM), $controller->get_userid())) {
00103                 $warnings[] = get_string('mnetrestore_extusers_admin', 'admin');
00104             // User not admin
00105             } else {
00106                 $errors[] = get_string('mnetrestore_extusers_noadmin', 'admin');
00107             }
00108         }
00109 
00110         // Load all the inforef files, we are going to need them
00111         foreach ($inforeffiles as $inforeffile) {
00112             restore_dbops::load_inforef_to_tempids($restoreid, $inforeffile); // Load each inforef file to temp_ids
00113         }
00114 
00115         // If restoring users, check we are able to create all them
00116         if ($restoreusers) {
00117             $file = $controller->get_plan()->get_basepath() . '/users.xml';
00118             restore_dbops::load_users_to_tempids($restoreid, $file); // Load needed users to temp_ids
00119             if ($problems = restore_dbops::precheck_included_users($restoreid, $courseid, $userid, $samesite)) {
00120                 $errors = array_merge($errors, $problems);
00121             }
00122         }
00123 
00124         // Note: restore won't create roles at all. Only mapping/skip!
00125         $file = $controller->get_plan()->get_basepath() . '/roles.xml';
00126         restore_dbops::load_roles_to_tempids($restoreid, $file); // Load needed roles to temp_ids
00127         if ($problems = restore_dbops::precheck_included_roles($restoreid, $courseid, $userid, $samesite, $rolemappings)) {
00128             $errors = array_key_exists('errors', $problems) ? array_merge($errors, $problems['errors']) : $errors;
00129             $warnings = array_key_exists('warnings', $problems) ? array_merge($warnings, $problems['warnings']) : $warnings;
00130         }
00131 
00132         // Check we are able to restore and the categories and questions
00133         $file = $controller->get_plan()->get_basepath() . '/questions.xml';
00134         restore_dbops::load_categories_and_questions_to_tempids($restoreid, $file);
00135         if ($problems = restore_dbops::precheck_categories_and_questions($restoreid, $courseid, $userid, $samesite)) {
00136             $errors = array_key_exists('errors', $problems) ? array_merge($errors, $problems['errors']) : $errors;
00137             $warnings = array_key_exists('warnings', $problems) ? array_merge($warnings, $problems['warnings']) : $warnings;
00138         }
00139 
00140         // Prepare results and return
00141         $results = array();
00142         if (!empty($errors)) {
00143             $results['errors'] = $errors;
00144         }
00145         if (!empty($warnings)) {
00146             $results['warnings'] = $warnings;
00147         }
00148         // Warnings/errors detected or want to do so explicitly, drop temp tables
00149         if (!empty($results) || $droptemptablesafter) {
00150             restore_controller_dbops::drop_restore_temp_tables($controller->get_restoreid());
00151         }
00152         return $results;
00153     }
00154 }
00155 
00156 /*
00157  * Exception class used by all the @restore_prechecks_helper stuff
00158  */
00159 class restore_prechecks_helper_exception extends backup_exception {
00160 
00161     public function __construct($errorcode, $a=NULL, $debuginfo=null) {
00162         parent::__construct($errorcode, $a, $debuginfo);
00163     }
00164 }
 All Data Structures Namespaces Files Functions Variables Enumerations