MDL-53471 tool_lp: Hooking in to report when scale is being used
authorFrederic Massart <fred@moodle.com>
Tue, 15 Mar 2016 03:03:58 +0000 (11:03 +0800)
committerFrederic Massart <fred@moodle.com>
Mon, 18 Apr 2016 02:59:00 +0000 (10:59 +0800)
admin/tool/lp/classes/api.php
admin/tool/lp/lib.php
admin/tool/lp/tests/api_test.php

index cf26396..ef26a04 100644 (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.
      *
index b98236d..d952b76 100644 (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);
+}
index c7ceda8..e829229 100644 (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));
+    }
 }