From 459244b41740ffb72eeb268c7bd790c4e2953697 Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Mon, 4 Sep 2017 18:33:37 +0800 Subject: [PATCH] #134 Renamed duplicate() function and modified behaviour --- classes/template.php | 17 +++-------------- load_template.php | 2 +- manage_templates.php | 12 ++++++++++-- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/classes/template.php b/classes/template.php index 3c165ec..c713dcd 100644 --- a/classes/template.php +++ b/classes/template.php @@ -313,24 +313,13 @@ class template { } /** - * Handles duplicating the template. + * Handles copying this template into another. * - * @param int|null $copytotemplateid The template id to copy to, null if creating a new template + * @param int $copytotemplateid The template id to copy to */ - public function duplicate($copytotemplateid = null) { + public function copy_to_template($copytotemplateid) { 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. diff --git a/load_template.php b/load_template.php index 60f672e..e962e33 100644 --- a/load_template.php +++ b/load_template.php @@ -62,7 +62,7 @@ if ($confirm && confirm_sesskey()) { $DB->delete_records('customcert_pages', array('templateid' => $template->get_id())); // Copy the items across. - $loadtemplate->duplicate($template->get_id()); + $loadtemplate->copy_to_template($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 5ff0cd4..6cfe590 100644 --- a/manage_templates.php +++ b/manage_templates.php @@ -99,8 +99,16 @@ if ($tid) { exit(); } - // Duplicate the template. - $template->duplicate(); + // Create another template to copy the data to. + $newtemplate = new \stdClass(); + $newtemplate->name = $template->get_name() . ' (' . strtolower(get_string('duplicate', 'customcert')) . ')'; + $newtemplate->contextid = $template->get_contextid(); + $newtemplate->timecreated = time(); + $newtemplate->timemodified = $newtemplate->timecreated; + $newtemplateid = $DB->insert_record('customcert_templates', $newtemplate); + + // Copy the data to the new template. + $template->copy_to_template($newtemplateid); // Redirect back to the manage templates page. redirect(new moodle_url('/mod/customcert/manage_templates.php'));