MDL-53471 tool_lp: Hooking in to report when scale is being used

This commit is contained in:
Frederic Massart
2016-04-18 10:59:00 +08:00
parent dff98d3316
commit 4e89144ca7
3 changed files with 121 additions and 0 deletions
+23
View File
@@ -66,6 +66,29 @@ class api {
}
}
/**
* Checks whether a scale is used anywhere in the plugin.
*
* This public API has two exceptions:
* - It MUST NOT perform any capability checks.
* - It MUST ignore whether competencies are enabled or not ({@link self::is_enabled()}).
*
* @param int $scaleid The scale ID.
* @return bool
*/
public static function is_scale_used_anywhere($scaleid) {
global $DB;
$sql = "SELECT s.id
FROM {scale} s
LEFT JOIN {" . competency_framework::TABLE ."} f
ON f.scaleid = :scaleid1
LEFT JOIN {" . competency::TABLE ."} c
ON c.scaleid = :scaleid2
WHERE f.id IS NOT NULL
OR c.id IS NOT NULL";
return $DB->record_exists_sql($sql, ['scaleid1' => $scaleid, 'scaleid2' => $scaleid]);
}
/**
* Validate if current user have acces to the course_module if hidden.
*
+10
View File
@@ -510,3 +510,13 @@ function tool_lp_coursemodule_edit_post_actions($data, $course) {
return $data;
}
/**
* Reports whether a scale is being used in the plugin.
*
* @param int $scaleid The scale ID.
* @return bool
*/
function tool_lp_scale_used_anywhere($scaleid) {
return \tool_lp\api::is_scale_used_anywhere($scaleid);
}
+88
View File
@@ -4180,4 +4180,92 @@ class tool_lp_api_testcase extends advanced_testcase {
$this->assertContains($one->get_id(), $leastarray);
}
}
public function test_is_scale_used_anywhere() {
$this->resetAfterTest();
$dg = $this->getDataGenerator();
$lpg = $dg->get_plugin_generator('tool_lp');
$scale1 = $dg->create_scale();
$scale2 = $dg->create_scale();
$scale3 = $dg->create_scale();
$scale4 = $dg->create_scale();
$this->assertFalse(api::is_scale_used_anywhere($scale1->id));
$this->assertFalse(api::is_scale_used_anywhere($scale2->id));
$this->assertFalse(api::is_scale_used_anywhere($scale3->id));
$this->assertFalse(api::is_scale_used_anywhere($scale4->id));
// Using scale 1 in a framework.
$f1 = $lpg->create_framework([
'scaleid' => $scale1->id,
'scaleconfiguration' => json_encode([
['scaleid' => $scale1->id],
['id' => 1, 'scaledefault' => 1, 'proficient' => 1]
])
]);
$this->assertTrue(api::is_scale_used_anywhere($scale1->id));
$this->assertFalse(api::is_scale_used_anywhere($scale2->id));
$this->assertFalse(api::is_scale_used_anywhere($scale3->id));
$this->assertFalse(api::is_scale_used_anywhere($scale4->id));
// Using scale 2 in a competency.
$f2 = $lpg->create_framework();
$c2 = $lpg->create_competency([
'competencyframeworkid' => $f2->get_id(),
'scaleid' => $scale2->id,
'scaleconfiguration' => json_encode([
['scaleid' => $scale2->id],
['id' => 1, 'scaledefault' => 1, 'proficient' => 1]
])
]);
$this->assertTrue(api::is_scale_used_anywhere($scale1->id));
$this->assertTrue(api::is_scale_used_anywhere($scale2->id));
$this->assertFalse(api::is_scale_used_anywhere($scale3->id));
$this->assertFalse(api::is_scale_used_anywhere($scale4->id));
// Using scale 3 in a framework, and scale 4 in a competency of that framework.
$f3 = $lpg->create_framework([
'scaleid' => $scale3->id,
'scaleconfiguration' => json_encode([
['scaleid' => $scale3->id],
['id' => 1, 'scaledefault' => 1, 'proficient' => 1]
])
]);
$c3 = $lpg->create_competency([
'competencyframeworkid' => $f3->get_id(),
'scaleid' => $scale4->id,
'scaleconfiguration' => json_encode([
['scaleid' => $scale4->id],
['id' => 1, 'scaledefault' => 1, 'proficient' => 1]
])
]);
$this->assertTrue(api::is_scale_used_anywhere($scale1->id));
$this->assertTrue(api::is_scale_used_anywhere($scale2->id));
$this->assertTrue(api::is_scale_used_anywhere($scale3->id));
$this->assertTrue(api::is_scale_used_anywhere($scale4->id));
// Multiple occurrences of the same scale (3, and 4).
$f4 = $lpg->create_framework([
'scaleid' => $scale3->id,
'scaleconfiguration' => json_encode([
['scaleid' => $scale3->id],
['id' => 1, 'scaledefault' => 1, 'proficient' => 1]
])
]);
$c4 = $lpg->create_competency([
'competencyframeworkid' => $f3->get_id(),
'scaleid' => $scale4->id,
'scaleconfiguration' => json_encode([
['scaleid' => $scale4->id],
['id' => 1, 'scaledefault' => 1, 'proficient' => 1]
])
]);
$this->assertTrue(api::is_scale_used_anywhere($scale1->id));
$this->assertTrue(api::is_scale_used_anywhere($scale2->id));
$this->assertTrue(api::is_scale_used_anywhere($scale3->id));
$this->assertTrue(api::is_scale_used_anywhere($scale4->id));
}
}