From: nicolasconnault Date: Mon, 14 May 2007 04:14:22 +0000 (+0000) Subject: MDL-9506 Finally cracked the category aggregation. However, more thorough unit tests... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=096858ffcff9a2bf85f267ced34e16c480c44aec;p=moodle.git MDL-9506 Finally cracked the category aggregation. However, more thorough unit tests need to be written, with exact expectations instead of ranges, as currently used. All unit tests pass. --- diff --git a/lib/grade/grade_category.php b/lib/grade/grade_category.php index 08c0d6fefa..f56638a2e4 100644 --- a/lib/grade/grade_category.php +++ b/lib/grade/grade_category.php @@ -374,7 +374,7 @@ class grade_category extends grade_object { } // 3. Aggregate the grades $aggregated_grades = $this->aggregate_grades($final_grades_for_aggregation); - + // 4. Save the resulting array of grades as raw grades $this->load_grade_item(); $this->grade_item->save_raw($aggregated_grades); @@ -396,6 +396,7 @@ class grade_category extends grade_object { if (empty($final_grade_sets)) { return null; } + $aggregated_grades = array(); $pooled_grades = array(); @@ -406,7 +407,7 @@ class grade_category extends grade_object { $pooled_grades[$userid][] = $value; } } - + foreach ($pooled_grades as $userid => $grades) { $aggregated_value = null; @@ -440,6 +441,11 @@ class grade_category extends grade_object { $aggregated_value = $sum / $num; break; } + + // If the gradevalue is null, we have a problem + if (empty($aggregated_value)) { + return false; + } $grade_raw = new grade_grades_raw(); $grade_raw->userid = $userid; @@ -449,6 +455,7 @@ class grade_category extends grade_object { $grade_raw->itemid = $this->grade_item->id; $aggregated_grades[$userid] = $grade_raw; } + return $aggregated_grades; } diff --git a/lib/grade/grade_item.php b/lib/grade/grade_item.php index d4fa038ebb..5ca7f1e713 100644 --- a/lib/grade/grade_item.php +++ b/lib/grade/grade_item.php @@ -308,7 +308,7 @@ class grade_item extends grade_object { $final_grades = $this->load_final(true); foreach ($final_grades as $userid => $final) { - $standardised_finals[$userid] = standardise_score($final->gradevalue, $this->grademin, $this->grademax, 0, 1, true); + $standardised_finals[$userid] = standardise_score($final->gradevalue, $this->grademin, $this->grademax, 0, 1); } return $standardised_finals; @@ -530,10 +530,10 @@ class grade_item extends grade_object { } $success = true; - + foreach ($this->grade_grades_raw as $raw_grade) { $final_grade = new grade_grades_final(); - $final_grade->gradevalue = $this->adjust_grade($raw_grade, null, 'gradevalue'); + $final_grade->gradevalue = $this->adjust_grade($raw_grade); $final_grade->itemid = $this->id; $final_grade->userid = $raw_grade->userid; $success = $success & $final_grade->insert(); @@ -769,7 +769,7 @@ class grade_item extends grade_object { function adjust_grade($grade_raw, $gradevalue=NULL) { $raw_offset = 0; $item_offset = 0; - + if ($this->gradetype == GRADE_TYPE_VALUE) { // Dealing with numerical grade if (empty($gradevalue)) { $gradevalue = $grade_raw->gradevalue; @@ -797,7 +797,7 @@ class grade_item extends grade_object { } elseif ($this->gradetype != GRADE_TYPE_TEXT) { // Something's wrong, the raw grade has no value!? return "Error: The gradeitem did not have a valid gradetype value, was $this->gradetype instead"; } - + // Standardise score to the new grade range $gradevalue = standardise_score($gradevalue, $grade_raw->grademin, $grade_raw->grademax, $this->grademin, $this->grademax); @@ -807,7 +807,7 @@ class grade_item extends grade_object { // Apply other grade_item factors $gradevalue *= $this->multfactor; $gradevalue += $this->plusfactor; - } + } return $gradevalue; } diff --git a/lib/gradelib.php b/lib/gradelib.php index 4e43a85038..e3d0f49796 100644 --- a/lib/gradelib.php +++ b/lib/gradelib.php @@ -223,17 +223,18 @@ function grades_grab_grades() { * @return float Converted value */ function standardise_score($gradevalue, $source_min, $source_max, $target_min, $target_max, $debug=false) { + $factor = ($gradevalue - $source_min) / ($source_max - $source_min); + $diff = $target_max - $target_min; + $standardised_value = $factor * $diff + $target_min; if ($debug) { echo 'standardise_score debug info: (lib/gradelib.php)'; print_object(array('gradevalue' => $gradevalue, 'source_min' => $source_min, 'source_max' => $source_max, 'target_min' => $target_min, - 'target_max' => $target_max)); + 'target_max' => $target_max, + 'result' => $standardised_value)); } - $factor = ($gradevalue - $source_min) / ($source_max - $source_min); - $diff = $target_max - $target_min; - $gradevalue = $factor * $diff + $target_min; - return $gradevalue; + return $standardised_value; } ?> diff --git a/lib/simpletest/grade/simpletest/testgradecategory.php b/lib/simpletest/grade/simpletest/testgradecategory.php index d14cbfe9fd..a1cf3d3325 100755 --- a/lib/simpletest/grade/simpletest/testgradecategory.php +++ b/lib/simpletest/grade/simpletest/testgradecategory.php @@ -176,7 +176,7 @@ class grade_category_test extends gradelib_test { $this->assertEqual(3, count($raw_grades)); $this->assertEqual(3, count($final_grades)); } -/** + function test_grade_category_aggregate_grades() { $category = new grade_category($this->grade_categories[0]); $this->assertTrue(method_exists($category, 'aggregate_grades')); @@ -197,7 +197,7 @@ class grade_category_test extends gradelib_test { $this->assertWithinMargin($aggregated_grades[rand(0, count($aggregated_grades))]->gradevalue, 0, 100); $this->assertWithinMargin($aggregated_grades[rand(0, count($aggregated_grades))]->gradevalue, 0, 100); } -*/ + function generate_random_raw_grade($item, $userid) { $raw_grade = new grade_grades_raw(); $raw_grade->itemid = $item->id; diff --git a/lib/simpletest/grade/simpletest/testgradeitem.php b/lib/simpletest/grade/simpletest/testgradeitem.php index f6c88821a1..d5e9d0059c 100755 --- a/lib/simpletest/grade/simpletest/testgradeitem.php +++ b/lib/simpletest/grade/simpletest/testgradeitem.php @@ -443,6 +443,8 @@ class grade_item_test extends gradelib_test { $grade_item->categoryid = $this->grade_categories[1]->id; $grade_item->itemname = 'unittestgradeitem4'; $grade_item->itemtype = 'mod'; + $grade_item->grademin = 0; + $grade_item->grademax = 100; $grade_item->itemmodule = 'quiz'; $grade_item->iteminfo = 'Grade item used for unit testing'; diff --git a/lib/simpletest/testgradelib.php b/lib/simpletest/testgradelib.php index 656d6d4e4e..9db24fc9a8 100644 --- a/lib/simpletest/testgradelib.php +++ b/lib/simpletest/testgradelib.php @@ -398,7 +398,7 @@ class gradelib_test extends UnitTestCase { $grade_category->fullname = 'unittestcategory2'; $grade_category->courseid = $this->courseid; - $grade_category->aggregation = GRADE_AGGREGATE_MODE; + $grade_category->aggregation = GRADE_AGGREGATE_MEAN; $grade_category->keephigh = 100; $grade_category->droplow = 10; $grade_category->hidden = 0; @@ -414,7 +414,7 @@ class gradelib_test extends UnitTestCase { $grade_category->fullname = 'unittestcategory3'; $grade_category->courseid = $this->courseid; - $grade_category->aggregation = GRADE_AGGREGATE_MODE; + $grade_category->aggregation = GRADE_AGGREGATE_MEAN; $grade_category->keephigh = 100; $grade_category->droplow = 10; $grade_category->hidden = 0; @@ -500,6 +500,8 @@ class gradelib_test extends UnitTestCase { $grade_item->itemname = 'unittestgradeitemcategory1'; $grade_item->needsupdate = true; $grade_item->itemtype = 'category'; + $grade_item->grademin = 0; + $grade_item->grademax = 100; $grade_item->iteminfo = 'Grade item used for unit testing'; $grade_item->timecreated = mktime(); $grade_item->timemodified = mktime(); @@ -515,6 +517,8 @@ class gradelib_test extends UnitTestCase { $grade_item->itemname = 'unittestgradeitemcategory2'; $grade_item->itemtype = 'category'; $grade_item->needsupdate = true; + $grade_item->grademin = 0; + $grade_item->grademax = 100; $grade_item->iteminfo = 'Grade item used for unit testing'; $grade_item->timecreated = mktime(); $grade_item->timemodified = mktime(); @@ -530,6 +534,8 @@ class gradelib_test extends UnitTestCase { $grade_item->itemname = 'unittestgradeitemcategory3'; $grade_item->itemtype = 'category'; $grade_item->needsupdate = true; + $grade_item->grademin = 0; + $grade_item->grademax = 100; $grade_item->iteminfo = 'Grade item used for unit testing'; $grade_item->timecreated = mktime(); $grade_item->timemodified = mktime(); @@ -977,6 +983,8 @@ class gradelib_test extends UnitTestCase { $params->itemname = 'unittestgradeitem4'; $params->itemtype = 'mod'; $params->itemmodule = 'database'; + $params->grademin = 0; + $params->grademax = 100; $params->iteminstance = 4; $params->iteminfo = 'Grade item used for unit testing'; $params->timecreated = mktime();