MDL-53700 competency: Migrating backup logic to core
[moodle.git] / backup / moodle2 / restore_course_task.class.php
CommitLineData
864842aa
EL
1<?php
2
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.
14//
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/>.
17
18/**
f9dab4be
DM
19 * Defines restore_course_task class
20 *
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
864842aa
EL
26 */
27
f9dab4be
DM
28defined('MOODLE_INTERNAL') || die();
29
864842aa
EL
30/**
31 * course task that provides all the properties and common steps to be performed
32 * when one course is being restored
33 *
34 * TODO: Finish phpdocs
35 */
36class restore_course_task extends restore_task {
37
38 protected $info; // info related to course gathered from backup file
024c288d 39 protected $contextid; // course context id
864842aa
EL
40
41 /**
42 * Constructor - instantiates one object of this class
43 */
44 public function __construct($name, $info, $plan = null) {
45 $this->info = $info;
46 parent::__construct($name, $plan);
47 }
48
49 /**
50 * Course tasks have their own directory to read files
51 */
52 public function get_taskbasepath() {
53
54 return $this->get_basepath() . '/course';
55 }
56
024c288d
EL
57 public function get_contextid() {
58 return $this->contextid;
59 }
60
864842aa
EL
61 /**
62 * Create all the steps that will be part of this task
63 */
64 public function build() {
65
024c288d 66 // Define the task contextid (the course one)
a689cd1d 67 $this->contextid = context_course::instance($this->get_courseid())->id;
024c288d 68
cfea745e 69 // Executed conditionally if restoring to new course or if overwrite_conf setting is enabled
482aac65
EL
70 if ($this->get_target() == backup::TARGET_NEW_COURSE || $this->get_setting_value('overwrite_conf') == true) {
71 $this->add_step(new restore_course_structure_step('course_info', 'course.xml'));
72 }
864842aa 73
c9deaa8e
EM
74 $this->add_step(new restore_course_legacy_files_step('legacy_files'));
75
4b75f49a
PS
76 // Deal with enrolment methods and user enrolments.
77 if ($this->plan->get_mode() == backup::MODE_IMPORT) {
78 // No need to do anything with enrolments.
79
80 } else if (!$this->get_setting_value('users') or $this->plan->get_mode() == backup::MODE_HUB) {
81 if ($this->get_target() == backup::TARGET_CURRENT_ADDING or $this->get_target() == backup::TARGET_EXISTING_ADDING) {
82 // Keep current enrolments unchanged.
83 } else {
84 // If no instances yet add default enrol methods the same way as when creating new course in UI.
85 $this->add_step(new restore_default_enrolments_step('default_enrolments'));
86 }
87
88 } else {
89 // Restore course enrolment data.
9ab5df6b
EL
90 $this->add_step(new restore_enrolments_structure_step('course_enrolments', 'enrolments.xml'));
91 }
024c288d 92
7881024e
PS
93 // Populate groups, this must be done after enrolments because only enrolled users may be in groups.
94 $this->add_step(new restore_groups_members_structure_step('create_groups_members', '../groups.xml'));
95
7a7b8a1f
PS
96 // Restore course role assignments and overrides (internally will observe the role_assignments setting),
97 // this must be done after all users are enrolled.
98 $this->add_step(new restore_ras_and_caps_structure_step('course_ras_and_caps', 'roles.xml'));
99
21e51c86
EL
100 // Restore course filters (conditionally)
101 if ($this->get_setting_value('filters')) {
102 $this->add_step(new restore_filters_structure_step('course_filters', 'filters.xml'));
103 }
104
105 // Restore course comments (conditionally)
106 if ($this->get_setting_value('comments')) {
107 $this->add_step(new restore_comments_structure_step('course_comments', 'comments.xml'));
108 }
109
8331a159
AA
110 // Calendar events (conditionally)
111 if ($this->get_setting_value('calendarevents')) {
112 $this->add_step(new restore_calendarevents_structure_step('course_calendar', 'calendar.xml'));
113 }
114
8417c3a6
FM
115 // Course competencies.
116 $this->add_step(new restore_course_competencies_structure_step('course_competencies', 'competencies.xml'));
117
864842aa
EL
118 // At the end, mark it as built
119 $this->built = true;
120 }
121
122 /**
5ab8d2de
EL
123 * Define the contents in the course that must be
124 * processed by the link decoder
864842aa 125 */
5ab8d2de
EL
126 static public function define_decode_contents() {
127 $contents = array();
864842aa 128
5ab8d2de 129 $contents[] = new restore_decode_content('course', 'summary');
bc213518 130 $contents[] = new restore_decode_content('event', 'description');
5ab8d2de
EL
131
132 return $contents;
133 }
134
135 /**
136 * Define the decoding rules for links belonging
137 * to the course to be executed by the link decoder
138 */
139 static public function define_decode_rules() {
140 $rules = array();
141
3d1c4e18
TH
142 // Link to the course main page (it also covers "&topic=xx" and "&week=xx"
143 // because they don't become transformed (section number) in backup/restore.
144 $rules[] = new restore_decode_rule('COURSEVIEWBYID', '/course/view.php?id=$1', 'course');
5ab8d2de 145
3d1c4e18
TH
146 // A few other key course links.
147 $rules[] = new restore_decode_rule('GRADEINDEXBYID', '/grade/index.php?id=$1', 'course');
148 $rules[] = new restore_decode_rule('GRADEREPORTINDEXBYID', '/grade/report/index.php?id=$1', 'course');
149 $rules[] = new restore_decode_rule('BADGESVIEWBYID', '/badges/view.php?type=2&id=$1', 'course');
150 $rules[] = new restore_decode_rule('USERINDEXVIEWBYID', '/user/index.php?id=$1', 'course');
864842aa 151
3d1c4e18 152 return $rules;
864842aa
EL
153 }
154
155// Protected API starts here
156
157 /**
482aac65 158 * Define the common setting that any restore course will have
864842aa
EL
159 */
160 protected function define_settings() {
161
785d6603
SH
162 //$name, $vtype, $value = null, $visibility = self::VISIBLE, $status = self::NOT_LOCKED
163 $fullname = new restore_course_generic_text_setting('course_fullname', base_setting::IS_TEXT, $this->get_info()->original_course_fullname);
164 $fullname->get_ui()->set_label(get_string('setting_course_fullname', 'backup'));
165 $this->add_setting($fullname);
166
167 $shortname = new restore_course_generic_text_setting('course_shortname', base_setting::IS_TEXT, $this->get_info()->original_course_shortname);
168 $shortname->get_ui()->set_label(get_string('setting_course_shortname', 'backup'));
169 $this->add_setting($shortname);
170
171 $startdate = new restore_course_generic_text_setting('course_startdate', base_setting::IS_INTEGER, $this->get_info()->original_course_startdate);
172 $startdate->set_ui(new backup_setting_ui_dateselector($startdate, get_string('setting_course_startdate', 'backup')));
173 $this->add_setting($startdate);
174
1f238ad4
PS
175 $keep_enrols = new restore_course_generic_setting('keep_roles_and_enrolments', base_setting::IS_BOOLEAN, false);
176 $keep_enrols->set_ui(new backup_setting_ui_select($keep_enrols, $keep_enrols->get_name(), array(1=>get_string('yes'), 0=>get_string('no'))));
177 $keep_enrols->get_ui()->set_label(get_string('setting_keep_roles_and_enrolments', 'backup'));
d53e3298 178 if ($this->get_target() != backup::TARGET_CURRENT_DELETING and $this->get_target() != backup::TARGET_EXISTING_DELETING) {
1f238ad4
PS
179 $keep_enrols->set_value(false);
180 $keep_enrols->set_status(backup_setting::LOCKED_BY_CONFIG);
181 $keep_enrols->set_visibility(backup_setting::HIDDEN);
d53e3298 182 }
1f238ad4 183 $this->add_setting($keep_enrols);
d53e3298 184
1f238ad4
PS
185 $keep_groups = new restore_course_generic_setting('keep_groups_and_groupings', base_setting::IS_BOOLEAN, false);
186 $keep_groups->set_ui(new backup_setting_ui_select($keep_groups, $keep_groups->get_name(), array(1=>get_string('yes'), 0=>get_string('no'))));
187 $keep_groups->get_ui()->set_label(get_string('setting_keep_groups_and_groupings', 'backup'));
d53e3298 188 if ($this->get_target() != backup::TARGET_CURRENT_DELETING and $this->get_target() != backup::TARGET_EXISTING_DELETING) {
1f238ad4
PS
189 $keep_groups->set_value(false);
190 $keep_groups->set_status(backup_setting::LOCKED_BY_CONFIG);
191 $keep_groups->set_visibility(backup_setting::HIDDEN);
d53e3298 192 }
1f238ad4 193 $this->add_setting($keep_groups);
d53e3298 194
864842aa
EL
195 // Define overwrite_conf to decide if course configuration will be restored over existing one
196 $overwrite = new restore_course_overwrite_conf_setting('overwrite_conf', base_setting::IS_BOOLEAN, false);
197 $overwrite->set_ui(new backup_setting_ui_select($overwrite, $overwrite->get_name(), array(1=>get_string('yes'), 0=>get_string('no'))));
785d6603 198 $overwrite->get_ui()->set_label(get_string('setting_overwriteconf', 'backup'));
7f32340b 199 if ($this->get_target() == backup::TARGET_NEW_COURSE) {
39bc4c6f
SH
200 $overwrite->set_value(true);
201 $overwrite->set_status(backup_setting::LOCKED_BY_CONFIG);
202 $overwrite->set_visibility(backup_setting::HIDDEN);
203 }
864842aa
EL
204 $this->add_setting($overwrite);
205
206 }
207}