This commit is contained in:
Jun Pataleta 2019-08-08 10:40:51 +08:00
commit d3455c0da1
7 changed files with 130 additions and 0 deletions

View File

@ -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.
*

View File

@ -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, [

View File

@ -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.
*

View File

@ -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.
*

View File

@ -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';

View File

@ -0,0 +1,24 @@
@tool @tool_usertours
Feature: Duplicate a user tour
As an administrator
I want to duplicate a user tour
@javascript
Scenario: Tour can be duplicated
Given the following "users" exist:
| username | firstname | lastname | email |
| student1 | Student | 1 | student1@example.com |
And I log in as "admin"
And I add a new user tour with:
| Name | First tour |
| Description | My first tour |
| Apply to URL match | /my/% |
| Tour is enabled | 0 |
And I add steps to the "First tour" tour:
| targettype | Title | Content |
| Display in middle of page | Welcome | Welcome to your personal learning space. We'd like to give you a quick tour to show you some of the areas you may find helpful |
And I open the User tour settings page
And I should see "1" occurrences of "First tour" in the "admintable" "table"
And I click on "Duplicate" "link" in the "My first tour" "table_row"
And I open the User tour settings page
Then I should see "1" occurrences of "First tour (copy)" in the "admintable" "table"

View File

@ -1799,4 +1799,41 @@ class behat_general extends behat_base {
$node = $this->get_selected_node($selectortype, $element);
$this->js_trigger_click($node);
}
/**
* Checks, that the specified element contains the specified text a certain amount of times.
* When running Javascript tests it also considers that texts may be hidden.
*
* @Then /^I should see "(?P<elementscount_number>\d+)" occurrences of "(?P<text_string>(?:[^"]|\\")*)" in the "(?P<element_string>(?:[^"]|\\")*)" "(?P<text_selector_string>[^"]*)"$/
* @throws ElementNotFoundException
* @throws ExpectationException
* @param int $elementscount How many occurrences of the element we look for.
* @param string $text
* @param string $element Element we look in.
* @param string $selectortype The type of element where we are looking in.
*/
public function i_should_see_occurrences_of_in_element($elementscount, $text, $element, $selectortype) {
// Getting the container where the text should be found.
$container = $this->get_selected_node($selectortype, $element);
// Looking for all the matching nodes without any other descendant matching the
// same xpath (we are using contains(., ....).
$xpathliteral = behat_context_helper::escape($text);
$xpath = "/descendant-or-self::*[contains(., $xpathliteral)]" .
"[count(descendant::*[contains(., $xpathliteral)]) = 0]";
$nodes = $this->find_all('xpath', $xpath, false, $container);
if ($this->running_javascript()) {
$nodes = array_filter($nodes, function($node) {
return $node->isVisible();
});
}
if ($elementscount != count($nodes)) {
throw new ExpectationException('Found '.count($nodes).' elements in column. Expected '.$elementscount,
$this->getSession());
}
}
}