Moving some helper functions from the renderer to the workshop API

This commit is contained in:
David Mudrak 2010-01-04 18:15:48 +00:00
parent a84cc57f26
commit d5506aac98
3 changed files with 43 additions and 24 deletions

View 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 {
);
}
}

View 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
*

View 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);
}
}