MDL-23752 backup - export/import mode. Fixed various details.
[moodle.git] / backup / moodle2 / backup_final_task.class.php
CommitLineData
77547b46
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/**
19 * @package moodlecore
20 * @subpackage backup-moodle2
21 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25/**
26 * Final task that provides all the final steps necessary in order to finish one
27 * backup (mainly gathering references and creating the main xml) apart from
28 * some final cleaning
29 *
30 * TODO: Finish phpdocs
31 */
32class backup_final_task extends backup_task {
33
34 /**
35 * Create all the steps that will be part of this task
36 */
37 public function build() {
38
39 // Set the backup::VAR_CONTEXTID setting to course context as far as next steps require that
40 $coursectxid = get_context_instance(CONTEXT_COURSE, $this->get_courseid())->id;
41 $this->add_setting(new backup_activity_generic_setting(backup::VAR_CONTEXTID, base_setting::IS_INTEGER, $coursectxid));
42
d39595cc
EL
43 // Set the backup::VAR_COURSEID setting to course, we'll need that in some steps
44 $courseid = $this->get_courseid();
45 $this->add_setting(new backup_activity_generic_setting(backup::VAR_COURSEID, base_setting::IS_INTEGER, $courseid));
46
77547b46
EL
47 // Generate the groups file with the final annotated groups and groupings
48 // including membership based on setting
49 $this->add_step(new backup_groups_structure_step('groups', 'groups.xml'));
50
767cb7f0
EL
51 // Annotate all the question files for the already annotated question
52 // categories (this is performed here and not in the structure step because
53 // it involves multiple contexts and as far as we are always backup-ing
54 // complete question banks we don't need to restrict at all and can be
55 // done in a single pass
56 $this->add_step(new backup_annotate_all_question_files('question_files'));
57
58 // Generate the questions file with the final annotated question_categories
59 $this->add_step(new backup_questions_structure_step('questions', 'questions.xml'));
60
76cfb124 61 // Annotate all the user files (conditionally) (private, profile and icon files)
77547b46
EL
62 // Because each user has its own context, we need a separate/specialised step here
63 // This step also ensures that the contexts for all the users exist, so next
64 // step can be safely executed (join between users and contexts)
65 // Not executed if backup is without users of anonymized
66 if ($this->get_setting_value('users') && !$this->get_setting_value('anonymize')) {
67 $this->add_step(new backup_annotate_all_user_files('user_files'));
68 }
69
e439a954 70 // Generate the users file (conditionally) with the final annotated users
77547b46
EL
71 // including custom profile fields, preferences, tags, role assignments and
72 // overrides
73 if ($this->get_setting_value('users')) {
74 $this->add_step(new backup_users_structure_step('users', 'users.xml'));
75 }
76
77 // Generate the top roles file with all the final annotated roles
78 // that have been detected along the whole process. It's just
79 // the list of role definitions (no assignments nor permissions)
80 $this->add_step(new backup_final_roles_structure_step('roleslist', 'roles.xml'));
81
76cfb124
EL
82 // Generate the gradebook file with categories and course grade items. Do it conditionally, using
83 // execute_condition() so only will be excuted if ALL module grade_items in course have been exported
84 $this->add_step(new backup_gradebook_structure_step('course_gradebook','gradebook.xml'));
85
bd39b6f2
SH
86 // Generate the course completion
87 $this->add_step(new backup_course_completion_structure_step('course_completion', 'completion.xml'));
88
76cfb124 89 // Generate the scales file with all the (final) annotated scales
77547b46
EL
90 $this->add_step(new backup_final_scales_structure_step('scaleslist', 'scales.xml'));
91
76cfb124 92 // Generate the outcomes file with all the (final) annotated outcomes
77547b46
EL
93 $this->add_step(new backup_final_outcomes_structure_step('outcomeslist', 'outcomes.xml'));
94
95 // Migrate the pending annotations to final (prev steps may have added some files)
96 // This must be executed before backup files
97 $this->add_step(new move_inforef_annotations_to_final('migrate_inforef'));
98
99 // Generate the files.xml file with all the (final) annotated files. At the same
100 // time copy all the files from moodle storage to backup storage (uses custom
101 // backup_nested_element for that)
102 $this->add_step(new backup_final_files_structure_step('fileslist', 'files.xml'));
103
104 // Write the main moodle_backup.xml file, with all the information related
105 // to the backup, settings, license, versions and other useful information
106 $this->add_step(new backup_main_structure_step('mainfile', 'moodle_backup.xml'));
107
dc1e4cce
EL
108 // On backup::MODE_IMPORT, we don't have to zip nor store the the file, skip these steps
109 if ($this->plan->get_mode() != backup::MODE_IMPORT) {
110 // Generate the zip file (mbz extension)
111 $this->add_step(new backup_zip_contents('zip_contents'));
ce937f99 112
dc1e4cce
EL
113 // Copy the generated zip (.mbz) file to final destination
114 $this->add_step(new backup_store_backup_file('save_backupfile'));
115 }
ce937f99 116
dc1e4cce
EL
117 // Clean the temp dir (conditionally) and drop temp tables
118 $cleanstep = new drop_and_clean_temp_stuff('drop_and_clean_temp_stuff');
119 // Decide about to delete the temp dir (based on backup::MODE_IMPORT)
120 $cleanstep->skip_cleaning_temp_dir($this->plan->get_mode() == backup::MODE_IMPORT);
121 $this->add_step($cleanstep);
2de3539b 122
77547b46
EL
123 $this->built = true;
124 }
125
126// Protected API starts here
127
128 /**
129 * Define the common setting that any backup type will have
130 */
131 protected function define_settings() {
132 // This task has not settings (could have them, like destination or so in the future, let's see)
133 }
134}