/** * 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(); }
/** * 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()); }