#134 Add ability to duplicate templates

This commit is contained in:
Mark Nelson 2017-09-03 19:50:42 +08:00
parent a211f97931
commit 909d376b95
6 changed files with 116 additions and 45 deletions

View file

@ -93,6 +93,17 @@ class manage_templates_table extends \table_sql {
$editlink = new \moodle_url('/mod/customcert/edit.php', array('tid' => $template->id));
$editicon = $OUTPUT->action_icon($editlink, new \pix_icon('t/edit', get_string('edit')));
// Link to duplicate the template.
$duplicatelink = new \moodle_url('/mod/customcert/manage_templates.php',
array(
'tid' => $template->id,
'action' => 'duplicate',
'sesskey' => sesskey()
)
);
$duplicateicon = $OUTPUT->action_icon($duplicatelink, new \pix_icon('t/copy', get_string('duplicate')), null,
array('class' => 'action-icon duplicate-icon'));
// Link to delete the template.
$deletelink = new \moodle_url('/mod/customcert/manage_templates.php',
array(
@ -104,7 +115,7 @@ class manage_templates_table extends \table_sql {
$deleteicon = $OUTPUT->action_icon($deletelink, new \pix_icon('t/delete', get_string('delete')), null,
array('class' => 'action-icon delete-icon'));
return $editicon . $deleteicon;
return $editicon . $duplicateicon . $deleteicon;
}
/**

View file

@ -312,6 +312,57 @@ class template {
}
}
/**
* Handles duplicating the template.
*
* @param int|null $copytotemplateid The template id to copy to, null if creating a new template
*/
public function duplicate($copytotemplateid = null) {
global $DB;
if (is_null($copytotemplateid)) {
// Create another template at the same level.
$template = new \stdClass();
$template->name = $this->name . ' (' . strtolower(get_string('duplicate', 'customcert')) . ')';
$template->contextid = $this->contextid;
$template->timecreated = time();
$template->timemodified = $template->timecreated;
$copytotemplateid = $DB->insert_record('customcert_templates', $template);
}
// Get the pages for the template, there should always be at least one page for each template.
if ($templatepages = $DB->get_records('customcert_pages', array('templateid' => $this->id))) {
// Loop through the pages.
foreach ($templatepages as $templatepage) {
$page = clone($templatepage);
$page->templateid = $copytotemplateid;
$page->timecreated = time();
$page->timemodified = $page->timecreated;
// Insert into the database.
$page->id = $DB->insert_record('customcert_pages', $page);
// Now go through the elements we want to load.
if ($templateelements = $DB->get_records('customcert_elements', array('pageid' => $templatepage->id))) {
foreach ($templateelements as $templateelement) {
$element = clone($templateelement);
$element->pageid = $page->id;
$element->timecreated = time();
$element->timemodified = $element->timecreated;
// Ok, now we want to insert this into the database.
$element->id = $DB->insert_record('customcert_elements', $element);
// Load any other information the element may need to for the template.
if ($e = \mod_customcert\element_factory::get_element_instance($element)) {
if (!$e->copy_element($templateelement)) {
// Failed to copy - delete the element.
$e->delete();
}
}
}
}
}
}
}
/**
* Handles moving an item on a template.
*

View file

@ -47,6 +47,9 @@ $string['deleteissueconfirm'] = 'Are you sure you want to delete this certificat
$string['deletepageconfirm'] = 'Are you sure you want to delete this certificate page?';
$string['deletetemplateconfirm'] = 'Are you sure you want to delete this certificate template?';
$string['description'] = 'Description';
$string['duplicate'] = 'Duplicate';
$string['duplicateconfirm'] = 'Duplicate confirmation';
$string['duplicatetemplateconfirm'] = 'Are you sure you want to duplicate this certificate template?';
$string['editcustomcert'] = 'Edit certificate';
$string['editelement'] = 'Edit element';
$string['edittemplate'] = 'Edit template';

View file

@ -61,39 +61,8 @@ if ($confirm && confirm_sesskey()) {
// Delete the pages.
$DB->delete_records('customcert_pages', array('templateid' => $template->get_id()));
// Store the current time in a variable.
$time = time();
// Now, get the template data we want to load.
if ($templatepages = $DB->get_records('customcert_pages', array('templateid' => $ltid))) {
// Loop through the pages.
foreach ($templatepages as $templatepage) {
$page = clone($templatepage);
$page->templateid = $tid;
$page->timecreated = $time;
$page->timemodified = $time;
// Insert into the database.
$page->id = $DB->insert_record('customcert_pages', $page);
// Now go through the elements we want to load.
if ($templateelements = $DB->get_records('customcert_elements', array('pageid' => $templatepage->id))) {
foreach ($templateelements as $templateelement) {
$element = clone($templateelement);
$element->pageid = $page->id;
$element->timecreated = $time;
$element->timemodified = $time;
// Ok, now we want to insert this into the database.
$element->id = $DB->insert_record('customcert_elements', $element);
// Load any other information the element may need to for the template.
if ($e = \mod_customcert\element_factory::get_element_instance($element)) {
if (!$e->copy_element($templateelement)) {
// Failed to copy - delete the element.
$e->delete();
}
}
}
}
}
}
// Copy the items across.
$loadtemplate->duplicate($template->get_id());
// Redirect.
$url = new moodle_url('/mod/customcert/edit.php', array('tid' => $tid));

View file

@ -53,21 +53,21 @@ $pageurl = new moodle_url('/mod/customcert/manage_templates.php');
// Additional page setup.
$PAGE->navbar->add(get_string('managetemplates', 'customcert'));
// Check if we are deleting a template.
if ($tid) {
if ($action && confirm_sesskey()) {
if ($action == 'delete') {
if (!$confirm) {
$nourl = new moodle_url('/mod/customcert/manage_templates.php');
$yesurl = new moodle_url('/mod/customcert/manage_templates.php',
array(
'tid' => $tid,
'action' => 'delete',
'action' => $action,
'confirm' => 1,
'sesskey' => sesskey()
)
);
// Check if we are deleting a template.
if ($action == 'delete') {
if (!$confirm) {
// Show a confirmation page.
$strheading = get_string('deleteconfirm', 'customcert');
$PAGE->navbar->add($strheading);
@ -83,6 +83,25 @@ if ($tid) {
// Delete the template.
$template->delete();
// Redirect back to the manage templates page.
redirect(new moodle_url('/mod/customcert/manage_templates.php'));
} else if ($action == 'duplicate') {
if (!$confirm) {
// Show a confirmation page.
$strheading = get_string('duplicateconfirm', 'customcert');
$PAGE->navbar->add($strheading);
$PAGE->set_title($strheading);
$message = get_string('duplicatetemplateconfirm', 'customcert');
echo $OUTPUT->header();
echo $OUTPUT->heading($strheading);
echo $OUTPUT->confirm($message, $yesurl, $nourl);
echo $OUTPUT->footer();
exit();
}
// Duplicate the template.
$template->duplicate();
// Redirect back to the manage templates page.
redirect(new moodle_url('/mod/customcert/manage_templates.php'));
}

View file

@ -82,3 +82,21 @@ Feature: Being able to manage site templates
And I click on ".delete-icon" "css_element" in the "Site template" "table_row"
And I press "Continue"
And I should not see "Site template"
Scenario: Duplicating a site template
And I navigate to "Plugins" in site administration
And I follow "Manage activities"
And I click on "Settings" "link" in the "Custom certificate" "table_row"
And I follow "Manage templates"
And I press "Create template"
And I set the field "Name" to "Site template"
And I press "Save changes"
And I follow "Manage templates"
And I click on ".duplicate-icon" "css_element" in the "Site template" "table_row"
And I press "Cancel"
And I should see "Site template"
And I should not see "Site template (duplicate)"
And I click on ".duplicate-icon" "css_element" in the "Site template" "table_row"
And I press "Continue"
And I should see "Site template"
And I should see "Site template (duplicate)"