| Línea 53... | Línea 53... | 
          
            | 53 | Restore backup into provided category or course.
 | 53 | Restore backup into provided category or course.
 | 
          
            | 54 | If courseid is set, course module/s will be added into the course.
 | 54 | If courseid is set, course module/s will be added into the course.
 | 
          
            | Línea 55... | Línea 55... | 
          
            | 55 |  
 | 55 |  
 | 
          
            | 56 | Options:
 | 56 | Options:
 | 
          
            | 57 | -f, --file=STRING       Path to the backup file.
 | 57 | -f, --file=STRING       Path to the backup file.
 | 
          
            | 58 | -c, --categoryid=INT    ID of the course category to restore to.
 | 58 | -c, --categoryid=INT    ID of the course category to restore to. This option is ignored when restoring an activity and courseid is set.
 | 
          
            | 59 | -C, --courseid=INT      ID of the course to restore to. This option is ignored if categoryid is set.
 | 59 | -C, --courseid=INT      ID of the course to restore to. This option is ignored when restoring a course and the categoryid is set.
 | 
          
            | 60 | -s, --showdebugging     Show developer level debugging information
 | 60 | -s, --showdebugging     Show developer level debugging information
 | 
          
            | Línea 61... | Línea 61... | 
          
            | 61 | -h, --help              Print out this help.
 | 61 | -h, --help              Print out this help.
 | 
          
            | 62 |  
 | 62 |  
 | 
          
            | - |   | 63 | Example:
 | 
          
            | 63 | Example:
 | 64 | \$sudo -u www-data /usr/bin/php admin/cli/restore_backup.php --file=/path/to/backup/coursebackup.mbz --categoryid=1\n
 | 
          
            | Línea 64... | Línea 65... | 
          
            | 64 | \$sudo -u www-data /usr/bin/php admin/cli/restore_backup.php --file=/path/to/backup/file.mbz --categoryid=1\n
 | 65 | \$sudo -u www-data /usr/bin/php admin/cli/restore_backup.php --file=/path/to/backup/activitybackup.mbz --courseid=1\n
 | 
          
            | 65 | EOL;
 | 66 | EOL;
 | 
          
            | 66 |  
 | 67 |  
 | 
          
            | Línea 82... | Línea 83... | 
          
            | 82 |  
 | 83 |  
 | 
          
            | 83 | if ($options['categoryid']) {
 | 84 | if ($options['categoryid']) {
 | 
          
            | 84 |     if (!$category = $DB->get_record('course_categories', ['id' => $options['categoryid']], 'id')) {
 | 85 |     if (!$category = $DB->get_record('course_categories', ['id' => $options['categoryid']], 'id')) {
 | 
          
            | 85 |         throw new \moodle_exception('invalidcategoryid');
 | 86 |         throw new \moodle_exception('invalidcategoryid');
 | 
          
            | - |   | 87 |     }
 | 
          
            | - |   | 88 | }
 | 
          
            | 86 |     }
 | 89 |  
 | 
          
            | 87 | } else if ($options['courseid']) {
 | 90 | if ($options['courseid']) {
 | 
          
            | 88 |     if (!$course = $DB->get_record('course', ['id' => $options['courseid']], 'id')) {
 | 91 |     if (!$course = $DB->get_record('course', ['id' => $options['courseid']], 'id')) {
 | 
          
            | 89 |         throw new \moodle_exception('invalidcourseid');
 | 92 |         throw new \moodle_exception('invalidcourseid');
 | 
          
            | 90 |     }
 | 93 |     }
 | 
          
            | - |   | 94 | }
 | 
          
            | - |   | 95 |  
 | 
          
            | 91 | } else {
 | 96 | if (empty($category) && empty($course)) {
 | 
          
            | 92 |     throw new \moodle_exception('invalidoption');
 | 97 |     throw new \moodle_exception('invalidoption');
 | 
          
            | Línea 93... | Línea 98... | 
          
            | 93 | }
 | 98 | }
 | 
          
            | 94 |  
 | 99 |  
 | 
          
            | Línea 98... | Línea 103... | 
          
            | 98 | cli_heading(get_string('extractingbackupfileto', 'backup', $path));
 | 103 | cli_heading(get_string('extractingbackupfileto', 'backup', $path));
 | 
          
            | 99 | $fp = get_file_packer('application/vnd.moodle.backup');
 | 104 | $fp = get_file_packer('application/vnd.moodle.backup');
 | 
          
            | 100 | $fp->extract_to_pathname($options['file'], $path);
 | 105 | $fp->extract_to_pathname($options['file'], $path);
 | 
          
            | Línea 101... | Línea 106... | 
          
            | 101 |  
 | 106 |  
 | 
          
            | 102 | cli_heading(get_string('preprocessingbackupfile'));
 | - |   | 
          
            | 103 | try {
 | - |   | 
          
            | 104 |     list($fullname, $shortname) = restore_dbops::calculate_course_names(0, get_string('restoringcourse', 'backup'),
 | - |   | 
          
            | Línea 105... | Línea 107... | 
          
            | 105 |         get_string('restoringcourseshortname', 'backup'));
 | 107 | cli_heading(get_string('preprocessingbackupfile'));
 | 
          
            | 106 |  
 | 108 |  
 | 
          
            | 107 |     if (!empty($course)) {
 | 109 | try {
 | 
          
            | 108 |         $courseid = $course->id;
 | 110 |     // Create a temporary restore controller to determine the restore type.
 | 
          
            | - |   | 111 |     $tmprc = new restore_controller($backupdir, SITEID, backup::INTERACTIVE_NO,
 | 
          
            | - |   | 112 |         backup::MODE_GENERAL, $admin->id, backup::TARGET_EXISTING_ADDING);
 | 
          
            | - |   | 113 |     // Restore the backup into a new course if:
 | 
          
            | - |   | 114 |     // - It is a course backup and the category is set.
 | 
          
            | - |   | 115 |     // - It is an activity backup and the course is not set.
 | 
          
            | - |   | 116 |     $restoreasnewcourse = ($tmprc->get_type() === backup::TYPE_1COURSE && !empty($category)) ||
 | 
          
            | - |   | 117 |         ($tmprc->get_type() !== backup::TYPE_1COURSE && empty($course));
 | 
          
            | - |   | 118 |     // Make sure to clean up the temporary restore controller.
 | 
          
            | 109 |         $rc = new restore_controller($backupdir, $courseid, backup::INTERACTIVE_NO,
 | 119 |     $tmprc->destroy();
 | 
          
            | - |   | 120 |  
 | 
          
            | - |   | 121 |     if ($restoreasnewcourse) {
 | 
          
            | 110 |             backup::MODE_GENERAL, $admin->id, backup::TARGET_EXISTING_ADDING);
 | 122 |         list($fullname, $shortname) = restore_dbops::calculate_course_names(0, get_string('restoringcourse', 'backup'),
 | 
          
            | 111 |     } else {
 | 123 |             get_string('restoringcourseshortname', 'backup'));
 | 
          
            | 112 |         $courseid = restore_dbops::create_new_course($fullname, $shortname, $category->id);
 | 124 |         $courseid = restore_dbops::create_new_course($fullname, $shortname, $category->id);
 | 
          
            | - |   | 125 |         $rc = new restore_controller($backupdir, $courseid, backup::INTERACTIVE_NO,
 | 
          
            | - |   | 126 |             backup::MODE_GENERAL, $admin->id, backup::TARGET_NEW_COURSE);
 | 
          
            | - |   | 127 |     } else {
 | 
          
            | - |   | 128 |         $courseid = $course->id;
 | 
          
            | 113 |         $rc = new restore_controller($backupdir, $courseid, backup::INTERACTIVE_NO,
 | 129 |         $rc = new restore_controller($backupdir, $courseid, backup::INTERACTIVE_NO,
 | 
          
            | 114 |             backup::MODE_GENERAL, $admin->id, backup::TARGET_NEW_COURSE);
 | 130 |             backup::MODE_GENERAL, $admin->id, backup::TARGET_EXISTING_ADDING);
 | 
          
            | 115 |     }
 | 131 |     }
 | 
          
            | 116 |     $rc->execute_precheck();
 | 132 |     $rc->execute_precheck();
 | 
          
            | Línea 117... | Línea 133... | 
          
            | 117 |     $rc->execute_plan();
 | 133 |     $rc->execute_plan();
 | 
          
            | 118 |     $rc->destroy();
 | 134 |     $rc->destroy();
 | 
          
            | - |   | 135 |  
 | 
          
            | 119 |  
 | 136 |     // Rename the course's full and short names with the backup file's original names if the backup file is an activity backup
 | 
          
            | - |   | 137 |     // that is restored to a new course.
 | 
          
            | 120 |     // Rename course name if the backup is from course module and restore to category.
 | 138 |     if ($restoreasnewcourse && $rc->get_type() !== backup::TYPE_1COURSE) {
 | 
          
            | - |   | 139 |         $course = get_course($courseid);
 | 
          
            | - |   | 140 |         $backupinfo = $rc->get_info();
 | 
          
            | - |   | 141 |         $tmpfullname = $backupinfo->original_course_fullname ?? get_string('restoretonewcourse', 'backup');
 | 
          
            | - |   | 142 |         $tmpshortname = $backupinfo->original_course_shortname ?? get_string('newcourse');
 | 
          
            | 121 |     if (empty($course)) {
 | 143 |         list($fullname, $shortname) = restore_dbops::calculate_course_names(
 | 
          
            | - |   | 144 |             courseid: 0,
 | 
          
            | 122 |         $course = get_course($courseid);
 | 145 |             fullname: $tmpfullname,
 | 
          
            | 123 |         list($fullname, $shortname) = restore_dbops::calculate_course_names(0, get_string('restoretonewcourse', 'backup'),
 | 146 |             shortname: $tmpshortname,
 | 
          
            | 124 |             get_string('newcourse'));
 | 147 |         );
 | 
          
            | 125 |         $course->fullname = $fullname;
 | 148 |         $course->fullname = $fullname;
 | 
          
            | 126 |         $course->shortname = $shortname;
 | 149 |         $course->shortname = $shortname;
 |