/** * Duplicate a learning plan template. * * Requires moodle/competency:templatemanage capability at the template context. * * @param int $id the template id. * @return template */ public static function duplicate_template($id) { static::require_enabled(); $template = new template($id); // First we do a permissions check. if (!$template->can_manage()) { throw new required_capability_exception($template->get_context(), 'moodle/competency:templatemanage', 'nopermissions', ''); } // OK - all set. $competencies = template_competency::list_competencies($id, false); // Adding the suffix copy. $template->set_shortname(get_string('duplicateditemname', 'core_competency', $template->get_shortname())); $template->set_id(0); $duplicatedtemplate = $template->create(); // Associate each competency for the duplicated template. foreach ($competencies as $competency) { self::add_competency_to_template($duplicatedtemplate->get_id(), $competency->get_id()); } // Trigger a template created event. \core\event\competency_template_created::create_from_template($duplicatedtemplate)->trigger(); return $duplicatedtemplate; }
/** * Create a template. * * @param array|stdClass $record * @return template */ public function create_template($record = null) { $this->templatecount++; $i = $this->templatecount; $record = (object) $record; if (!isset($record->shortname)) { $record->shortname = "Template shortname {$i}"; } if (!isset($record->description)) { $record->description = "Template {$i} description "; } if (!isset($record->contextid)) { $record->contextid = context_system::instance()->id; } $template = new template(0, $record); $template->create(); return $template; }