From 9507a87d462f69d31fcdebf71092d11ab64516ac Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Sun, 3 Sep 2017 19:50:42 +0800 Subject: [PATCH] #134 Add ability to duplicate templates --- classes/manage_templates_table.php | 13 ++++++- classes/template.php | 51 ++++++++++++++++++++++++++ lang/en/customcert.php | 3 ++ load_template.php | 35 +----------------- manage_templates.php | 41 +++++++++++++++------ tests/behat/managing_templates.feature | 18 +++++++++ 6 files changed, 116 insertions(+), 45 deletions(-) diff --git a/classes/manage_templates_table.php b/classes/manage_templates_table.php index 3c03036..8aad21c 100644 --- a/classes/manage_templates_table.php +++ b/classes/manage_templates_table.php @@ -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; } /** diff --git a/classes/template.php b/classes/template.php index 97bdf2e..3c165ec 100644 --- a/classes/template.php +++ b/classes/template.php @@ -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. * diff --git a/lang/en/customcert.php b/lang/en/customcert.php index 47f6b44..bf04bb7 100644 --- a/lang/en/customcert.php +++ b/lang/en/customcert.php @@ -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'; diff --git a/load_template.php b/load_template.php index 2e5f5c5..60f672e 100644 --- a/load_template.php +++ b/load_template.php @@ -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)); diff --git a/manage_templates.php b/manage_templates.php index 6b413c9..5ff0cd4 100644 --- a/manage_templates.php +++ b/manage_templates.php @@ -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()) { + $nourl = new moodle_url('/mod/customcert/manage_templates.php'); + $yesurl = new moodle_url('/mod/customcert/manage_templates.php', + array( + 'tid' => $tid, + 'action' => $action, + 'confirm' => 1, + 'sesskey' => sesskey() + ) + ); + + // Check if we are deleting a template. 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', - 'confirm' => 1, - 'sesskey' => sesskey() - ) - ); - // 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')); } diff --git a/tests/behat/managing_templates.feature b/tests/behat/managing_templates.feature index 5abb9eb..a79fabc 100644 --- a/tests/behat/managing_templates.feature +++ b/tests/behat/managing_templates.feature @@ -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)"