diff --git a/lib/evalmath/evalmath.class.php b/lib/evalmath/evalmath.class.php index d8875b56866..26e204bb73d 100644 --- a/lib/evalmath/evalmath.class.php +++ b/lib/evalmath/evalmath.class.php @@ -114,8 +114,8 @@ class EvalMath { 'average'=>array(-1), 'max'=>array(-1), 'min'=>array(-1), 'mod'=>array(2), 'pi'=>array(0), 'power'=>array(2), 'round'=>array(1, 2), 'sum'=>array(-1), 'rand_int'=>array(2), - 'rand_float'=>array(0), 'ifthenelse'=>array(3)); - var $fcsynonyms = array('if' => 'ifthenelse'); + 'rand_float'=>array(0), 'ifthenelse'=>array(3), 'cond_and'=>array(-1), 'cond_or'=>array(-1)); + var $fcsynonyms = array('if' => 'ifthenelse', 'and' => 'cond_and', 'or' => 'cond_or'); var $allowimplicitmultiplication; @@ -534,6 +534,27 @@ class EvalMathFuncs { return $else; } } + + static function cond_and() { + $args = func_get_args(); + foreach($args as $a) { + if ($a == false) { + return 0; + } + } + return 1; + } + + static function cond_or() { + $args = func_get_args(); + foreach($args as $a) { + if($a == true) { + return 1; + } + } + return 0; + } + static function average() { $args = func_get_args(); return (call_user_func_array(array('self', 'sum'), $args) / count($args)); diff --git a/lib/evalmath/readme_moodle.txt b/lib/evalmath/readme_moodle.txt index c9935c53634..c34b31d4241 100644 --- a/lib/evalmath/readme_moodle.txt +++ b/lib/evalmath/readme_moodle.txt @@ -22,3 +22,9 @@ skodak, Tim Hunt Changes by Juan Pablo de Castro (MDL-14274): * operators >,<,>=,<=,== added. * function if[thenelse](condition, true_value, false_value) + +Changes by Stefan Erlachner, Thomas Niedermaier (MDL-64414): +* add function or: +e.g. if (or(condition_1, condition_2, ... condition_n)) +* add function and: +e.g. if (and(condition_1, condition_2, ... condition_n)) \ No newline at end of file diff --git a/lib/tests/mathslib_test.php b/lib/tests/mathslib_test.php index 6d37bcfdc41..999217590ed 100644 --- a/lib/tests/mathslib_test.php +++ b/lib/tests/mathslib_test.php @@ -82,6 +82,7 @@ class core_mathslib_testcase extends basic_testcase { } public function test_conditional_functions() { + // Test ifthenelse. $formula = new calc_formula('=ifthenelse(1,2,3)'); $this->assertSame(2, (int)$formula->evaluate()); @@ -91,7 +92,7 @@ class core_mathslib_testcase extends basic_testcase { $formula = new calc_formula('=ifthenelse(2<3,2,3)'); $this->assertSame(2, (int) $formula->evaluate()); - // Test synonim if. + // Test synonym if. $formula = new calc_formula('=if(1,2,3)'); $this->assertSame(2, (int)$formula->evaluate()); @@ -100,6 +101,46 @@ class core_mathslib_testcase extends basic_testcase { $formula = new calc_formula('=if(2<3,2,3)'); $this->assertSame(2, (int) $formula->evaluate()); + + // Test cond_and. + $formula = new calc_formula('=cond_and(1,1,1)'); + $this->assertSame(1, (int)$formula->evaluate()); + + $formula = new calc_formula('=cond_and(1,1,0)'); + $this->assertSame(0, (int) $formula->evaluate()); + + $formula = new calc_formula('=cond_and(0,0,0)'); + $this->assertSame(0, (int) $formula->evaluate()); + + // Test synonym and. + $formula = new calc_formula('=and(1,1,1)'); + $this->assertSame(1, (int)$formula->evaluate()); + + $formula = new calc_formula('=and(1,1,0)'); + $this->assertSame(0, (int) $formula->evaluate()); + + $formula = new calc_formula('=and(0,0,0)'); + $this->assertSame(0, (int) $formula->evaluate()); + + // Test cond_or. + $formula = new calc_formula('=cond_or(1,1,1)'); + $this->assertSame(1, (int)$formula->evaluate()); + + $formula = new calc_formula('=cond_or(1,1,0)'); + $this->assertSame(1, (int) $formula->evaluate()); + + $formula = new calc_formula('=cond_or(0,0,0)'); + $this->assertSame(0, (int) $formula->evaluate()); + + // Test synonym or. + $formula = new calc_formula('=or(1,1,1)'); + $this->assertSame(1, (int)$formula->evaluate()); + + $formula = new calc_formula('=or(1,1,0)'); + $this->assertSame(1, (int) $formula->evaluate()); + + $formula = new calc_formula('=or(0,0,0)'); + $this->assertSame(0, (int) $formula->evaluate()); } public function test_conditional_operators() {