示例#1
0
 /**
  * Remove a competency from a plan.
  *
  * @param int $planid The plan id
  * @param int $competencyid The id of the competency
  * @return bool
  */
 public static function remove_competency_from_plan($planid, $competencyid)
 {
     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 removed from a learning plan based on a template');
         }
     }
     if (!$plan->can_be_edited()) {
         throw new coding_exception('A competency can not be removed from a learning plan completed');
     }
     $link = plan_competency::get_record(array('planid' => $planid, 'competencyid' => $competencyid));
     if ($link) {
         return $link->delete();
     }
     return false;
 }
示例#2
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());
 }