PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/backup/moodle2/restore_course_task.class.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 227 lines | 114 code | 41 blank | 72 comment | 33 complexity | 87a6f34b0a0c78a34c1e306dbaccbf3c MD5 | raw file
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Defines restore_course_task class
  18. *
  19. * @package core_backup
  20. * @subpackage moodle2
  21. * @category backup
  22. * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24. */
  25. defined('MOODLE_INTERNAL') || die();
  26. /**
  27. * course task that provides all the properties and common steps to be performed
  28. * when one course is being restored
  29. *
  30. * TODO: Finish phpdocs
  31. */
  32. class restore_course_task extends restore_task {
  33. protected $info; // info related to course gathered from backup file
  34. protected $contextid; // course context id
  35. /**
  36. * Constructor - instantiates one object of this class
  37. */
  38. public function __construct($name, $info, $plan = null) {
  39. $this->info = $info;
  40. parent::__construct($name, $plan);
  41. }
  42. /**
  43. * Course tasks have their own directory to read files
  44. */
  45. public function get_taskbasepath() {
  46. return $this->get_basepath() . '/course';
  47. }
  48. public function get_contextid() {
  49. return $this->contextid;
  50. }
  51. /**
  52. * Create all the steps that will be part of this task
  53. */
  54. public function build() {
  55. // Define the task contextid (the course one)
  56. $this->contextid = context_course::instance($this->get_courseid())->id;
  57. // Executed conditionally if restoring to new course or if overwrite_conf setting is enabled
  58. if ($this->get_target() == backup::TARGET_NEW_COURSE || $this->get_setting_value('overwrite_conf') == true) {
  59. $this->add_step(new restore_course_structure_step('course_info', 'course.xml'));
  60. // Search reindexing (if enabled).
  61. if (\core_search\manager::is_indexing_enabled()) {
  62. $this->add_step(new restore_course_search_index('course_search_index'));
  63. }
  64. }
  65. $this->add_step(new restore_course_legacy_files_step('legacy_files'));
  66. // Deal with enrolment methods and user enrolments.
  67. if ($this->plan->get_mode() == backup::MODE_IMPORT) {
  68. // No need to do anything with enrolments.
  69. } else if (!$this->get_setting_value('users') or $this->plan->get_mode() == backup::MODE_HUB) {
  70. if ($this->get_setting_value('enrolments') == backup::ENROL_ALWAYS && $this->plan->get_mode() != backup::MODE_HUB) {
  71. // Restore enrolment methods.
  72. $this->add_step(new restore_enrolments_structure_step('course_enrolments', 'enrolments.xml'));
  73. } else if ($this->get_target() == backup::TARGET_CURRENT_ADDING or $this->get_target() == backup::TARGET_EXISTING_ADDING) {
  74. // Keep current enrolments unchanged.
  75. } else {
  76. // If no instances yet add default enrol methods the same way as when creating new course in UI.
  77. $this->add_step(new restore_default_enrolments_step('default_enrolments'));
  78. }
  79. } else {
  80. // Restore course enrolment data.
  81. $this->add_step(new restore_enrolments_structure_step('course_enrolments', 'enrolments.xml'));
  82. }
  83. // Populate groups, this must be done after enrolments because only enrolled users may be in groups.
  84. $this->add_step(new restore_groups_members_structure_step('create_groups_members', '../groups.xml'));
  85. // Restore course role assignments and overrides (internally will observe the role_assignments setting),
  86. // this must be done after all users are enrolled.
  87. $this->add_step(new restore_ras_and_caps_structure_step('course_ras_and_caps', 'roles.xml'));
  88. // Restore course filters (conditionally)
  89. if ($this->get_setting_value('filters')) {
  90. $this->add_step(new restore_filters_structure_step('course_filters', 'filters.xml'));
  91. }
  92. // Restore course comments (conditionally)
  93. if ($this->get_setting_value('comments')) {
  94. $this->add_step(new restore_comments_structure_step('course_comments', 'comments.xml'));
  95. }
  96. // Calendar events (conditionally)
  97. if ($this->get_setting_value('calendarevents')) {
  98. $this->add_step(new restore_calendarevents_structure_step('course_calendar', 'calendar.xml'));
  99. }
  100. // Course competencies.
  101. $this->add_step(new restore_course_competencies_structure_step('course_competencies', 'competencies.xml'));
  102. // Activity completion defaults.
  103. $this->add_step(new restore_completion_defaults_structure_step('course_completion_defaults', 'completiondefaults.xml'));
  104. // At the end, mark it as built
  105. $this->built = true;
  106. }
  107. /**
  108. * Define the contents in the course that must be
  109. * processed by the link decoder
  110. */
  111. static public function define_decode_contents() {
  112. $contents = array();
  113. $contents[] = new restore_decode_content('course', 'summary');
  114. $contents[] = new restore_decode_content('event', 'description');
  115. return $contents;
  116. }
  117. /**
  118. * Define the decoding rules for links belonging
  119. * to the course to be executed by the link decoder
  120. */
  121. static public function define_decode_rules() {
  122. $rules = array();
  123. // Link to the course main page (it also covers "&topic=xx" and "&week=xx"
  124. // because they don't become transformed (section number) in backup/restore.
  125. $rules[] = new restore_decode_rule('COURSEVIEWBYID', '/course/view.php?id=$1', 'course');
  126. // A few other key course links.
  127. $rules[] = new restore_decode_rule('GRADEINDEXBYID', '/grade/index.php?id=$1', 'course');
  128. $rules[] = new restore_decode_rule('GRADEREPORTINDEXBYID', '/grade/report/index.php?id=$1', 'course');
  129. $rules[] = new restore_decode_rule('BADGESVIEWBYID', '/badges/view.php?type=2&id=$1', 'course');
  130. $rules[] = new restore_decode_rule('USERINDEXVIEWBYID', '/user/index.php?id=$1', 'course');
  131. return $rules;
  132. }
  133. // Protected API starts here
  134. /**
  135. * Define the common setting that any restore course will have
  136. */
  137. protected function define_settings() {
  138. // Define overwrite_conf to decide if course configuration will be restored over existing one.
  139. $overwrite = new restore_course_overwrite_conf_setting('overwrite_conf', base_setting::IS_BOOLEAN, false);
  140. $overwrite->set_ui(new backup_setting_ui_select($overwrite, $overwrite->get_name(),
  141. array(1 => get_string('yes'), 0 => get_string('no'))));
  142. $overwrite->get_ui()->set_label(get_string('setting_overwrite_conf', 'backup'));
  143. if ($this->get_target() == backup::TARGET_NEW_COURSE) {
  144. $overwrite->set_value(true);
  145. $overwrite->set_status(backup_setting::LOCKED_BY_CONFIG);
  146. $overwrite->set_visibility(backup_setting::HIDDEN);
  147. $course = (object)['fullname' => null, 'shortname' => null, 'startdate' => null];
  148. } else {
  149. $course = get_course($this->get_courseid());
  150. }
  151. $this->add_setting($overwrite);
  152. $fullnamedefaultvalue = $this->get_info()->original_course_fullname;
  153. $fullname = new restore_course_defaultcustom_setting('course_fullname', base_setting::IS_TEXT, $fullnamedefaultvalue);
  154. $fullname->set_ui(new backup_setting_ui_defaultcustom($fullname, get_string('setting_course_fullname', 'backup'),
  155. ['customvalue' => $fullnamedefaultvalue, 'defaultvalue' => $course->fullname]));
  156. $this->add_setting($fullname);
  157. $shortnamedefaultvalue = $this->get_info()->original_course_shortname;
  158. $shortname = new restore_course_defaultcustom_setting('course_shortname', base_setting::IS_TEXT, $shortnamedefaultvalue);
  159. $shortname->set_ui(new backup_setting_ui_defaultcustom($shortname, get_string('setting_course_shortname', 'backup'),
  160. ['customvalue' => $shortnamedefaultvalue, 'defaultvalue' => $course->shortname]));
  161. $this->add_setting($shortname);
  162. $startdatedefaultvalue = $this->get_info()->original_course_startdate;
  163. $startdate = new restore_course_defaultcustom_setting('course_startdate', base_setting::IS_INTEGER, $startdatedefaultvalue);
  164. $startdate->set_ui(new backup_setting_ui_defaultcustom($startdate, get_string('setting_course_startdate', 'backup'),
  165. ['customvalue' => $startdatedefaultvalue, 'defaultvalue' => $course->startdate, 'type' => 'date_time_selector']));
  166. $this->add_setting($startdate);
  167. $keep_enrols = new restore_course_generic_setting('keep_roles_and_enrolments', base_setting::IS_BOOLEAN, false);
  168. $keep_enrols->set_ui(new backup_setting_ui_select($keep_enrols, $keep_enrols->get_name(), array(1=>get_string('yes'), 0=>get_string('no'))));
  169. $keep_enrols->get_ui()->set_label(get_string('setting_keep_roles_and_enrolments', 'backup'));
  170. if ($this->get_target() != backup::TARGET_CURRENT_DELETING and $this->get_target() != backup::TARGET_EXISTING_DELETING) {
  171. $keep_enrols->set_value(false);
  172. $keep_enrols->set_status(backup_setting::LOCKED_BY_CONFIG);
  173. $keep_enrols->set_visibility(backup_setting::HIDDEN);
  174. }
  175. $this->add_setting($keep_enrols);
  176. $keep_groups = new restore_course_generic_setting('keep_groups_and_groupings', base_setting::IS_BOOLEAN, false);
  177. $keep_groups->set_ui(new backup_setting_ui_select($keep_groups, $keep_groups->get_name(), array(1=>get_string('yes'), 0=>get_string('no'))));
  178. $keep_groups->get_ui()->set_label(get_string('setting_keep_groups_and_groupings', 'backup'));
  179. if ($this->get_target() != backup::TARGET_CURRENT_DELETING and $this->get_target() != backup::TARGET_EXISTING_DELETING) {
  180. $keep_groups->set_value(false);
  181. $keep_groups->set_status(backup_setting::LOCKED_BY_CONFIG);
  182. $keep_groups->set_visibility(backup_setting::HIDDEN);
  183. }
  184. $this->add_setting($keep_groups);
  185. }
  186. }