From: skodak Date: Sat, 21 Jul 2007 08:43:51 +0000 (+0000) Subject: MDL-10364 improved internal formula format X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=9acbd7269c870e1aae63ff836b5623e60d9cdb9e;p=moodle.git MDL-10364 improved internal formula format --- diff --git a/lib/grade/grade_item.php b/lib/grade/grade_item.php index 4dab7e6303..4a15d22b95 100644 --- a/lib/grade/grade_item.php +++ b/lib/grade/grade_item.php @@ -868,13 +868,13 @@ class grade_item extends grade_object { } /* - * The main reason why we use the [#gixxx#] instead of [idnumber] is speed of depends_on(), + * The main reason why we use the ##gixxx## instead of [[idnumber]] is speed of depends_on(), * we would have to fetch all course grade items to find out the ids. * Also if user changes the idnumber the formula does not need to be updated. */ - // first detect if we need to change calculation formula from [idnumber] to [#giXXX#] (after backup, etc.) - if (!$this->calculation_normalized and preg_match('/\[(?!#gi)(.*?)\]/', $this->calculation)) { + // first detect if we need to change calculation formula from [[idnumber]] to ##giXXX## (after backup, etc.) + if (!$this->calculation_normalized and preg_match('/##gi\d+##/', $this->calculation)) { $this->set_calculation($this->calculation); } @@ -918,12 +918,12 @@ class grade_item extends grade_object { return ''; } - // denormalize formula - convert [#giXX#] to [idnumber] - if (preg_match_all('/\[#gi([0-9]+)#\]/', $formula, $matches)) { + // denormalize formula - convert ##giXX## to [[idnumber]] + if (preg_match_all('/##gi(\d+)##/', $formula, $matches)) { foreach ($matches[1] as $id) { if ($grade_item = grade_item::fetch(array('id'=>$id, 'courseid'=>$courseid))) { if (!empty($grade_item->idnumber)) { - $formula = str_replace('[#gi'.$grade_item->id.'#]', '['.$grade_item->idnumber.']', $formula); + $formula = str_replace('##gi'.$grade_item->id.'##', '[['.$grade_item->idnumber.']]', $formula); } } } @@ -947,10 +947,10 @@ class grade_item extends grade_object { } - // normalize formula - we want grade item ids [#giXXX#] instead of [idnumber] + // normalize formula - we want grade item ids ##giXXX## instead of [[idnumber]] if ($grade_items = grade_item::fetch_all(array('courseid'=>$courseid))) { foreach ($grade_items as $grade_item) { - $formula = str_replace('['.$grade_item->idnumber.']', '[#gi'.$grade_item->id.'#]', $formula); + $formula = str_replace('[['.$grade_item->idnumber.']]', '##gi'.$grade_item->id.'##', $formula); } } @@ -1072,7 +1072,7 @@ class grade_item extends grade_object { } if ($this->is_calculated()) { - if (preg_match_all('/\[#gi([0-9]+)#\]/', $this->calculation, $matches)) { + if (preg_match_all('/##gi(\d+)##/', $this->calculation, $matches)) { return array_unique($matches[1]); // remove duplicates } else { return array(); @@ -1367,7 +1367,7 @@ class grade_item extends grade_object { $useditems = $this->depends_on(); // prepare formula and init maths library - $formula = preg_replace('/\[#(gi[0-9]+)#\]/', '\1', $this->calculation); + $formula = preg_replace('/##(gi\d+)##/', '\1', $this->calculation); $this->formula = new calc_formula($formula); // where to look for final grades? @@ -1512,7 +1512,7 @@ class grade_item extends grade_object { } // prepare formula and init maths library - $formula = preg_replace('/\[#(gi[0-9]+)#\]/', '\1', $formula); + $formula = preg_replace('/##(gi\d+)##/', '\1', $formula); $formula = new calc_formula($formula); // get used items diff --git a/lib/grade/simpletest/testgradeitem.php b/lib/grade/simpletest/testgradeitem.php index 855df9e289..bef65f7013 100755 --- a/lib/grade/simpletest/testgradeitem.php +++ b/lib/grade/simpletest/testgradeitem.php @@ -475,7 +475,7 @@ class grade_item_test extends grade_test { $grade_item = new grade_item($this->grade_items[1]); $this->assertTrue(method_exists($grade_item, 'is_calculated')); $grade_itemsource = new grade_item($this->grade_items[0]); - $normalizedformula = str_replace('['.$grade_itemsource->idnumber.']', '[#gi'.$grade_itemsource->id.'#]', $this->grade_items[1]->calculation); + $normalizedformula = str_replace('[['.$grade_itemsource->idnumber.']]', '##gi'.$grade_itemsource->id.'##', $this->grade_items[1]->calculation); $this->assertTrue($grade_item->is_calculated()); $this->assertEqual($normalizedformula, $grade_item->calculation); @@ -486,10 +486,10 @@ class grade_item_test extends grade_test { $this->assertTrue(method_exists($grade_item, 'set_calculation')); $grade_itemsource = new grade_item($this->grade_items[0]); - $grade_item->set_calculation('=['.$grade_itemsource->idnumber.']'); + $grade_item->set_calculation('=[['.$grade_itemsource->idnumber.']]'); $this->assertTrue(!empty($grade_item->needsupdate)); - $this->assertEqual('=[#gi'.$grade_itemsource->id.'#]', $grade_item->calculation); + $this->assertEqual('=##gi'.$grade_itemsource->id.'##', $grade_item->calculation); } function test_grade_item_get_calculation() { @@ -497,7 +497,7 @@ class grade_item_test extends grade_test { $this->assertTrue(method_exists($grade_item, 'get_calculation')); $grade_itemsource = new grade_item($this->grade_items[0]); - $denormalizedformula = str_replace('[#gi'.$grade_itemsource->id.'#]', '['.$grade_itemsource->idnumber.']', $this->grade_items[1]->calculation); + $denormalizedformula = str_replace('##gi'.$grade_itemsource->id.'##', '[['.$grade_itemsource->idnumber.']]', $this->grade_items[1]->calculation); $formula = $grade_item->get_calculation(); $this->assertTrue(!empty($grade_item->needsupdate)); diff --git a/lib/simpletest/fixtures/gradetest.php b/lib/simpletest/fixtures/gradetest.php index 7a6cedb1b9..8fa4448f01 100644 --- a/lib/simpletest/fixtures/gradetest.php +++ b/lib/simpletest/fixtures/gradetest.php @@ -754,7 +754,7 @@ class grade_test extends UnitTestCase { $grade_item->itemname = 'unittestgradeitem2'; $grade_item->itemtype = 'import'; $grade_item->itemmodule = 'assignment'; - $grade_item->calculation = '= [#gi'.$this->grade_items[0]->id.'#] + 30 + [item id 0] - [item id 0]'; + $grade_item->calculation = '= ##gi'.$this->grade_items[0]->id.'## + 30 + [[item id 0]] - [[item id 0]]'; $grade_item->gradetype = GRADE_TYPE_VALUE; $grade_item->iteminstance = 2; $grade_item->itemnumber = null;