From: David Mudrak Date: Mon, 4 Jan 2010 18:15:48 +0000 (+0000) Subject: Moving some helper functions from the renderer to the workshop API X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=d5506aac989e0951c90d42a86d2af3faf6323beb;p=moodle.git Moving some helper functions from the renderer to the workshop API --- diff --git a/mod/workshop/locallib.php b/mod/workshop/locallib.php index 9adbf25292..f076c4651a 100644 --- a/mod/workshop/locallib.php +++ b/mod/workshop/locallib.php @@ -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 { ); } - } diff --git a/mod/workshop/renderer.php b/mod/workshop/renderer.php index 6e1f3bdf2c..01271b2ed3 100644 --- a/mod/workshop/renderer.php +++ b/mod/workshop/renderer.php @@ -444,7 +444,7 @@ class moodle_mod_workshop_renderer extends moodle_renderer_base { // compute the number of 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 * diff --git a/mod/workshop/simpletest/testlocallib.php b/mod/workshop/simpletest/testlocallib.php index ad6d667ee7..c6f2b8e20f 100644 --- a/mod/workshop/simpletest/testlocallib.php +++ b/mod/workshop/simpletest/testlocallib.php @@ -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); + } }