@@ -332,6 +332,21 @@ public function get_executiontime() {
332332 public function get_plan () {
333333 return $ this ->plan ;
334334 }
335+ /**
336+ * Gets the value for the requested setting
337+ *
338+ * @param string $name
339+ * @param bool $default
340+ * @return mixed
341+ */
342+ public function get_setting_value ($ name , $ default = false ) {
343+ try {
344+ return $ this ->get_plan ()->get_setting ($ name )->get_value ();
345+ } catch (Exception $ e ) {
346+ debugging ('Failed to find the setting: ' .$ name , DEBUG_DEVELOPER );
347+ return $ default ;
348+ }
349+ }
335350
336351 public function get_info () {
337352 return $ this ->info ;
@@ -341,6 +356,14 @@ public function execute_plan() {
341356 // Basic/initial prevention against time/memory limits
342357 core_php_time_limit::raise (1 * 60 * 60 ); // 1 hour for 1 course initially granted
343358 raise_memory_limit (MEMORY_EXTRA );
359+
360+ // Do course cleanup precheck, if required. This was originally in restore_ui. Moved to handle async backup/restore.
361+ if ($ this ->get_target () == backup::TARGET_CURRENT_DELETING || $ this ->get_target () == backup::TARGET_EXISTING_DELETING ) {
362+ $ options = array ();
363+ $ options ['keep_roles_and_enrolments ' ] = $ this ->get_setting_value ('keep_roles_and_enrolments ' );
364+ $ options ['keep_groups_and_groupings ' ] = $ this ->get_setting_value ('keep_groups_and_groupings ' );
365+ restore_dbops::delete_course_content ($ this ->get_courseid (), $ options );
366+ }
344367 // If this is not a course restore or single activity restore (e.g. duplicate), inform the plan we are not
345368 // including all the activities for sure. This will affect any
346369 // task/step executed conditionally to stop processing information
0 commit comments