]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-10364 improved internal formula format
authorskodak <skodak>
Sat, 21 Jul 2007 08:43:51 +0000 (08:43 +0000)
committerskodak <skodak>
Sat, 21 Jul 2007 08:43:51 +0000 (08:43 +0000)
lib/grade/grade_item.php
lib/grade/simpletest/testgradeitem.php
lib/simpletest/fixtures/gradetest.php

index 4dab7e6303ba9a879f6190893701bb6f4f6ad26b..4a15d22b957a1ca3a0783256edf11ee867746cb1 100644 (file)
@@ -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
index 855df9e289110ed6c9d529ced58299438a4ee817..bef65f70137bbdc2fae141276023ffd78675a83e 100755 (executable)
@@ -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));
index 7a6cedb1b96a51e0357528253bee1842d1c0ce84..8fa4448f018ebd130c4227604d8518d0e7c064b7 100644 (file)
@@ -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;