source: moodle/trunk/fuentes/backup/util/ui/base_ui.class.php @ 1331

Last change on this file since 1331 was 1331, checked in by jrpelegrina, 3 years ago

Updated to moodle 3.0.3

File size: 10.6 KB
RevLine 
[136]1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * This file contains the backup user interface class
19 *
[1331]20 * @package   core_backup
[136]21 * @copyright 2010 Sam Hemelryk
22 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25/**
26 * This is the backup user interface class
27 *
28 * The backup user interface class manages the user interface and backup for
29 * Moodle.
30 *
[1331]31 * @package   core_backup
[136]32 * @copyright 2010 Sam Hemelryk
33 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
35abstract class base_ui {
36    /**
37     * The progress of this instance of the backup ui class
[1331]38     * It is in the initial stage.
[136]39     */
40    const PROGRESS_INTIAL = 0;
[1331]41
42    /**
43     * The progress of this instance of the backup ui class
44     * It is processed.
45     */
[136]46    const PROGRESS_PROCESSED = 1;
[1331]47
48    /**
49     * The progress of this instance of the backup ui class
50     * It is saved.
51     */
[136]52    const PROGRESS_SAVED = 2;
[1331]53
54    /**
55     * The progress of this instance of the backup ui class
56     * It has been executed.
57     */
[136]58    const PROGRESS_EXECUTED = 3;
[1331]59
[136]60    /**
61     * The controller
62     * @var backup_controller|restore_controller
63     */
64    protected $controller;
[1331]65
[136]66    /**
67     * The current stage
68     * @var base_ui_stage
69     */
70    protected $stage;
[1331]71
[136]72    /**
73     * The current progress of the UI
74     * @var int One of self::PROGRESS_*
75     */
76    protected $progress;
[1331]77
[136]78    /**
79     * The number of changes made by dependency enforcement
80     * @var int
81     */
82    protected $dependencychanges = 0;
83
84    /**
85     * Yay for constructors
86     * @param backup_controller $controller
[1331]87     * @param array $params
[136]88     */
[1331]89    public function __construct($controller, array $params = null) {
[136]90        $this->controller = $controller;
91        $this->progress = self::PROGRESS_INTIAL;
92        $this->stage = $this->initialise_stage(null, $params);
[1331]93        // Process UI event before to be safe.
[136]94        $this->controller->process_ui_event();
95    }
[1331]96
[136]97    /**
98     * Destorys the backup controller and the loaded stage.
99     */
100    public function destroy() {
101
102        if ($this->controller) {
103            $this->controller->destroy();
104        }
105        unset($this->stage);
106
107    }
[1331]108
[136]109    /**
110     * Intialises what ever stage is requested. If none are requested we check
111     * params for 'stage' and default to initial
112     *
113     * @param int|null $stage The desired stage to intialise or null for the default
[1331]114     * @param array $params
[136]115     * @return base_ui_stage
116     */
[1331]117    abstract protected function initialise_stage($stage = null, array $params = null);
118
[136]119    /**
120     * This processes the current stage of the backup
[1331]121     * @throws backup_ui_exception
[136]122     * @return bool
123     */
124    public function process() {
125        if ($this->progress >= self::PROGRESS_PROCESSED) {
126            throw new backup_ui_exception('backupuialreadyprocessed');
127        }
128        $this->progress = self::PROGRESS_PROCESSED;
129
130        if (optional_param('previous', false, PARAM_BOOL) && $this->stage->get_stage() > $this->get_first_stage_id()) {
131            $this->stage = $this->initialise_stage($this->stage->get_prev_stage(), $this->stage->get_params());
132            return false;
133        }
134
[1331]135        // Process the stage.
[136]136        $processoutcome = $this->stage->process();
137
138        if ($processoutcome !== false) {
139            $this->stage = $this->initialise_stage($this->stage->get_next_stage(), $this->stage->get_params());
140        }
141
[1331]142        // Process UI event after to check changes are valid.
[136]143        $this->controller->process_ui_event();
144        return $processoutcome;
145    }
[1331]146
[136]147    /**
148     * Saves the backup controller.
149     *
150     * Once this has been called nothing else can be changed in the controller.
151     *
[1331]152     * @throws base_ui_exception
[136]153     * @return bool
154     */
155    public function save_controller() {
156        if ($this->progress >= self::PROGRESS_SAVED) {
157            throw new base_ui_exception('backupuialreadysaved');
158        }
159        $this->progress = self::PROGRESS_SAVED;
[1331]160        // First enforce dependencies.
[136]161        $this->enforce_dependencies();
[1331]162        // Process UI event after to check any changes are valid.
[136]163        $this->controller->process_ui_event();
[1331]164        // Save the controller.
[136]165        $this->controller->save_controller();
166        return true;
167    }
[1331]168
[136]169    /**
170     * Displays the UI for the backup!
171     *
[1331]172     * @throws base_ui_exception
[136]173     * @param core_backup_renderer $renderer
174     * @return string HTML code to echo
175     */
176    public function display(core_backup_renderer $renderer) {
177        if ($this->progress < self::PROGRESS_SAVED) {
178            throw new base_ui_exception('backupsavebeforedisplay');
179        }
180        return $this->stage->display($renderer);
181    }
[1331]182
[136]183    /**
184     * Gets all backup tasks from the controller
185     * @return array Array of backup_task
186     */
187    public function get_tasks() {
188        $plan = $this->controller->get_plan();
189        $tasks = $plan->get_tasks();
190        return $tasks;
191    }
[1331]192
[136]193    /**
194     * Gets the stage we are on
195     * @return int
196     */
197    public function get_stage() {
198        return $this->stage->get_stage();
199    }
[1331]200
[136]201    /**
202     * Gets the name of the stage we are on
203     * @return string
204     */
205    public function get_stage_name() {
206        return $this->stage->get_name();
207    }
[1331]208
[136]209    /**
210     * Gets the backup id from the controller
211     * @return string
212     */
213    abstract public function get_uniqueid();
[1331]214
[136]215    /**
216     * Executes the backup plan
217     * @return bool
218     */
219    abstract public function execute();
[1331]220
[136]221    /**
222     * Enforces dependencies on all settings. Call before save
223     * @return bool True if dependencies were enforced and changes were made
224     */
225    protected function enforce_dependencies() {
[1331]226        // Get the plan.
[136]227        $plan = $this->controller->get_plan();
[1331]228        // Get the tasks as a var so we can iterate by reference.
[136]229        $tasks = $plan->get_tasks();
230        $changes = 0;
231        foreach ($tasks as &$task) {
[1331]232            // Store as a var so we can iterate by reference.
[136]233            $settings = $task->get_settings();
234            foreach ($settings as &$setting) {
[1331]235                // Get all dependencies for iteration by reference.
[136]236                $dependencies = $setting->get_dependencies();
237                foreach ($dependencies as &$dependency) {
[1331]238                    // Enforce each dependency.
[136]239                    if ($dependency->enforce()) {
240                        $changes++;
241                    }
242                }
243            }
244        }
[1331]245        // Store the number of settings that changed through enforcement.
[136]246        $this->dependencychanges = $changes;
[1331]247        return ($changes > 0);
[136]248    }
[1331]249
[136]250    /**
251     * Returns true if enforce_dependencies changed any settings
252     * @return bool
253     */
254    public function enforce_changed_dependencies() {
255        return ($this->dependencychanges > 0);
256    }
[1331]257
[136]258    /**
259     * Loads the backup controller if we are tracking one
[1331]260     * @throws coding_exception
261     * @param string|bool $uniqueid
[136]262     * @return backup_controller|false
263     */
[1331]264    public static function load_controller($uniqueid = false) {
[136]265        throw new coding_exception('load_controller() method needs to be overridden in each subclass of base_ui');
266    }
267
268    /**
269     * Cancels the current backup/restore and redirects the user back to the relevant place
270     */
271    public function cancel_process() {
272        global $PAGE;
[1331]273        // Determine the appropriate URL to redirect the user to.
[136]274        if ($PAGE->context->contextlevel == CONTEXT_MODULE && $PAGE->cm !== null) {
[1331]275            $relevanturl = new moodle_url('/mod/'.$PAGE->cm->modname.'/view.php', array('id' => $PAGE->cm->id));
[136]276        } else {
[1331]277            $relevanturl = new moodle_url('/course/view.php', array('id' => $PAGE->course->id));
[136]278        }
279        redirect($relevanturl);
280    }
281
282    /**
283     * Gets an array of progress bar items that can be displayed through the backup renderer.
284     * @return array Array of items for the progress bar
285     */
286    abstract public function get_progress_bar();
287    /**
288     * Gets the format for the backup
289     * @return int
290     */
291    public function get_format() {
292        return $this->controller->get_format();
293    }
294    /**
295     * Gets the type of the backup
296     * @return int
297     */
298    public function get_type() {
299        return $this->controller->get_type();
300    }
[1331]301
302    /**
303     * Returns the controller object.
304     * @return backup_controller|restore_controller
305     */
[136]306    public function get_controller() {
307        return $this->controller;
308    }
309    /**
310     * Gets the ID used in creating the controller. Relates to course/section/cm
311     * @return int
312     */
313    public function get_controller_id() {
314        return $this->controller->get_id();
315    }
316    /**
317     * Gets the requested setting
318     * @param string $name
[1331]319     * @param bool $default
[136]320     * @return mixed
321     */
322    public function get_setting($name, $default = false) {
323        try {
324            return $this->controller->get_plan()->get_setting($name);
325        } catch (Exception $e) {
326            debugging('Failed to find the setting: '.$name, DEBUG_DEVELOPER);
327            return $default;
328        }
329    }
330    /**
331     * Gets the value for the requested setting
332     *
333     * @param string $name
[1331]334     * @param bool $default
[136]335     * @return mixed
336     */
337    public function get_setting_value($name, $default = false) {
338        try {
339            return $this->controller->get_plan()->get_setting($name)->get_value();
340        } catch (Exception $e) {
341            debugging('Failed to find the setting: '.$name, DEBUG_DEVELOPER);
342            return $default;
343        }
344    }
345
[1331]346    /**
347     * Returns the name of this stage.
348     * @return mixed
349     */
[136]350    abstract public function get_name();
351
[1331]352    /**
353     * Returns the first stage ID.
354     * @return mixed
355     */
[136]356    abstract public function get_first_stage_id();
357}
358
359/**
360 * Backup user interface exception. Modelled off the backup_exception class
[1331]361 *
362 * @package   core_backup
363 * @copyright 2010 Sam Hemelryk
364 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
[136]365 */
366class base_ui_exception extends backup_exception {}
Note: See TracBrowser for help on using the repository browser.