Skip to content

Commit

Permalink
MDL-18772 fixed aut oupdating of grademax for SUM aggregation type
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Apr 5, 2009
1 parent 9748193 commit 3a03653
Showing 1 changed file with 47 additions and 22 deletions.
69 changes: 47 additions & 22 deletions lib/grade/grade_category.php
Expand Up @@ -424,6 +424,9 @@ public function generate_grades($userid=null) {
$items = $DB->get_records_sql($sql, $params);
}

// needed mostly for SUM agg type
$this->auto_update_max($items);

$grade_inst = new grade_grade();
$fields = 'g.'.implode(',g.', $grade_inst->required_fields);

Expand Down Expand Up @@ -691,6 +694,50 @@ public function aggregate_values($grade_values, $items) {
return $agg_grade;
}

/**
* Some aggregation tpyes may update max grade
* @param array $items sub items
* @return void
*/
private function auto_update_max($items) {
if ($this->aggregation != GRADE_AGGREGATE_SUM) {
// not needed at all
return;
}

if (!$items) {
if ($this->grade_item->grademax != 0 or $this->grade_item->gradetype != GRADE_TYPE_VALUE) {
$this->grade_item->grademax = 0;
$this->grade_item->grademin = 0;
$this->grade_item->gradetype = GRADE_TYPE_VALUE;
$this->grade_item->update('aggregation');
}
return;
}

$max = 0;

//find max grade
foreach ($items as $item) {
if ($item->aggregationcoef > 0) {
// extra credit from this activity - does not affect total
continue;
}
if ($item->gradetype == GRADE_TYPE_VALUE) {
$max += $item->grademax;
} else if ($item->gradetype == GRADE_TYPE_SCALE) {
$max += $item->grademax - 1; // scales min is 1
}
}

if ($this->grade_item->grademax != $max or $this->grade_item->grademin != 0 or $this->grade_item->gradetype != GRADE_TYPE_VALUE){
$this->grade_item->grademax = $max;
$this->grade_item->grademin = 0;
$this->grade_item->gradetype = GRADE_TYPE_VALUE;
$this->grade_item->update('aggregation');
}
}

/**
* internal function for category grades summing
*
Expand Down Expand Up @@ -721,28 +768,6 @@ private function sum_grades(&$grade, $oldfinalgrade, $items, $grade_values, $exc
}
}

$max = 0;

//find max grade
foreach ($items as $item) {
if ($item->aggregationcoef > 0) {
// extra credit from this activity - does not affect total
continue;
}
if ($item->gradetype == GRADE_TYPE_VALUE) {
$max += $item->grademax;
} else if ($item->gradetype == GRADE_TYPE_SCALE) {
$max += $item->grademax - 1; // scales min is 1
}
}

if ($this->grade_item->grademax != $max or $this->grade_item->grademin != 0 or $this->grade_item->gradetype != GRADE_TYPE_VALUE){
$this->grade_item->grademax = $max;
$this->grade_item->grademin = 0;
$this->grade_item->gradetype = GRADE_TYPE_VALUE;
$this->grade_item->update('aggregation');
}

$this->apply_limit_rules($grade_values);

$sum = array_sum($grade_values);
Expand Down

0 comments on commit 3a03653

Please sign in to comment.