MDL-10364 improved internal formula format

This commit is contained in:
skodak 2007-07-21 08:43:51 +00:00
parent 713b2a2f71
commit 9acbd7269c
3 changed files with 16 additions and 16 deletions

View 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

View file

@ -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));

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