diff --git a/admin/tool/usertours/classes/helper.php b/admin/tool/usertours/classes/helper.php index 1e4cdbbc972..df04ed926ee 100644 --- a/admin/tool/usertours/classes/helper.php +++ b/admin/tool/usertours/classes/helper.php @@ -203,6 +203,21 @@ class helper { return $link; } + /** + * Get the link used to duplicate the tour. + * + * @param int $tourid The ID of the tour to duplicate. + * @return moodle_url The URL. + */ + public static function get_duplicate_tour_link($tourid) { + $link = new \moodle_url('/admin/tool/usertours/configure.php', [ + 'action' => manager::ACTION_DUPLICATETOUR, + 'id' => $tourid, + ]); + + return $link; + } + /** * Get the link used to delete the tour. * diff --git a/admin/tool/usertours/classes/local/table/tour_list.php b/admin/tool/usertours/classes/local/table/tour_list.php index 10de685c127..3611de820d9 100644 --- a/admin/tool/usertours/classes/local/table/tour_list.php +++ b/admin/tool/usertours/classes/local/table/tour_list.php @@ -138,6 +138,7 @@ class tour_list extends \flexible_table { $actions[] = helper::format_icon_link($tour->get_view_link(), 't/viewdetails', get_string('view')); $actions[] = helper::format_icon_link($tour->get_edit_link(), 't/edit', get_string('edit')); + $actions[] = helper::format_icon_link($tour->get_duplicate_link(), 't/copy', get_string('duplicate')); $actions[] = helper::format_icon_link($tour->get_export_link(), 't/export', get_string('exporttour', 'tool_usertours'), 'tool_usertours'); $actions[] = helper::format_icon_link($tour->get_delete_link(), 't/delete', get_string('delete'), null, [ diff --git a/admin/tool/usertours/classes/manager.php b/admin/tool/usertours/classes/manager.php index f29f481aae3..33cd7529248 100644 --- a/admin/tool/usertours/classes/manager.php +++ b/admin/tool/usertours/classes/manager.php @@ -78,6 +78,11 @@ class manager { */ const ACTION_VIEWTOUR = 'viewtour'; + /** + * @var ACTION_DUPLICATETOUR The action to duplicate the tour. + */ + const ACTION_DUPLICATETOUR = 'duplicatetour'; + /** * @var ACTION_NEWSTEP The action to create a new step. */ @@ -163,6 +168,10 @@ class manager { $this->view_tour(required_param('id', PARAM_INT)); break; + case self::ACTION_DUPLICATETOUR: + $this->duplicate_tour(required_param('id', PARAM_INT)); + break; + case self::ACTION_HIDETOUR: $this->hide_tour(required_param('id', PARAM_INT)); break; @@ -486,6 +495,39 @@ class manager { $this->footer(); } + /** + * Duplicate an existing tour. + * + * @param int $tourid The ID of the tour to duplicate. + */ + protected function duplicate_tour($tourid) { + $tour = helper::get_tour($tourid); + + $export = $tour->to_record(); + // Remove the id. + unset($export->id); + + // Set the version. + $export->version = get_config('tool_usertours', 'version'); + + $export->name = get_string('duplicatetour_name', 'tool_usertours', $export->name); + + // Step export. + $export->steps = []; + foreach ($tour->get_steps() as $step) { + $record = $step->to_record(); + unset($record->id); + unset($record->tourid); + + $export->steps[] = $record; + } + + $exportstring = json_encode($export); + $newtour = self::import_tour_from_json($exportstring); + + redirect($newtour->get_view_link()); + } + /** * Show the tour. * diff --git a/admin/tool/usertours/classes/tour.php b/admin/tool/usertours/classes/tour.php index 5717e24af88..4c4a2014790 100644 --- a/admin/tool/usertours/classes/tour.php +++ b/admin/tool/usertours/classes/tour.php @@ -356,6 +356,15 @@ class tour { return helper::get_export_tour_link($this->id); } + /** + * The link to duplicate this tour. + * + * @return moodle_url + */ + public function get_duplicate_link() { + return helper::get_duplicate_tour_link($this->id); + } + /** * The link to remove this tour. * diff --git a/admin/tool/usertours/lang/en/tool_usertours.php b/admin/tool/usertours/lang/en/tool_usertours.php index f61d13c51b9..b3b22cf8238 100644 --- a/admin/tool/usertours/lang/en/tool_usertours.php +++ b/admin/tool/usertours/lang/en/tool_usertours.php @@ -44,6 +44,8 @@ $string['cssselector'] = 'CSS selector'; $string['defaultvalue'] = 'Default ({$a})'; $string['delay'] = 'Delay before showing the step'; $string['done'] = 'Done'; +$string['duplicatetour'] = 'Duplicate tour'; +$string['duplicatetour_name'] = '{$a} (copy)'; $string['editstep'] = 'Editing "{$a}"'; $string['tourisenabled'] = 'Tour is enabled'; $string['enabled'] = 'Enabled';