/** * Takes the original course object as an argument, backs up the * course and returns a backupid which can be used for restoring the course. * * @param object $course Original course object * @param array $activityids all cm ids to backup * @param array $activityids all section ids to backup * @throws coding_exception Moodle coding_exception * @retun string backupid Backupid */ private function backup($course, $activityids, $sectionids) { global $CFG, $DB, $USER; // MODE_IMPORT option is required to prevent it from zipping up the // result at the end. Unfortunately this breaks the system in some // other ways (see other comments). // NOTE: We cannot use MODE_SAMESITE here because that option will // zip the result and anyway, MODE_IMPORT already turns off the files. $bc = new \backup_controller(\backup::TYPE_1COURSE, $course->id, \backup::FORMAT_MOODLE, \backup::INTERACTIVE_NO, \backup::MODE_IMPORT, $USER->id); // Setup custom logger that prints dots. $logger = new logger(\backup::LOG_INFO, false, true); self::$currentlogger = $logger; $logger->potential_dot(); $bc->get_logger()->set_next($logger); $bc->set_progress(new progress()); foreach ($bc->get_plan()->get_tasks() as $taskindex => $task) { $settings = $task->get_settings(); foreach ($settings as $settingindex => $setting) { $setting->set_status(\backup_setting::NOT_LOCKED); // Modify the values of the intial backup settings. if ($taskindex == 0) { if (isset($this->backupsettings[$setting->get_name()])) { $setting->set_value($this->backupsettings[$setting->get_name()]); } } else { list($name, $id, $type) = $this->parse_backup_course_module($setting->get_name()); // Include user data on glossary if the role 'contributingstudent' // does not have the capability mod/glossary:write on that glossary. // This is so that we include course-team glossary data but not // glossaries that are written by students. if ($name === 'glossary' && $type === 'userinfo') { // Find the contributing student role id. If there // isn't one, use the student role id, for dev servers etc. $roles = $DB->get_records_select('role', "shortname IN ('contributingstudent', 'student')", null, 'shortname', 'id'); if (!$roles) { continue; } $roleid = reset($roles)->id; // Get the list of roles which have the capability in the context // of the glossary. $context = \context_module::instance($id); list($allowed, $forbidden) = get_roles_with_cap_in_context($context, 'mod/glossary:write'); // If student has this capability then the user data is false. if (!empty($allowed[$roleid]) && empty($forbidden[$roleid])) { $setting->set_value(false); } else { $setting->set_value(true); } } // Ignone any sections not in subpage. if ($name === 'section' && $type === 'included' && !in_array($id, $sectionids)) { $setting->set_value(false); } // Ignone any activities not in subpage. if ($name !== 'section' && $type === 'included' && !in_array($id, $activityids)) { $setting->set_value(false); } } } } $logger->potential_dot(); $bc->save_controller(); $backupid = $bc->get_backupid(); $this->backupbasepath = $bc->get_plan()->get_basepath(); $bc->execute_plan(); $bc->destroy(); return $backupid; }