MDL-52196 tool_lp: API to create a template/cohort relation

This commit is contained in:
Frederic Massart
2016-04-18 10:58:42 +08:00
parent 474090c80e
commit bee480a4e7
4 changed files with 163 additions and 0 deletions
+66
View File
@@ -1198,6 +1198,72 @@ class api {
return $competencyfrom->update();
}
/**
* Create a relation between a template and a cohort.
*
* This silently ignores when the relation already existed.
*
* @param template|int $templateorid The template or its ID.
* @param stdClass|int $cohortid The cohort ot its ID.
* @return template_cohort
*/
public static function create_template_cohort($templateorid, $cohortorid) {
global $DB;
$template = $templateorid;
if (!is_object($template)) {
$template = new template($template);
}
require_capability('tool/lp:templatemanage', $template->get_context());
$cohort = $cohortorid;
if (!is_object($cohort)) {
$cohort = $DB->get_record('cohort', array('id' => $cohort), '*', MUST_EXIST);
}
// Check that the user can at least view this cohort.
$cohortcontext = context::instance_by_id($cohort->contextid);
if (!has_any_capability(array('moodle/cohort:manage', 'moodle/cohort:view'), $cohortcontext)) {
throw new required_capability_exception($cohortcontext, 'moodle/cohort:view', 'nopermissions', '');
}
$tplcohort = template_cohort::get_relation($template->get_id(), $cohort->id);
if (!$tplcohort->get_id()) {
$tplcohort->create();
}
return $tplcohort;
}
/**
* Remove a relation between a template and a cohort.
*
* @param template|int $templateorid The template or its ID.
* @param stdClass|int $cohortid The cohort ot its ID.
* @return boolean True on success or when the relation did not exist.
*/
public static function delete_template_cohort($templateorid, $cohortorid) {
global $DB;
$template = $templateorid;
if (!is_object($template)) {
$template = new template($template);
}
require_capability('tool/lp:templatemanage', $template->get_context());
$cohort = $cohortorid;
if (!is_object($cohort)) {
$cohort = $DB->get_record('cohort', array('id' => $cohort), '*', MUST_EXIST);
}
$tplcohort = template_cohort::get_relation($template->get_id(), $cohort->id);
if (!$tplcohort->get_id()) {
return true;
}
return $tplcohort->delete();
}
/**
* Lists user plans.
*
+58
View File
@@ -733,4 +733,62 @@ class tool_lp_api_testcase extends advanced_testcase {
$this->assertNull($plancompetencies[2]->usercompetency);
}
public function test_create_template_cohort() {
$this->resetAfterTest(true);
$this->setAdminUser();
$dg = $this->getDataGenerator();
$lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
$c1 = $dg->create_cohort();
$c2 = $dg->create_cohort();
$t1 = $lpg->create_template();
$t2 = $lpg->create_template();
$this->assertEquals(0, \tool_lp\template_cohort::count_records());
// Create two relations with mixed parameters.
$result = api::create_template_cohort($t1->get_id(), $c1->id);
$result = api::create_template_cohort($t1, $c2);
$this->assertEquals(2, \tool_lp\template_cohort::count_records());
$this->assertInstanceOf('tool_lp\template_cohort', $result);
$this->assertEquals($c2->id, $result->get_cohortid());
$this->assertEquals($t1->get_id(), $result->get_templateid());
$this->assertEquals(2, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t1->get_id())));
$this->assertEquals(0, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t2->get_id())));
}
public function test_delete_template_cohort() {
$this->resetAfterTest(true);
$this->setAdminUser();
$dg = $this->getDataGenerator();
$lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
$c1 = $dg->create_cohort();
$c2 = $dg->create_cohort();
$t1 = $lpg->create_template();
$t2 = $lpg->create_template();
$tc1 = $lpg->create_template_cohort(array('templateid' => $t1->get_id(), 'cohortid' => $c1->id));
$tc1 = $lpg->create_template_cohort(array('templateid' => $t2->get_id(), 'cohortid' => $c2->id));
$this->assertEquals(2, \tool_lp\template_cohort::count_records());
$this->assertEquals(1, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t1->get_id())));
$this->assertEquals(1, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t2->get_id())));
// Delete existing.
$result = api::delete_template_cohort($t1->get_id(), $c1->id);
$this->assertTrue($result);
$this->assertEquals(1, \tool_lp\template_cohort::count_records());
$this->assertEquals(0, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t1->get_id())));
$this->assertEquals(1, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t2->get_id())));
// Delete non-existant.
$result = api::delete_template_cohort($t1->get_id(), $c1->id);
$this->assertTrue($result);
$this->assertEquals(1, \tool_lp\template_cohort::count_records());
$this->assertEquals(0, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t1->get_id())));
$this->assertEquals(1, \tool_lp\template_cohort::count_records_select('templateid = :id', array('id' => $t2->get_id())));
}
}
+23
View File
@@ -29,6 +29,7 @@ use tool_lp\external;
use tool_lp\plan;
use tool_lp\related_competency;
use tool_lp\template;
use tool_lp\template_cohort;
use tool_lp\template_competency;
use tool_lp\user_competency;
use tool_lp\user_competency_plan;
@@ -343,5 +344,27 @@ class tool_lp_generator extends component_generator_base {
return $plancompetency;
}
/**
* Create a new template cohort.
*
* @param array|stdClass $record
* @return template_cohort
*/
public function create_template_cohort($record = null) {
$record = (object) $record;
if (!isset($record->templateid)) {
throw new coding_exception('The templateid value is required.');
}
if (!isset($record->cohortid)) {
throw new coding_exception('The cohortid value is required.');
}
$tplcohort = new template_cohort(0, $record);
$tplcohort->create();
return $tplcohort;
}
}
+16
View File
@@ -28,6 +28,7 @@ use tool_lp\competency_framework;
use tool_lp\plan;
use tool_lp\related_competency;
use tool_lp\template;
use tool_lp\template_cohort;
use tool_lp\template_competency;
use tool_lp\user_competency;
use tool_lp\user_competency_plan;
@@ -180,5 +181,20 @@ class tool_lp_generator_testcase extends advanced_testcase {
$this->assertEquals($plan->get_id(), $pc1->get_planid());
}
public function test_create_template_cohort() {
$this->resetAfterTest(true);
$lpg = $this->getDataGenerator()->get_plugin_generator('tool_lp');
$c1 = $this->getDataGenerator()->create_cohort();
$c2 = $this->getDataGenerator()->create_cohort();
$t1 = $lpg->create_template();
$this->assertEquals(0, template_cohort::count_records());
$tc = $lpg->create_template_cohort(array('templateid' => $t1->get_id(), 'cohortid' => $c1->id));
$this->assertEquals(1, template_cohort::count_records());
$tc = $lpg->create_template_cohort(array('templateid' => $t1->get_id(), 'cohortid' => $c2->id));
$this->assertEquals(2, template_cohort::count_records());
$this->assertInstanceOf('\tool_lp\template_cohort', $tc);
}
}