}
// 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);
if (empty($final_grade_sets)) {
return null;
}
+
$aggregated_grades = array();
$pooled_grades = array();
$pooled_grades[$userid][] = $value;
}
}
-
+
foreach ($pooled_grades as $userid => $grades) {
$aggregated_value = null;
$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;
$grade_raw->itemid = $this->grade_item->id;
$aggregated_grades[$userid] = $grade_raw;
}
+
return $aggregated_grades;
}
$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;
}
$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();
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;
} 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);
// Apply other grade_item factors
$gradevalue *= $this->multfactor;
$gradevalue += $this->plusfactor;
- }
+ }
return $gradevalue;
}
* @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;
}
?>
$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'));
$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;
$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';
$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;
$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;
$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();
$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();
$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();
$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();