/** * Unlink a plan from its template. * * @param \core_competency\plan|int $planorid The plan or its ID. * @return bool */ public static function unlink_plan_from_template($planorid) { global $DB; static::require_enabled(); $plan = $planorid; if (!is_object($planorid)) { $plan = new plan($planorid); } // The user must be allowed to manage the plans of the user, nothing about the template. if (!$plan->can_manage()) { throw new required_capability_exception($plan->get_context(), 'moodle/competency:planmanage', 'nopermissions', ''); } // Only plan with status DRAFT or ACTIVE can be unliked.. if ($plan->get_status() == plan::STATUS_COMPLETE) { throw new coding_exception('Only draft or active plan can be unliked from a template'); } // Early exit, it's already done... if (!$plan->is_based_on_template()) { return true; } // Fetch the template. $template = new template($plan->get_templateid()); // Now, proceed by copying all competencies to the plan, then update the plan. $transaction = $DB->start_delegated_transaction(); $competencies = template_competency::list_competencies($template->get_id(), false); $i = 0; foreach ($competencies as $competency) { $record = (object) array('planid' => $plan->get_id(), 'competencyid' => $competency->get_id(), 'sortorder' => $i++); $pc = new plan_competency(null, $record); $pc->create(); } $plan->set_origtemplateid($template->get_id()); $plan->set_templateid(null); $success = $plan->update(); $transaction->allow_commit(); // Trigger unlinked event. \core\event\competency_plan_unlinked::create_from_plan($plan)->trigger(); return $success; }
public function test_delete_template_unlink_plans() { $this->resetAfterTest(true); $this->setAdminUser(); $dg = $this->getDataGenerator(); $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency'); $u1 = $dg->create_user(); $f = $lpg->create_framework(); $c1 = $lpg->create_competency(array('competencyframeworkid' => $f->get_id())); $c2 = $lpg->create_competency(array('competencyframeworkid' => $f->get_id())); $tpl = $lpg->create_template(); $tplc1 = $lpg->create_template_competency(array('templateid' => $tpl->get_id(), 'competencyid' => $c1->get_id(), 'sortorder' => 1)); $tplc2 = $lpg->create_template_competency(array('templateid' => $tpl->get_id(), 'competencyid' => $c2->get_id(), 'sortorder' => 2)); $p1 = $lpg->create_plan(array('templateid' => $tpl->get_id(), 'userid' => $u1->id)); // Check pre-test. $this->assertTrue(\core_competency\template::record_exists($tpl->get_id())); $this->assertEquals(2, \core_competency\template_competency::count_competencies($tpl->get_id())); $this->assertEquals(1, count(\core_competency\plan::get_records(array('templateid' => $tpl->get_id())))); $result = api::delete_template($tpl->get_id(), false); $this->assertTrue($result); // Check that the template does not exist anymore. $this->assertFalse(\core_competency\template::record_exists($tpl->get_id())); // Check that associated competencies are also deleted. $this->assertEquals(0, \core_competency\template_competency::count_competencies($tpl->get_id())); // Check that associated plan still exist but unlink from template. $plans = \core_competency\plan::get_records(array('id' => $p1->get_id())); $this->assertEquals(1, count($plans)); $this->assertEquals($plans[0]->get_origtemplateid(), $tpl->get_id()); $this->assertNull($plans[0]->get_templateid()); }
/** * Create a template competency. * * @param array|stdClass $record * @return template_competency */ public function create_template_competency($record = null) { $record = (object) $record; if (!isset($record->competencyid)) { throw new coding_exception('Property competencyid is required.'); } if (!isset($record->templateid)) { throw new coding_exception('Property templateid is required.'); } $relation = new template_competency(0, $record); $relation->create(); return $relation; }
/** * Check if we can delete competencies safely. * * This moethod does not check any capablities. * Check if competency is used in a plan and user competency. * Check if competency is used in a template. * Check if competency is linked to a course. * * @param array $ids Array of competencies ids. * @return bool True if we can delete the competencies. */ public static function can_all_be_deleted($ids) { if (empty($ids)) { return true; } // Check if competency is used in template. if (template_competency::has_records_for_competencies($ids)) { return false; } // Check if competency is used in plan. if (plan_competency::has_records_for_competencies($ids)) { return false; } // Check if competency is used in course. if (course_competency::has_records_for_competencies($ids)) { return false; } // Check if competency is used in user_competency. if (user_competency::has_records_for_competencies($ids)) { return false; } // Check if competency is used in user_competency_plan. if (user_competency_plan::has_records_for_competencies($ids)) { return false; } return true; }
/** * Test we can re-order competency frameworks. */ public function test_reorder_template_competencies() { $this->setUser($this->creator); $syscontext = context_system::instance(); $onehour = time() + 60 * 60; // Create a template. $template = $this->create_template(1, true); // Create a competency framework. $framework = $this->create_competency_framework(1, true); // Create multiple competencies. $competency1 = $this->create_competency(1, $framework->id); $competency2 = $this->create_competency(2, $framework->id); $competency3 = $this->create_competency(3, $framework->id); $competency4 = $this->create_competency(4, $framework->id); // Add the competencies. external::add_competency_to_template($template->id, $competency1->id); external::add_competency_to_template($template->id, $competency2->id); external::add_competency_to_template($template->id, $competency3->id); external::add_competency_to_template($template->id, $competency4->id); // Test if removing competency from template don't create sortorder holes. external::remove_competency_from_template($template->id, $competency3->id); $templcomp4 = template_competency::get_record(array('templateid' => $template->id, 'competencyid' => $competency4->id)); $this->assertEquals(2, $templcomp4->get_sortorder()); // This is a move up. external::reorder_template_competency($template->id, $competency4->id, $competency2->id); $result = external::list_competencies_in_template($template->id); $result = external_api::clean_returnvalue(external::list_competencies_in_template_returns(), $result); $r1 = (object) $result[0]; $r2 = (object) $result[1]; $r3 = (object) $result[2]; $this->assertEquals($competency1->id, $r1->id); $this->assertEquals($competency4->id, $r2->id); $this->assertEquals($competency2->id, $r3->id); // This is a move down. external::reorder_template_competency($template->id, $competency1->id, $competency4->id); $result = external::list_competencies_in_template($template->id); $result = external_api::clean_returnvalue(external::list_competencies_in_template_returns(), $result); $r1 = (object) $result[0]; $r2 = (object) $result[1]; $r3 = (object) $result[2]; $this->assertEquals($competency4->id, $r1->id); $this->assertEquals($competency1->id, $r2->id); $this->assertEquals($competency2->id, $r3->id); $this->setExpectedException('required_capability_exception'); $this->setUser($this->user); external::reorder_template_competency($template->id, $competency1->id, $competency2->id); }
public function test_create_template_competency() { $this->resetAfterTest(true); $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency'); $this->assertEquals(0, template_competency::count_records()); $framework = $lpg->create_framework(); $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $template = $lpg->create_template(); $relation = $lpg->create_template_competency(array('competencyid' => $c1->get_id(), 'templateid' => $template->get_id())); $relation = $lpg->create_template_competency(array('competencyid' => $c2->get_id(), 'templateid' => $template->get_id())); $this->assertEquals(2, template_competency::count_records()); $this->assertInstanceOf('\\core_competency\\template_competency', $relation); }
/** * Get a single competency from this plan. * * This will throw an exception if the competency does not belong to the plan. * * @param int $competencyid The competency ID. * @return competency */ public function get_competency($competencyid) { $competency = null; if ($this->get_status() == self::STATUS_COMPLETE) { // Get the competency from the archive of the plan. $competency = user_competency_plan::get_competency_by_planid($this->get_id(), $competencyid); } else { if ($this->is_based_on_template()) { // Get the competency from the template. $competency = template_competency::get_competency($this->get_templateid(), $competencyid); } else { // Get the competency from the plan. $competency = plan_competency::get_competency($this->get_id(), $competencyid); } } return $competency; }