Ejemplo n.º 1
0
 /**
  * Move the plan competency up or down in the display list.
  *
  * Requires moodle/competency:planmanage capability at the system context.
  *
  * @param int $planid The plan  id
  * @param int $competencyidfrom The id of the competency we are moving.
  * @param int $competencyidto The id of the competency we are moving to.
  * @return boolean
  */
 public static function reorder_plan_competency($planid, $competencyidfrom, $competencyidto)
 {
     static::require_enabled();
     $plan = new plan($planid);
     // First we do a permissions check.
     if (!$plan->can_manage()) {
         $context = context_user::instance($plan->get_userid());
         throw new required_capability_exception($context, 'moodle/competency:planmanage', 'nopermissions', '');
     } else {
         if ($plan->is_based_on_template()) {
             throw new coding_exception('A competency can not be reordered in a learning plan based on a template');
         }
     }
     if (!$plan->can_be_edited()) {
         throw new coding_exception('A competency can not be reordered in a learning plan completed');
     }
     $down = true;
     $matches = plan_competency::get_records(array('planid' => $planid, 'competencyid' => $competencyidfrom));
     if (count($matches) == 0) {
         throw new coding_exception('The link does not exist');
     }
     $competencyfrom = array_pop($matches);
     $matches = plan_competency::get_records(array('planid' => $planid, 'competencyid' => $competencyidto));
     if (count($matches) == 0) {
         throw new coding_exception('The link does not exist');
     }
     $competencyto = array_pop($matches);
     $all = plan_competency::get_records(array('planid' => $planid), 'sortorder', 'ASC', 0, 0);
     if ($competencyfrom->get_sortorder() > $competencyto->get_sortorder()) {
         // We are moving up, so put it before the "to" item.
         $down = false;
     }
     foreach ($all as $id => $plancompetency) {
         $sort = $plancompetency->get_sortorder();
         if ($down && $sort > $competencyfrom->get_sortorder() && $sort <= $competencyto->get_sortorder()) {
             $plancompetency->set_sortorder($plancompetency->get_sortorder() - 1);
             $plancompetency->update();
         } else {
             if (!$down && $sort >= $competencyto->get_sortorder() && $sort < $competencyfrom->get_sortorder()) {
                 $plancompetency->set_sortorder($plancompetency->get_sortorder() + 1);
                 $plancompetency->update();
             }
         }
     }
     $competencyfrom->set_sortorder($competencyto->get_sortorder());
     return $competencyfrom->update();
 }
Ejemplo n.º 2
0
 public function test_unlink_plan_from_template()
 {
     $this->resetAfterTest(true);
     $dg = $this->getDataGenerator();
     $lpg = $dg->get_plugin_generator('core_competency');
     $u1 = $dg->create_user();
     $u2 = $dg->create_user();
     $this->setAdminUser();
     $f1 = $lpg->create_framework();
     $f2 = $lpg->create_framework();
     $c1a = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
     $c2a = $lpg->create_competency(array('competencyframeworkid' => $f2->get_id()));
     $c1b = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id()));
     $tpl1 = $lpg->create_template();
     $tpl2 = $lpg->create_template();
     $tplc1a = $lpg->create_template_competency(array('templateid' => $tpl1->get_id(), 'competencyid' => $c1a->get_id(), 'sortorder' => 9));
     $tplc1b = $lpg->create_template_competency(array('templateid' => $tpl1->get_id(), 'competencyid' => $c1b->get_id(), 'sortorder' => 8));
     $tplc2a = $lpg->create_template_competency(array('templateid' => $tpl2->get_id(), 'competencyid' => $c2a->get_id()));
     $plan1 = $lpg->create_plan(array('userid' => $u1->id, 'templateid' => $tpl1->get_id(), 'status' => plan::STATUS_ACTIVE));
     $plan2 = $lpg->create_plan(array('userid' => $u2->id, 'templateid' => $tpl2->get_id()));
     $plan3 = $lpg->create_plan(array('userid' => $u1->id, 'templateid' => $tpl1->get_id(), 'status' => plan::STATUS_COMPLETE));
     // Check that we have what we expect at this stage.
     $this->assertEquals(2, \core_competency\template_competency::count_records(array('templateid' => $tpl1->get_id())));
     $this->assertEquals(1, \core_competency\template_competency::count_records(array('templateid' => $tpl2->get_id())));
     $this->assertEquals(0, \core_competency\plan_competency::count_records(array('planid' => $plan1->get_id())));
     $this->assertEquals(0, \core_competency\plan_competency::count_records(array('planid' => $plan2->get_id())));
     $this->assertTrue($plan1->is_based_on_template());
     $this->assertTrue($plan2->is_based_on_template());
     // Let's do this!
     $tpl1comps = \core_competency\template_competency::list_competencies($tpl1->get_id(), true);
     $tpl2comps = \core_competency\template_competency::list_competencies($tpl2->get_id(), true);
     api::unlink_plan_from_template($plan1);
     $plan1->read();
     $plan2->read();
     $this->assertCount(2, $tpl1comps);
     $this->assertCount(1, $tpl2comps);
     $this->assertEquals(2, \core_competency\template_competency::count_records(array('templateid' => $tpl1->get_id())));
     $this->assertEquals(1, \core_competency\template_competency::count_records(array('templateid' => $tpl2->get_id())));
     $this->assertEquals(2, \core_competency\plan_competency::count_records(array('planid' => $plan1->get_id())));
     $this->assertEquals(0, \core_competency\plan_competency::count_records(array('planid' => $plan2->get_id())));
     $this->assertFalse($plan1->is_based_on_template());
     $this->assertEquals($tpl1->get_id(), $plan1->get_origtemplateid());
     $this->assertTrue($plan2->is_based_on_template());
     $this->assertEquals(null, $plan2->get_origtemplateid());
     // Check we can unlink draft plan.
     try {
         api::unlink_plan_from_template($plan2);
     } catch (coding_exception $e) {
         $this->fail('Fail to unlink draft plan.');
     }
     // Check we can not unlink completed plan.
     try {
         api::unlink_plan_from_template($plan3);
         $this->fail('We can not unlink completed plan.');
     } catch (coding_exception $e) {
         // All good.
     }
     // Even the order remains.
     $plan1comps = \core_competency\plan_competency::list_competencies($plan1->get_id());
     $before = reset($tpl1comps);
     $after = reset($plan1comps);
     $this->assertEquals($before->get_id(), $after->get_id());
     $this->assertEquals($before->get_sortorder(), $after->get_sortorder());
     $before = next($tpl1comps);
     $after = next($plan1comps);
     $this->assertEquals($before->get_id(), $after->get_id());
     $this->assertEquals($before->get_sortorder(), $after->get_sortorder());
 }
