Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/backup/util/ui/restore_ui.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 
00035 class restore_ui extends base_ui {
00039     const STAGE_CONFIRM = 1;
00040     const STAGE_DESTINATION = 2;
00041     const STAGE_SETTINGS = 4;
00042     const STAGE_SCHEMA = 8;
00043     const STAGE_REVIEW = 16;
00044     const STAGE_PROCESS = 32;
00045     const STAGE_COMPLETE = 64;
00046 
00051     protected $stage = null;
00052 
00057     public static $stages = array(
00058         restore_ui::STAGE_CONFIRM       => 'confirm',
00059         restore_ui::STAGE_DESTINATION   => 'destination',
00060         restore_ui::STAGE_SETTINGS      => 'settings',
00061         restore_ui::STAGE_SCHEMA        => 'schema',
00062         restore_ui::STAGE_REVIEW        => 'review',
00063         restore_ui::STAGE_PROCESS       => 'process',
00064         restore_ui::STAGE_COMPLETE      => 'complete'
00065     );
00073     protected function initialise_stage($stage = null, array $params=null) {
00074         if ($stage == null) {
00075             $stage = optional_param('stage', self::STAGE_CONFIRM, PARAM_INT);
00076         }
00077         $class = 'restore_ui_stage_'.self::$stages[$stage];
00078         if (!class_exists($class)) {
00079             throw new restore_ui_exception('unknownuistage');
00080         }
00081         $stage = new $class($this, $params);
00082         return $stage;
00083     }
00088     public function process() {
00089         if ($this->progress >= self::PROGRESS_PROCESSED) {
00090             throw new restore_ui_exception('restoreuialreadyprocessed');
00091         }
00092         $this->progress = self::PROGRESS_PROCESSED;
00093 
00094         if (optional_param('previous', false, PARAM_BOOL) && $this->stage->get_stage() > self::STAGE_CONFIRM) {
00095             $this->stage = $this->initialise_stage($this->stage->get_prev_stage(), $this->stage->get_params());
00096             return false;
00097         }
00098 
00099         // Process the stage
00100         $processoutcome = $this->stage->process();
00101         if ($processoutcome !== false && !($this->get_stage()==self::STAGE_PROCESS && optional_param('substage', false, PARAM_BOOL))) {
00102             $this->stage = $this->initialise_stage($this->stage->get_next_stage(), $this->stage->get_params());
00103         }
00104 
00105         // Process UI event after to check changes are valid
00106         $this->controller->process_ui_event();
00107         return $processoutcome;
00108     }
00113     public function is_independent() {
00114         return false;
00115     }
00120     public function get_uniqueid() {
00121         return $this->get_restoreid();
00122     }
00127     public function get_restoreid() {
00128         return $this->controller->get_restoreid();
00129     }
00134     public function execute() {
00135         if ($this->progress >= self::PROGRESS_EXECUTED) {
00136             throw new restore_ui_exception('restoreuialreadyexecuted');
00137         }
00138         if ($this->stage->get_stage() < self::STAGE_PROCESS) {
00139             throw new restore_ui_exception('restoreuifinalisedbeforeexecute');
00140         }
00141         if ($this->controller->get_target() == backup::TARGET_CURRENT_DELETING || $this->controller->get_target() == backup::TARGET_EXISTING_DELETING) {
00142             $options = array();
00143             $options['keep_roles_and_enrolments'] = $this->get_setting_value('keep_roles_and_enrolments');
00144             $options['keep_groups_and_groupings'] = $this->get_setting_value('keep_groups_and_groupings');
00145             restore_dbops::delete_course_content($this->controller->get_courseid(), $options);
00146         }
00147         $this->controller->execute_plan();
00148         $this->progress = self::PROGRESS_EXECUTED;
00149         $this->stage = new restore_ui_stage_complete($this, $this->stage->get_params(), $this->controller->get_results());
00150         return true;
00151     }
00152 
00156     public function cleanup() {
00157         $courseid = $this->controller->get_courseid();
00158         if ($this->is_temporary_course_created($courseid)) {
00159             delete_course($courseid, false);
00160         }
00161     }
00162 
00168     protected function is_temporary_course_created($courseid) {
00169         global $DB;
00170         //Check if current controller instance has created new course.
00171         if ($this->controller->get_target() == backup::TARGET_NEW_COURSE) {
00172             $results = $DB->record_exists_sql("SELECT bc.itemid
00173                                                FROM {backup_controllers} bc, {course} c
00174                                                WHERE bc.operation = 'restore'
00175                                                  AND bc.type = 'course'
00176                                                  AND bc.itemid = c.id
00177                                                  AND bc.itemid = ?",
00178                                                array($courseid)
00179                                              );
00180             return $results;
00181         }
00182         return false;
00183     }
00184 
00189     public function enforce_changed_dependencies() {
00190         return ($this->dependencychanges > 0);
00191     }
00196     final public static function load_controller($restoreid=false) {
00197         // Get the restore id optional param
00198         if ($restoreid) {
00199             try {
00200                 // Try to load the controller with it.
00201                 // If it fails at this point it is likely because this is the first load
00202                 $controller = restore_controller::load_controller($restoreid);
00203                 return $controller;
00204             } catch (Exception $e) {
00205                 return false;
00206             }
00207         }
00208         return $restoreid;
00209     }
00217     final public static function engage_independent_stage($stage, $contextid) {
00218         if (!($stage & self::STAGE_CONFIRM + self::STAGE_DESTINATION)) {
00219             throw new restore_ui_exception('dependentstagerequested');
00220         }
00221         $class = 'restore_ui_stage_'.self::$stages[$stage];
00222         if (!class_exists($class)) {
00223             throw new restore_ui_exception('unknownuistage');
00224         }
00225         return new $class($contextid);
00226     }
00230     public function cancel_process() {
00231         //Delete temporary restore course if exists.
00232         if ($this->controller->get_target() == backup::TARGET_NEW_COURSE) {
00233             $this->cleanup();
00234         }
00235         parent::cancel_process();
00236     }
00241     public function get_progress_bar() {
00242         global $PAGE;
00243 
00244         $stage = self::STAGE_COMPLETE;
00245         $currentstage = $this->stage->get_stage();
00246         $items = array();
00247         while ($stage > 0) {
00248             $classes = array('backup_stage');
00249             if (floor($stage/2) == $currentstage) {
00250                 $classes[] = 'backup_stage_next';
00251             } else if ($stage == $currentstage) {
00252                 $classes[] = 'backup_stage_current';
00253             } else if ($stage < $currentstage) {
00254                 $classes[] = 'backup_stage_complete';
00255             }
00256             $item = array('text' => strlen(decbin($stage)).'. '.get_string('restorestage'.$stage, 'backup'),'class' => join(' ', $classes));
00257             if ($stage < $currentstage && $currentstage < self::STAGE_COMPLETE && $stage > self::STAGE_DESTINATION) {
00258                 $item['link'] = new moodle_url($PAGE->url, array('restore'=>$this->get_restoreid(), 'stage'=>$stage));
00259             }
00260             array_unshift($items, $item);
00261             $stage = floor($stage/2);
00262         }
00263         return $items;
00264     }
00269     public function get_name() {
00270         return 'restore';
00271     }
00276     public function get_first_stage_id() {
00277         return self::STAGE_CONFIRM;
00278     }
00283     public function requires_substage() {
00284         return ($this->stage->has_sub_stages() && !$this->stage->process());
00285     }
00292     public function display($renderer) {
00293         if ($this->progress < self::PROGRESS_SAVED) {
00294             throw new base_ui_exception('backupsavebeforedisplay');
00295         }
00296         return $this->stage->display($renderer);
00297     }
00298 }
00299 
00303 class restore_ui_exception extends base_ui_exception {}
 All Data Structures Namespaces Files Functions Variables Enumerations