/** * Convenience method to instantiate the event. * * @param user_competency_plan $usercompetencyplan The user competency plan. * @return self */ public static function create_from_user_competency_plan(user_competency_plan $usercompetencyplan) { if (!$usercompetencyplan->get_id()) { throw new \coding_exception('The user competency plan ID must be set.'); } $event = static::create(array('contextid' => $usercompetencyplan->get_context()->id, 'objectid' => $usercompetencyplan->get_id(), 'relateduserid' => $usercompetencyplan->get_userid(), 'other' => array('planid' => $usercompetencyplan->get_planid(), 'competencyid' => $usercompetencyplan->get_competencyid()))); $event->add_record_snapshot(user_competency_plan::TABLE, $usercompetencyplan->to_record()); return $event; }
/** * Test that completed plan created form a template does not change when template is modified. */ public function test_completed_plan_doesnot_change() { global $DB; $this->resetAfterTest(true); $this->setAdminUser(); $dg = $this->getDataGenerator(); $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency'); $user = $dg->create_user(); // Create a framework and assign competencies. $framework = $lpg->create_framework(); $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $c3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $c4 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); // Create template and assign competencies. $tp = $lpg->create_template(); $tpc1 = $lpg->create_template_competency(array('templateid' => $tp->get_id(), 'competencyid' => $c1->get_id())); $tpc2 = $lpg->create_template_competency(array('templateid' => $tp->get_id(), 'competencyid' => $c2->get_id())); $tpc3 = $lpg->create_template_competency(array('templateid' => $tp->get_id(), 'competencyid' => $c3->get_id())); // Create a plan form template and change it status to complete. $plan = $lpg->create_plan(array('userid' => $user->id, 'templateid' => $tp->get_id())); api::complete_plan($plan); // Check user competency plan created correctly. $this->assertEquals(3, \core_competency\user_competency_plan::count_records()); $ucp = \core_competency\user_competency_plan::get_records(); $this->assertEquals($ucp[0]->get_competencyid(), $c1->get_id()); $this->assertEquals($ucp[1]->get_competencyid(), $c2->get_id()); $this->assertEquals($ucp[2]->get_competencyid(), $c3->get_id()); // Add and remove a competency from the template. api::add_competency_to_template($tp->get_id(), $c4->get_id()); api::remove_competency_from_template($tp->get_id(), $c1->get_id()); // Check that user competency plan did not change. $competencies = $plan->get_competencies(); $this->assertEquals(3, count($competencies)); $ucp1 = array($c1->get_id(), $c2->get_id(), $c3->get_id()); $ucp2 = array(); foreach ($competencies as $id => $cmp) { $ucp2[] = $id; } $this->assertEquals(0, count(array_diff($ucp1, $ucp2))); }
/** * Create a new user competency plan. * * @param array|stdClass $record * @return user_competency_plan */ public function create_user_competency_plan($record = null) { $record = (object) $record; if (!isset($record->userid)) { throw new coding_exception('The userid value is required.'); } if (!isset($record->competencyid)) { throw new coding_exception('The competencyid value is required.'); } if (!isset($record->planid)) { throw new coding_exception('The planid value is required.'); } if (!isset($record->sortorder)) { $record->sortorder = 0; } $usercompetencyplan = new user_competency_plan(0, $record); $usercompetencyplan->create(); return $usercompetencyplan; }
/** * Get the most often not completed competency for this template. * * Requires moodle/competency:templateview capability at the system context. * * @param mixed $templateorid The id or the template. * @param int $skip The number of records to skip * @param int $limit The max number of records to return * @return competency[] */ public static function get_least_proficient_competencies_for_template($templateorid, $skip = 0, $limit = 100) { static::require_enabled(); $template = $templateorid; if (!is_object($template)) { $template = new template($template); } // First we do a permissions check. if (!$template->can_read()) { throw new required_capability_exception($template->get_context(), 'moodle/competency:templateview', 'nopermissions', ''); } return user_competency_plan::get_least_proficient_competencies_for_template($template->get_id(), $skip, $limit); }
/** * Returns true when some competencies of the framework have user competencies. * * This is useful to determine if the framework, or part of it, should be locked down. * * @return boolean */ public function has_user_competencies() { return user_competency::has_records_for_framework($this->get_id()) || user_competency_plan::has_records_for_framework($this->get_id()); }
/** * 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; }
public function test_delete_plan_removes_relations() { $this->setAdminUser(); $dg = $this->getDataGenerator(); $lpg = $dg->get_plugin_generator('core_competency'); $user = $dg->create_user(); $plan = $lpg->create_plan(array('userid' => $user->id)); $framework = $lpg->create_framework(); $comp1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $comp2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $comp3 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $pc1 = $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $comp1->get_id())); $pc2 = $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $comp2->get_id())); $pc3 = $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $comp3->get_id())); // Complete the plan to generate user_competency_plan entries. api::complete_plan($plan); // Confirm the data we have. $this->assertEquals(3, plan_competency::count_records(array('planid' => $plan->get_id()))); $this->assertEquals(3, user_competency_plan::count_records(array('planid' => $plan->get_id(), 'userid' => $user->id))); // Delete the plan now. api::delete_plan($plan->get_id()); $this->assertEquals(0, plan_competency::count_records(array('planid' => $plan->get_id()))); $this->assertEquals(0, user_competency_plan::count_records(array('planid' => $plan->get_id(), 'userid' => $user->id))); }
public function test_create_user_competency_plan() { $this->resetAfterTest(true); $user = $this->getDataGenerator()->create_user(); $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency'); $framework = $lpg->create_framework(); $c1 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $c2 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id())); $plan = $lpg->create_plan(array('userid' => $user->id)); $this->assertEquals(0, user_competency_plan::count_records()); $ucp = $lpg->create_user_competency_plan(array('userid' => $user->id, 'competencyid' => $c1->get_id(), 'planid' => $plan->get_id())); $ucp = $lpg->create_user_competency_plan(array('userid' => $user->id, 'competencyid' => $c2->get_id(), 'planid' => $plan->get_id())); $this->assertEquals(2, user_competency_plan::count_records()); $this->assertInstanceOf('\\core_competency\\user_competency_plan', $ucp); }
/** * 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; }