From 4e89144ca7142ff7756d7f6174a3b1254b17a922 Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Tue, 15 Mar 2016 11:03:58 +0800 Subject: [PATCH] MDL-53471 tool_lp: Hooking in to report when scale is being used --- admin/tool/lp/classes/api.php | 23 +++++++++ admin/tool/lp/lib.php | 10 ++++ admin/tool/lp/tests/api_test.php | 88 ++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+) diff --git a/admin/tool/lp/classes/api.php b/admin/tool/lp/classes/api.php index cf263967d80..ef26a0403ac 100644 --- a/admin/tool/lp/classes/api.php +++ b/admin/tool/lp/classes/api.php @@ -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. * diff --git a/admin/tool/lp/lib.php b/admin/tool/lp/lib.php index b98236d0891..d952b76fd8d 100644 --- a/admin/tool/lp/lib.php +++ b/admin/tool/lp/lib.php @@ -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); +} diff --git a/admin/tool/lp/tests/api_test.php b/admin/tool/lp/tests/api_test.php index c7ceda8d25e..e8292299e6a 100644 --- a/admin/tool/lp/tests/api_test.php +++ b/admin/tool/lp/tests/api_test.php @@ -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)); + } } -- 2.43.0