Merge branch 'MDL-66273-MOODLE-311-v2' of https://github.com/golenkovm/moodle into...
[moodle.git] / backup / moodle2 / backup_section_task.class.php
blobb84d3388cd33acb715cc293375adb6a3d167fed7
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Defines backup_section_task class
21 * @package core_backup
22 * @subpackage moodle2
23 * @category backup
24 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 defined('MOODLE_INTERNAL') || die();
30 /**
31 * section task that provides all the properties and common steps to be performed
32 * when one section is being backup
34 * TODO: Finish phpdocs
36 class backup_section_task extends backup_task {
38 protected $sectionid;
40 /**
41 * Constructor - instantiates one object of this class
43 public function __construct($name, $sectionid, $plan = null) {
44 global $DB;
46 // Check section exists
47 if (!$section = $DB->get_record('course_sections', array('id' => $sectionid))) {
48 throw new backup_task_exception('section_task_section_not_found', $sectionid);
51 $this->sectionid = $sectionid;
53 parent::__construct($name, $plan);
56 public function get_sectionid() {
57 return $this->sectionid;
60 /**
61 * Section tasks have their own directory to write files
63 public function get_taskbasepath() {
65 return $this->get_basepath() . '/sections/section_' . $this->sectionid;
68 /**
69 * Create all the steps that will be part of this task
71 public function build() {
73 // Set the backup::VAR_CONTEXTID setting to course context as far as next steps require that
74 $coursectxid = context_course::instance($this->get_courseid())->id;
75 $this->add_setting(new backup_activity_generic_setting(backup::VAR_CONTEXTID, base_setting::IS_INTEGER, $coursectxid));
77 // Add some extra settings that related processors are going to need
78 $this->add_setting(new backup_activity_generic_setting(backup::VAR_SECTIONID, base_setting::IS_INTEGER, $this->sectionid));
79 $this->add_setting(new backup_activity_generic_setting(backup::VAR_COURSEID, base_setting::IS_INTEGER, $this->get_courseid()));
81 // Create the section directory
82 $this->add_step(new create_taskbasepath_directory('create_section_directory'));
84 // Create the section.xml common file (course_sections)
85 $this->add_step(new backup_section_structure_step('section_commons', 'section.xml'));
87 // Generate the inforef file (must be after ALL steps gathering annotations of ANY type)
88 $this->add_step(new backup_inforef_structure_step('section_inforef', 'inforef.xml'));
90 // Migrate the already exported inforef entries to final ones
91 $this->add_step(new move_inforef_annotations_to_final('migrate_inforef'));
93 // At the end, mark it as built
94 $this->built = true;
97 /**
98 * Exceptionally override the execute method, so, based in the section_included setting, we are able
99 * to skip the execution of one task completely
101 public function execute() {
103 // Find section_included_setting
104 if (!$this->get_setting_value('included')) {
105 $this->log('section skipped by _included setting', backup::LOG_DEBUG, $this->name);
107 } else { // Setting tells us it's ok to execute
108 parent::execute();
113 * Specialisation that, first of all, looks for the setting within
114 * the task with the the prefix added and later, delegates to parent
115 * without adding anything
117 public function get_setting($name) {
118 $namewithprefix = 'section_' . $this->sectionid . '_' . $name;
119 $result = null;
120 foreach ($this->settings as $key => $setting) {
121 if ($setting->get_name() == $namewithprefix) {
122 if ($result != null) {
123 throw new base_task_exception('multiple_settings_by_name_found', $namewithprefix);
124 } else {
125 $result = $setting;
129 if ($result) {
130 return $result;
131 } else {
132 // Fallback to parent
133 return parent::get_setting($name);
137 // Protected API starts here
140 * Define the common setting that any backup section will have
142 protected function define_settings() {
143 global $DB;
145 // All the settings related to this activity will include this prefix
146 $settingprefix = 'section_' . $this->sectionid . '_';
148 // All these are common settings to be shared by all sections
150 $section = $DB->get_record('course_sections', array('id' => $this->sectionid), '*', MUST_EXIST);
151 $course = $DB->get_record('course', array('id' => $section->course), '*', MUST_EXIST);
153 // Define section_included (to decide if the whole task must be really executed)
154 $settingname = $settingprefix . 'included';
155 $section_included = new backup_section_included_setting($settingname, base_setting::IS_BOOLEAN, true);
156 $section_included->get_ui()->set_label(get_section_name($course, $section));
157 $this->add_setting($section_included);
159 // Define section_userinfo. Dependent of:
160 // - users root setting
161 // - section_included setting
162 $settingname = $settingprefix . 'userinfo';
163 $section_userinfo = new backup_section_userinfo_setting($settingname, base_setting::IS_BOOLEAN, true);
164 $section_userinfo->get_ui()->set_label(get_string('includeuserinfo','backup'));
165 $this->add_setting($section_userinfo);
166 // Look for "users" root setting
167 $users = $this->plan->get_setting('users');
168 $users->add_dependency($section_userinfo);
169 // Look for "section_included" section setting
170 $section_included->add_dependency($section_userinfo);