MDL-52220 tool_lp: API method to unlink a plan from a template

This commit is contained in:
Frederic Massart 2015-11-19 19:54:00 +08:00
parent 4de877eba0
commit 6d2c2e8674
7 changed files with 136 additions and 1 deletions

View File

@ -1368,6 +1368,54 @@ class api {
return $plan;
}
/**
* Unlink a plan from its template.
*
* @param \tool_lp\plan|int $planorid The plan or its ID.
* @return bool
*/
public static function unlink_plan_from_template($planorid) {
global $DB;
$plan = $planorid;
if (!is_object($planorid)) {
$plan = new plan($planorid);
}
// The user must be allowed to manage the plans of the user.
if (!$plan->can_manage()) {
throw new required_capability_exception($plan->get_context(), 'tool/lp:planmanage', 'nopermissions', '');
}
// Early exit, it's already done...
if (!$plan->is_based_on_template()) {
return true;
}
// Fetch the template.
$template = new template($plan->get_templateid());
// Now, proceed by copying all competencies to the plan, then update the plan.
$transaction = $DB->start_delegated_transaction();
$competencies = template_competency::list_competencies($template->get_id(), false);
$i = 0;
foreach ($competencies as $competency) {
$record = (object) array(
'planid' => $plan->get_id(),
'competencyid' => $competency->get_id(),
'sortorder' => $i++
);
$pc = new plan_competency(null, $record);
$pc->create();
}
$plan->set_origtemplateid($template->get_id());
$plan->set_templateid(null);
$success = $plan->update();
$transaction->allow_commit();
return $success;
}
/**
* Updates a plan.
*

View File

@ -73,6 +73,11 @@ class plan extends persistent {
'default' => null,
'null' => NULL_ALLOWED,
),
'origtemplateid' => array(
'type' => PARAM_INT,
'default' => null,
'null' => NULL_ALLOWED,
),
'status' => array(
'choices' => array(self::STATUS_DRAFT, self::STATUS_COMPLETE, self::STATUS_ACTIVE),
'type' => PARAM_INT,

View File

@ -73,6 +73,7 @@ class plan_competency extends persistent {
ORDER BY plancomp.sortorder ASC';
$params = array($planid);
// TODO MDL-52229 Handle hidden competencies.
$results = $DB->get_records_sql($sql, $params);
$instances = array();

View File

@ -78,6 +78,7 @@
<FIELD NAME="descriptionformat" TYPE="int" LENGTH="4" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="templateid" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="origtemplateid" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="The template ID this plan was based on originally"/>
<FIELD NAME="status" TYPE="int" LENGTH="1" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="duedate" TYPE="int" LENGTH="10" NOTNULL="false" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="timecreated" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>

View File

@ -444,5 +444,20 @@ function xmldb_tool_lp_upgrade($oldversion) {
upgrade_plugin_savepoint(true, 2015111012, 'tool', 'lp');
}
if ($oldversion < 2015111013) {
// Define field origtemplateid to be added to tool_lp_plan.
$table = new xmldb_table('tool_lp_plan');
$field = new xmldb_field('origtemplateid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'templateid');
// Conditionally launch add field origtemplateid.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Lp savepoint reached.
upgrade_plugin_savepoint(true, 2015111013, 'tool', 'lp');
}
return true;
}

View File

@ -489,6 +489,71 @@ class tool_lp_api_testcase extends advanced_testcase {
}
}
public function test_unlink_plan_from_template() {
$this->resetAfterTest(true);
$dg = $this->getDataGenerator();
$lpg = $dg->get_plugin_generator('tool_lp');
$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()));
$plan2 = $lpg->create_plan(array('userid' => $u2->id, 'templateid' => $tpl2->get_id()));
// Check that we have what we expect at this stage.
$this->assertEquals(2, \tool_lp\template_competency::count_records(array('templateid' => $tpl1->get_id())));
$this->assertEquals(1, \tool_lp\template_competency::count_records(array('templateid' => $tpl2->get_id())));
$this->assertEquals(0, \tool_lp\plan_competency::count_records(array('planid' => $plan1->get_id())));
$this->assertEquals(0, \tool_lp\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 = \tool_lp\template_competency::list_competencies($tpl1->get_id(), true);
$tpl2comps = \tool_lp\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, \tool_lp\template_competency::count_records(array('templateid' => $tpl1->get_id())));
$this->assertEquals(1, \tool_lp\template_competency::count_records(array('templateid' => $tpl2->get_id())));
$this->assertEquals(2, \tool_lp\plan_competency::count_records(array('planid' => $plan1->get_id())));
$this->assertEquals(0, \tool_lp\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());
// Even the order remains.
$plan1comps = \tool_lp\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());
}
/**
* Test that the method to complete a plan.
*/

View File

@ -25,7 +25,7 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2015111012; // The current plugin version (Date: YYYYMMDDXX).
$plugin->version = 2015111013; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2014110400; // Requires this Moodle version.
$plugin->component = 'tool_lp'; // Full name of the plugin (used for diagnostics).