Ejemplo n.º 3
0
 /**
  * Create a new plan competency.
  *
  * @param array|stdClass $record
  * @return plan_competency
  */
 public function create_plan_competency($record = null)
 {
     $record = (object) $record;
     if (!isset($record->planid)) {
         throw new coding_exception('The planid value is required.');
     }
     if (!isset($record->competencyid)) {
         throw new coding_exception('The competencyid value is required.');
     }
     $plancompetency = new plan_competency(0, $record);
     $plancompetency->create();
     return $plancompetency;
 }
Ejemplo n.º 4
0
 /**
  * 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;
 }
Ejemplo n.º 5
0
 /**
  * Test that we can add competency to plan if we have the right capability.
  *
  * @return void
  */
 public function test_reorder_plan_competency()
 {
     $this->resetAfterTest(true);
     $dg = $this->getDataGenerator();
     $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
     $usermanage = $dg->create_user();
     $user = $dg->create_user();
     $syscontext = context_system::instance();
     // Creating specific roles.
     $managerole = $dg->create_role(array('name' => 'User manage', 'shortname' => 'manage'));
     assign_capability('moodle/competency:planmanage', CAP_ALLOW, $managerole, $syscontext->id);
     assign_capability('moodle/competency:planview', CAP_ALLOW, $managerole, $syscontext->id);
     $dg->role_assign($managerole, $usermanage->id, $syscontext->id);
     $this->setUser($usermanage);
     $plan = array('userid' => $usermanage->id, 'status' => \core_competency\plan::STATUS_ACTIVE);
     $pl1 = $lpg->create_plan($plan);
     $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()));
     $c5 = $lpg->create_competency(array('competencyframeworkid' => $framework->get_id()));
     $lpg->create_plan_competency(array('planid' => $pl1->get_id(), 'competencyid' => $c1->get_id(), 'sortorder' => 1));
     $lpg->create_plan_competency(array('planid' => $pl1->get_id(), 'competencyid' => $c2->get_id(), 'sortorder' => 2));
     $lpg->create_plan_competency(array('planid' => $pl1->get_id(), 'competencyid' => $c3->get_id(), 'sortorder' => 3));
     $lpg->create_plan_competency(array('planid' => $pl1->get_id(), 'competencyid' => $c4->get_id(), 'sortorder' => 4));
     $lpg->create_plan_competency(array('planid' => $pl1->get_id(), 'competencyid' => $c5->get_id(), 'sortorder' => 5));
     // Test if removing competency from plan don't create sortorder holes.
     external::remove_competency_from_plan($pl1->get_id(), $c4->get_id());
     $plancomp5 = plan_competency::get_record(array('planid' => $pl1->get_id(), 'competencyid' => $c5->get_id()));
     $this->assertEquals(3, $plancomp5->get_sortorder());
     $this->assertTrue(external::reorder_plan_competency($pl1->get_id(), $c2->get_id(), $c5->get_id()));
     $this->assertTrue(external::reorder_plan_competency($pl1->get_id(), $c3->get_id(), $c1->get_id()));
     $plancompetencies = plan_competency::get_records(array('planid' => $pl1->get_id()), 'sortorder', 'ASC');
     $plcmp1 = $plancompetencies[0];
     $plcmp2 = $plancompetencies[1];
     $plcmp3 = $plancompetencies[2];
     $plcmp4 = $plancompetencies[3];
     $this->assertEquals($plcmp1->get_competencyid(), $c3->get_id());
     $this->assertEquals($plcmp2->get_competencyid(), $c1->get_id());
     $this->assertEquals($plcmp3->get_competencyid(), $c5->get_id());
     $this->assertEquals($plcmp4->get_competencyid(), $c2->get_id());
 }
Ejemplo n.º 6
0
 public function test_create_plan_competency()
 {
     $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));
     $pc1 = $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c1->get_id()));
     $pc2 = $lpg->create_plan_competency(array('planid' => $plan->get_id(), 'competencyid' => $c2->get_id()));
     $this->assertEquals(2, plan_competency::count_records());
     $this->assertInstanceOf('\\core_competency\\plan_competency', $pc1);
     $this->assertInstanceOf('\\core_competency\\plan_competency', $pc2);
     $this->assertEquals($plan->get_id(), $pc1->get_planid());
 }
Ejemplo n.º 7
0
 /**
  * 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;
 }