/** * Duplicate a competency framework by id. * * Requires moodle/competency:competencymanage capability at the system context. * * @param int $id The record to duplicate. All competencies associated and related will be duplicated. * @return competency_framework the framework duplicated */ public static function duplicate_framework($id) { global $DB; static::require_enabled(); $framework = new competency_framework($id); require_capability('moodle/competency:competencymanage', $framework->get_context()); // Starting transaction. $transaction = $DB->start_delegated_transaction(); try { // Get a uniq idnumber based on the origin framework. $idnumber = competency_framework::get_unused_idnumber($framework->get_idnumber()); $framework->set_idnumber($idnumber); // Adding the suffix copy to the shortname. $framework->set_shortname(get_string('duplicateditemname', 'core_competency', $framework->get_shortname())); $framework->set_id(0); $framework = $framework->create(); // Array that match the old competencies ids with the new one to use when copying related competencies. $frameworkcompetency = competency::get_framework_tree($id); $matchids = self::duplicate_competency_tree($framework->get_id(), $frameworkcompetency, 0, 0); // Copy the related competencies. $relcomps = related_competency::get_multiple_relations(array_keys($matchids)); foreach ($relcomps as $relcomp) { $compid = $relcomp->get_competencyid(); $relcompid = $relcomp->get_relatedcompetencyid(); if (isset($matchids[$compid]) && isset($matchids[$relcompid])) { $newcompid = $matchids[$compid]->get_id(); $newrelcompid = $matchids[$relcompid]->get_id(); if ($newcompid < $newrelcompid) { $relcomp->set_competencyid($newcompid); $relcomp->set_relatedcompetencyid($newrelcompid); } else { $relcomp->set_competencyid($newrelcompid); $relcomp->set_relatedcompetencyid($newcompid); } $relcomp->set_id(0); $relcomp->create(); } else { // Debugging message when there is no match found. debugging('related competency id not found'); } } // Setting rules on duplicated competencies. self::migrate_competency_tree_rules($frameworkcompetency, $matchids); $transaction->allow_commit(); } catch (\Exception $e) { $transaction->rollback($e); } // Trigger a competency framework created event. \core\event\competency_framework_created::create_from_framework($framework)->trigger(); return $framework; }
/** * Set-up a competency page. * * Example: * list($title, $subtitle) = page_helper::setup_for_competency($pagecontextid, $url, $competency, $pagetitle); * echo $OUTPUT->heading($title); * echo $OUTPUT->heading($subtitle, 3); * * @param int $pagecontextid The page context ID. * @param moodle_url $url The current page. * @param \core_competency\competency_framework $framework The competency framework. * @param \core_competency\competency $competency The competency, if any. * @param \core_competency\competency $parent The parent competency, if any. * @return array With the following: * - Page title * - Page sub title * - Return URL (main competencies page) * @throws coding_exception */ public static function setup_for_competency($pagecontextid, moodle_url $url, $framework, $competency = null, $parent = null) { global $PAGE, $SITE; // Set page context. $pagecontext = context::instance_by_id($pagecontextid); $PAGE->set_context($pagecontext); // Set page heading. if ($pagecontext->contextlevel == CONTEXT_SYSTEM) { $heading = $SITE->fullname; } else { if ($pagecontext->contextlevel == CONTEXT_COURSECAT) { $heading = $pagecontext->get_context_name(); } else { throw new coding_exception('Unexpected context!'); } } $PAGE->set_heading($heading); // Set override active url. $frameworksurl = new moodle_url('/admin/tool/lp/competencyframeworks.php', ['pagecontextid' => $pagecontextid]); $PAGE->navigation->override_active_url($frameworksurl); // Set return url. $returnurloptions = ['competencyframeworkid' => $framework->get_id(), 'pagecontextid' => $pagecontextid]; $returnurl = new moodle_url('/admin/tool/lp/competencies.php', $returnurloptions); $PAGE->navbar->add($framework->get_shortname(), $returnurl); // Set page layout. $PAGE->set_pagelayout('admin'); if (empty($competency)) { // Add mode. $title = format_string($framework->get_shortname(), true, ['context' => $pagecontext]); // Set the sub-title for add mode. $level = $parent ? $parent->get_level() + 1 : 1; $subtitle = get_string('taxonomy_add_' . $framework->get_taxonomy($level), 'tool_lp'); } else { // Edit mode. $title = format_string($competency->get_shortname(), true, ['context' => $competency->get_context()]); // Add competency name to breadcrumbs, if available. $PAGE->navbar->add($title); // Set the sub-title for edit mode. $subtitle = get_string('taxonomy_edit_' . $framework->get_taxonomy($competency->get_level()), 'tool_lp'); } // Set page title. $PAGE->set_title($title); // Set page url. $PAGE->set_url($url); // Add editing mode link to breadcrumbs, if available. if (!empty($subtitle)) { $PAGE->navbar->add($subtitle, $url); } return [$title, $subtitle, $returnurl]; }