MDL-52242 tool_lp: Competencies listed in a completed plan must never change

This commit is contained in:
Serge Gauthier
2016-04-18 10:58:47 +08:00
committed by Frederic Massart
parent 5475357231
commit d327579538
4 changed files with 113 additions and 11 deletions
+12 -1
View File
@@ -1738,9 +1738,10 @@ class api {
// Check if the plan was already completed.
if ($plan->get_status() == plan::STATUS_COMPLETE) {
throw new coding_exception('The plan is already complete.');
throw new coding_exception('The plan is already completed.');
}
$originalstatus = $plan->get_status();
$plan->set_status(plan::STATUS_COMPLETE);
// The user should also be able to manage the plan when it's completed.
@@ -1748,9 +1749,13 @@ class api {
throw new required_capability_exception($plan->get_context(), 'tool/lp:planmanage', 'nopermissions', '');
}
// Put back original status because archive needs it to extract competencies from the right table.
$plan->set_status($originalstatus);
// Do the things.
$transaction = $DB->start_delegated_transaction();
self::archive_user_competencies_in_plan($plan);
$plan->set_status(plan::STATUS_COMPLETE);
$success = $plan->update();
if (!$success) {
@@ -2328,6 +2333,12 @@ class api {
* @return void
*/
protected static function archive_user_competencies_in_plan($plan) {
// Check if the plan was already completed.
if ($plan->get_status() == plan::STATUS_COMPLETE) {
throw new coding_exception('The plan is already completed.');
}
$competencies = $plan->get_competencies();
$usercompetencies = user_competency::get_multiple($plan->get_userid(), $competencies);
+6 -1
View File
@@ -122,13 +122,18 @@ class plan extends persistent {
*/
public function get_competencies() {
$competencies = array();
if ($this->get_templateid()) {
if ($this->get_status() == self::STATUS_COMPLETE) {
// Get the competencies from the archive of the plan.
$competencies = user_competency_plan::list_competencies($this->get_id(), $this->get_userid());
} else if ($this->is_based_on_template()) {
// Get the competencies from the template.
$competencies = template_competency::list_competencies($this->get_templateid(), true);
} else {
// Get the competencies from the plan.
$competencies = plan_competency::list_competencies($this->get_id());
}
return $competencies;
}
@@ -161,6 +161,36 @@ class user_competency_plan extends persistent {
return $relation;
}
/**
* List the competencies in this plan.
*
* @param int $planid The plan ID
* @param int $userid The user ID
* @return competency[]
*/
public static function list_competencies($planid, $userid) {
global $DB;
// TODO Fix ordering. The order set in template_competency, or plan_competency is not applied here.
// Perhaps we should have copied the sortorder here as well.
$sql = 'SELECT c.*
FROM {' . competency::TABLE . '} c
JOIN {' . self::TABLE . '} ucp
ON ucp.competencyid = c.id
AND ucp.userid = :userid
WHERE ucp.planid = :planid';
$params = array('userid' => $userid, 'planid' => $planid);
$results = $DB->get_recordset_sql($sql, $params);
$instances = array();
foreach ($results as $key => $result) {
$instances[$key] = new competency(0, $result);
}
$results->close();
return $instances;
}
/**
* Get multiple user_competency_plan for a user.
*
+65 -9
View File
@@ -667,6 +667,7 @@ class tool_lp_api_testcase extends advanced_testcase {
$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 two plans and assign competencies.
$plan = $lpg->create_plan(array('userid' => $user->id));
@@ -714,6 +715,20 @@ class tool_lp_api_testcase extends advanced_testcase {
$this->assertNull($usercompetenciesplan[2]->get_grade());
$this->assertEquals($plan->get_id(), $usercompetenciesplan[2]->get_planid());
// Check we can not add competency to completed plan.
try {
api::add_competency_to_plan($plan->get_id(), $c4->get_id());
$this->fail('We can not add competency to completed plan.');
} catch (coding_exception $e) {
}
// Check we can not remove competency to completed plan.
try {
api::remove_competency_from_plan($plan->get_id(), $c3->get_id());
$this->fail('We can not remove competency to completed plan.');
} catch (coding_exception $e) {
}
// Completing a plan that is completed throws an exception.
$this->setExpectedException('coding_exception');
api::complete_plan($plan);
@@ -938,15 +953,6 @@ class tool_lp_api_testcase extends advanced_testcase {
'planid' => $completedplan->get_id()));
$ucp2 = $lpg->create_user_competency_plan(array('userid' => $user->id, 'competencyid' => $c2->get_id(),
'planid' => $completedplan->get_id()));
// Check that an exception is thrown when a user competency plan is missing.
try {
$plancompetencies = api::list_plan_competencies($completedplan);
$this->fail('All competencies in the plan must be associated to a user competency plan');
} catch (coding_exception $e) {
$this->assertTrue(true);
}
$ucp3 = $lpg->create_user_competency_plan(array('userid' => $user->id, 'competencyid' => $c3->get_id(),
'planid' => $completedplan->get_id()));
@@ -1698,4 +1704,54 @@ class tool_lp_api_testcase extends advanced_testcase {
$this->assertInstanceOf('\tool_lp\template_cohort', $templatecohort);
}
/**
* 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('tool_lp');
$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, \tool_lp\user_competency_plan::count_records());
$ucp = \tool_lp\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)));
}
}