Skip to content

Commit

Permalink
MDL-47056 core_grades: Prevent items being displayed as 'Error'
Browse files Browse the repository at this point in the history
Before this patch we would be marking some items as 'needsupdate'
during an update. Leaving them aside and not effectively updating
them.

Part of: MDL-46576
  • Loading branch information
Frederic Massart authored and abgreeve committed Oct 3, 2014
1 parent 32ee0f8 commit fcf6e01
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
40 changes: 30 additions & 10 deletions lib/grade/grade_category.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,16 @@ public function force_regrading() {
$grade_item->force_regrading();
}

/**
* Something that should be called before we start regrading the whole course.
*
* @return void
*/
public function pre_regrade_final_grades() {
$this->auto_update_max();
$this->auto_update_weights();
}

/**
* Generates and saves final grades in associated category grade item.
* These immediate children must already have their own final grades.
Expand Down Expand Up @@ -458,12 +468,6 @@ public function generate_grades($userid=null) {
$items = $DB->get_records_sql($sql, $params);
}

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

// Needed for Natural aggregation type.
$this->auto_update_weights();

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

Expand Down Expand Up @@ -930,7 +934,7 @@ public function aggregate_values_and_adjust_bounds($grade_values, $items, & $wei

case GRADE_AGGREGATE_SUM: // Add up all the items.
$num = count($grade_values);
$total = $this->grade_item->grademax;
$total = $category_item->grademax;
$sum = 0;
foreach ($grade_values as $itemid => $grade_value) {
$sum += ($grade_value / $items[$itemid]->grademax) * ($items[$itemid]->aggregationcoef2*1) * $total;
Expand Down Expand Up @@ -977,16 +981,30 @@ public function aggregate_values($grade_values, $items) {
}

/**
* Some aggregation types may automatically update max grade
* Some aggregation types may need to update their max grade.
*
* @param array $items sub items
* @return void
*/
private function auto_update_max($items) {
private function auto_update_max() {
global $DB;
if ($this->aggregation != GRADE_AGGREGATE_SUM) {
// not needed at all
return;
}

// Find grade items of immediate children (category or grade items) and force site settings.
$this->load_grade_item();
$depends_on = $this->grade_item->depends_on();

$items = false;
if (!empty($depends_on)) {
list($usql, $params) = $DB->get_in_or_equal($depends_on);
$sql = "SELECT *
FROM {grade_items}
WHERE id $usql";
$items = $DB->get_records_sql($sql, $params);
}

if (!$items) {

if ($this->grade_item->grademax != 0 or $this->grade_item->gradetype != GRADE_TYPE_VALUE) {
Expand Down Expand Up @@ -1030,6 +1048,8 @@ private function auto_update_max($items) {

/**
* Recalculate the weights of the grade items in this category.
*
* @return void
*/
private function auto_update_weights() {
if ($this->aggregation != GRADE_AGGREGATE_SUM) {
Expand Down
7 changes: 7 additions & 0 deletions lib/gradelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,13 @@ function grade_regrade_final_grades($courseid, $userid=null, $updated_item=null)
}
}

// Categories might have to run some processing before we fetch the grade items.
// This gives them a final opportunity to update and mark their children to be updated.
$cats = grade_category::fetch_all(array('courseid' => $courseid));
foreach ($cats as $cat) {
$cat->pre_regrade_final_grades();
}

$grade_items = grade_item::fetch_all(array('courseid'=>$courseid));
$depends_on = array();

Expand Down

0 comments on commit fcf6e01

Please sign in to comment.