Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/backup/util/plan/backup_structure_step.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 
00030 abstract class backup_structure_step extends backup_step {
00031 
00032     protected $filename; // Name of the file to be generated
00033     protected $contenttransformer; // xml content transformer being used
00034                                    // (need it here, apart from xml_writer,
00035                                    // thanks to serialized data to process -
00036                                    // say thanks to blocks!)
00037 
00041     public function __construct($name, $filename, $task = null) {
00042         if (!is_null($task) && !($task instanceof backup_task)) {
00043             throw new backup_step_exception('wrong_backup_task_specified');
00044         }
00045         $this->filename = $filename;
00046         $this->contenttransformer = null;
00047         parent::__construct($name, $task);
00048     }
00049 
00050     public function execute() {
00051 
00052         if (!$this->execute_condition()) { // Check any condition to execute this
00053             return;
00054         }
00055 
00056         $fullpath = $this->task->get_taskbasepath();
00057 
00058         // We MUST have one fullpath here, else, error
00059         if (empty($fullpath)) {
00060             throw new backup_step_exception('backup_structure_step_undefined_fullpath');
00061         }
00062 
00063         // Append the filename to the fullpath
00064         $fullpath = rtrim($fullpath, '/') . '/' . $this->filename;
00065 
00066         // Create output, transformer, writer, processor
00067         $xo = new file_xml_output($fullpath);
00068         $xt = null;
00069         if (class_exists('backup_xml_transformer')) {
00070             $xt = new backup_xml_transformer($this->get_courseid());
00071             $this->contenttransformer = $xt; // Save the reference to the transformer
00072                                              // as far as we are going to need it out
00073                                              // from xml_writer (blame serialized data!)
00074         }
00075         $xw = new xml_writer($xo, $xt);
00076         $pr = new backup_structure_processor($xw);
00077 
00078         // Set processor variables from settings
00079         foreach ($this->get_settings() as $setting) {
00080             $pr->set_var($setting->get_name(), $setting->get_value());
00081         }
00082         // Add backupid as one more var for processor
00083         $pr->set_var(backup::VAR_BACKUPID, $this->get_backupid());
00084 
00085         // Get structure definition
00086         $structure = $this->define_structure();
00087         if (! $structure instanceof backup_nested_element) {
00088             throw new backup_step_exception('backup_structure_step_wrong_structure');
00089         }
00090 
00091         // Start writer
00092         $xw->start();
00093 
00094         // Process structure definition
00095         $structure->process($pr);
00096 
00097         // Close everything
00098         $xw->stop();
00099 
00100         // Destroy the structure. It helps PHP 5.2 memory a lot!
00101         $structure->destroy();
00102     }
00103 
00108     public function get_task() {
00109         return $this->task;
00110     }
00111 
00112 // Protected API starts here
00113 
00123     protected function add_plugin_structure($plugintype, $element, $multiple) {
00124 
00125         global $CFG;
00126 
00127         // Check the requested plugintype is a valid one
00128         if (!array_key_exists($plugintype, get_plugin_types($plugintype))) {
00129              throw new backup_step_exception('incorrect_plugin_type', $plugintype);
00130         }
00131 
00132         // Arrived here, plugin is correct, let's create the optigroup
00133         $optigroupname = $plugintype . '_' . $element->get_name() . '_plugin';
00134         $optigroup = new backup_optigroup($optigroupname, null, $multiple);
00135         $element->add_child($optigroup); // Add optigroup to stay connected since beginning
00136 
00137         // Get all the optigroup_elements, looking across all the plugin dirs
00138         $pluginsdirs = get_plugin_list($plugintype);
00139         foreach ($pluginsdirs as $name => $plugindir) {
00140             $classname = 'backup_' . $plugintype . '_' . $name . '_plugin';
00141             $backupfile = $plugindir . '/backup/moodle2/' . $classname . '.class.php';
00142             if (file_exists($backupfile)) {
00143                 require_once($backupfile);
00144                 $backupplugin = new $classname($plugintype, $name, $optigroup, $this);
00145                 // Add plugin returned structure to optigroup
00146                 $backupplugin->define_plugin_structure($element->get_name());
00147             }
00148         }
00149     }
00150 
00159     protected function execute_condition() {
00160         return true;
00161     }
00162 
00167     abstract protected function define_structure();
00168 }
 All Data Structures Namespaces Files Functions Variables Enumerations