. /** * @package moodlecore * @subpackage backup-moodle2 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ /** * section task that provides all the properties and common steps to be performed * when one section is being restored * * TODO: Finish phpdocs */ class restore_section_task extends restore_task { protected $info; // info related to section gathered from backup file /** * Constructor - instantiates one object of this class */ public function __construct($name, $info, $plan = null) { $this->info = $info; parent::__construct($name, $plan); } /** * Section tasks have their own directory to read files */ public function get_taskbasepath() { return $this->get_basepath() . '/sections/section_' . $this->info->sectionid; } /** * Create all the steps that will be part of this task */ public function build() { // TODO: Link all the section steps here // At the end, mark it as built $this->built = true; } /** * Exceptionally override the execute method, so, based in the section_included setting, we are able * to skip the execution of one task completely */ public function execute() { // Find activity_included_setting if (!$this->get_setting_value('included')) { $this->log('activity skipped by _included setting', backup::LOG_DEBUG, $this->name); } else { // Setting tells us it's ok to execute parent::execute(); } } /** * Specialisation that, first of all, looks for the setting within * the task with the the prefix added and later, delegates to parent * without adding anything */ public function get_setting($name) { $namewithprefix = 'section_' . $this->info->sectionid . '_' . $name; $result = null; foreach ($this->settings as $key => $setting) { if ($setting->get_name() == $namewithprefix) { if ($result != null) { throw new base_task_exception('multiple_settings_by_name_found', $namewithprefix); } else { $result = $setting; } } } if ($result) { return $result; } else { // Fallback to parent return parent::get_setting($name); } } // Protected API starts here /** * Define the common setting that any restore section will have */ protected function define_settings() { // All the settings related to this activity will include this prefix $settingprefix = 'section_' . $this->info->sectionid . '_'; // All these are common settings to be shared by all sections // Define section_included (to decide if the whole task must be really executed) $settingname = $settingprefix . 'included'; $section_included = new restore_section_included_setting($settingname, base_setting::IS_BOOLEAN, true); $this->add_setting($section_included); // Define section_userinfo. Dependent of: // - users root setting // - section_included setting $settingname = $settingprefix . 'userinfo'; $selectvalues = array(0=>get_string('no')); // Safer options $defaultvalue = false; // Safer default if (isset($info->settings[$settingname]) && $info->settings[$settingname]) { // Only enabled when available $selectvalues = array(1=>get_string('yes'), 0=>get_string('no')); $defaultvalue = true; } $section_userinfo = new restore_section_userinfo_setting($settingname, base_setting::IS_BOOLEAN, $defaultvalue); $section_userinfo->set_ui(new backup_setting_ui_select($section_userinfo, get_string('includeuserinfo','backup'), $selectvalues)); $this->add_setting($section_userinfo); // Look for "users" root setting $users = $this->plan->get_setting('users'); $users->add_dependency($section_userinfo); // Look for "section_included" section setting $section_included->add_dependency($section_userinfo); } }