From 395dae3084c2adf2d0ad6edbaaf127229638e4a4 Mon Sep 17 00:00:00 2001 From: Eloy Lafuente Date: Wed, 21 Jul 2010 15:52:03 +0000 Subject: [PATCH] MDL-21432 backup - restore course/tags/rolenames/files --- backup/moodle2/backup_stepslib.php | 2 +- backup/moodle2/restore_course_task.class.php | 2 +- backup/moodle2/restore_final_task.class.php | 2 + backup/moodle2/restore_root_task.class.php | 4 - backup/moodle2/restore_stepslib.php | 87 +++++++++++++++++++- 5 files changed, 88 insertions(+), 9 deletions(-) diff --git a/backup/moodle2/backup_stepslib.php b/backup/moodle2/backup_stepslib.php index 994546c970f..9ec69699e36 100644 --- a/backup/moodle2/backup_stepslib.php +++ b/backup/moodle2/backup_stepslib.php @@ -269,7 +269,7 @@ class backup_course_structure_step extends backup_structure_step { 'defaultgroupingid', 'lang', 'theme', 'timecreated', 'timemodified', 'requested', 'restrictmodules', - 'enablecompletion')); + 'enablecompletion', 'completionstartonenrol', 'completionnotify')); $category = new backup_nested_element('category', array('id'), array( 'name', 'description')); diff --git a/backup/moodle2/restore_course_task.class.php b/backup/moodle2/restore_course_task.class.php index 4aaedced2b5..eccc25f08ec 100644 --- a/backup/moodle2/restore_course_task.class.php +++ b/backup/moodle2/restore_course_task.class.php @@ -54,7 +54,7 @@ class restore_course_task extends restore_task { public function build() { // TODO: Link all the course steps here - // Executed conditionally if restoring to new course or if overwrite_conf setting is enabled + // Executed conditionally if restoring to new course or deleting or if overwrite_conf setting is enabled if ($this->get_target() == backup::TARGET_NEW_COURSE || $this->get_setting_value('overwrite_conf') == true) { $this->add_step(new restore_course_structure_step('course_info', 'course.xml')); } diff --git a/backup/moodle2/restore_final_task.class.php b/backup/moodle2/restore_final_task.class.php index 6af9cae6ec4..d0a958c3a0e 100644 --- a/backup/moodle2/restore_final_task.class.php +++ b/backup/moodle2/restore_final_task.class.php @@ -35,6 +35,8 @@ class restore_final_task extends restore_task { */ public function build() { + // TODO: Apply all the block_positions accumulated along the process + // Clean the temp dir (conditionally) and drop temp table $this->add_step(new restore_drop_and_clean_temp_stuff('drop_and_clean_temp_stuff')); diff --git a/backup/moodle2/restore_root_task.class.php b/backup/moodle2/restore_root_task.class.php index 98128fea5f8..5c3012b4919 100644 --- a/backup/moodle2/restore_root_task.class.php +++ b/backup/moodle2/restore_root_task.class.php @@ -66,10 +66,6 @@ class restore_root_task extends restore_task { // Unconditionally, load create all the needed outcomes $this->add_step(new restore_outcomes_structure_step('create_scales', 'outcomes.xml')); - // Unconditionally, load create all the needed outcomes. - // TODO: restore outcomes - // $this->add_step(new restore_outcomes_structure_step('create_outcomes', 'outcomes.xml')); - // At the end, mark it as built $this->built = true; } diff --git a/backup/moodle2/restore_stepslib.php b/backup/moodle2/restore_stepslib.php index 59e012fb8b6..47c52c77717 100644 --- a/backup/moodle2/restore_stepslib.php +++ b/backup/moodle2/restore_stepslib.php @@ -456,7 +456,9 @@ class restore_outcomes_structure_step extends restore_structure_step { /* * Structure step that will read the course.xml file, loading it and performing - * various actions depending of the site/restore settings + * various actions depending of the site/restore settings. Note that target + * course always exist before arriving here so this step will be updating + * the course record (never inserting) */ class restore_course_structure_step extends restore_structure_step { @@ -472,8 +474,87 @@ class restore_course_structure_step extends restore_structure_step { // Processing functions go here public function process_course($data) { - // TODO: don't forget to remap defaultgroupingid - print_object('stopped before processing course. Continue here'); + global $CFG, $DB; + + $data = (object)$data; + $coursetags = isset($data->tags['tag']) ? $data->tags['tag'] : array(); + $coursemodules = isset($data->allowed_modules['module']) ? $data->allowed_modules['module'] : array(); + $oldid = $data->id; // We'll need this later + debugging ('review the these lines of process_course() to change to settings once available', DEBUG_DEVELOPER); + // TODO: Get fullname, shortname, startdate and category from settings + // $fullname = $this->get_setting_value('course_fullname'); + // $shortname = $this->get_setting_value('course_shortname'); + // $category = $this->get_setting_value('course_category'); + // $startdate = $this->get_setting_value('course_startdate'); + // TODO: Delete this lines once we are getting the vars above from settings + $fullname = $this->task->get_info()->original_course_fullname; + $shortname= $this->task->get_info()->original_course_shortname; + $startdate= $this->task->get_info()->original_course_startdate; + $category = get_course_category()->id; + + // Calculate final course names, to avoid dupes + list($fullname, $shortname) = restore_dbops::calculate_course_names($this->get_courseid(), $fullname, $shortname); + + // Need to change some fields before updating the course record + $data->id = $this->get_courseid(); + $data->fullname = $fullname; + $data->shortname= $shortname; + $data->idnumber = ''; + $data->category = $category; + $data->startdate= $this->apply_date_offset($data->startdate); + if ($data->defaultgroupingid) { + $data->defaultgroupingid = $this->get_mappingid('grouping', $data->defaultgroupingid); + } + if (empty($CFG->enablecompletion) || !$this->get_setting_value('userscompletion')) { + $data->enablecompletion = 0; + $data->completionstartonenrol = 0; + $data->completionnotify = 0; + } + $languages = get_string_manager()->get_list_of_translations(); // Get languages for quick search + if (!array_key_exists($data->lang, $languages)) { + $data->lang = ''; + } + $themes = get_list_of_themes(); // Get themes for quick search later + if (!in_array($data->theme, $themes) || empty($CFG->allowcoursethemes)) { + $data->theme = ''; + } + + // Course record ready, update it + $DB->update_record('course', $data); + + // Course tags + if (!empty($CFG->usetags) && isset($coursetags)) { // if enabled in server and present in backup + $tags = array(); + foreach ($coursetags as $coursetag) { + $coursetag = (object)$coursetag; + $tags[] = $coursetag->rawname; + } + tag_set('course', $this->get_courseid(), $tags); + } + // Course allowed modules + if (!empty($data->restrictmodules) && !empty($coursemodules)) { + $available = get_plugin_list('mod'); + foreach ($coursemodules as $coursemodule) { + $mname = $coursemodule['modulename']; + if (array_key_exists($mname, $available)) { + if ($module = $DB->get_record('modules', array('name' => $mname, 'visible' => 1))) { + $rec = new stdclass(); + $rec->course = $this->get_courseid(); + $rec->module = $module->id; + if (!$DB->record_exists('course_allowed_modules', (array)$rec)) { + $DB->insert_record('course_allowed_modules', $rec); + } + } + } + } + } + // Role name aliases + restore_dbops::set_course_role_names($this->get_restoreid(), $this->get_courseid()); } + protected function after_execute() { + // Add course related files, without itemid to match + $this->add_related_files('course', 'summary', null); + $this->add_related_files('course', 'legacy', null); + } } -- 2.43.0