]> git.mjollnir.org Git - moodle.git/commitdiff
Moving some helper functions from the renderer to the workshop API
authorDavid Mudrak <david.mudrak@gmail.com>
Mon, 4 Jan 2010 18:15:48 +0000 (18:15 +0000)
committerDavid Mudrak <david.mudrak@gmail.com>
Mon, 4 Jan 2010 18:15:48 +0000 (18:15 +0000)
mod/workshop/locallib.php
mod/workshop/renderer.php
mod/workshop/simpletest/testlocallib.php

index 9adbf25292d4a37bbde3573da9a2f219a2924b2b..f076c4651a36b158339d48919911141fdf6cb89d 100644 (file)
@@ -1212,6 +1212,31 @@ class workshop {
         // todo
     }
 
+    ////////////////////////////////////////////////////////////////////////////
+    // Helper methods                                                         //
+    ////////////////////////////////////////////////////////////////////////////
+
+    /**
+     * Helper function returning the greatest common divisor
+     *
+     * @param int $a
+     * @param int $b
+     * @return int
+     */
+    public static function gcd($a, $b) {
+        return ($b == 0) ? ($a):(self::gcd($b, $a % $b));
+    }
+
+    /**
+     * Helper function returning the least common multiple
+     *
+     * @param int $a
+     * @param int $b
+     * @return int
+     */
+    public static function lcm($a, $b) {
+        return ($a / self::gcd($a,$b)) * $b;
+    }
 
     ////////////////////////////////////////////////////////////////////////////////
     // Internal methods (implementation details)                                  //
@@ -1312,5 +1337,4 @@ class workshop {
         );
     }
 
-
 }
index 6e1f3bdf2cae66be3f66ec12b5de80cea081cfc3..01271b2ed32ff6e6df6608aaba429813b75ce30c 100644 (file)
@@ -444,7 +444,7 @@ class moodle_mod_workshop_renderer extends moodle_renderer_base {
 
             // compute the number of <tr> table rows needed to display this participant
             if ($numofreceived > 0 and $numofgiven > 0) {
-                $numoftrs       = self::lcm($numofreceived, $numofgiven);
+                $numoftrs       = workshop::lcm($numofreceived, $numofgiven);
                 $spanreceived   = $numoftrs / $numofreceived;
                 $spangiven      = $numoftrs / $numofgiven;
             } elseif ($numofreceived == 0 and $numofgiven > 0) {
@@ -640,28 +640,6 @@ class moodle_mod_workshop_renderer extends moodle_renderer_base {
     // Helper methods                                                         //
     ////////////////////////////////////////////////////////////////////////////
 
-    /**
-     * Helper function returning the greatest common divisor
-     *
-     * @param int $a
-     * @param int $b
-     * @return int
-     */
-    protected static function gcd($a, $b) {
-        return ($b == 0) ? ($a):(self::gcd($b, $a % $b));
-    }
-
-    /**
-     * Helper function returning the least common multiple
-     *
-     * @param int $a
-     * @param int $b
-     * @return int
-     */
-    protected static function lcm($a, $b) {
-        return ($a / self::gcd($a,$b)) * $b;
-    }
-
     /**
      * Helper function returning the n-th item of the array
      *
index ad6d667ee7a8fc83a9d8473b16a74a5972bbe432..c6f2b8e20f24dfc856cd885eda1f2858a3ff6d75 100644 (file)
@@ -273,4 +273,21 @@ class workshop_internal_api_test extends UnitTestCase {
         $part = workshop::percent_to_value($percent, $total);
     }
 
+    public function test_lcm() {
+        // fixture setup + excercise SUT + verify in one step
+        $this->assertEqual(workshop::lcm(1,4), 4);
+        $this->assertEqual(workshop::lcm(2,4), 4);
+        $this->assertEqual(workshop::lcm(4,2), 4);
+        $this->assertEqual(workshop::lcm(2,3), 6);
+        $this->assertEqual(workshop::lcm(6,4), 12);
+    }
+
+    public function test_lcm_array() {
+        // fixture setup
+        $numbers = array(5,3,15);
+        // excersise SUT
+        $lcm = array_reduce($numbers, 'workshop::lcm', 1);
+        // verify
+        $this->assertEqual($lcm, 15);
+    }
 }