|
Moodle
2.2.1
http://www.collinsharper.com
|
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 restore_section_task extends restore_task { 00032 00033 protected $info; // info related to section gathered from backup file 00034 protected $contextid; // course context id 00035 protected $sectionid; // new (target) id of the course section 00036 00040 public function __construct($name, $info, $plan = null) { 00041 $this->info = $info; 00042 $this->sectionid = 0; 00043 parent::__construct($name, $plan); 00044 } 00045 00049 public function get_taskbasepath() { 00050 00051 return $this->get_basepath() . '/sections/section_' . $this->info->sectionid; 00052 } 00053 00054 public function set_sectionid($sectionid) { 00055 $this->sectionid = $sectionid; 00056 } 00057 00058 public function get_contextid() { 00059 return $this->contextid; 00060 } 00061 00062 public function get_sectionid() { 00063 return $this->sectionid; 00064 } 00065 00069 public function build() { 00070 00071 // Define the task contextid (the course one) 00072 $this->contextid = get_context_instance(CONTEXT_COURSE, $this->get_courseid())->id; 00073 00074 // Executed conditionally if restoring to new course or deleting or if overwrite_conf setting is enabled 00075 if ($this->get_target() == backup::TARGET_NEW_COURSE || $this->get_target() == backup::TARGET_CURRENT_DELETING || 00076 $this->get_target() == backup::TARGET_EXISTING_DELETING || $this->get_setting_value('overwrite_conf') == true) { 00077 $this->add_step(new restore_section_structure_step('course_info', 'section.xml')); 00078 } 00079 00080 // At the end, mark it as built 00081 $this->built = true; 00082 } 00083 00088 public function execute() { 00089 00090 // Find activity_included_setting 00091 if (!$this->get_setting_value('included')) { 00092 $this->log('activity skipped by _included setting', backup::LOG_DEBUG, $this->name); 00093 00094 } else { // Setting tells us it's ok to execute 00095 parent::execute(); 00096 } 00097 } 00098 00104 public function get_setting($name) { 00105 $namewithprefix = 'section_' . $this->info->sectionid . '_' . $name; 00106 $result = null; 00107 foreach ($this->settings as $key => $setting) { 00108 if ($setting->get_name() == $namewithprefix) { 00109 if ($result != null) { 00110 throw new base_task_exception('multiple_settings_by_name_found', $namewithprefix); 00111 } else { 00112 $result = $setting; 00113 } 00114 } 00115 } 00116 if ($result) { 00117 return $result; 00118 } else { 00119 // Fallback to parent 00120 return parent::get_setting($name); 00121 } 00122 } 00123 00128 static public function define_decode_contents() { 00129 $contents = array(); 00130 00131 $contents[] = new restore_decode_content('course_sections', 'summary', 'course_section'); 00132 00133 return $contents; 00134 } 00135 00140 static public function define_decode_rules() { 00141 return array(); 00142 } 00143 00144 // Protected API starts here 00145 00149 protected function define_settings() { 00150 00151 // All the settings related to this activity will include this prefix 00152 $settingprefix = 'section_' . $this->info->sectionid . '_'; 00153 00154 // All these are common settings to be shared by all sections 00155 00156 // Define section_included (to decide if the whole task must be really executed) 00157 $settingname = $settingprefix . 'included'; 00158 $section_included = new restore_section_included_setting($settingname, base_setting::IS_BOOLEAN, true); 00159 if (is_number($this->info->title)) { 00160 $label = get_string('includesection', 'backup', $this->info->title); 00161 } else { 00162 $label = $this->info->title; 00163 } 00164 $section_included->get_ui()->set_label($label); 00165 $this->add_setting($section_included); 00166 00167 // Define section_userinfo. Dependent of: 00168 // - users root setting 00169 // - section_included setting 00170 $settingname = $settingprefix . 'userinfo'; 00171 $selectvalues = array(0=>get_string('no')); // Safer options 00172 $defaultvalue = false; // Safer default 00173 if (isset($this->info->settings[$settingname]) && $this->info->settings[$settingname]) { // Only enabled when available 00174 $selectvalues = array(1=>get_string('yes'), 0=>get_string('no')); 00175 $defaultvalue = true; 00176 } 00177 $section_userinfo = new restore_section_userinfo_setting($settingname, base_setting::IS_BOOLEAN, $defaultvalue); 00178 $section_userinfo->set_ui(new backup_setting_ui_select($section_userinfo, get_string('includeuserinfo','backup'), $selectvalues)); 00179 $this->add_setting($section_userinfo); 00180 // Look for "users" root setting 00181 $users = $this->plan->get_setting('users'); 00182 $users->add_dependency($section_userinfo); 00183 // Look for "section_included" section setting 00184 $section_included->add_dependency($section_userinfo); 00185 } 00186 }