Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/backup/moodle2/backup_section_task.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 
00031 class backup_section_task extends backup_task {
00032 
00033     protected $sectionid;
00034 
00038     public function __construct($name, $sectionid, $plan = null) {
00039         global $DB;
00040 
00041         // Check section exists
00042         if (!$section = $DB->get_record('course_sections', array('id' => $sectionid))) {
00043             throw new backup_task_exception('section_task_section_not_found', $sectionid);
00044         }
00045 
00046         $this->sectionid  = $sectionid;
00047 
00048         parent::__construct($name, $plan);
00049     }
00050 
00051     public function get_sectionid() {
00052         return $this->sectionid;
00053     }
00054 
00058     public function get_taskbasepath() {
00059 
00060         return $this->get_basepath() . '/sections/section_' . $this->sectionid;
00061     }
00062 
00066     public function build() {
00067 
00068         // Set the backup::VAR_CONTEXTID setting to course context as far as next steps require that
00069         $coursectxid = get_context_instance(CONTEXT_COURSE, $this->get_courseid())->id;
00070         $this->add_setting(new backup_activity_generic_setting(backup::VAR_CONTEXTID, base_setting::IS_INTEGER, $coursectxid));
00071 
00072         // Add some extra settings that related processors are going to need
00073         $this->add_setting(new backup_activity_generic_setting(backup::VAR_SECTIONID, base_setting::IS_INTEGER, $this->sectionid));
00074         $this->add_setting(new backup_activity_generic_setting(backup::VAR_COURSEID, base_setting::IS_INTEGER, $this->get_courseid()));
00075 
00076         // Create the section directory
00077         $this->add_step(new create_taskbasepath_directory('create_section_directory'));
00078 
00079         // Create the section.xml common file (course_sections)
00080         $this->add_step(new backup_section_structure_step('section_commons', 'section.xml'));
00081 
00082         // Generate the inforef file (must be after ALL steps gathering annotations of ANY type)
00083         $this->add_step(new backup_inforef_structure_step('section_inforef', 'inforef.xml'));
00084 
00085         // Migrate the already exported inforef entries to final ones
00086         $this->add_step(new move_inforef_annotations_to_final('migrate_inforef'));
00087 
00088         // At the end, mark it as built
00089         $this->built = true;
00090     }
00091 
00096     public function execute() {
00097 
00098         // Find section_included_setting
00099         if (!$this->get_setting_value('included')) {
00100             $this->log('section skipped by _included setting', backup::LOG_DEBUG, $this->name);
00101 
00102         } else { // Setting tells us it's ok to execute
00103             parent::execute();
00104         }
00105     }
00106 
00112     public function get_setting($name) {
00113         $namewithprefix = 'section_' . $this->sectionid . '_' . $name;
00114         $result = null;
00115         foreach ($this->settings as $key => $setting) {
00116             if ($setting->get_name() == $namewithprefix) {
00117                 if ($result != null) {
00118                     throw new base_task_exception('multiple_settings_by_name_found', $namewithprefix);
00119                 } else {
00120                     $result = $setting;
00121                 }
00122             }
00123         }
00124         if ($result) {
00125             return $result;
00126         } else {
00127             // Fallback to parent
00128             return parent::get_setting($name);
00129         }
00130     }
00131 
00132 // Protected API starts here
00133 
00137     protected function define_settings() {
00138         global $DB;
00139 
00140         // All the settings related to this activity will include this prefix
00141         $settingprefix = 'section_' . $this->sectionid . '_';
00142 
00143         // All these are common settings to be shared by all sections
00144 
00145         $section = $DB->get_record('course_sections', array('id' => $this->sectionid), '*', MUST_EXIST);
00146         $course = $DB->get_record('course', array('id' => $section->course), '*', MUST_EXIST);
00147 
00148         // Define section_included (to decide if the whole task must be really executed)
00149         $settingname = $settingprefix . 'included';
00150         $section_included = new backup_section_included_setting($settingname, base_setting::IS_BOOLEAN, true);
00151         $section_included->get_ui()->set_label(get_section_name($course, $section));
00152         $this->add_setting($section_included);
00153 
00154         // Define section_userinfo. Dependent of:
00155         // - users root setting
00156         // - section_included setting
00157         $settingname = $settingprefix . 'userinfo';
00158         $section_userinfo = new backup_section_userinfo_setting($settingname, base_setting::IS_BOOLEAN, true);
00159         $section_userinfo->get_ui()->set_label(get_string('includeuserinfo','backup'));
00160         $this->add_setting($section_userinfo);
00161         // Look for "users" root setting
00162         $users = $this->plan->get_setting('users');
00163         $users->add_dependency($section_userinfo);
00164         // Look for "section_included" section setting
00165         $section_included->add_dependency($section_userinfo);
00166     }
00167 }
 All Data Structures Namespaces Files Functions Variables Enumerations