#134 Renamed duplicate() function and modified behaviour

This commit is contained in:
Mark Nelson 2017-09-04 18:33:37 +08:00
parent 9507a87d46
commit 459244b417
3 changed files with 14 additions and 17 deletions

View file

@ -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; 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. // 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))) { if ($templatepages = $DB->get_records('customcert_pages', array('templateid' => $this->id))) {
// Loop through the pages. // Loop through the pages.

View file

@ -62,7 +62,7 @@ if ($confirm && confirm_sesskey()) {
$DB->delete_records('customcert_pages', array('templateid' => $template->get_id())); $DB->delete_records('customcert_pages', array('templateid' => $template->get_id()));
// Copy the items across. // Copy the items across.
$loadtemplate->duplicate($template->get_id()); $loadtemplate->copy_to_template($template->get_id());
// Redirect. // Redirect.
$url = new moodle_url('/mod/customcert/edit.php', array('tid' => $tid)); $url = new moodle_url('/mod/customcert/edit.php', array('tid' => $tid));

View file

@ -99,8 +99,16 @@ if ($tid) {
exit(); exit();
} }
// Duplicate the template. // Create another template to copy the data to.
$template->duplicate(); $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 back to the manage templates page.
redirect(new moodle_url('/mod/customcert/manage_templates.php')); redirect(new moodle_url('/mod/customcert/manage_templates.php'));